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