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 |
|
|
import java.util.HashMap; |
7 |
|
|
import java.util.HashSet; |
8 |
|
|
|
9 |
|
|
import javax.swing.JDialog; |
10 |
|
|
|
11 |
|
|
import org.apache.log4j.Logger; |
12 |
|
|
|
13 |
|
|
public abstract class DialogManager<KEY, DIALOG extends JDialog> extends |
14 |
|
|
JDialog { |
15 |
|
|
|
16 |
|
|
public abstract class FactoryInterface { |
17 |
|
|
|
18 |
|
|
public abstract DIALOG create(); |
19 |
|
|
|
20 |
|
|
} |
21 |
|
|
|
22 |
|
|
final Logger LOGGER = Logger.getLogger(DialogManager.class); |
23 |
|
|
|
24 |
|
|
private HashMap<KEY, DIALOG> dialogCache = new HashMap<KEY, DIALOG>(); |
25 |
|
|
|
26 |
|
|
public DialogManager() { |
27 |
|
|
} |
28 |
|
|
|
29 |
|
|
/** |
30 |
|
|
* This will be done with every dialog that an instance is required for. |
31 |
|
|
* |
32 |
|
|
* @param dialog |
33 |
|
|
* @return |
34 |
|
|
*/ |
35 |
|
|
protected DIALOG bringup(DIALOG dialog) { |
36 |
|
|
if (!dialog.isVisible()) |
37 |
|
|
dialog.setVisible(true); |
38 |
|
|
dialog.toFront(); |
39 |
|
|
|
40 |
|
|
return dialog; |
41 |
|
|
} |
42 |
|
|
|
43 |
|
|
public abstract DIALOG getInstanceFor(final KEY key, final Component owner, |
44 |
|
|
final Object... constArgs); |
45 |
|
|
|
46 |
|
|
/** |
47 |
|
|
* @return Is there an open/visible dialog for the given layer id? |
48 |
|
|
*/ |
49 |
|
|
public boolean isVisibleFor(KEY key) { |
50 |
|
|
return dialogCache.containsKey(key) && dialogCache.get(key).isVisible(); |
51 |
|
|
} |
52 |
|
|
|
53 |
|
|
/** |
54 |
|
|
* Will dispose any dialog that is registered to the given parent |
55 |
|
|
* {@link Component} |
56 |
|
|
* |
57 |
|
|
* @param parent |
58 |
|
|
*/ |
59 |
|
|
public void disposeInstanceForParent(final Component parent) { |
60 |
|
|
|
61 |
|
|
final HashMap<KEY, JDialog> clonedHashMap = (HashMap<KEY, JDialog>) dialogCache |
62 |
|
|
.clone(); |
63 |
|
|
|
64 |
|
|
for (KEY chartId : clonedHashMap.keySet()) { |
65 |
|
|
if (dialogCache.get(chartId).getParent() == parent) { |
66 |
|
|
disposeInstanceFor(chartId); |
67 |
|
|
} |
68 |
|
|
} |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
public boolean disposeInstanceFor(KEY chartId) { |
72 |
|
|
synchronized (dialogCache) { |
73 |
|
|
|
74 |
|
|
final DIALOG dialog = dialogCache.get(chartId); |
75 |
|
|
if (dialog != null) { |
76 |
|
|
dialog.dispose(); |
77 |
|
|
if (dialogCache.remove(chartId) == null) { |
78 |
|
|
LOGGER.warn("Hae?!?!"); //TODO cleanup |
79 |
|
|
} |
80 |
|
|
return true; |
81 |
|
|
} |
82 |
|
|
return false; |
83 |
|
|
} |
84 |
|
|
} |
85 |
|
|
|
86 |
|
|
/** |
87 |
|
|
* Checks whether there already is an instance for that key and otherwise |
88 |
|
|
* will create the instance by invoking the {@link FactoryInterface} #create |
89 |
|
|
* method. |
90 |
|
|
* |
91 |
|
|
* @param key |
92 |
|
|
* @param factory |
93 |
|
|
* {@link FactoryInterface} that creates the DIALOG |
94 |
|
|
* |
95 |
|
|
* @return Always a visible and inFront instance for the given key. |
96 |
|
|
*/ |
97 |
|
|
public DIALOG getInstanceFor(final KEY key, FactoryInterface factory) { |
98 |
|
|
DIALOG dialog; |
99 |
|
|
if (isVisibleFor(key)) { |
100 |
|
|
dialog = dialogCache.get(key); |
101 |
|
|
} else { |
102 |
|
|
|
103 |
|
|
dialog = factory.create(); |
104 |
|
|
|
105 |
|
|
dialogCache.put(key, dialog); |
106 |
|
|
|
107 |
|
|
dialog.addWindowListener(new WindowAdapter() { |
108 |
|
|
@Override |
109 |
|
|
public void windowClosed(final WindowEvent e) { |
110 |
|
|
disposeInstanceFor(key); |
111 |
|
|
} |
112 |
|
|
}); |
113 |
|
|
} |
114 |
|
|
return dialog; |
115 |
|
|
} |
116 |
|
|
|
117 |
|
|
|
118 |
|
|
/** |
119 |
|
|
* Disposes all open instances. |
120 |
|
|
* |
121 |
|
|
* @return <code>true</code> if at least one window has been disposed. |
122 |
|
|
*/ |
123 |
|
|
public boolean disposeAll() { |
124 |
|
|
|
125 |
|
|
boolean atLeastOne = false; |
126 |
|
|
HashSet<KEY> tempKeys = new HashSet<KEY>(dialogCache.keySet()); |
127 |
|
|
for (KEY key : tempKeys) { |
128 |
|
|
DIALOG dialog = dialogCache |
129 |
|
|
.get(key); |
130 |
|
|
if (dialog != null) { |
131 |
|
|
dialog.dispose(); |
132 |
|
|
atLeastOne = true; |
133 |
|
|
} |
134 |
|
|
} |
135 |
|
|
tempKeys.clear(); |
136 |
|
|
dialogCache.clear(); |
137 |
|
|
return atLeastOne; |
138 |
|
|
} |
139 |
|
|
|
140 |
|
|
} |