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 |
421 |
public abstract class DialogManager<KEY, DIALOG extends JDialog> { |
15 |
|
|
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 |
|
|
public void appendListeners(DIALOG newInstance){}; |
23 |
|
|
// |
24 |
|
|
// /** May be overridden to remove Listeners added earlier **/ |
25 |
|
|
// public void removeListeners(DIALOG newInstance){}; |
26 |
alfonx |
420 |
|
27 |
|
|
} |
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 |
|
|
public abstract DIALOG getInstanceFor(final KEY key, final Component owner, |
54 |
|
|
final Object... constArgs); |
55 |
|
|
|
56 |
|
|
/** |
57 |
|
|
* @return Is there an open/visible dialog for the given layer id? |
58 |
|
|
*/ |
59 |
|
|
public boolean isVisibleFor(KEY key) { |
60 |
|
|
return dialogCache.containsKey(key) && dialogCache.get(key).isVisible(); |
61 |
|
|
} |
62 |
|
|
|
63 |
|
|
/** |
64 |
|
|
* Will dispose any dialog that is registered to the given parent |
65 |
|
|
* {@link Component} |
66 |
|
|
* |
67 |
|
|
* @param parent |
68 |
|
|
*/ |
69 |
|
|
public void disposeInstanceForParent(final Component parent) { |
70 |
|
|
|
71 |
|
|
final HashMap<KEY, JDialog> clonedHashMap = (HashMap<KEY, JDialog>) dialogCache |
72 |
|
|
.clone(); |
73 |
|
|
|
74 |
|
|
for (KEY chartId : clonedHashMap.keySet()) { |
75 |
|
|
if (dialogCache.get(chartId).getParent() == parent) { |
76 |
|
|
disposeInstanceFor(chartId); |
77 |
|
|
} |
78 |
|
|
} |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
public boolean disposeInstanceFor(KEY chartId) { |
82 |
|
|
synchronized (dialogCache) { |
83 |
|
|
|
84 |
|
|
final DIALOG dialog = dialogCache.get(chartId); |
85 |
|
|
if (dialog != null) { |
86 |
|
|
dialog.dispose(); |
87 |
alfonx |
421 |
dialogCache.remove(chartId); |
88 |
alfonx |
420 |
return true; |
89 |
|
|
} |
90 |
|
|
return false; |
91 |
|
|
} |
92 |
|
|
} |
93 |
|
|
|
94 |
|
|
/** |
95 |
|
|
* Checks whether there already is an instance for that key and otherwise |
96 |
|
|
* will create the instance by invoking the {@link FactoryInterface} #create |
97 |
|
|
* method. |
98 |
|
|
* |
99 |
|
|
* @param factory |
100 |
|
|
* {@link FactoryInterface} that creates the DIALOG |
101 |
|
|
* |
102 |
alfonx |
421 |
* @return Always a visible and inFront instance of DIALOG for the given |
103 |
|
|
* key. |
104 |
alfonx |
420 |
*/ |
105 |
|
|
public DIALOG getInstanceFor(final KEY key, FactoryInterface factory) { |
106 |
|
|
DIALOG dialog; |
107 |
|
|
if (isVisibleFor(key)) { |
108 |
|
|
dialog = dialogCache.get(key); |
109 |
|
|
} else { |
110 |
|
|
|
111 |
|
|
dialog = factory.create(); |
112 |
|
|
|
113 |
|
|
dialogCache.put(key, dialog); |
114 |
|
|
|
115 |
|
|
dialog.addWindowListener(new WindowAdapter() { |
116 |
|
|
@Override |
117 |
|
|
public void windowClosed(final WindowEvent e) { |
118 |
|
|
disposeInstanceFor(key); |
119 |
|
|
} |
120 |
|
|
}); |
121 |
alfonx |
433 |
|
122 |
|
|
factory.appendListeners(dialog); |
123 |
alfonx |
420 |
} |
124 |
|
|
return dialog; |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
/** |
128 |
alfonx |
421 |
* Disposes all open instances and removes them from the cache. |
129 |
alfonx |
420 |
* |
130 |
|
|
* @return <code>true</code> if at least one window has been disposed. |
131 |
|
|
*/ |
132 |
|
|
public boolean disposeAll() { |
133 |
alfonx |
421 |
|
134 |
alfonx |
420 |
boolean atLeastOne = false; |
135 |
|
|
HashSet<KEY> tempKeys = new HashSet<KEY>(dialogCache.keySet()); |
136 |
|
|
for (KEY key : tempKeys) { |
137 |
alfonx |
421 |
DIALOG dialog = dialogCache.get(key); |
138 |
alfonx |
420 |
if (dialog != null) { |
139 |
|
|
dialog.dispose(); |
140 |
|
|
atLeastOne = true; |
141 |
|
|
} |
142 |
|
|
} |
143 |
|
|
tempKeys.clear(); |
144 |
|
|
dialogCache.clear(); |
145 |
|
|
return atLeastOne; |
146 |
|
|
} |
147 |
alfonx |
421 |
|
148 |
alfonx |
422 |
/** |
149 |
|
|
* @return All instances of DIALOG as they are cached. |
150 |
|
|
*/ |
151 |
|
|
public Collection<DIALOG> getAllInstances() { |
152 |
|
|
return dialogCache.values(); |
153 |
|
|
} |
154 |
|
|
|
155 |
|
|
|
156 |
alfonx |
420 |
} |