1   package org.wcb.gui.util;
2   
3   /**
4    * <small>
5    * <p/>
6    * Copyright (c)  2006  wbogaardt.
7    * This library is free software; you can redistribute it and/or
8    * modify it under the terms of the GNU Lesser General Public
9    * License as published by the Free Software Foundation; either
10   * version 2.1 of the License, or (at your option) any later version.
11   *
12   * This library is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this library; if not, write to the Free Software
19   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20   * <p/>
21   * $File:  $ <br>
22   * $Change:  $ submitted by $Author: wbogaardt $ at $DateTime: Sep 19, 2006 11:37:37 AM $ <br>
23   * </small>
24   *
25   * @author wbogaardt
26   *         Saves the state of the application after the user closes it.
27   */
28  
29  import javax.swing.*;
30  import java.awt.event.*;
31  import java.awt.*;
32  import java.util.Map;
33  import java.util.HashMap;
34  import java.util.logging.Logger;
35  import java.util.logging.Level;
36  
37  
38  public class WindowStateSaver implements AWTEventListener {
39  
40      private static WindowStateSaver saver;
41      private Map framemap;
42      private Logger logger = Logger.getLogger(WindowStateSaver.class.getName());
43      private WindowStateSaver() {
44          framemap = new HashMap();
45      }
46  
47      public static WindowStateSaver getInstance() {
48          if (saver == null) {
49              saver = new WindowStateSaver();
50          }
51          return saver;
52      }
53  
54      public void eventDispatched(AWTEvent evt) {
55          try {
56              if (evt.getID() == WindowEvent.WINDOW_OPENED) {
57                  ComponentEvent cev = (ComponentEvent) evt;
58                  if (cev.getComponent() instanceof JFrame) {
59                      JFrame frame = (JFrame) cev.getComponent();
60                      loadSettings(frame);
61                  }
62              }
63          } catch(Exception ex) {
64              logger.log(Level.WARNING, "Unable to load config settings.", ex);
65          }
66      }
67  
68      public static void loadSettings(JFrame frame) {
69          String name = frame.getName();
70          int x = ApplicationPreferences.getInstance().getInt(name + ".x", 10);
71          int y = ApplicationPreferences.getInstance().getInt(name + ".y", 10);
72          int w = ApplicationPreferences.getInstance().getInt(name + ".w", 800);
73          int h = ApplicationPreferences.getInstance().getInt(name + ".h", 620);
74          if( x == 10 && y == 10) {
75              UIHelper.centerApplicationToScreen(frame);
76          }  else {
77              frame.setLocation(x, y);
78          }
79          frame.setSize(new Dimension(w, h));
80          saver.framemap.put(name, frame);
81          frame.validate();
82      }
83  
84      public static void saveSettings(JFrame frame) {
85          String name = frame.getName();
86          ApplicationPreferences.getInstance().putInt(name + ".x", frame.getX());
87          ApplicationPreferences.getInstance().putInt(name + ".y", frame.getY());
88          ApplicationPreferences.getInstance().putInt(name + ".w", frame.getWidth());
89          ApplicationPreferences.getInstance().putInt(name + ".h", frame.getHeight());
90      }
91  
92  }
93  	
94