KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > SessionProperties


1 /*
2  * SessionProperties.java
3  *
4  * Copyright (C) 1998-2003 Peter Graves
5  * $Id: SessionProperties.java,v 1.3 2003/06/29 00:19:34 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import java.awt.Dimension JavaDoc;
25 import java.awt.Rectangle JavaDoc;
26 import java.awt.Toolkit JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.io.OutputStream JavaDoc;
30 import java.util.Properties JavaDoc;
31
32 public final class SessionProperties
33 {
34     private Properties JavaDoc properties;
35     private File file;
36     private File backupFile;
37
38     public SessionProperties()
39     {
40         properties = new Properties JavaDoc();
41         file = File.getInstance(Directories.getEditorDirectory(), "props");
42         backupFile = File.getInstance(Directories.getEditorDirectory(), "props~");
43         try {
44             InputStream JavaDoc in = null;
45             if (file != null && file.isFile()) {
46                 in = file.getInputStream();
47             } else if (backupFile != null && backupFile.isFile()) {
48                 Log.debug("SessionProperties constructor loading backup file");
49                 in = backupFile.getInputStream();
50             }
51             if (in != null) {
52                 properties.load(in);
53                 in.close();
54             }
55         }
56         catch (IOException JavaDoc e) {
57             Log.error(e);
58         }
59     }
60
61     public void saveWindowPlacement()
62     {
63         for (int i = 0; i < Editor.getFrameCount(); i++)
64             saveWindowPlacement(Editor.getFrame(i));
65     }
66
67     private void saveWindowPlacement(Frame frame)
68     {
69         saveSidebarState(frame);
70         String JavaDoc prefix = getPrefix(frame);
71         if (prefix != null) {
72             setBooleanProperty(prefix + "toolbar.show", frame.getShowToolbar());
73             Dimension JavaDoc screen = Toolkit.getDefaultToolkit().getScreenSize();
74             Rectangle JavaDoc r = frame.getRect();
75             if (r.x >= 0 && r.y >= 0 && r.width < screen.width && r.height < screen.height) {
76                 setIntegerProperty(prefix + "x", r.x);
77                 setIntegerProperty(prefix + "y", r.y);
78                 setIntegerProperty(prefix + "width", r.width);
79                 setIntegerProperty(prefix + "height", r.height);
80             }
81             setIntegerProperty(prefix + "extendedState", frame.retrieveExtendedState());
82         }
83     }
84
85     public Rectangle JavaDoc getWindowPlacement(int index)
86     {
87         Rectangle JavaDoc r = new Rectangle JavaDoc();
88         String JavaDoc prefix = getPrefix(index);
89         if (prefix != null) {
90             r.x = getIntegerProperty(prefix + "x", 0);
91             r.y = getIntegerProperty(prefix + "y", 0);
92             r.width = getIntegerProperty(prefix + "width", 0);
93             r.height = getIntegerProperty(prefix + "height", 0);
94         }
95         return r;
96     }
97
98     public int getExtendedState(int index)
99     {
100         String JavaDoc prefix = getPrefix(index);
101         return getIntegerProperty(prefix + "extendedState", 0);
102     }
103
104     public void saveSidebarState(Frame frame)
105     {
106         String JavaDoc prefix = getPrefix(frame);
107         if (prefix != null) {
108             if (frame.getSidebar() != null) {
109                 int width = frame.getSidebarSplitPane().getDividerLocation();
110                 if (width > 0)
111                     setIntegerProperty(prefix.concat("sidebar.width"), width);
112                 int dividerLocation = frame.getSidebar().getDividerLocation();
113                 if (dividerLocation >= 0) {
114                     // If < 0, sidebar has no divider.
115
setIntegerProperty(prefix.concat("sidebar.dividerLocation"),
116                         dividerLocation);
117                 }
118             }
119             setBooleanProperty(prefix.concat("sidebar.show"),
120                 frame.getSidebar() != null);
121         }
122     }
123
124     public boolean getShowSidebar(Frame frame)
125     {
126         int index = Editor.indexOf(frame);
127
128         // By default, only show sidebar in primary frame.
129
boolean toBeReturned = index == 0;
130
131         String JavaDoc prefix = getPrefix(frame);
132         if (prefix != null)
133             toBeReturned = getBooleanProperty(prefix.concat("sidebar.show"),
134                 toBeReturned);
135         return toBeReturned;
136     }
137
138     public int getSidebarWidth(Frame frame)
139     {
140         int toBeReturned = 150;
141         String JavaDoc prefix = getPrefix(frame);
142         if (prefix != null)
143             toBeReturned = getIntegerProperty(prefix.concat("sidebar.width"),
144                 toBeReturned);
145         return toBeReturned;
146     }
147
148     public int getSidebarDividerLocation(Frame frame)
149     {
150         int toBeReturned = 200;
151         String JavaDoc prefix = getPrefix(frame);
152         if (prefix != null)
153             toBeReturned = getIntegerProperty(prefix.concat("sidebar.dividerLocation"),
154                 toBeReturned);
155         return toBeReturned;
156     }
157
158     public boolean getShowToolbar(Frame frame)
159     {
160         String JavaDoc prefix = getPrefix(frame);
161         if (prefix != null)
162             return getBooleanProperty(prefix.concat("toolbar.show"), true);
163         return true;
164     }
165
166     public void setShowToolbar(Frame frame, boolean show)
167     {
168         String JavaDoc prefix = getPrefix(frame);
169         if (prefix != null)
170             setBooleanProperty(prefix.concat("toolbar.show"), show);
171     }
172
173     public int getIntegerProperty(String JavaDoc key, int defaultValue)
174     {
175         try {
176             String JavaDoc s = properties.getProperty(key);
177             if (s != null)
178                 return Integer.parseInt(s);
179         }
180         catch (NumberFormatException JavaDoc e) {}
181         return defaultValue;
182     }
183
184     public void setBooleanProperty(String JavaDoc key, boolean value)
185     {
186         properties.put(key, value ? "true" : "false");
187     }
188
189     public boolean getBooleanProperty(String JavaDoc key, boolean defaultValue)
190     {
191         String JavaDoc s = properties.getProperty(key);
192         if (s != null) {
193             if (s.equals("true"))
194                 return true;
195             if (s.equals("false"))
196                 return false;
197         }
198         return defaultValue;
199     }
200
201     public void setIntegerProperty(String JavaDoc key, int value)
202     {
203         properties.put(key, String.valueOf(value));
204     }
205
206     public float getFloatProperty(String JavaDoc key, float defaultValue)
207     {
208         try {
209             String JavaDoc s = properties.getProperty(key);
210             if (s != null)
211                 return Float.parseFloat(s);
212         }
213         catch (NumberFormatException JavaDoc e) {}
214         return defaultValue;
215     }
216
217     public void setFloatProperty(String JavaDoc key, float value)
218     {
219         properties.put(key, String.valueOf(value));
220     }
221
222     public String JavaDoc getStringProperty(String JavaDoc key, String JavaDoc defaultValue)
223     {
224         String JavaDoc s = properties.getProperty(key);
225         if (s != null)
226             return s;
227         return defaultValue;
228     }
229
230     public void setStringProperty(String JavaDoc key, String JavaDoc value)
231     {
232         if (value != null)
233             properties.put(key, value);
234         else
235             properties.remove(key);
236     }
237
238     public void save()
239     {
240         try {
241             File tempFile = Utilities.getTempFile();
242             OutputStream JavaDoc out = tempFile.getOutputStream();
243             properties.store(out, null);
244             out.flush();
245             out.close();
246             if (file.exists() && !Utilities.deleteRename(file, backupFile)) {
247                 Log.error("SessionProperties.save deleteRename failed");
248                 Log.error("source = " + file);
249                 Log.error("destination = " + backupFile);
250             }
251             if (!Utilities.deleteRename(tempFile, file)) {
252                 Log.error("SessionProperties.save deleteRename failed");
253                 Log.error("source = " + tempFile);
254                 Log.error("destination = " + file);
255             }
256         }
257         catch (IOException JavaDoc e) {
258             Log.error(e);
259         }
260     }
261
262     private String JavaDoc getPrefix(Frame frame)
263     {
264         return getPrefix(Editor.indexOf(frame));
265     }
266
267     private String JavaDoc getPrefix(int index)
268     {
269         if (index < 0) {
270             // Should never happen.
271
Debug.assertTrue(false);
272             return null;
273         }
274         return "frame." + index + ".";
275     }
276 }
277
Popular Tags