/[schmitzm]/branches/2.2.x/src/skrueger/swing/DialogManager.java
ViewVC logotype

Annotation of /branches/2.2.x/src/skrueger/swing/DialogManager.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 959 - (hide annotations)
Tue Aug 10 12:12:43 2010 UTC (14 years, 6 months ago) by alfonx
File MIME type: text/plain
File size: 4286 byte(s)
JavaDoc
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 alfonx 422 import java.util.Collection;
7 alfonx 420 import java.util.HashMap;
8     import java.util.HashSet;
9    
10     import javax.swing.JDialog;
11    
12     import org.apache.log4j.Logger;
13    
14 alfonx 959 import schmitzm.swing.ExceptionDialog;
15    
16 alfonx 461 public abstract class DialogManager<KEY, DIALOG extends AtlasDialog> {
17 alfonx 421 final static private Logger LOGGER = Logger.getLogger(DialogManager.class);
18 alfonx 420
19     public abstract class FactoryInterface {
20    
21     public abstract DIALOG create();
22 alfonx 959
23 alfonx 433 /** May be overridden to add Listeners **/
24 alfonx 959 public void afterCreation(DIALOG newInstance) {
25     };
26 alfonx 420
27 alfonx 435 /** May be overridden to remove Listeners added earlier **/
28 alfonx 959 public void beforeDispose(DIALOG newInstance) {
29     };
30 alfonx 435
31 alfonx 420 }
32    
33 alfonx 422 protected HashMap<KEY, DIALOG> dialogCache = new HashMap<KEY, DIALOG>();
34 alfonx 420
35 alfonx 421 /**
36     * A {@link DialogManager} instance can be created for any extension of
37     * {@link JDialog} that will implement the
38     * {@link #getInstanceFor(Object, Component, Object...)} method.
39     */
40 alfonx 420 public DialogManager() {
41     }
42    
43     /**
44     * This will be done with every dialog that an instance is required for.
45     *
46     * @param dialog
47     * @return
48     */
49     protected DIALOG bringup(DIALOG dialog) {
50     if (!dialog.isVisible())
51     dialog.setVisible(true);
52     dialog.toFront();
53    
54     return dialog;
55     }
56    
57 alfonx 474 /**
58 alfonx 959 * Implementing this should have a <br/>
59     * <code>try { ... return ... } catch (Exception e) {
60     ExceptionDialog.show(owner, e);
61     return null;
62     } </code> block.
63     *
64 alfonx 474 * @param key
65     * @param owner
66     * @param constArgs
67 alfonx 959 * @return a cached instance or creates a new instance. Instances are always
68     * returned visible and toFront.
69 alfonx 474 */
70 alfonx 420 public abstract DIALOG getInstanceFor(final KEY key, final Component owner,
71     final Object... constArgs);
72    
73     /**
74     * @return Is there an open/visible dialog for the given layer id?
75     */
76     public boolean isVisibleFor(KEY key) {
77     return dialogCache.containsKey(key) && dialogCache.get(key).isVisible();
78     }
79    
80     /**
81     * Will dispose any dialog that is registered to the given parent
82     * {@link Component}
83     *
84     * @param parent
85     */
86     public void disposeInstanceForParent(final Component parent) {
87    
88     final HashMap<KEY, JDialog> clonedHashMap = (HashMap<KEY, JDialog>) dialogCache
89     .clone();
90    
91     for (KEY chartId : clonedHashMap.keySet()) {
92     if (dialogCache.get(chartId).getParent() == parent) {
93     disposeInstanceFor(chartId);
94     }
95     }
96     }
97    
98 alfonx 470 public boolean disposeInstanceFor(KEY key) {
99 alfonx 420 synchronized (dialogCache) {
100    
101 alfonx 470 final DIALOG dialog = dialogCache.get(key);
102 alfonx 420 if (dialog != null) {
103     dialog.dispose();
104 alfonx 470 dialogCache.remove(key);
105 alfonx 420 return true;
106     }
107     return false;
108     }
109     }
110    
111     /**
112     * Checks whether there already is an instance for that key and otherwise
113     * will create the instance by invoking the {@link FactoryInterface} #create
114     * method.
115     *
116     * @param factory
117     * {@link FactoryInterface} that creates the DIALOG
118     *
119 alfonx 421 * @return Always a visible and inFront instance of DIALOG for the given
120     * key.
121 alfonx 420 */
122 alfonx 435 public DIALOG getInstanceFor(final KEY key, final FactoryInterface factory) {
123     final DIALOG dialog;
124 alfonx 420 if (isVisibleFor(key)) {
125     dialog = dialogCache.get(key);
126     } else {
127    
128     dialog = factory.create();
129     dialogCache.put(key, dialog);
130 alfonx 459 dialog.setVisible(true);
131     dialog.toFront();
132 alfonx 420
133     dialog.addWindowListener(new WindowAdapter() {
134     @Override
135     public void windowClosed(final WindowEvent e) {
136 alfonx 618 dialogCache.remove(key);
137 alfonx 459 factory.beforeDispose(dialog);
138 alfonx 420 }
139 alfonx 618
140     @Override
141     public void windowClosing(final WindowEvent e) {
142     }
143 alfonx 420 });
144 alfonx 959
145 alfonx 459 factory.afterCreation(dialog);
146 alfonx 420 }
147 alfonx 459
148 alfonx 420 return dialog;
149     }
150    
151     /**
152 alfonx 421 * Disposes all open instances and removes them from the cache.
153 alfonx 420 *
154     * @return <code>true</code> if at least one window has been disposed.
155     */
156     public boolean disposeAll() {
157 alfonx 421
158 alfonx 420 boolean atLeastOne = false;
159     HashSet<KEY> tempKeys = new HashSet<KEY>(dialogCache.keySet());
160     for (KEY key : tempKeys) {
161 alfonx 421 DIALOG dialog = dialogCache.get(key);
162 alfonx 420 if (dialog != null) {
163     dialog.dispose();
164     atLeastOne = true;
165     }
166     }
167     tempKeys.clear();
168     dialogCache.clear();
169     return atLeastOne;
170     }
171 alfonx 421
172 alfonx 422 /**
173     * @return All instances of DIALOG as they are cached.
174     */
175     public Collection<DIALOG> getAllInstances() {
176     return dialogCache.values();
177     }
178    
179 alfonx 420 }

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