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 |
|
|
52 |
return dialog; |
return dialog; |
53 |
} |
} |
54 |
|
|
55 |
|
/** |
56 |
|
* Implementing this should have a <br/> |
57 |
|
* <code>try { ... return ... } catch (Exception e) { |
58 |
|
ExceptionDialog.show(owner, e); |
59 |
|
return null; |
60 |
|
} </code> block. |
61 |
|
* |
62 |
|
* @param key |
63 |
|
* @param owner |
64 |
|
* @param constArgs |
65 |
|
* @return a cached instance or creates a new instance. Instances are always |
66 |
|
* returned visible and toFront. |
67 |
|
*/ |
68 |
public abstract DIALOG getInstanceFor(final KEY key, final Component owner, |
public abstract DIALOG getInstanceFor(final KEY key, final Component owner, |
69 |
final Object... constArgs); |
final Object... constArgs); |
70 |
|
|
93 |
} |
} |
94 |
} |
} |
95 |
|
|
96 |
public boolean disposeInstanceFor(KEY chartId) { |
public boolean disposeInstanceFor(KEY key) { |
97 |
synchronized (dialogCache) { |
synchronized (dialogCache) { |
98 |
|
|
99 |
final DIALOG dialog = dialogCache.get(chartId); |
final DIALOG dialog = dialogCache.get(key); |
100 |
if (dialog != null) { |
if (dialog != null) { |
101 |
dialog.dispose(); |
dialog.dispose(); |
102 |
if (dialogCache.remove(chartId) == null) { |
dialogCache.remove(key); |
|
LOGGER.warn("Hae?!?!"); //TODO cleanup |
|
|
} |
|
103 |
return true; |
return true; |
104 |
} |
} |
105 |
return false; |
return false; |
111 |
* will create the instance by invoking the {@link FactoryInterface} #create |
* will create the instance by invoking the {@link FactoryInterface} #create |
112 |
* method. |
* method. |
113 |
* |
* |
|
* @param key |
|
114 |
* @param factory |
* @param factory |
115 |
* {@link FactoryInterface} that creates the DIALOG |
* {@link FactoryInterface} that creates the DIALOG |
116 |
* |
* |
117 |
* @return Always a visible and inFront instance for the given key. |
* @return Always a visible and inFront instance of DIALOG for the given |
118 |
|
* key. |
119 |
*/ |
*/ |
120 |
public DIALOG getInstanceFor(final KEY key, FactoryInterface factory) { |
public DIALOG getInstanceFor(final KEY key, final FactoryInterface factory) { |
121 |
DIALOG dialog; |
final DIALOG dialog; |
122 |
if (isVisibleFor(key)) { |
if (isVisibleFor(key)) { |
123 |
dialog = dialogCache.get(key); |
dialog = dialogCache.get(key); |
124 |
} else { |
} else { |
125 |
|
|
126 |
dialog = factory.create(); |
dialog = factory.create(); |
|
|
|
127 |
dialogCache.put(key, dialog); |
dialogCache.put(key, dialog); |
128 |
|
dialog.setVisible(true); |
129 |
|
dialog.toFront(); |
130 |
|
|
131 |
dialog.addWindowListener(new WindowAdapter() { |
dialog.addWindowListener(new WindowAdapter() { |
132 |
@Override |
@Override |
133 |
public void windowClosed(final WindowEvent e) { |
public void windowClosed(final WindowEvent e) { |
134 |
disposeInstanceFor(key); |
dialogCache.remove(key); |
135 |
|
factory.beforeDispose(dialog); |
136 |
|
} |
137 |
|
|
138 |
|
@Override |
139 |
|
public void windowClosing(final WindowEvent e) { |
140 |
} |
} |
141 |
}); |
}); |
142 |
|
|
143 |
|
factory.afterCreation(dialog); |
144 |
} |
} |
145 |
|
|
146 |
return dialog; |
return dialog; |
147 |
} |
} |
|
|
|
148 |
|
|
149 |
/** |
/** |
150 |
* Disposes all open instances. |
* Disposes all open instances and removes them from the cache. |
151 |
* |
* |
152 |
* @return <code>true</code> if at least one window has been disposed. |
* @return <code>true</code> if at least one window has been disposed. |
153 |
*/ |
*/ |
154 |
public boolean disposeAll() { |
public boolean disposeAll() { |
155 |
|
|
156 |
boolean atLeastOne = false; |
boolean atLeastOne = false; |
157 |
HashSet<KEY> tempKeys = new HashSet<KEY>(dialogCache.keySet()); |
HashSet<KEY> tempKeys = new HashSet<KEY>(dialogCache.keySet()); |
158 |
for (KEY key : tempKeys) { |
for (KEY key : tempKeys) { |
159 |
DIALOG dialog = dialogCache |
DIALOG dialog = dialogCache.get(key); |
|
.get(key); |
|
160 |
if (dialog != null) { |
if (dialog != null) { |
161 |
dialog.dispose(); |
dialog.dispose(); |
162 |
atLeastOne = true; |
atLeastOne = true; |
166 |
dialogCache.clear(); |
dialogCache.clear(); |
167 |
return atLeastOne; |
return atLeastOne; |
168 |
} |
} |
169 |
|
|
170 |
|
/** |
171 |
|
* @return All instances of DIALOG as they are cached. |
172 |
|
*/ |
173 |
|
public Collection<DIALOG> getAllInstances() { |
174 |
|
return dialogCache.values(); |
175 |
|
} |
176 |
|
|
177 |
} |
} |