/[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

revision 420 by alfonx, Thu Oct 1 20:22:48 2009 UTC revision 433 by alfonx, Sun Oct 4 16:57:29 2009 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 JDialog> {
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 appendListeners(DIALOG newInstance){};
23    //
24    //              /** May be overridden to remove Listeners added earlier **/
25    //              public void removeListeners(DIALOG newInstance){};
26    
27          }          }
28    
29          final Logger LOGGER = Logger.getLogger(DialogManager.class);          protected HashMap<KEY, DIALOG> dialogCache = new HashMap<KEY, DIALOG>();
   
         private 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 74  public abstract class DialogManager<KEY, Line 84  public abstract class DialogManager<KEY,
84                          final DIALOG dialog = dialogCache.get(chartId);                          final DIALOG dialog = dialogCache.get(chartId);
85                          if (dialog != null) {                          if (dialog != null) {
86                                  dialog.dispose();                                  dialog.dispose();
87                                  if (dialogCache.remove(chartId) == null) {                                  dialogCache.remove(chartId);
                                         LOGGER.warn("Hae?!?!"); //TODO cleanup  
                                 }  
88                                  return true;                                  return true;
89                          }                          }
90                          return false;                          return false;
# Line 88  public abstract class DialogManager<KEY, Line 96  public abstract class DialogManager<KEY,
96           * will create the instance by invoking the {@link FactoryInterface} #create           * will create the instance by invoking the {@link FactoryInterface} #create
97           * method.           * method.
98           *           *
          * @param key  
99           * @param factory           * @param factory
100           *            {@link FactoryInterface} that creates the DIALOG           *            {@link FactoryInterface} that creates the DIALOG
101           *           *
102           * @return Always a visible and inFront instance for the given key.           * @return Always a visible and inFront instance of DIALOG for the given
103             *         key.
104           */           */
105          public DIALOG getInstanceFor(final KEY key, FactoryInterface factory) {          public DIALOG getInstanceFor(final KEY key, FactoryInterface factory) {
106                  DIALOG dialog;                  DIALOG dialog;
# Line 110  public abstract class DialogManager<KEY, Line 118  public abstract class DialogManager<KEY,
118                                          disposeInstanceFor(key);                                          disposeInstanceFor(key);
119                                  }                                  }
120                          });                          });
121                            
122                            factory.appendListeners(dialog);
123                  }                  }
124                  return dialog;                  return dialog;
125          }          }
           
126    
127          /**          /**
128           * Disposes all open instances.           * Disposes all open instances and removes them from the cache.
129           *           *
130           * @return <code>true</code> if at least one window has been disposed.           * @return <code>true</code> if at least one window has been disposed.
131           */           */
132          public boolean disposeAll() {          public boolean disposeAll() {
133                    
134                  boolean atLeastOne = false;                  boolean atLeastOne = false;
135                  HashSet<KEY> tempKeys = new HashSet<KEY>(dialogCache.keySet());                  HashSet<KEY> tempKeys = new HashSet<KEY>(dialogCache.keySet());
136                  for (KEY key : tempKeys) {                  for (KEY key : tempKeys) {
137                          DIALOG dialog = dialogCache                          DIALOG dialog = dialogCache.get(key);
                                         .get(key);  
138                          if (dialog != null) {                          if (dialog != null) {
139                                  dialog.dispose();                                  dialog.dispose();
140                                  atLeastOne = true;                                  atLeastOne = true;
# Line 136  public abstract class DialogManager<KEY, Line 144  public abstract class DialogManager<KEY,
144                  dialogCache.clear();                  dialogCache.clear();
145                  return atLeastOne;                  return atLeastOne;
146          }          }
147            
148            /**
149             * @return All instances of DIALOG as they are cached.
150             */
151            public Collection<DIALOG> getAllInstances() {
152                    return dialogCache.values();
153            }
154    
155    
156  }  }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26