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 |
|
|
25 |
|
/** May be overridden to remove Listeners added earlier **/ |
26 |
|
public void beforeDispose(DIALOG newInstance) { |
27 |
|
}; |
28 |
|
|
29 |
final Logger LOGGER = Logger.getLogger(DialogManager.class); |
} |
30 |
|
|
31 |
private HashMap<KEY, DIALOG> dialogCache = new HashMap<KEY, DIALOG>(); |
protected HashMap<KEY, DIALOG> dialogCache = new HashMap<KEY, DIALOG>(); |
32 |
|
|
33 |
|
/** |
34 |
|
* A {@link DialogManager} instance can be created for any extension of |
35 |
|
* {@link JDialog} that will implement the |
36 |
|
* {@link #getInstanceFor(Object, Component, Object...)} method. |
37 |
|
*/ |
38 |
public DialogManager() { |
public DialogManager() { |
39 |
} |
} |
40 |
|
|
45 |
* @return |
* @return |
46 |
*/ |
*/ |
47 |
protected DIALOG bringup(DIALOG dialog) { |
protected DIALOG bringup(DIALOG dialog) { |
48 |
|
if (dialog == null){ |
49 |
|
// The dialog creation may have been cancelled |
50 |
|
return null; |
51 |
|
} |
52 |
if (!dialog.isVisible()) |
if (!dialog.isVisible()) |
53 |
dialog.setVisible(true); |
dialog.setVisible(true); |
54 |
dialog.toFront(); |
dialog.toFront(); |
56 |
return dialog; |
return dialog; |
57 |
} |
} |
58 |
|
|
59 |
|
/** |
60 |
|
* Implementing this should have a <br/> |
61 |
|
* <code>try { ... return ... } catch (Exception e) { |
62 |
|
ExceptionDialog.show(owner, e); |
63 |
|
return null; |
64 |
|
} </code> block. |
65 |
|
* |
66 |
|
* @param key |
67 |
|
* @param owner |
68 |
|
* @param constArgs |
69 |
|
* @return a cached instance or creates a new instance. Instances are always |
70 |
|
* returned visible and toFront. |
71 |
|
*/ |
72 |
public abstract DIALOG getInstanceFor(final KEY key, final Component owner, |
public abstract DIALOG getInstanceFor(final KEY key, final Component owner, |
73 |
final Object... constArgs); |
final Object... constArgs); |
74 |
|
|
97 |
} |
} |
98 |
} |
} |
99 |
|
|
100 |
public boolean disposeInstanceFor(KEY chartId) { |
public boolean disposeInstanceFor(KEY key) { |
101 |
synchronized (dialogCache) { |
synchronized (dialogCache) { |
102 |
|
|
103 |
final DIALOG dialog = dialogCache.get(chartId); |
final DIALOG dialog = dialogCache.get(key); |
104 |
if (dialog != null) { |
if (dialog != null) { |
105 |
dialog.dispose(); |
dialog.dispose(); |
106 |
if (dialogCache.remove(chartId) == null) { |
dialogCache.remove(key); |
|
LOGGER.warn("Hae?!?!"); //TODO cleanup |
|
|
} |
|
107 |
return true; |
return true; |
108 |
} |
} |
109 |
return false; |
return false; |
115 |
* will create the instance by invoking the {@link FactoryInterface} #create |
* will create the instance by invoking the {@link FactoryInterface} #create |
116 |
* method. |
* method. |
117 |
* |
* |
|
* @param key |
|
118 |
* @param factory |
* @param factory |
119 |
* {@link FactoryInterface} that creates the DIALOG |
* {@link FactoryInterface} that creates the DIALOG |
120 |
* |
* |
121 |
* @return Always a visible and inFront instance for the given key. |
* @return Always a visible and inFront instance of DIALOG for the given |
122 |
|
* key. |
123 |
*/ |
*/ |
124 |
public DIALOG getInstanceFor(final KEY key, FactoryInterface factory) { |
public DIALOG getInstanceFor(final KEY key, final FactoryInterface factory) { |
125 |
DIALOG dialog; |
final DIALOG dialog; |
126 |
if (isVisibleFor(key)) { |
if (isVisibleFor(key)) { |
127 |
dialog = dialogCache.get(key); |
dialog = dialogCache.get(key); |
128 |
} else { |
} else { |
129 |
|
|
130 |
dialog = factory.create(); |
dialog = factory.create(); |
131 |
|
if (dialog.isDisposed) { |
132 |
|
// The creation of the Dialog may have been cancelled for some reason |
133 |
|
return null; |
134 |
|
} |
135 |
dialogCache.put(key, dialog); |
dialogCache.put(key, dialog); |
136 |
|
dialog.setVisible(true); |
137 |
|
dialog.toFront(); |
138 |
|
|
139 |
dialog.addWindowListener(new WindowAdapter() { |
dialog.addWindowListener(new WindowAdapter() { |
140 |
@Override |
@Override |
141 |
public void windowClosed(final WindowEvent e) { |
public void windowClosed(final WindowEvent e) { |
142 |
disposeInstanceFor(key); |
dialogCache.remove(key); |
143 |
|
factory.beforeDispose(dialog); |
144 |
|
} |
145 |
|
|
146 |
|
@Override |
147 |
|
public void windowClosing(final WindowEvent e) { |
148 |
} |
} |
149 |
}); |
}); |
150 |
|
|
151 |
|
factory.afterCreation(dialog); |
152 |
} |
} |
153 |
|
|
154 |
return dialog; |
return dialog; |
155 |
} |
} |
|
|
|
156 |
|
|
157 |
/** |
/** |
158 |
* Disposes all open instances. |
* Disposes all open instances and removes them from the cache. |
159 |
* |
* |
160 |
* @return <code>true</code> if at least one window has been disposed. |
* @return <code>true</code> if at least one window has been disposed. |
161 |
*/ |
*/ |
162 |
public boolean disposeAll() { |
public boolean disposeAll() { |
163 |
|
|
164 |
boolean atLeastOne = false; |
boolean atLeastOne = false; |
165 |
HashSet<KEY> tempKeys = new HashSet<KEY>(dialogCache.keySet()); |
HashSet<KEY> tempKeys = new HashSet<KEY>(dialogCache.keySet()); |
166 |
for (KEY key : tempKeys) { |
for (KEY key : tempKeys) { |
167 |
DIALOG dialog = dialogCache |
DIALOG dialog = dialogCache.get(key); |
|
.get(key); |
|
168 |
if (dialog != null) { |
if (dialog != null) { |
169 |
dialog.dispose(); |
dialog.dispose(); |
170 |
atLeastOne = true; |
atLeastOne = true; |
174 |
dialogCache.clear(); |
dialogCache.clear(); |
175 |
return atLeastOne; |
return atLeastOne; |
176 |
} |
} |
177 |
|
|
178 |
|
/** |
179 |
|
* @return All instances of DIALOG as they are cached. |
180 |
|
*/ |
181 |
|
public Collection<DIALOG> getAllInstances() { |
182 |
|
return dialogCache.values(); |
183 |
|
} |
184 |
|
|
185 |
} |
} |