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 |
|
|
if (!dialog.isVisible()) |
49 |
|
|
dialog.setVisible(true); |
50 |
|
|
dialog.toFront(); |
51 |
|
|
|
52 |
|
|
return dialog; |
53 |
|
|
} |
54 |
|
|
|
55 |
alfonx |
474 |
/** |
56 |
alfonx |
960 |
* 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 |
alfonx |
474 |
* @param key |
63 |
|
|
* @param owner |
64 |
|
|
* @param constArgs |
65 |
alfonx |
960 |
* @return a cached instance or creates a new instance. Instances are always |
66 |
|
|
* returned visible and toFront. |
67 |
alfonx |
474 |
*/ |
68 |
alfonx |
420 |
public abstract DIALOG getInstanceFor(final KEY key, final Component owner, |
69 |
|
|
final Object... constArgs); |
70 |
|
|
|
71 |
|
|
/** |
72 |
|
|
* @return Is there an open/visible dialog for the given layer id? |
73 |
|
|
*/ |
74 |
|
|
public boolean isVisibleFor(KEY key) { |
75 |
|
|
return dialogCache.containsKey(key) && dialogCache.get(key).isVisible(); |
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
/** |
79 |
|
|
* Will dispose any dialog that is registered to the given parent |
80 |
|
|
* {@link Component} |
81 |
|
|
* |
82 |
|
|
* @param parent |
83 |
|
|
*/ |
84 |
|
|
public void disposeInstanceForParent(final Component parent) { |
85 |
|
|
|
86 |
|
|
final HashMap<KEY, JDialog> clonedHashMap = (HashMap<KEY, JDialog>) dialogCache |
87 |
|
|
.clone(); |
88 |
|
|
|
89 |
|
|
for (KEY chartId : clonedHashMap.keySet()) { |
90 |
|
|
if (dialogCache.get(chartId).getParent() == parent) { |
91 |
|
|
disposeInstanceFor(chartId); |
92 |
|
|
} |
93 |
|
|
} |
94 |
|
|
} |
95 |
|
|
|
96 |
alfonx |
470 |
public boolean disposeInstanceFor(KEY key) { |
97 |
alfonx |
420 |
synchronized (dialogCache) { |
98 |
|
|
|
99 |
alfonx |
470 |
final DIALOG dialog = dialogCache.get(key); |
100 |
alfonx |
420 |
if (dialog != null) { |
101 |
|
|
dialog.dispose(); |
102 |
alfonx |
470 |
dialogCache.remove(key); |
103 |
alfonx |
420 |
return true; |
104 |
|
|
} |
105 |
|
|
return false; |
106 |
|
|
} |
107 |
|
|
} |
108 |
|
|
|
109 |
|
|
/** |
110 |
|
|
* Checks whether there already is an instance for that key and otherwise |
111 |
|
|
* will create the instance by invoking the {@link FactoryInterface} #create |
112 |
|
|
* method. |
113 |
|
|
* |
114 |
|
|
* @param factory |
115 |
|
|
* {@link FactoryInterface} that creates the DIALOG |
116 |
|
|
* |
117 |
alfonx |
421 |
* @return Always a visible and inFront instance of DIALOG for the given |
118 |
|
|
* key. |
119 |
alfonx |
420 |
*/ |
120 |
alfonx |
435 |
public DIALOG getInstanceFor(final KEY key, final FactoryInterface factory) { |
121 |
|
|
final DIALOG dialog; |
122 |
alfonx |
420 |
if (isVisibleFor(key)) { |
123 |
|
|
dialog = dialogCache.get(key); |
124 |
|
|
} else { |
125 |
|
|
|
126 |
|
|
dialog = factory.create(); |
127 |
|
|
dialogCache.put(key, dialog); |
128 |
alfonx |
459 |
dialog.setVisible(true); |
129 |
|
|
dialog.toFront(); |
130 |
alfonx |
420 |
|
131 |
|
|
dialog.addWindowListener(new WindowAdapter() { |
132 |
|
|
@Override |
133 |
|
|
public void windowClosed(final WindowEvent e) { |
134 |
alfonx |
618 |
dialogCache.remove(key); |
135 |
alfonx |
459 |
factory.beforeDispose(dialog); |
136 |
alfonx |
420 |
} |
137 |
alfonx |
618 |
|
138 |
|
|
@Override |
139 |
|
|
public void windowClosing(final WindowEvent e) { |
140 |
|
|
} |
141 |
alfonx |
420 |
}); |
142 |
alfonx |
960 |
|
143 |
alfonx |
459 |
factory.afterCreation(dialog); |
144 |
alfonx |
420 |
} |
145 |
alfonx |
459 |
|
146 |
alfonx |
420 |
return dialog; |
147 |
|
|
} |
148 |
|
|
|
149 |
|
|
/** |
150 |
alfonx |
421 |
* Disposes all open instances and removes them from the cache. |
151 |
alfonx |
420 |
* |
152 |
|
|
* @return <code>true</code> if at least one window has been disposed. |
153 |
|
|
*/ |
154 |
|
|
public boolean disposeAll() { |
155 |
alfonx |
421 |
|
156 |
alfonx |
420 |
boolean atLeastOne = false; |
157 |
|
|
HashSet<KEY> tempKeys = new HashSet<KEY>(dialogCache.keySet()); |
158 |
|
|
for (KEY key : tempKeys) { |
159 |
alfonx |
421 |
DIALOG dialog = dialogCache.get(key); |
160 |
alfonx |
420 |
if (dialog != null) { |
161 |
|
|
dialog.dispose(); |
162 |
|
|
atLeastOne = true; |
163 |
|
|
} |
164 |
|
|
} |
165 |
|
|
tempKeys.clear(); |
166 |
|
|
dialogCache.clear(); |
167 |
|
|
return atLeastOne; |
168 |
|
|
} |
169 |
alfonx |
421 |
|
170 |
alfonx |
422 |
/** |
171 |
|
|
* @return All instances of DIALOG as they are cached. |
172 |
|
|
*/ |
173 |
|
|
public Collection<DIALOG> getAllInstances() { |
174 |
|
|
return dialogCache.values(); |
175 |
|
|
} |
176 |
|
|
|
177 |
alfonx |
420 |
} |