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 |
433 |
|
21 |
|
|
/** May be overridden to add Listeners **/ |
22 |
alfonx |
459 |
public void afterCreation(DIALOG newInstance){}; |
23 |
alfonx |
420 |
|
24 |
alfonx |
435 |
/** May be overridden to remove Listeners added earlier **/ |
25 |
alfonx |
459 |
public void beforeDispose(DIALOG newInstance){}; |
26 |
alfonx |
435 |
|
27 |
alfonx |
420 |
} |
28 |
|
|
|
29 |
alfonx |
422 |
protected HashMap<KEY, DIALOG> dialogCache = new HashMap<KEY, DIALOG>(); |
30 |
alfonx |
420 |
|
31 |
alfonx |
421 |
/** |
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 |
alfonx |
420 |
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 |
alfonx |
474 |
/** |
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 |
alfonx |
420 |
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 |
alfonx |
470 |
public boolean disposeInstanceFor(KEY key) { |
88 |
alfonx |
420 |
synchronized (dialogCache) { |
89 |
|
|
|
90 |
alfonx |
470 |
final DIALOG dialog = dialogCache.get(key); |
91 |
alfonx |
420 |
if (dialog != null) { |
92 |
|
|
dialog.dispose(); |
93 |
alfonx |
470 |
dialogCache.remove(key); |
94 |
alfonx |
420 |
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 |
alfonx |
421 |
* @return Always a visible and inFront instance of DIALOG for the given |
109 |
|
|
* key. |
110 |
alfonx |
420 |
*/ |
111 |
alfonx |
435 |
public DIALOG getInstanceFor(final KEY key, final FactoryInterface factory) { |
112 |
|
|
final DIALOG dialog; |
113 |
alfonx |
420 |
if (isVisibleFor(key)) { |
114 |
|
|
dialog = dialogCache.get(key); |
115 |
|
|
} else { |
116 |
|
|
|
117 |
|
|
dialog = factory.create(); |
118 |
|
|
dialogCache.put(key, dialog); |
119 |
alfonx |
459 |
dialog.setVisible(true); |
120 |
|
|
dialog.toFront(); |
121 |
alfonx |
420 |
|
122 |
|
|
dialog.addWindowListener(new WindowAdapter() { |
123 |
|
|
@Override |
124 |
|
|
public void windowClosed(final WindowEvent e) { |
125 |
alfonx |
461 |
dialog.setVisible(false); |
126 |
alfonx |
459 |
factory.beforeDispose(dialog); |
127 |
alfonx |
420 |
disposeInstanceFor(key); |
128 |
|
|
} |
129 |
|
|
}); |
130 |
alfonx |
433 |
|
131 |
alfonx |
459 |
factory.afterCreation(dialog); |
132 |
alfonx |
420 |
} |
133 |
alfonx |
435 |
|
134 |
alfonx |
459 |
|
135 |
alfonx |
420 |
return dialog; |
136 |
|
|
} |
137 |
|
|
|
138 |
|
|
/** |
139 |
alfonx |
421 |
* Disposes all open instances and removes them from the cache. |
140 |
alfonx |
420 |
* |
141 |
|
|
* @return <code>true</code> if at least one window has been disposed. |
142 |
|
|
*/ |
143 |
|
|
public boolean disposeAll() { |
144 |
alfonx |
421 |
|
145 |
alfonx |
420 |
boolean atLeastOne = false; |
146 |
|
|
HashSet<KEY> tempKeys = new HashSet<KEY>(dialogCache.keySet()); |
147 |
|
|
for (KEY key : tempKeys) { |
148 |
alfonx |
421 |
DIALOG dialog = dialogCache.get(key); |
149 |
alfonx |
420 |
if (dialog != null) { |
150 |
|
|
dialog.dispose(); |
151 |
|
|
atLeastOne = true; |
152 |
|
|
} |
153 |
|
|
} |
154 |
|
|
tempKeys.clear(); |
155 |
|
|
dialogCache.clear(); |
156 |
|
|
return atLeastOne; |
157 |
|
|
} |
158 |
alfonx |
421 |
|
159 |
alfonx |
422 |
/** |
160 |
|
|
* @return All instances of DIALOG as they are cached. |
161 |
|
|
*/ |
162 |
|
|
public Collection<DIALOG> getAllInstances() { |
163 |
|
|
return dialogCache.values(); |
164 |
|
|
} |
165 |
|
|
|
166 |
|
|
|
167 |
alfonx |
420 |
} |