View Javadoc

1   /*
2    * Copyright (c) 2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.aequo;
14  
15  import java.awt.Dimension;
16  import java.awt.Toolkit;
17  import java.awt.event.KeyEvent;
18  import java.awt.event.KeyListener;
19  import java.awt.event.WindowEvent;
20  import java.awt.event.WindowListener;
21  import java.io.IOException;
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import javax.swing.JFrame;
26  
27  import org.abstracthorizon.aequo.util.Preferences;
28  
29  /**
30   * Global context
31   *
32   * @author Daniel Sendula
33   */
34  public class GlobalContext {
35  
36      /** Preferences */
37      protected Preferences preferences;
38      
39      /** Map of window ids */
40      protected Map<JFrame, String> windows = new HashMap<JFrame, String>();
41      
42      /** Window listener */
43      protected WindowListener windowListener = new WindowListener() {
44          public void windowActivated(WindowEvent e) { }
45  
46          public void windowClosed(WindowEvent e) {
47              closeWindow((JFrame)e.getComponent());
48          }
49  
50          public void windowClosing(WindowEvent e) {
51              closeWindow((JFrame)e.getComponent());
52          }
53  
54          public void windowDeactivated(WindowEvent e) { }
55  
56          public void windowDeiconified(WindowEvent e) { }
57  
58          public void windowIconified(WindowEvent e) { }
59  
60          public void windowOpened(WindowEvent e) { }
61      };
62      
63      /** Key listener */
64      protected KeyListener keyListener = new KeyListener() {
65          public void keyPressed(KeyEvent e) {
66              if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
67                  closeWindow((JFrame)e.getComponent());
68              }
69          }
70          
71          public void keyReleased(KeyEvent e) { }
72  
73          public void keyTyped(KeyEvent e) {
74              if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
75                  closeWindow((JFrame)e.getComponent());
76              }
77          }
78      };
79      
80      /**
81       * Constructor
82       * 
83       * @throws IOException thrown in loading of preferences failed
84       */
85      public GlobalContext() throws IOException {
86          preferences = new Preferences(".aequo.config");
87          preferences.load();
88      }
89  
90      /**
91       * Returns preferences 
92       * @return preferences
93       */
94      public Preferences getPreferences() {
95          return preferences;
96      }
97      
98      /**
99       * Adds new window. It checks if that id already exists in the preferences and if so
100      * sets window's bounds to previously stored.
101      * 
102      * @param window window
103      * @param id window id
104      */
105     public void addWindow(JFrame window, String id) {
106         windows.put(window, id);
107         
108         window.addWindowListener(windowListener);
109         window.addKeyListener(keyListener);
110         
111         Dimension windowSize = window.getPreferredSize();
112         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
113         
114         
115         String xs = preferences.getProperty(id + ".x", Integer.toString((screenSize.width - windowSize.width) / 2));
116         String ys = preferences.getProperty(id + ".y", Integer.toString((screenSize.height - windowSize.height) / 2));
117         String ws = preferences.getProperty(id + ".w", Integer.toString(windowSize.width));
118         String hs = preferences.getProperty(id + ".h", Integer.toString(windowSize.height));
119         if ((xs != null) && (ys != null) && (hs != null) && (ws != null)) {
120             try {
121                 int x = Integer.parseInt(xs);
122                 int y = Integer.parseInt(ys);
123                 int w = Integer.parseInt(ws);
124                 int h = Integer.parseInt(hs);
125                 window.setBounds(x, y, w, h);
126             } catch (NumberFormatException ignore) {
127                 window.pack();
128             }
129         }
130     }
131     
132     /**
133      * Stores the bounds of the window in preferences and closes it.
134      * 
135      * @param window window
136      */
137     public void closeWindow(JFrame window) {
138         String id = windows.get(window);
139         windows.remove(window);
140         if (id != null) {
141             if (window.isVisible()) {
142                 preferences.setProperty(id + ".x", Integer.toString(window.getX()));
143                 preferences.setProperty(id + ".y", Integer.toString(window.getY()));
144                 preferences.setProperty(id + ".w", Integer.toString(window.getWidth()));
145                 preferences.setProperty(id + ".h", Integer.toString(window.getHeight()));
146                 try {
147                     preferences.save();
148                 } catch (IOException ignore) {
149                 }
150             }
151         }
152         window.removeWindowListener(windowListener);
153         window.removeKeyListener(keyListener);
154         window.dispose();
155         if (windows.size() == 0) {
156             System.exit(0);
157         }
158     }
159     
160     
161 }