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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

branches/1.0-gt2-2.6/src/skrueger/swing/DialogManager.java revision 420 by alfonx, Thu Oct 1 20:22:48 2009 UTC branches/2.0-RC2/src/skrueger/swing/DialogManager.java revision 621 by alfonx, Thu Jan 28 10:06:05 2010 UTC
# Line 3  package skrueger.swing; Line 3  package skrueger.swing;
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    
# Line 10  import javax.swing.JDialog; Line 11  import javax.swing.JDialog;
11    
12  import org.apache.log4j.Logger;  import org.apache.log4j.Logger;
13    
14  public abstract class DialogManager<KEY, DIALOG extends JDialog> extends  public abstract class DialogManager<KEY, DIALOG extends AtlasDialog> {
15                  JDialog {          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          final Logger LOGGER = Logger.getLogger(DialogManager.class);          }
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
33             * {@link JDialog} that will implement the
34             * {@link #getInstanceFor(Object, Component, Object...)} method.
35             */
36          public DialogManager() {          public DialogManager() {
37          }          }
38    
# Line 40  public abstract class DialogManager<KEY, Line 50  public abstract class DialogManager<KEY,
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    
# Line 68  public abstract class DialogManager<KEY, Line 84  public abstract class DialogManager<KEY,
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                                  if (dialogCache.remove(chartId) == null) {                                  dialogCache.remove(key);
                                         LOGGER.warn("Hae?!?!"); //TODO cleanup  
                                 }  
94                                  return true;                                  return true;
95                          }                          }
96                          return false;                          return false;
# Line 88  public abstract class DialogManager<KEY, Line 102  public abstract class DialogManager<KEY,
102           * will create the instance by invoking the {@link FactoryInterface} #create           * will create the instance by invoking the {@link FactoryInterface} #create
103           * method.           * method.
104           *           *
          * @param key  
105           * @param factory           * @param factory
106           *            {@link FactoryInterface} that creates the DIALOG           *            {@link FactoryInterface} that creates the DIALOG
107           *           *
108           * @return Always a visible and inFront instance for the given key.           * @return Always a visible and inFront instance of DIALOG for the given
109             *         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                                          disposeInstanceFor(key);                                          dialogCache.remove(key);
126                                            factory.beforeDispose(dialog);
127                                    }
128    
129                                    @Override
130                                    public void windowClosing(final WindowEvent e) {
131                                  }                                  }
132                          });                          });
133                            
134                            factory.afterCreation(dialog);
135                  }                  }
136                    
137    
138                  return dialog;                  return dialog;
139          }          }
           
140    
141          /**          /**
142           * Disposes all open instances.           * Disposes all open instances and removes them from the cache.
143           *           *
144           * @return <code>true</code> if at least one window has been disposed.           * @return <code>true</code> if at least one window has been disposed.
145           */           */
146          public boolean disposeAll() {          public boolean disposeAll() {
147                    
148                  boolean atLeastOne = false;                  boolean atLeastOne = false;
149                  HashSet<KEY> tempKeys = new HashSet<KEY>(dialogCache.keySet());                  HashSet<KEY> tempKeys = new HashSet<KEY>(dialogCache.keySet());
150                  for (KEY key : tempKeys) {                  for (KEY key : tempKeys) {
151                          DIALOG dialog = dialogCache                          DIALOG dialog = dialogCache.get(key);
                                         .get(key);  
152                          if (dialog != null) {                          if (dialog != null) {
153                                  dialog.dispose();                                  dialog.dispose();
154                                  atLeastOne = true;                                  atLeastOne = true;
# Line 136  public abstract class DialogManager<KEY, Line 158  public abstract class DialogManager<KEY,
158                  dialogCache.clear();                  dialogCache.clear();
159                  return atLeastOne;                  return atLeastOne;
160          }          }
161            
162            /**
163             * @return All instances of DIALOG as they are cached.
164             */
165            public Collection<DIALOG> getAllInstances() {
166                    return dialogCache.values();
167            }
168    
169    
170  }  }

Legend:
Removed from v.420  
changed lines
  Added in v.621

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26