3 |
import java.awt.Component; |
import java.awt.Component; |
4 |
import java.awt.event.WindowAdapter; |
import java.awt.event.WindowAdapter; |
5 |
import java.awt.event.WindowEvent; |
import java.awt.event.WindowEvent; |
6 |
|
import java.util.Collection; |
7 |
import java.util.HashMap; |
import java.util.HashMap; |
8 |
import java.util.HashSet; |
import java.util.HashSet; |
9 |
|
|
11 |
|
|
12 |
import org.apache.log4j.Logger; |
import org.apache.log4j.Logger; |
13 |
|
|
14 |
public abstract class DialogManager<KEY, DIALOG extends JDialog> extends |
public abstract class DialogManager<KEY, DIALOG extends AtlasDialog> { |
15 |
JDialog { |
final static private Logger LOGGER = Logger.getLogger(DialogManager.class); |
16 |
|
|
17 |
public abstract class FactoryInterface { |
public abstract class FactoryInterface { |
18 |
|
|
19 |
public abstract DIALOG create(); |
public abstract DIALOG create(); |
20 |
|
|
21 |
|
/** May be overridden to add Listeners **/ |
22 |
|
public void afterCreation(DIALOG newInstance){}; |
23 |
|
|
24 |
} |
/** May be overridden to remove Listeners added earlier **/ |
25 |
|
public void beforeDispose(DIALOG newInstance){}; |
26 |
|
|
27 |
final Logger LOGGER = Logger.getLogger(DialogManager.class); |
} |
28 |
|
|
29 |
private HashMap<KEY, DIALOG> dialogCache = new HashMap<KEY, DIALOG>(); |
protected HashMap<KEY, DIALOG> dialogCache = new HashMap<KEY, DIALOG>(); |
30 |
|
|
31 |
|
/** |
32 |
|
* A {@link DialogManager} instance can be created for any extension of |
33 |
|
* {@link JDialog} that will implement the |
34 |
|
* {@link #getInstanceFor(Object, Component, Object...)} method. |
35 |
|
*/ |
36 |
public DialogManager() { |
public DialogManager() { |
37 |
} |
} |
38 |
|
|
84 |
final DIALOG dialog = dialogCache.get(chartId); |
final DIALOG dialog = dialogCache.get(chartId); |
85 |
if (dialog != null) { |
if (dialog != null) { |
86 |
dialog.dispose(); |
dialog.dispose(); |
87 |
if (dialogCache.remove(chartId) == null) { |
dialogCache.remove(chartId); |
|
LOGGER.warn("Hae?!?!"); //TODO cleanup |
|
|
} |
|
88 |
return true; |
return true; |
89 |
} |
} |
90 |
return false; |
return false; |
96 |
* will create the instance by invoking the {@link FactoryInterface} #create |
* will create the instance by invoking the {@link FactoryInterface} #create |
97 |
* method. |
* method. |
98 |
* |
* |
|
* @param key |
|
99 |
* @param factory |
* @param factory |
100 |
* {@link FactoryInterface} that creates the DIALOG |
* {@link FactoryInterface} that creates the DIALOG |
101 |
* |
* |
102 |
* @return Always a visible and inFront instance for the given key. |
* @return Always a visible and inFront instance of DIALOG for the given |
103 |
|
* key. |
104 |
*/ |
*/ |
105 |
public DIALOG getInstanceFor(final KEY key, FactoryInterface factory) { |
public DIALOG getInstanceFor(final KEY key, final FactoryInterface factory) { |
106 |
DIALOG dialog; |
final DIALOG dialog; |
107 |
if (isVisibleFor(key)) { |
if (isVisibleFor(key)) { |
108 |
dialog = dialogCache.get(key); |
dialog = dialogCache.get(key); |
109 |
} else { |
} else { |
110 |
|
|
111 |
dialog = factory.create(); |
dialog = factory.create(); |
|
|
|
112 |
dialogCache.put(key, dialog); |
dialogCache.put(key, dialog); |
113 |
|
dialog.setVisible(true); |
114 |
|
dialog.toFront(); |
115 |
|
|
116 |
dialog.addWindowListener(new WindowAdapter() { |
dialog.addWindowListener(new WindowAdapter() { |
117 |
@Override |
@Override |
118 |
public void windowClosed(final WindowEvent e) { |
public void windowClosed(final WindowEvent e) { |
119 |
|
dialog.setVisible(false); |
120 |
|
factory.beforeDispose(dialog); |
121 |
disposeInstanceFor(key); |
disposeInstanceFor(key); |
122 |
} |
} |
123 |
}); |
}); |
124 |
|
|
125 |
|
factory.afterCreation(dialog); |
126 |
} |
} |
127 |
|
|
128 |
|
|
129 |
return dialog; |
return dialog; |
130 |
} |
} |
|
|
|
131 |
|
|
132 |
/** |
/** |
133 |
* Disposes all open instances. |
* Disposes all open instances and removes them from the cache. |
134 |
* |
* |
135 |
* @return <code>true</code> if at least one window has been disposed. |
* @return <code>true</code> if at least one window has been disposed. |
136 |
*/ |
*/ |
137 |
public boolean disposeAll() { |
public boolean disposeAll() { |
138 |
|
|
139 |
boolean atLeastOne = false; |
boolean atLeastOne = false; |
140 |
HashSet<KEY> tempKeys = new HashSet<KEY>(dialogCache.keySet()); |
HashSet<KEY> tempKeys = new HashSet<KEY>(dialogCache.keySet()); |
141 |
for (KEY key : tempKeys) { |
for (KEY key : tempKeys) { |
142 |
DIALOG dialog = dialogCache |
DIALOG dialog = dialogCache.get(key); |
|
.get(key); |
|
143 |
if (dialog != null) { |
if (dialog != null) { |
144 |
dialog.dispose(); |
dialog.dispose(); |
145 |
atLeastOne = true; |
atLeastOne = true; |
149 |
dialogCache.clear(); |
dialogCache.clear(); |
150 |
return atLeastOne; |
return atLeastOne; |
151 |
} |
} |
152 |
|
|
153 |
|
/** |
154 |
|
* @return All instances of DIALOG as they are cached. |
155 |
|
*/ |
156 |
|
public Collection<DIALOG> getAllInstances() { |
157 |
|
return dialogCache.values(); |
158 |
|
} |
159 |
|
|
160 |
|
|
161 |
} |
} |