001: /*
002: * SessionProperties.java
003: *
004: * Copyright (C) 1998-2003 Peter Graves
005: * $Id: SessionProperties.java,v 1.3 2003/06/29 00:19:34 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import java.awt.Dimension;
025: import java.awt.Rectangle;
026: import java.awt.Toolkit;
027: import java.io.IOException;
028: import java.io.InputStream;
029: import java.io.OutputStream;
030: import java.util.Properties;
031:
032: public final class SessionProperties {
033: private Properties properties;
034: private File file;
035: private File backupFile;
036:
037: public SessionProperties() {
038: properties = new Properties();
039: file = File.getInstance(Directories.getEditorDirectory(),
040: "props");
041: backupFile = File.getInstance(Directories.getEditorDirectory(),
042: "props~");
043: try {
044: InputStream in = null;
045: if (file != null && file.isFile()) {
046: in = file.getInputStream();
047: } else if (backupFile != null && backupFile.isFile()) {
048: Log
049: .debug("SessionProperties constructor loading backup file");
050: in = backupFile.getInputStream();
051: }
052: if (in != null) {
053: properties.load(in);
054: in.close();
055: }
056: } catch (IOException e) {
057: Log.error(e);
058: }
059: }
060:
061: public void saveWindowPlacement() {
062: for (int i = 0; i < Editor.getFrameCount(); i++)
063: saveWindowPlacement(Editor.getFrame(i));
064: }
065:
066: private void saveWindowPlacement(Frame frame) {
067: saveSidebarState(frame);
068: String prefix = getPrefix(frame);
069: if (prefix != null) {
070: setBooleanProperty(prefix + "toolbar.show", frame
071: .getShowToolbar());
072: Dimension screen = Toolkit.getDefaultToolkit()
073: .getScreenSize();
074: Rectangle r = frame.getRect();
075: if (r.x >= 0 && r.y >= 0 && r.width < screen.width
076: && r.height < screen.height) {
077: setIntegerProperty(prefix + "x", r.x);
078: setIntegerProperty(prefix + "y", r.y);
079: setIntegerProperty(prefix + "width", r.width);
080: setIntegerProperty(prefix + "height", r.height);
081: }
082: setIntegerProperty(prefix + "extendedState", frame
083: .retrieveExtendedState());
084: }
085: }
086:
087: public Rectangle getWindowPlacement(int index) {
088: Rectangle r = new Rectangle();
089: String prefix = getPrefix(index);
090: if (prefix != null) {
091: r.x = getIntegerProperty(prefix + "x", 0);
092: r.y = getIntegerProperty(prefix + "y", 0);
093: r.width = getIntegerProperty(prefix + "width", 0);
094: r.height = getIntegerProperty(prefix + "height", 0);
095: }
096: return r;
097: }
098:
099: public int getExtendedState(int index) {
100: String prefix = getPrefix(index);
101: return getIntegerProperty(prefix + "extendedState", 0);
102: }
103:
104: public void saveSidebarState(Frame frame) {
105: String prefix = getPrefix(frame);
106: if (prefix != null) {
107: if (frame.getSidebar() != null) {
108: int width = frame.getSidebarSplitPane()
109: .getDividerLocation();
110: if (width > 0)
111: setIntegerProperty(prefix.concat("sidebar.width"),
112: width);
113: int dividerLocation = frame.getSidebar()
114: .getDividerLocation();
115: if (dividerLocation >= 0) {
116: // If < 0, sidebar has no divider.
117: setIntegerProperty(prefix
118: .concat("sidebar.dividerLocation"),
119: dividerLocation);
120: }
121: }
122: setBooleanProperty(prefix.concat("sidebar.show"), frame
123: .getSidebar() != null);
124: }
125: }
126:
127: public boolean getShowSidebar(Frame frame) {
128: int index = Editor.indexOf(frame);
129:
130: // By default, only show sidebar in primary frame.
131: boolean toBeReturned = index == 0;
132:
133: String prefix = getPrefix(frame);
134: if (prefix != null)
135: toBeReturned = getBooleanProperty(prefix
136: .concat("sidebar.show"), toBeReturned);
137: return toBeReturned;
138: }
139:
140: public int getSidebarWidth(Frame frame) {
141: int toBeReturned = 150;
142: String prefix = getPrefix(frame);
143: if (prefix != null)
144: toBeReturned = getIntegerProperty(prefix
145: .concat("sidebar.width"), toBeReturned);
146: return toBeReturned;
147: }
148:
149: public int getSidebarDividerLocation(Frame frame) {
150: int toBeReturned = 200;
151: String prefix = getPrefix(frame);
152: if (prefix != null)
153: toBeReturned = getIntegerProperty(prefix
154: .concat("sidebar.dividerLocation"), toBeReturned);
155: return toBeReturned;
156: }
157:
158: public boolean getShowToolbar(Frame frame) {
159: String prefix = getPrefix(frame);
160: if (prefix != null)
161: return getBooleanProperty(prefix.concat("toolbar.show"),
162: true);
163: return true;
164: }
165:
166: public void setShowToolbar(Frame frame, boolean show) {
167: String prefix = getPrefix(frame);
168: if (prefix != null)
169: setBooleanProperty(prefix.concat("toolbar.show"), show);
170: }
171:
172: public int getIntegerProperty(String key, int defaultValue) {
173: try {
174: String s = properties.getProperty(key);
175: if (s != null)
176: return Integer.parseInt(s);
177: } catch (NumberFormatException e) {
178: }
179: return defaultValue;
180: }
181:
182: public void setBooleanProperty(String key, boolean value) {
183: properties.put(key, value ? "true" : "false");
184: }
185:
186: public boolean getBooleanProperty(String key, boolean defaultValue) {
187: String s = properties.getProperty(key);
188: if (s != null) {
189: if (s.equals("true"))
190: return true;
191: if (s.equals("false"))
192: return false;
193: }
194: return defaultValue;
195: }
196:
197: public void setIntegerProperty(String key, int value) {
198: properties.put(key, String.valueOf(value));
199: }
200:
201: public float getFloatProperty(String key, float defaultValue) {
202: try {
203: String s = properties.getProperty(key);
204: if (s != null)
205: return Float.parseFloat(s);
206: } catch (NumberFormatException e) {
207: }
208: return defaultValue;
209: }
210:
211: public void setFloatProperty(String key, float value) {
212: properties.put(key, String.valueOf(value));
213: }
214:
215: public String getStringProperty(String key, String defaultValue) {
216: String s = properties.getProperty(key);
217: if (s != null)
218: return s;
219: return defaultValue;
220: }
221:
222: public void setStringProperty(String key, String value) {
223: if (value != null)
224: properties.put(key, value);
225: else
226: properties.remove(key);
227: }
228:
229: public void save() {
230: try {
231: File tempFile = Utilities.getTempFile();
232: OutputStream out = tempFile.getOutputStream();
233: properties.store(out, null);
234: out.flush();
235: out.close();
236: if (file.exists()
237: && !Utilities.deleteRename(file, backupFile)) {
238: Log.error("SessionProperties.save deleteRename failed");
239: Log.error("source = " + file);
240: Log.error("destination = " + backupFile);
241: }
242: if (!Utilities.deleteRename(tempFile, file)) {
243: Log.error("SessionProperties.save deleteRename failed");
244: Log.error("source = " + tempFile);
245: Log.error("destination = " + file);
246: }
247: } catch (IOException e) {
248: Log.error(e);
249: }
250: }
251:
252: private String getPrefix(Frame frame) {
253: return getPrefix(Editor.indexOf(frame));
254: }
255:
256: private String getPrefix(int index) {
257: if (index < 0) {
258: // Should never happen.
259: Debug.assertTrue(false);
260: return null;
261: }
262: return "frame." + index + ".";
263: }
264: }
|