RestorableFrame.java
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13: package xeij;
14:
15: import java.awt.*;
16: import java.awt.event.*;
17: import java.awt.image.*;
18: import java.util.*;
19: import javax.swing.*;
20:
21: public class RestorableFrame extends JFrame {
22:
23: static final boolean DEBUG = false;
24:
25:
26:
27:
28:
29: static class Info {
30:
31: String key;
32: RestorableFrame frame;
33: int x, y;
34: int width, height;
35: int state;
36: boolean opened;
37:
38:
39:
40: Info (String key, RestorableFrame frame) {
41: if (DEBUG) {
42: System.out.println ("Info " + key + (frame == null ? " null" : " frame"));
43: }
44: this.key = key;
45: this.frame = frame;
46: }
47:
48: void setFrame (RestorableFrame frame) {
49: if (DEBUG) {
50: System.out.println ("Info.setFrame" + key);
51: }
52: this.frame = frame;
53: }
54:
55:
56:
57: void set (int[] rect, int state, boolean opened) {
58: if (DEBUG) {
59: System.out.println ("Info.set " + key);
60: }
61: x = rect[0];
62: y = rect[1];
63: width = rect[2];
64: height = rect[3];
65: this.state = state;
66: this.opened = opened;
67: }
68:
69:
70:
71: void restore () {
72: if (DEBUG) {
73: System.out.println ("Info.restore " + key);
74: }
75:
76: test:
77: {
78: Rectangle testBounds = new Rectangle (x, y, width, 48);
79: for (GraphicsDevice gd : GraphicsEnvironment.getLocalGraphicsEnvironment ().getScreenDevices ()) {
80: for (GraphicsConfiguration gc : gd.getConfigurations ()) {
81: Rectangle intersectionBounds = testBounds.intersection (gc.getBounds ());
82: if (48 <= intersectionBounds.width && 48 <= intersectionBounds.height) {
83:
84: break test;
85: }
86: }
87: }
88:
89:
90: GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment ().getDefaultScreenDevice ().getDefaultConfiguration ();
91: Rectangle s = gc.getBounds ();
92: x = Math.min (x, s.x + s.width - 48);
93: if (0 < width) {
94: x = Math.max (x + width, s.x + 48) - width;
95: }
96: y = Math.min (y, s.y + s.height - 48);
97: y = Math.max (y, s.y);
98: }
99:
100: frame.setLocation (x, y);
101: if (0 < width && 0 < height) {
102: frame.setSize (width, height);
103: frame.setPreferredSize (new Dimension (width, height));
104: }
105: frame.setExtendedState (state);
106:
107: frame.addComponentListener (new ComponentAdapter () {
108: @Override public void componentMoved (ComponentEvent ce) {
109: record (false);
110: }
111: @Override public void componentResized (ComponentEvent ce) {
112: record (false);
113: }
114: });
115: frame.addWindowStateListener (new WindowStateListener () {
116: @Override public void windowStateChanged (WindowEvent we) {
117: record (false);
118: }
119: });
120: frame.addWindowListener (new WindowAdapter () {
121: @Override public void windowClosing (WindowEvent we) {
122:
123:
124: record (true);
125: }
126: @Override public void windowOpened (WindowEvent we) {
127:
128: record (false);
129: }
130: });
131: }
132:
133:
134:
135: void record (boolean closing) {
136: if (DEBUG) {
137: System.out.println ("Info.record " + key);
138: }
139: if (frame != GraphicsEnvironment.getLocalGraphicsEnvironment ().getDefaultScreenDevice ().getFullScreenWindow ()) {
140: boolean opened = !closing && frame.isShowing ();
141: if (opened) {
142: Point p = frame.getLocationOnScreen ();
143: Dimension d = frame.getSize ();
144: int state = frame.getExtendedState ();
145: if ((state & (Frame.ICONIFIED | Frame.MAXIMIZED_HORIZ)) == 0) {
146: x = p.x;
147: width = d.width;
148: }
149: if ((state & (Frame.ICONIFIED | Frame.MAXIMIZED_VERT)) == 0) {
150: y = p.y;
151: height = d.height;
152: }
153: this.state = state;
154: }
155: this.opened = opened;
156: }
157: }
158:
159:
160:
161: int[] getRect () {
162: if (DEBUG) {
163: System.out.println ("Info.getRect " + key);
164: }
165: return new int[] { x, y, width, height };
166: }
167:
168:
169:
170: int getState () {
171: if (DEBUG) {
172: System.out.println ("Info.getState " + key);
173: }
174: return state;
175: }
176:
177:
178:
179: boolean getOpened () {
180: if (DEBUG) {
181: System.out.println ("Info.getOpened " + key);
182: }
183: return opened;
184: }
185:
186:
187:
188: BufferedImage capture () {
189: if (DEBUG) {
190: System.out.println ("Info.capture " + key);
191: }
192: if (frame != null) {
193: try {
194: Point p = frame.getLocationOnScreen ();
195: Dimension d = frame.getSize ();
196: if (0 < d.width && 0 < d.height) {
197: return new Robot ().createScreenCapture (new Rectangle (p.x, p.y, d.width, d.height));
198: }
199: } catch (Exception e) {
200:
201:
202:
203:
204: }
205: }
206: return null;
207: }
208:
209: }
210:
211:
212:
213: static HashMap<String,Info> rfmMap = new HashMap<String,Info> ();
214:
215:
216:
217: public static void rfmSet (String key, int[] rect, int state, boolean opened) {
218: if (DEBUG) {
219: System.out.println ("rfmSet " + key);
220: }
221: Info i = rfmMap.get (key);
222: if (i == null) {
223: i = new Info (key, null);
224: rfmMap.put (key, i);
225: i.set (rect, state, opened);
226: } else {
227: i.set (rect, state, opened);
228: i.restore ();
229: }
230: }
231:
232:
233:
234: public static int[] rfmGetRect (String key) {
235: if (DEBUG) {
236: System.out.println ("rfmGetRect " + key);
237: }
238: return rfmMap.get (key).getRect ();
239: }
240:
241:
242:
243: public static int rfmGetState (String key) {
244: if (DEBUG) {
245: System.out.println ("rfmGetState " + key);
246: }
247: return rfmMap.get (key).getState ();
248: }
249:
250:
251:
252: public static boolean rfmGetOpened (String key) {
253: if (DEBUG) {
254: System.out.println ("rfmGetOpened " + key);
255: }
256: return rfmMap.get (key).getOpened ();
257: }
258:
259:
260:
261: Info info;
262:
263:
264:
265: public RestorableFrame (String key, String title) {
266: super (title, GraphicsEnvironment.getLocalGraphicsEnvironment ().getDefaultScreenDevice ().getDefaultConfiguration ());
267: if (DEBUG) {
268: System.out.println ("RestorableFrame " + key);
269: }
270: Info i = rfmMap.get (key);
271: if (i == null) {
272: info = i = new Info (key, this);
273: } else {
274: info = i;
275: i.setFrame (this);
276: i.restore ();
277: }
278: }
279:
280:
281:
282: public static BufferedImage rfmCapture (String key) {
283: if (DEBUG) {
284: System.out.println ("rfmCapture " + key);
285: }
286: return rfmMap.get (key).capture ();
287: }
288:
289:
290:
291: }
292:
293:
294: