1 21 22 package org.armedbear.j; 23 24 import java.awt.Dimension ; 25 import java.awt.Rectangle ; 26 import java.awt.Toolkit ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.io.OutputStream ; 30 import java.util.Properties ; 31 32 public final class SessionProperties 33 { 34 private Properties properties; 35 private File file; 36 private File backupFile; 37 38 public SessionProperties() 39 { 40 properties = new Properties (); 41 file = File.getInstance(Directories.getEditorDirectory(), "props"); 42 backupFile = File.getInstance(Directories.getEditorDirectory(), "props~"); 43 try { 44 InputStream 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 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 prefix = getPrefix(frame); 71 if (prefix != null) { 72 setBooleanProperty(prefix + "toolbar.show", frame.getShowToolbar()); 73 Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); 74 Rectangle 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 getWindowPlacement(int index) 86 { 87 Rectangle r = new Rectangle (); 88 String 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 prefix = getPrefix(index); 101 return getIntegerProperty(prefix + "extendedState", 0); 102 } 103 104 public void saveSidebarState(Frame frame) 105 { 106 String 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 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 boolean toBeReturned = index == 0; 130 131 String 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 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 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 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 prefix = getPrefix(frame); 169 if (prefix != null) 170 setBooleanProperty(prefix.concat("toolbar.show"), show); 171 } 172 173 public int getIntegerProperty(String key, int defaultValue) 174 { 175 try { 176 String s = properties.getProperty(key); 177 if (s != null) 178 return Integer.parseInt(s); 179 } 180 catch (NumberFormatException e) {} 181 return defaultValue; 182 } 183 184 public void setBooleanProperty(String key, boolean value) 185 { 186 properties.put(key, value ? "true" : "false"); 187 } 188 189 public boolean getBooleanProperty(String key, boolean defaultValue) 190 { 191 String 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 key, int value) 202 { 203 properties.put(key, String.valueOf(value)); 204 } 205 206 public float getFloatProperty(String key, float defaultValue) 207 { 208 try { 209 String s = properties.getProperty(key); 210 if (s != null) 211 return Float.parseFloat(s); 212 } 213 catch (NumberFormatException e) {} 214 return defaultValue; 215 } 216 217 public void setFloatProperty(String key, float value) 218 { 219 properties.put(key, String.valueOf(value)); 220 } 221 222 public String getStringProperty(String key, String defaultValue) 223 { 224 String s = properties.getProperty(key); 225 if (s != null) 226 return s; 227 return defaultValue; 228 } 229 230 public void setStringProperty(String key, String 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 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 e) { 258 Log.error(e); 259 } 260 } 261 262 private String getPrefix(Frame frame) 263 { 264 return getPrefix(Editor.indexOf(frame)); 265 } 266 267 private String getPrefix(int index) 268 { 269 if (index < 0) { 270 Debug.assertTrue(false); 272 return null; 273 } 274 return "frame." + index + "."; 275 } 276 } 277 | Popular Tags |