/[schmitzm]/trunk/src/skrueger/swing/DialogManager.java
ViewVC logotype

Contents of /trunk/src/skrueger/swing/DialogManager.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 461 - (show annotations)
Tue Oct 13 09:22:23 2009 UTC (15 years, 4 months ago) by alfonx
Original Path: branches/1.0-gt2-2.6/src/skrueger/swing/DialogManager.java
File MIME type: text/plain
File size: 3858 byte(s)
* New class AtlasDialog is a standardized JDialog that reacts to ESC and disposes on WindowClose-Event.
* Regression BugFix: ChartDialog now correctly kills selection after close.
* AS-Bugfix: Class numbers are now correctly updated. No more black histogram images.
1 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.Collection;
7 import java.util.HashMap;
8 import java.util.HashSet;
9
10 import javax.swing.JDialog;
11
12 import org.apache.log4j.Logger;
13
14 public abstract class DialogManager<KEY, DIALOG extends AtlasDialog> {
15 final static private Logger LOGGER = Logger.getLogger(DialogManager.class);
16
17 public abstract class FactoryInterface {
18
19 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 protected HashMap<KEY, DIALOG> dialogCache = new HashMap<KEY, DIALOG>();
30
31 /**
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 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 dialogCache.remove(chartId);
88 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 * @return Always a visible and inFront instance of DIALOG for the given
103 * key.
104 */
105 public DIALOG getInstanceFor(final KEY key, final FactoryInterface factory) {
106 final DIALOG dialog;
107 if (isVisibleFor(key)) {
108 dialog = dialogCache.get(key);
109 } else {
110
111 dialog = factory.create();
112 dialogCache.put(key, dialog);
113 dialog.setVisible(true);
114 dialog.toFront();
115
116 dialog.addWindowListener(new WindowAdapter() {
117 @Override
118 public void windowClosed(final WindowEvent e) {
119 dialog.setVisible(false);
120 factory.beforeDispose(dialog);
121 disposeInstanceFor(key);
122 }
123 });
124
125 factory.afterCreation(dialog);
126 }
127
128
129 return dialog;
130 }
131
132 /**
133 * Disposes all open instances and removes them from the cache.
134 *
135 * @return <code>true</code> if at least one window has been disposed.
136 */
137 public boolean disposeAll() {
138
139 boolean atLeastOne = false;
140 HashSet<KEY> tempKeys = new HashSet<KEY>(dialogCache.keySet());
141 for (KEY key : tempKeys) {
142 DIALOG dialog = dialogCache.get(key);
143 if (dialog != null) {
144 dialog.dispose();
145 atLeastOne = true;
146 }
147 }
148 tempKeys.clear();
149 dialogCache.clear();
150 return atLeastOne;
151 }
152
153 /**
154 * @return All instances of DIALOG as they are cached.
155 */
156 public Collection<DIALOG> getAllInstances() {
157 return dialogCache.values();
158 }
159
160
161 }

Properties

Name Value
svn:eol-style native
svn:keywords Id URL
svn:mime-type text/plain

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26