1
2
3
4
5
6
7
8
9
10
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
31
32
33
34 public class GlobalContext {
35
36
37 protected Preferences preferences;
38
39
40 protected Map<JFrame, String> windows = new HashMap<JFrame, String>();
41
42
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
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
82
83
84
85 public GlobalContext() throws IOException {
86 preferences = new Preferences(".aequo.config");
87 preferences.load();
88 }
89
90
91
92
93
94 public Preferences getPreferences() {
95 return preferences;
96 }
97
98
99
100
101
102
103
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
134
135
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 }