1 |
alfonx |
420 |
package skrueger.swing; |
2 |
|
|
|
3 |
|
|
import java.awt.Component; |
4 |
|
|
import java.awt.event.WindowAdapter; |
5 |
|
|
import java.awt.event.WindowEvent; |
6 |
alfonx |
422 |
import java.util.Collection; |
7 |
alfonx |
420 |
import java.util.HashMap; |
8 |
|
|
import java.util.HashSet; |
9 |
|
|
|
10 |
|
|
import javax.swing.JDialog; |
11 |
|
|
|
12 |
|
|
import org.apache.log4j.Logger; |
13 |
|
|
|
14 |
alfonx |
461 |
public abstract class DialogManager<KEY, DIALOG extends AtlasDialog> { |
15 |
alfonx |
421 |
final static private Logger LOGGER = Logger.getLogger(DialogManager.class); |
16 |
alfonx |
420 |
|
17 |
|
|
public abstract class FactoryInterface { |
18 |
|
|
|
19 |
|
|
public abstract DIALOG create(); |
20 |
alfonx |
960 |
|
21 |
alfonx |
433 |
/** May be overridden to add Listeners **/ |
22 |
alfonx |
960 |
public void afterCreation(DIALOG newInstance) { |
23 |
|
|
}; |
24 |
alfonx |
420 |
|
25 |
alfonx |
435 |
/** May be overridden to remove Listeners added earlier **/ |
26 |
alfonx |
960 |
public void beforeDispose(DIALOG newInstance) { |
27 |
|
|
}; |
28 |
alfonx |
435 |
|
29 |
alfonx |
420 |
} |
30 |
|
|
|
31 |
alfonx |
422 |
protected HashMap<KEY, DIALOG> dialogCache = new HashMap<KEY, DIALOG>(); |
32 |
alfonx |
420 |
|
33 |
alfonx |
421 |
/** |
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 |
alfonx |
420 |
public DialogManager() { |
39 |
|
|
} |
40 |
|
|
|
41 |
|
|
/** |
42 |
|
|
* This will be done with every dialog that an instance is required for. |
43 |
|
|
* |
44 |
|
|
* @param dialog |
45 |
|
|
* @return |
46 |
|
|
*/ |
47 |
|
|
protected DIALOG bringup(DIALOG dialog) { |
48 |
alfonx |
1041 |
if (dialog == null){ |
49 |
|
|
// The dialog creation may have been cancelled |
50 |
|
|
return null; |
51 |
|
|
} |
52 |
alfonx |
420 |
if (!dialog.isVisible()) |
53 |
|
|
dialog.setVisible(true); |
54 |
|
|
dialog.toFront(); |
55 |
|
|
|
56 |
|
|
return dialog; |
57 |
|
|
} |
58 |
|
|
|
59 |
alfonx |
474 |
/** |
60 |
alfonx |
960 |
* 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 |
alfonx |
474 |
* @param key |
67 |
|
|
* @param owner |
68 |
|
|
* @param constArgs |
69 |
alfonx |
960 |
* @return a cached instance or creates a new instance. Instances are always |
70 |
|
|
* returned visible and toFront. |
71 |
alfonx |
474 |
*/ |
72 |
alfonx |
420 |
public abstract DIALOG getInstanceFor(final KEY key, final Component owner, |
73 |
|
|
final Object... constArgs); |
74 |
|
|
|
75 |
|
|
/** |
76 |
|
|
* @return Is there an open/visible dialog for the given layer id? |
77 |
|
|
*/ |
78 |
|
|
public boolean isVisibleFor(KEY key) { |
79 |
|
|
return dialogCache.containsKey(key) && dialogCache.get(key).isVisible(); |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
/** |
83 |
|
|
* Will dispose any dialog that is registered to the given parent |
84 |
|
|
* {@link Component} |
85 |
|
|
* |
86 |
|
|
* @param parent |
87 |
|
|
*/ |
88 |
|
|
public void disposeInstanceForParent(final Component parent) { |
89 |
|
|
|
90 |
|
|
final HashMap<KEY, JDialog> clonedHashMap = (HashMap<KEY, JDialog>) dialogCache |
91 |
|
|
.clone(); |
92 |
|
|
|
93 |
|
|
for (KEY chartId : clonedHashMap.keySet()) { |
94 |
|
|
if (dialogCache.get(chartId).getParent() == parent) { |
95 |
|
|
disposeInstanceFor(chartId); |
96 |
|
|
} |
97 |
|
|
} |
98 |
|
|
} |
99 |
|
|
|
100 |
alfonx |
470 |
public boolean disposeInstanceFor(KEY key) { |
101 |
alfonx |
420 |
synchronized (dialogCache) { |
102 |
|
|
|
103 |
alfonx |
470 |
final DIALOG dialog = dialogCache.get(key); |
104 |
alfonx |
420 |
if (dialog != null) { |
105 |
|
|
dialog.dispose(); |
106 |
alfonx |
470 |
dialogCache.remove(key); |
107 |
alfonx |
420 |
return true; |
108 |
|
|
} |
109 |
|
|
return false; |
110 |
|
|
} |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
/** |
114 |
|
|
* Checks whether there already is an instance for that key and otherwise |
115 |
|
|
* will create the instance by invoking the {@link FactoryInterface} #create |
116 |
|
|
* method. |
117 |
|
|
* |
118 |
|
|
* @param factory |
119 |
|
|
* {@link FactoryInterface} that creates the DIALOG |
120 |
|
|
* |
121 |
alfonx |
421 |
* @return Always a visible and inFront instance of DIALOG for the given |
122 |
|
|
* key. |
123 |
alfonx |
420 |
*/ |
124 |
alfonx |
435 |
public DIALOG getInstanceFor(final KEY key, final FactoryInterface factory) { |
125 |
|
|
final DIALOG dialog; |
126 |
alfonx |
420 |
if (isVisibleFor(key)) { |
127 |
|
|
dialog = dialogCache.get(key); |
128 |
|
|
} else { |
129 |
|
|
|
130 |
|
|
dialog = factory.create(); |
131 |
alfonx |
1041 |
if (dialog.isDisposed) { |
132 |
|
|
// The creation of the Dialog may have been cancelled for some reason |
133 |
|
|
return null; |
134 |
|
|
} |
135 |
alfonx |
420 |
dialogCache.put(key, dialog); |
136 |
alfonx |
459 |
dialog.setVisible(true); |
137 |
|
|
dialog.toFront(); |
138 |
alfonx |
420 |
|
139 |
|
|
dialog.addWindowListener(new WindowAdapter() { |
140 |
|
|
@Override |
141 |
|
|
public void windowClosed(final WindowEvent e) { |
142 |
alfonx |
618 |
dialogCache.remove(key); |
143 |
alfonx |
459 |
factory.beforeDispose(dialog); |
144 |
alfonx |
420 |
} |
145 |
alfonx |
618 |
|
146 |
|
|
@Override |
147 |
|
|
public void windowClosing(final WindowEvent e) { |
148 |
|
|
} |
149 |
alfonx |
420 |
}); |
150 |
alfonx |
960 |
|
151 |
alfonx |
459 |
factory.afterCreation(dialog); |
152 |
alfonx |
420 |
} |
153 |
alfonx |
459 |
|
154 |
alfonx |
420 |
return dialog; |
155 |
|
|
} |
156 |
|
|
|
157 |
|
|
/** |
158 |
alfonx |
421 |
* Disposes all open instances and removes them from the cache. |
159 |
alfonx |
420 |
* |
160 |
|
|
* @return <code>true</code> if at least one window has been disposed. |
161 |
|
|
*/ |
162 |
|
|
public boolean disposeAll() { |
163 |
alfonx |
421 |
|
164 |
alfonx |
420 |
boolean atLeastOne = false; |
165 |
|
|
HashSet<KEY> tempKeys = new HashSet<KEY>(dialogCache.keySet()); |
166 |
|
|
for (KEY key : tempKeys) { |
167 |
alfonx |
421 |
DIALOG dialog = dialogCache.get(key); |
168 |
alfonx |
420 |
if (dialog != null) { |
169 |
|
|
dialog.dispose(); |
170 |
|
|
atLeastOne = true; |
171 |
|
|
} |
172 |
|
|
} |
173 |
|
|
tempKeys.clear(); |
174 |
|
|
dialogCache.clear(); |
175 |
|
|
return atLeastOne; |
176 |
|
|
} |
177 |
alfonx |
421 |
|
178 |
alfonx |
422 |
/** |
179 |
|
|
* @return All instances of DIALOG as they are cached. |
180 |
|
|
*/ |
181 |
|
|
public Collection<DIALOG> getAllInstances() { |
182 |
|
|
return dialogCache.values(); |
183 |
|
|
} |
184 |
|
|
|
185 |
alfonx |
420 |
} |