1 package nl.tudelft.goal.ut2004.visualizer.util;
2
3 import java.awt.Dimension;
4 import java.awt.Point;
5 import java.awt.Window;
6 import java.awt.event.ComponentAdapter;
7 import java.awt.event.ComponentEvent;
8 import java.util.prefs.Preferences;
9
10
11
12
13
14
15 public class WindowPersistenceHelper {
16
17 private static final String WINDOW_Y = "WINDOW_Y";
18 private static final String WINDOW_X = "WINDOW_X";
19 private static final String WINDOW_HEIGHT = "WINDOW_HEIGHT";
20 private static final String WINDOW_WIDTH = "WINDOW_WIDTH";
21
22 private final Window window;
23 private final Preferences preferences;
24 private final String className;
25
26 public WindowPersistenceHelper(Window component) {
27 this.window = component;
28 this.preferences = Preferences.userNodeForPackage(component.getClass());
29 this.className = component.getClass().getSimpleName();
30
31
32 window.addComponentListener(new ComponentAdapter() {
33
34 @Override
35 public void componentResized(ComponentEvent e) {
36 save();
37
38 }
39
40 @Override
41 public void componentMoved(ComponentEvent e) {
42 save();
43 }
44
45 });
46 }
47
48 public void load() {
49
50 int width = preferences.getInt(className + WINDOW_WIDTH, window.getWidth());
51 int height = preferences.getInt(className + WINDOW_HEIGHT, window.getHeight());
52 window.setSize(width, height);
53
54 int x = preferences.getInt(className + WINDOW_X, window.getX());
55 int y = preferences.getInt(className + WINDOW_Y, window.getY());
56 window.setLocation(x, y);
57
58 }
59
60 public void save() {
61 Dimension size = window.getSize();
62 preferences.putInt(className + WINDOW_WIDTH, window.getWidth());
63 preferences.putInt(className + WINDOW_HEIGHT, window.getHeight());
64
65 Point position = window.getLocation();
66 preferences.putInt(className + WINDOW_X, window.getX());
67 preferences.putInt(className + WINDOW_Y, window.getY());
68
69 }
70
71 }