3 |
import java.awt.Component; |
import java.awt.Component; |
4 |
import java.awt.event.WindowAdapter; |
import java.awt.event.WindowAdapter; |
5 |
import java.awt.event.WindowEvent; |
import java.awt.event.WindowEvent; |
6 |
|
import java.util.Collection; |
7 |
import java.util.HashMap; |
import java.util.HashMap; |
8 |
import java.util.HashSet; |
import java.util.HashSet; |
9 |
|
|
11 |
|
|
12 |
import org.apache.log4j.Logger; |
import org.apache.log4j.Logger; |
13 |
|
|
14 |
public abstract class DialogManager<KEY, DIALOG extends JDialog> { |
public abstract class DialogManager<KEY, DIALOG extends AtlasDialog> { |
15 |
final static private Logger LOGGER = Logger.getLogger(DialogManager.class); |
final static private Logger LOGGER = Logger.getLogger(DialogManager.class); |
16 |
|
|
17 |
public abstract class FactoryInterface { |
public abstract class FactoryInterface { |
18 |
|
|
19 |
public abstract DIALOG create(); |
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 |
private HashMap<KEY, DIALOG> dialogCache = new HashMap<KEY, DIALOG>(); |
protected HashMap<KEY, DIALOG> dialogCache = new HashMap<KEY, DIALOG>(); |
30 |
|
|
31 |
/** |
/** |
32 |
* A {@link DialogManager} instance can be created for any extension of |
* A {@link DialogManager} instance can be created for any extension of |
50 |
return dialog; |
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, |
public abstract DIALOG getInstanceFor(final KEY key, final Component owner, |
60 |
final Object... constArgs); |
final Object... constArgs); |
61 |
|
|
84 |
} |
} |
85 |
} |
} |
86 |
|
|
87 |
public boolean disposeInstanceFor(KEY chartId) { |
public boolean disposeInstanceFor(KEY key) { |
88 |
synchronized (dialogCache) { |
synchronized (dialogCache) { |
89 |
|
|
90 |
final DIALOG dialog = dialogCache.get(chartId); |
final DIALOG dialog = dialogCache.get(key); |
91 |
if (dialog != null) { |
if (dialog != null) { |
92 |
dialog.dispose(); |
dialog.dispose(); |
93 |
dialogCache.remove(chartId); |
dialogCache.remove(key); |
94 |
return true; |
return true; |
95 |
} |
} |
96 |
return false; |
return false; |
108 |
* @return Always a visible and inFront instance of DIALOG for the given |
* @return Always a visible and inFront instance of DIALOG for the given |
109 |
* key. |
* key. |
110 |
*/ |
*/ |
111 |
public DIALOG getInstanceFor(final KEY key, FactoryInterface factory) { |
public DIALOG getInstanceFor(final KEY key, final FactoryInterface factory) { |
112 |
DIALOG dialog; |
final DIALOG dialog; |
113 |
if (isVisibleFor(key)) { |
if (isVisibleFor(key)) { |
114 |
dialog = dialogCache.get(key); |
dialog = dialogCache.get(key); |
115 |
} else { |
} else { |
116 |
|
|
117 |
dialog = factory.create(); |
dialog = factory.create(); |
|
|
|
118 |
dialogCache.put(key, dialog); |
dialogCache.put(key, dialog); |
119 |
|
dialog.setVisible(true); |
120 |
|
dialog.toFront(); |
121 |
|
|
122 |
dialog.addWindowListener(new WindowAdapter() { |
dialog.addWindowListener(new WindowAdapter() { |
123 |
@Override |
@Override |
124 |
public void windowClosed(final WindowEvent e) { |
public void windowClosed(final WindowEvent e) { |
125 |
|
dialog.setVisible(false); |
126 |
|
factory.beforeDispose(dialog); |
127 |
disposeInstanceFor(key); |
disposeInstanceFor(key); |
128 |
} |
} |
129 |
}); |
}); |
130 |
|
|
131 |
|
factory.afterCreation(dialog); |
132 |
} |
} |
133 |
|
|
134 |
|
|
135 |
return dialog; |
return dialog; |
136 |
} |
} |
137 |
|
|
156 |
return atLeastOne; |
return atLeastOne; |
157 |
} |
} |
158 |
|
|
159 |
|
/** |
160 |
|
* @return All instances of DIALOG as they are cached. |
161 |
|
*/ |
162 |
|
public Collection<DIALOG> getAllInstances() { |
163 |
|
return dialogCache.values(); |
164 |
|
} |
165 |
|
|
166 |
|
|
167 |
} |
} |