KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > finalist > jaggenerator > Settings


1 /* Copyright (C) 2003 Finalist IT Group
2  *
3  * This file is part of JAG - the Java J2EE Application Generator
4  *
5  * JAG is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * JAG is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with JAG; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */

17 package com.finalist.jaggenerator;
18
19 import javax.swing.*;
20 import java.util.prefs.Preferences JavaDoc;
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    // Retrieve current user's JAG-preferences
31
private static final Preferences JavaDoc PREFS = Preferences.userNodeForPackage(JagGenerator.class);
32
33    private static final String JavaDoc KEY_LAST_SELECTED_OUTPUT_DIR = "last_outputdir";
34
35    // Store Divider positions
36
private static final String JavaDoc KEY_VERTICAL_DIVIDER_POSITION = "vdiv";
37    private static final int DEFAULT_VERTICAL_DIVIDER_POSITION = 400;
38    private static final String JavaDoc KEY_HORIZONTAL_DIVIDER_POSITION = "hdiv";
39    private static final int DEFAULT_HORIZONTAL_DIVIDER_POSITION = 444;
40    // Store Window-position and size
41
private static final String JavaDoc KEY_WINDOW_X = "winx";
42    private static final int DEFAULT_WINDOW_X = Integer.MIN_VALUE;
43    private static final String JavaDoc KEY_WINDOW_Y = "winy";
44    private static final int DEFAULT_WINDOW_Y = Integer.MIN_VALUE;
45    private static final String JavaDoc KEY_WINDOW_WIDTH = "winwidth";
46    private static final int DEFAULT_WINDOW_WIDTH = 1000;
47    private static final String JavaDoc KEY_WINDOW_HEIGHT = "winheight";
48    private static final int DEFAULT_WINDOW_HEIGHT = 760;
49    // Store window-maximized state
50
private static final String JavaDoc KEY_MAXIMIZED = "winmaximized";
51    private static final boolean DEFAULT_MAXIMIZED = false;
52    // static properties holding the values to read and/or store
53
private static int verticalDividerPosition;
54    private static int horizontalDividerPosition;
55    private static int windowX;
56    private static int windowY;
57    private static int windowWidth;
58    private static int windowHeight;
59    private static boolean isMaximized;
60
61
62    public static String JavaDoc getLastSelectedOutputDir() {
63       return lastSelectedOutputDir;
64    }
65
66
67    public static void setLastSelectedOutputDir(String JavaDoc lastSelectedOutputDir) {
68       Settings.lastSelectedOutputDir = lastSelectedOutputDir;
69    }
70
71
72    private static String JavaDoc lastSelectedOutputDir;
73
74    /**
75     * Static method for reading the user-settings
76     */

77    public static void read() {
78       Settings.verticalDividerPosition = PREFS.getInt(KEY_VERTICAL_DIVIDER_POSITION, DEFAULT_VERTICAL_DIVIDER_POSITION);
79       Settings.horizontalDividerPosition = PREFS.getInt(KEY_HORIZONTAL_DIVIDER_POSITION, DEFAULT_HORIZONTAL_DIVIDER_POSITION);
80       Settings.windowX = PREFS.getInt(KEY_WINDOW_X, DEFAULT_WINDOW_X);
81       Settings.windowY = PREFS.getInt(KEY_WINDOW_Y, DEFAULT_WINDOW_Y);
82       Settings.windowWidth = PREFS.getInt(KEY_WINDOW_WIDTH, DEFAULT_WINDOW_WIDTH);
83       Settings.windowHeight = PREFS.getInt(KEY_WINDOW_HEIGHT, DEFAULT_WINDOW_HEIGHT);
84       Settings.isMaximized = PREFS.getBoolean(KEY_MAXIMIZED, DEFAULT_MAXIMIZED);
85       Settings.lastSelectedOutputDir = PREFS.get(KEY_LAST_SELECTED_OUTPUT_DIR, ".");
86    }
87
88    /**
89     * Static method for writing the user-settings.
90     */

91    public static void write() {
92       PREFS.putInt(KEY_VERTICAL_DIVIDER_POSITION, verticalDividerPosition);
93       PREFS.putInt(KEY_HORIZONTAL_DIVIDER_POSITION, horizontalDividerPosition);
94       PREFS.putInt(KEY_WINDOW_X, windowX);
95       PREFS.putInt(KEY_WINDOW_Y, windowY);
96       PREFS.putInt(KEY_WINDOW_WIDTH, windowWidth);
97       PREFS.putInt(KEY_WINDOW_HEIGHT, windowHeight);
98       PREFS.putBoolean(KEY_MAXIMIZED, isMaximized);
99       PREFS.put(KEY_LAST_SELECTED_OUTPUT_DIR, lastSelectedOutputDir);
100    }
101
102    /**
103     * Returns the last-known vertical divider position.
104     *
105     * @return the last-known vertical divider position.
106     */

107    public static int getVerticalDividerPosition() {
108       return verticalDividerPosition;
109    }
110
111    /**
112     * Sets a new value for the vertical divider position that will be stored
113     * the next time you call <code>write()</code>.
114     *
115     * @param verticalDividerPosition The value to store for the vertical divider position.
116     */

117    public static void setVerticalDividerPosition(int verticalDividerPosition) {
118       Settings.verticalDividerPosition = verticalDividerPosition;
119    }
120
121    /**
122     * Returns the last-known horizontal divider position.
123     *
124     * @return the last-known horizontal divider position.
125     */

126    public static int getHorizontalDividerPosition() {
127       return horizontalDividerPosition;
128    }
129
130    /**
131     * Sets a new value for the horizontal divider position that will be stored
132     * the next time you call <code>write()</code>.
133     *
134     * @param horizontalDividerPosition The value to store for the horizontal divider position.
135     */

136    public static void setHorizontalDividerPosition(int horizontalDividerPosition) {
137       Settings.horizontalDividerPosition = horizontalDividerPosition;
138    }
139
140    /**
141     * Returns the last-known maximized state.
142     *
143     * @return the last-known maximized state.
144     */

145    public static boolean isMaximized() {
146       return isMaximized;
147    }
148
149    /**
150     * Sets a new value for the maximized state that will be stored
151     * the next time you call <code>write()</code>.
152     *
153     * @param maximized The value to store for the maximized state.
154     */

155    public static void setMaximized(boolean maximized) {
156       isMaximized = maximized;
157    }
158
159    /**
160     * Returns the last-known window bounds.
161     *
162     * @return the last-known window bounds.
163     */

164    public static Rectangle getUserWindowBounds(JFrame frame) {
165       if(windowX == Integer.MIN_VALUE || windowY == Integer.MIN_VALUE) {
166          // Center the current window
167
Dimension screenSize = frame.getToolkit().getScreenSize();
168          windowX = (int) (screenSize.getWidth() /2 - windowWidth /2);
169          windowY = (int) (screenSize.getHeight()/2 - windowHeight/2);
170       }
171       return new Rectangle(windowX, windowY, windowWidth, windowHeight);
172    }
173
174    /**
175     * Sets a new value for the window bounds that will be stored
176     * the next time you call <code>write()</code>.
177     *
178     * @param bounds The value to store for the window bounds.
179     */

180    public static void setUserWindowBounds(Rectangle bounds) {
181       windowX = (int) bounds.getX();
182       windowY = (int) bounds.getY();
183       windowWidth = (int) bounds.getWidth();
184       windowHeight = (int) bounds.getHeight();
185    }
186 }
187
Popular Tags