1 22 23 package org.gjt.sp.jedit; 24 25 import java.io.*; 26 27 import org.xml.sax.Attributes ; 28 import org.xml.sax.InputSource ; 29 import org.xml.sax.helpers.DefaultHandler ; 30 31 import org.gjt.sp.util.Log; 32 import org.gjt.sp.util.XMLUtilities; 33 34 40 public class PerspectiveManager 41 { 42 48 public static boolean isPerspectiveDirty() 49 { 50 return dirty; 51 } 53 59 public static void setPerspectiveDirty(boolean dirty) 60 { 61 PerspectiveManager.dirty = dirty; 62 } 64 70 public static boolean isPerspectiveEnabled() 71 { 72 return enabled; 73 } 75 81 public static void setPerspectiveEnabled(boolean enabled) 82 { 83 PerspectiveManager.enabled = enabled; 84 } 86 public static View loadPerspective(boolean restoreFiles) 88 { 89 String settingsDirectory = jEdit.getSettingsDirectory(); 90 if(settingsDirectory == null) 91 return null; 92 93 File perspective = new File(MiscUtilities.constructPath( 94 settingsDirectory,"perspective.xml")); 95 96 if(!perspective.exists()) 97 return null; 98 99 Log.log(Log.MESSAGE,PerspectiveManager.class,"Loading " + perspective); 100 101 PerspectiveHandler handler = new PerspectiveHandler(restoreFiles); 102 try 103 { 104 XMLUtilities.parseXML(new FileInputStream(perspective), handler); 105 } 106 catch(IOException e) 107 { 108 Log.log(Log.ERROR,PerspectiveManager.class,e); 109 } 110 return handler.view; 111 } 113 public static void savePerspective(boolean autosave) 115 { 116 if(!isPerspectiveEnabled()) 117 return; 118 119 String settingsDirectory = jEdit.getSettingsDirectory(); 120 if(settingsDirectory == null) 121 return; 122 123 if(jEdit.getBufferCount() == 0) 125 return; 126 127 if(!autosave) 128 Log.log(Log.MESSAGE,PerspectiveManager.class,"Saving perspective.xml"); 129 130 File file1 = new File(MiscUtilities.constructPath( 131 settingsDirectory,"#perspective.xml#save#")); 132 File file2 = new File(MiscUtilities.constructPath( 133 settingsDirectory,"perspective.xml")); 134 135 String lineSep = System.getProperty("line.separator"); 136 137 BufferedWriter out = null; 138 139 try 140 { 141 out = new BufferedWriter(new FileWriter(file1)); 142 143 out.write("<?xml version=\"1.0\"?>"); 144 out.write(lineSep); 145 out.write("<!DOCTYPE PERSPECTIVE SYSTEM \"perspective.dtd\">"); 146 out.write(lineSep); 147 out.write("<PERSPECTIVE>"); 148 out.write(lineSep); 149 150 Buffer[] buffers = jEdit.getBuffers(); 151 for(int i = 0; i < buffers.length; i++) 152 { 153 Buffer buffer = buffers[i]; 154 if(buffer.isNewFile()) 155 continue; 156 out.write("<BUFFER AUTORELOAD=\""); 157 out.write(buffer.getAutoReload() ? "TRUE" : "FALSE"); 158 out.write("\" AUTORELOAD_DIALOG=\""); 159 out.write(buffer.getAutoReloadDialog() ? "TRUE" : "FALSE"); 160 out.write("\">"); 161 out.write(XMLUtilities.charsToEntities(buffer.getPath(), false)); 162 out.write("</BUFFER>"); 163 out.write(lineSep); 164 } 165 166 View[] views = jEdit.getViews(); 167 for(int i = 0; i < views.length; i++) 168 { 169 View view = views[i]; 170 if(view == jEdit.getActiveView() 174 && i != views.length - 1) 175 { 176 View last = views[views.length - 1]; 177 views[i] = last; 178 views[views.length - 1] = view; 179 view = last; 180 } 181 182 View.ViewConfig config = views[i].getViewConfig(); 183 out.write("<VIEW PLAIN=\""); 184 out.write(config.plainView ? "TRUE" : "FALSE"); 185 out.write("\">"); 186 187 out.write("<PANES>"); 188 out.write(lineSep); 189 out.write(XMLUtilities.charsToEntities( 190 config.splitConfig,false)); 191 out.write(lineSep); 192 out.write("</PANES>"); 193 out.write(lineSep); 194 195 out.write("<GEOMETRY X=\""); 196 out.write(String.valueOf(config.x)); 197 out.write("\" Y=\""); 198 out.write(String.valueOf(config.y)); 199 out.write("\" WIDTH=\""); 200 out.write(String.valueOf(config.width)); 201 out.write("\" HEIGHT=\""); 202 out.write(String.valueOf(config.height)); 203 out.write("\" EXT_STATE=\""); 204 out.write(String.valueOf(config.extState)); 205 out.write("\" />"); 206 out.write(lineSep); 207 208 out.write("<DOCKING LEFT=\""); 209 out.write(config.left == null ? "" : config.left); 210 out.write("\" TOP=\""); 211 out.write(config.top == null ? "" : config.top); 212 out.write("\" RIGHT=\""); 213 out.write(config.right == null ? "" : config.right); 214 out.write("\" BOTTOM=\""); 215 out.write(config.bottom == null ? "" : config.bottom); 216 out.write("\" LEFT_POS=\""); 217 out.write(String.valueOf(config.leftPos)); 218 out.write("\" TOP_POS=\""); 219 out.write(String.valueOf(config.topPos)); 220 out.write("\" RIGHT_POS=\""); 221 out.write(String.valueOf(config.rightPos)); 222 out.write("\" BOTTOM_POS=\""); 223 out.write(String.valueOf(config.bottomPos)); 224 out.write("\" />"); 225 out.write(lineSep); 226 227 out.write("</VIEW>"); 228 out.write(lineSep); 229 } 230 231 out.write("</PERSPECTIVE>"); 232 out.write(lineSep); 233 } 234 catch(IOException io) 235 { 236 Log.log(Log.ERROR,PerspectiveManager.class,"Error saving " + file1); 237 Log.log(Log.ERROR,PerspectiveManager.class,io); 238 } 239 finally 240 { 241 try 242 { 243 if(out != null) 244 out.close(); 245 } 246 catch(IOException e) 247 { 248 } 249 } 250 251 file2.delete(); 252 file1.renameTo(file2); 253 } 255 private static boolean dirty, enabled = true; 256 257 static class PerspectiveHandler extends DefaultHandler 259 { 260 View view; 261 Buffer currentBuffer; 262 StringBuffer charData; 263 View.ViewConfig config; 264 boolean restoreFiles; 265 String autoReload, autoReloadDialog; 266 267 PerspectiveHandler(boolean restoreFiles) 268 { 269 this.restoreFiles = restoreFiles; 270 config = new View.ViewConfig(); 271 charData = new StringBuffer (); 272 } 273 274 public InputSource resolveEntity(String publicId, String systemId) 275 { 276 return XMLUtilities.findEntity(systemId, "perspective.dtd", getClass()); 277 } 278 279 public void startElement(String uri, String localName, 280 String qName, Attributes attrs) 281 { 282 charData.setLength(0); 283 for (int i = 0; i < attrs.getLength(); i++) 284 { 285 String name = attrs.getQName(i); 286 String value = attrs.getValue(i); 287 attribute(name, value); 288 } 289 } 290 291 private void attribute(String aname, String value) 292 { 293 if(aname.equals("X")) 294 config.x = Integer.parseInt(value); 295 else if(aname.equals("Y")) 296 config.y = Integer.parseInt(value); 297 else if(aname.equals("WIDTH")) 298 config.width = Integer.parseInt(value); 299 else if(aname.equals("HEIGHT")) 300 config.height = Integer.parseInt(value); 301 else if(aname.equals("EXT_STATE")) 302 config.extState = Integer.parseInt(value); 303 else if(aname.equals("PLAIN")) 304 config.plainView = ("TRUE".equals(value)); 305 else if(aname.equals("TOP")) 306 config.top = value; 307 else if(aname.equals("LEFT")) 308 config.left = value; 309 else if(aname.equals("BOTTOM")) 310 config.bottom = value; 311 else if(aname.equals("RIGHT")) 312 config.right = value; 313 else if(aname.equals("TOP_POS")) 314 config.topPos = Integer.parseInt(value); 315 else if(aname.equals("LEFT_POS")) 316 config.leftPos = Integer.parseInt(value); 317 else if(aname.equals("BOTTOM_POS")) 318 config.bottomPos = Integer.parseInt(value); 319 else if(aname.equals("RIGHT_POS")) 320 config.rightPos = Integer.parseInt(value); 321 else if(aname.equals("AUTORELOAD")) 322 autoReload = value; 323 else if(aname.equals("AUTORELOAD_DIALOG")) 324 autoReloadDialog = value; 325 } 326 327 public void endElement(String uri, String localName, String name) 328 { 329 if(name.equals("BUFFER")) 330 { 331 if(restoreFiles) 332 { 333 currentBuffer = jEdit.openFile(null,charData.toString()); 334 if (currentBuffer != null) 337 { 338 if(autoReload != null) 339 currentBuffer.setAutoReload("TRUE".equals(autoReload)); 340 if(autoReloadDialog != null) 341 currentBuffer.setAutoReloadDialog("TRUE".equals(autoReloadDialog)); 342 } 343 } 344 } 345 else if(name.equals("PANES")) 346 config.splitConfig = charData.toString(); 347 else if(name.equals("VIEW")) 348 { 349 view = jEdit.newView(view,null,config); 350 config = new View.ViewConfig(); 351 } 352 } 353 354 public void characters(char[] ch, int start, int length) 355 { 356 charData.append(ch,start,length); 357 } 358 } } 360 | Popular Tags |