1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.finalist.jaggenerator;
18
19 import javax.swing.*;
20 import java.util.prefs.Preferences;
21 import java.awt.*;
22
23 /***
24 * The Settings class handles the loading and saving of user-settings like the current Window-size,
25 * divider-locations, etc.
26 *
27 * @author Onno Scheffers
28 */
29 public class Settings {
30
31 private static final Preferences PREFS = Preferences.userNodeForPackage(JagGenerator.class);
32
33 private static final String KEY_VERTICAL_DIVIDER_POSITION = "vdiv";
34 private static final int DEFAULT_VERTICAL_DIVIDER_POSITION = 400;
35 private static final String KEY_HORIZONTAL_DIVIDER_POSITION = "hdiv";
36 private static final int DEFAULT_HORIZONTAL_DIVIDER_POSITION = 444;
37
38 private static final String KEY_WINDOW_X = "winx";
39 private static final int DEFAULT_WINDOW_X = Integer.MIN_VALUE;
40 private static final String KEY_WINDOW_Y = "winy";
41 private static final int DEFAULT_WINDOW_Y = Integer.MIN_VALUE;
42 private static final String KEY_WINDOW_WIDTH = "winwidth";
43 private static final int DEFAULT_WINDOW_WIDTH = 1000;
44 private static final String KEY_WINDOW_HEIGHT = "winheight";
45 private static final int DEFAULT_WINDOW_HEIGHT = 760;
46
47 private static final String KEY_MAXIMIZED = "winmaximized";
48 private static final boolean DEFAULT_MAXIMIZED = false;
49
50 private static int verticalDividerPosition;
51 private static int horizontalDividerPosition;
52 private static int windowX;
53 private static int windowY;
54 private static int windowWidth;
55 private static int windowHeight;
56 private static boolean isMaximized;
57
58 /***
59 * Static method for reading the user-settings
60 */
61 public static void read() {
62 Settings.verticalDividerPosition = PREFS.getInt(KEY_VERTICAL_DIVIDER_POSITION, DEFAULT_VERTICAL_DIVIDER_POSITION);
63 Settings.horizontalDividerPosition = PREFS.getInt(KEY_HORIZONTAL_DIVIDER_POSITION, DEFAULT_HORIZONTAL_DIVIDER_POSITION);
64 Settings.windowX = PREFS.getInt(KEY_WINDOW_X, DEFAULT_WINDOW_X);
65 Settings.windowY = PREFS.getInt(KEY_WINDOW_Y, DEFAULT_WINDOW_Y);
66 Settings.windowWidth = PREFS.getInt(KEY_WINDOW_WIDTH, DEFAULT_WINDOW_WIDTH);
67 Settings.windowHeight = PREFS.getInt(KEY_WINDOW_HEIGHT, DEFAULT_WINDOW_HEIGHT);
68 Settings.isMaximized = PREFS.getBoolean(KEY_MAXIMIZED, DEFAULT_MAXIMIZED);
69 }
70
71 /***
72 * Static method for writing the user-settings.
73 */
74 public static void write() {
75 PREFS.putInt(KEY_VERTICAL_DIVIDER_POSITION, verticalDividerPosition);
76 PREFS.putInt(KEY_HORIZONTAL_DIVIDER_POSITION, horizontalDividerPosition);
77 PREFS.putInt(KEY_WINDOW_X, windowX);
78 PREFS.putInt(KEY_WINDOW_Y, windowY);
79 PREFS.putInt(KEY_WINDOW_WIDTH, windowWidth);
80 PREFS.putInt(KEY_WINDOW_HEIGHT, windowHeight);
81 PREFS.putBoolean(KEY_MAXIMIZED, isMaximized);
82 }
83
84 /***
85 * Returns the last-known vertical divider position.
86 *
87 * @return the last-known vertical divider position.
88 */
89 public static int getVerticalDividerPosition() {
90 return verticalDividerPosition;
91 }
92
93 /***
94 * Sets a new value for the vertical divider position that will be stored
95 * the next time you call <code>write()</code>.
96 *
97 * @param verticalDividerPosition The value to store for the vertical divider position.
98 */
99 public static void setVerticalDividerPosition(int verticalDividerPosition) {
100 Settings.verticalDividerPosition = verticalDividerPosition;
101 }
102
103 /***
104 * Returns the last-known horizontal divider position.
105 *
106 * @return the last-known horizontal divider position.
107 */
108 public static int getHorizontalDividerPosition() {
109 return horizontalDividerPosition;
110 }
111
112 /***
113 * Sets a new value for the horizontal divider position that will be stored
114 * the next time you call <code>write()</code>.
115 *
116 * @param horizontalDividerPosition The value to store for the horizontal divider position.
117 */
118 public static void setHorizontalDividerPosition(int horizontalDividerPosition) {
119 Settings.horizontalDividerPosition = horizontalDividerPosition;
120 }
121
122 /***
123 * Returns the last-known maximized state.
124 *
125 * @return the last-known maximized state.
126 */
127 public static boolean isMaximized() {
128 return isMaximized;
129 }
130
131 /***
132 * Sets a new value for the maximized state that will be stored
133 * the next time you call <code>write()</code>.
134 *
135 * @param maximized The value to store for the maximized state.
136 */
137 public static void setMaximized(boolean maximized) {
138 isMaximized = maximized;
139 }
140
141 /***
142 * Returns the last-known window bounds.
143 *
144 * @return the last-known window bounds.
145 */
146 public static Rectangle getUserWindowBounds(JFrame frame) {
147 if(windowX == Integer.MIN_VALUE || windowY == Integer.MIN_VALUE) {
148
149 Dimension screenSize = frame.getToolkit().getScreenSize();
150 windowX = (int) (screenSize.getWidth() /2 - windowWidth /2);
151 windowY = (int) (screenSize.getHeight()/2 - windowHeight/2);
152 }
153 return new Rectangle(windowX, windowY, windowWidth, windowHeight);
154 }
155
156 /***
157 * Sets a new value for the window bounds that will be stored
158 * the next time you call <code>write()</code>.
159 *
160 * @param bounds The value to store for the window bounds.
161 */
162 public static void setUserWindowBounds(Rectangle bounds) {
163 windowX = (int) bounds.getX();
164 windowY = (int) bounds.getY();
165 windowWidth = (int) bounds.getWidth();
166 windowHeight = (int) bounds.getHeight();
167 }
168 }