/[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 421 - (show annotations)
Thu Oct 1 21:06:37 2009 UTC (15 years, 5 months ago) by alfonx
Original Path: branches/1.0-gt2-2.6/src/skrueger/swing/DialogManager.java
File MIME type: text/plain
File size: 3292 byte(s)
* Last commit before I start changing the DesignAtlasMapViewDialog to extend CancellableDialogAdapter AND switch it's logic to always work on a copy/clone of the map.
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.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> {
14 final static private Logger LOGGER = Logger.getLogger(DialogManager.class);
15
16 public abstract class FactoryInterface {
17
18 public abstract DIALOG create();
19
20 }
21
22 private HashMap<KEY, DIALOG> dialogCache = new HashMap<KEY, DIALOG>();
23
24 /**
25 * A {@link DialogManager} instance can be created for any extension of
26 * {@link JDialog} that will implement the
27 * {@link #getInstanceFor(Object, Component, Object...)} method.
28 */
29 public DialogManager() {
30 }
31
32 /**
33 * This will be done with every dialog that an instance is required for.
34 *
35 * @param dialog
36 * @return
37 */
38 protected DIALOG bringup(DIALOG dialog) {
39 if (!dialog.isVisible())
40 dialog.setVisible(true);
41 dialog.toFront();
42
43 return dialog;
44 }
45
46 public abstract DIALOG getInstanceFor(final KEY key, final Component owner,
47 final Object... constArgs);
48
49 /**
50 * @return Is there an open/visible dialog for the given layer id?
51 */
52 public boolean isVisibleFor(KEY key) {
53 return dialogCache.containsKey(key) && dialogCache.get(key).isVisible();
54 }
55
56 /**
57 * Will dispose any dialog that is registered to the given parent
58 * {@link Component}
59 *
60 * @param parent
61 */
62 public void disposeInstanceForParent(final Component parent) {
63
64 final HashMap<KEY, JDialog> clonedHashMap = (HashMap<KEY, JDialog>) dialogCache
65 .clone();
66
67 for (KEY chartId : clonedHashMap.keySet()) {
68 if (dialogCache.get(chartId).getParent() == parent) {
69 disposeInstanceFor(chartId);
70 }
71 }
72 }
73
74 public boolean disposeInstanceFor(KEY chartId) {
75 synchronized (dialogCache) {
76
77 final DIALOG dialog = dialogCache.get(chartId);
78 if (dialog != null) {
79 dialog.dispose();
80 dialogCache.remove(chartId);
81 return true;
82 }
83 return false;
84 }
85 }
86
87 /**
88 * Checks whether there already is an instance for that key and otherwise
89 * will create the instance by invoking the {@link FactoryInterface} #create
90 * method.
91 *
92 * @param factory
93 * {@link FactoryInterface} that creates the DIALOG
94 *
95 * @return Always a visible and inFront instance of DIALOG for the given
96 * key.
97 */
98 public DIALOG getInstanceFor(final KEY key, FactoryInterface factory) {
99 DIALOG dialog;
100 if (isVisibleFor(key)) {
101 dialog = dialogCache.get(key);
102 } else {
103
104 dialog = factory.create();
105
106 dialogCache.put(key, dialog);
107
108 dialog.addWindowListener(new WindowAdapter() {
109 @Override
110 public void windowClosed(final WindowEvent e) {
111 disposeInstanceFor(key);
112 }
113 });
114 }
115 return dialog;
116 }
117
118 /**
119 * Disposes all open instances and removes them from the cache.
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.get(key);
129 if (dialog != null) {
130 dialog.dispose();
131 atLeastOne = true;
132 }
133 }
134 tempKeys.clear();
135 dialogCache.clear();
136 return atLeastOne;
137 }
138
139 }

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