001: /* Copyright (C) 2003 Finalist IT Group
002: *
003: * This file is part of JAG - the Java J2EE Application Generator
004: *
005: * JAG is free software; you can redistribute it and/or modify
006: * it under the terms of the GNU General Public License as published by
007: * the Free Software Foundation; either version 2 of the License, or
008: * (at your option) any later version.
009: * JAG is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: * You should have received a copy of the GNU General Public License
014: * along with JAG; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
016: */
017: package com.finalist.jaggenerator;
018:
019: import javax.swing.*;
020: import java.util.prefs.Preferences;
021: import java.awt.*;
022:
023: /**
024: * The Settings class handles the loading and saving of user-settings like the current Window-size,
025: * divider-locations, etc.
026: *
027: * @author Onno Scheffers
028: */
029: public class Settings {
030: // Retrieve current user's JAG-preferences
031: private static final Preferences PREFS = Preferences
032: .userNodeForPackage(JagGenerator.class);
033:
034: private static final String KEY_LAST_SELECTED_OUTPUT_DIR = "last_outputdir";
035:
036: // Store Divider positions
037: private static final String KEY_VERTICAL_DIVIDER_POSITION = "vdiv";
038: private static final int DEFAULT_VERTICAL_DIVIDER_POSITION = 400;
039: private static final String KEY_HORIZONTAL_DIVIDER_POSITION = "hdiv";
040: private static final int DEFAULT_HORIZONTAL_DIVIDER_POSITION = 444;
041: // Store Window-position and size
042: private static final String KEY_WINDOW_X = "winx";
043: private static final int DEFAULT_WINDOW_X = Integer.MIN_VALUE;
044: private static final String KEY_WINDOW_Y = "winy";
045: private static final int DEFAULT_WINDOW_Y = Integer.MIN_VALUE;
046: private static final String KEY_WINDOW_WIDTH = "winwidth";
047: private static final int DEFAULT_WINDOW_WIDTH = 1000;
048: private static final String KEY_WINDOW_HEIGHT = "winheight";
049: private static final int DEFAULT_WINDOW_HEIGHT = 760;
050: // Store window-maximized state
051: private static final String KEY_MAXIMIZED = "winmaximized";
052: private static final boolean DEFAULT_MAXIMIZED = false;
053: // static properties holding the values to read and/or store
054: private static int verticalDividerPosition;
055: private static int horizontalDividerPosition;
056: private static int windowX;
057: private static int windowY;
058: private static int windowWidth;
059: private static int windowHeight;
060: private static boolean isMaximized;
061:
062: public static String getLastSelectedOutputDir() {
063: return lastSelectedOutputDir;
064: }
065:
066: public static void setLastSelectedOutputDir(
067: String lastSelectedOutputDir) {
068: Settings.lastSelectedOutputDir = lastSelectedOutputDir;
069: }
070:
071: private static String lastSelectedOutputDir;
072:
073: /**
074: * Static method for reading the user-settings
075: */
076: public static void read() {
077: Settings.verticalDividerPosition = PREFS.getInt(
078: KEY_VERTICAL_DIVIDER_POSITION,
079: DEFAULT_VERTICAL_DIVIDER_POSITION);
080: Settings.horizontalDividerPosition = PREFS.getInt(
081: KEY_HORIZONTAL_DIVIDER_POSITION,
082: DEFAULT_HORIZONTAL_DIVIDER_POSITION);
083: Settings.windowX = PREFS.getInt(KEY_WINDOW_X, DEFAULT_WINDOW_X);
084: Settings.windowY = PREFS.getInt(KEY_WINDOW_Y, DEFAULT_WINDOW_Y);
085: Settings.windowWidth = PREFS.getInt(KEY_WINDOW_WIDTH,
086: DEFAULT_WINDOW_WIDTH);
087: Settings.windowHeight = PREFS.getInt(KEY_WINDOW_HEIGHT,
088: DEFAULT_WINDOW_HEIGHT);
089: Settings.isMaximized = PREFS.getBoolean(KEY_MAXIMIZED,
090: DEFAULT_MAXIMIZED);
091: Settings.lastSelectedOutputDir = PREFS.get(
092: KEY_LAST_SELECTED_OUTPUT_DIR, ".");
093: }
094:
095: /**
096: * Static method for writing the user-settings.
097: */
098: public static void write() {
099: PREFS.putInt(KEY_VERTICAL_DIVIDER_POSITION,
100: verticalDividerPosition);
101: PREFS.putInt(KEY_HORIZONTAL_DIVIDER_POSITION,
102: horizontalDividerPosition);
103: PREFS.putInt(KEY_WINDOW_X, windowX);
104: PREFS.putInt(KEY_WINDOW_Y, windowY);
105: PREFS.putInt(KEY_WINDOW_WIDTH, windowWidth);
106: PREFS.putInt(KEY_WINDOW_HEIGHT, windowHeight);
107: PREFS.putBoolean(KEY_MAXIMIZED, isMaximized);
108: PREFS.put(KEY_LAST_SELECTED_OUTPUT_DIR, lastSelectedOutputDir);
109: }
110:
111: /**
112: * Returns the last-known vertical divider position.
113: *
114: * @return the last-known vertical divider position.
115: */
116: public static int getVerticalDividerPosition() {
117: return verticalDividerPosition;
118: }
119:
120: /**
121: * Sets a new value for the vertical divider position that will be stored
122: * the next time you call <code>write()</code>.
123: *
124: * @param verticalDividerPosition The value to store for the vertical divider position.
125: */
126: public static void setVerticalDividerPosition(
127: int verticalDividerPosition) {
128: Settings.verticalDividerPosition = verticalDividerPosition;
129: }
130:
131: /**
132: * Returns the last-known horizontal divider position.
133: *
134: * @return the last-known horizontal divider position.
135: */
136: public static int getHorizontalDividerPosition() {
137: return horizontalDividerPosition;
138: }
139:
140: /**
141: * Sets a new value for the horizontal divider position that will be stored
142: * the next time you call <code>write()</code>.
143: *
144: * @param horizontalDividerPosition The value to store for the horizontal divider position.
145: */
146: public static void setHorizontalDividerPosition(
147: int horizontalDividerPosition) {
148: Settings.horizontalDividerPosition = horizontalDividerPosition;
149: }
150:
151: /**
152: * Returns the last-known maximized state.
153: *
154: * @return the last-known maximized state.
155: */
156: public static boolean isMaximized() {
157: return isMaximized;
158: }
159:
160: /**
161: * Sets a new value for the maximized state that will be stored
162: * the next time you call <code>write()</code>.
163: *
164: * @param maximized The value to store for the maximized state.
165: */
166: public static void setMaximized(boolean maximized) {
167: isMaximized = maximized;
168: }
169:
170: /**
171: * Returns the last-known window bounds.
172: *
173: * @return the last-known window bounds.
174: */
175: public static Rectangle getUserWindowBounds(JFrame frame) {
176: if (windowX == Integer.MIN_VALUE
177: || windowY == Integer.MIN_VALUE) {
178: // Center the current window
179: Dimension screenSize = frame.getToolkit().getScreenSize();
180: windowX = (int) (screenSize.getWidth() / 2 - windowWidth / 2);
181: windowY = (int) (screenSize.getHeight() / 2 - windowHeight / 2);
182: }
183: return new Rectangle(windowX, windowY, windowWidth,
184: windowHeight);
185: }
186:
187: /**
188: * Sets a new value for the window bounds that will be stored
189: * the next time you call <code>write()</code>.
190: *
191: * @param bounds The value to store for the window bounds.
192: */
193: public static void setUserWindowBounds(Rectangle bounds) {
194: windowX = (int) bounds.getX();
195: windowY = (int) bounds.getY();
196: windowWidth = (int) bounds.getWidth();
197: windowHeight = (int) bounds.getHeight();
198: }
199: }
|