/[schmitzm]/branches/1.0-gt2-2.6/src/skrueger/swing/DialogManager.java
ViewVC logotype

Contents of /branches/1.0-gt2-2.6/src/skrueger/swing/DialogManager.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 422 - (show annotations)
Fri Oct 2 00:47:48 2009 UTC (15 years, 5 months ago) by alfonx
File MIME type: text/plain
File size: 3472 byte(s)
* GP-Feature: Improved/unified dialog management moved further.
* GP-Feature: popup menu in MenuTree allows to edit DpEntries and Maps
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 JDialog> {
15 final static private Logger LOGGER = Logger.getLogger(DialogManager.class);
16
17 public abstract class FactoryInterface {
18
19 public abstract DIALOG create();
20
21 }
22
23 protected HashMap<KEY, DIALOG> dialogCache = new HashMap<KEY, DIALOG>();
24
25 /**
26 * A {@link DialogManager} instance can be created for any extension of
27 * {@link JDialog} that will implement the
28 * {@link #getInstanceFor(Object, Component, Object...)} method.
29 */
30 public DialogManager() {
31 }
32
33 /**
34 * This will be done with every dialog that an instance is required for.
35 *
36 * @param dialog
37 * @return
38 */
39 protected DIALOG bringup(DIALOG dialog) {
40 if (!dialog.isVisible())
41 dialog.setVisible(true);
42 dialog.toFront();
43
44 return dialog;
45 }
46
47 public abstract DIALOG getInstanceFor(final KEY key, final Component owner,
48 final Object... constArgs);
49
50 /**
51 * @return Is there an open/visible dialog for the given layer id?
52 */
53 public boolean isVisibleFor(KEY key) {
54 return dialogCache.containsKey(key) && dialogCache.get(key).isVisible();
55 }
56
57 /**
58 * Will dispose any dialog that is registered to the given parent
59 * {@link Component}
60 *
61 * @param parent
62 */
63 public void disposeInstanceForParent(final Component parent) {
64
65 final HashMap<KEY, JDialog> clonedHashMap = (HashMap<KEY, JDialog>) dialogCache
66 .clone();
67
68 for (KEY chartId : clonedHashMap.keySet()) {
69 if (dialogCache.get(chartId).getParent() == parent) {
70 disposeInstanceFor(chartId);
71 }
72 }
73 }
74
75 public boolean disposeInstanceFor(KEY chartId) {
76 synchronized (dialogCache) {
77
78 final DIALOG dialog = dialogCache.get(chartId);
79 if (dialog != null) {
80 dialog.dispose();
81 dialogCache.remove(chartId);
82 return true;
83 }
84 return false;
85 }
86 }
87
88 /**
89 * Checks whether there already is an instance for that key and otherwise
90 * will create the instance by invoking the {@link FactoryInterface} #create
91 * method.
92 *
93 * @param factory
94 * {@link FactoryInterface} that creates the DIALOG
95 *
96 * @return Always a visible and inFront instance of DIALOG for the given
97 * key.
98 */
99 public DIALOG getInstanceFor(final KEY key, FactoryInterface factory) {
100 DIALOG dialog;
101 if (isVisibleFor(key)) {
102 dialog = dialogCache.get(key);
103 } else {
104
105 dialog = factory.create();
106
107 dialogCache.put(key, dialog);
108
109 dialog.addWindowListener(new WindowAdapter() {
110 @Override
111 public void windowClosed(final WindowEvent e) {
112 disposeInstanceFor(key);
113 }
114 });
115 }
116 return dialog;
117 }
118
119 /**
120 * Disposes all open instances and removes them from the cache.
121 *
122 * @return <code>true</code> if at least one window has been disposed.
123 */
124 public boolean disposeAll() {
125
126 boolean atLeastOne = false;
127 HashSet<KEY> tempKeys = new HashSet<KEY>(dialogCache.keySet());
128 for (KEY key : tempKeys) {
129 DIALOG dialog = dialogCache.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 /**
141 * @return All instances of DIALOG as they are cached.
142 */
143 public Collection<DIALOG> getAllInstances() {
144 return dialogCache.values();
145 }
146
147
148 }

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