PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/org/gjt/sp/jedit/PerspectiveManager.java

#
Java | 355 lines | 256 code | 39 blank | 60 comment | 61 complexity | e9ef808876dd230de3e21c3ab4df4508 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * PerspectiveManager.java - Saves view configuration
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2003 Slava Pestov
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. package org.gjt.sp.jedit;
  23. import com.microstar.xml.*;
  24. import java.io.*;
  25. import org.gjt.sp.util.Log;
  26. /**
  27. * Manages persistence of open buffers and views across jEdit sessions.
  28. * @since jEdit 4.2pre1
  29. * @author Slava Pestov
  30. * @version $Id: PerspectiveManager.java 5053 2004-05-29 01:55:26Z spestov $
  31. */
  32. public class PerspectiveManager
  33. {
  34. //{{{ isPerspectiveDirty() method
  35. /**
  36. * We only autosave the perspective if it has changed, to avoid spinning
  37. * up the disk on laptops.
  38. * @since jEdit 4.2pre13
  39. */
  40. public static boolean isPerspectiveDirty()
  41. {
  42. return dirty;
  43. } //}}}
  44. //{{{ setPerspectiveDirty() method
  45. /**
  46. * We only autosave the perspective if it has changed, to avoid spinning
  47. * up the disk on laptops.
  48. * @since jEdit 4.2pre13
  49. */
  50. public static void setPerspectiveDirty(boolean dirty)
  51. {
  52. PerspectiveManager.dirty = dirty;
  53. } //}}}
  54. //{{{ loadPerspective() method
  55. public static View loadPerspective(boolean restoreFiles)
  56. {
  57. String settingsDirectory = jEdit.getSettingsDirectory();
  58. if(settingsDirectory == null)
  59. return null;
  60. File perspective = new File(MiscUtilities.constructPath(
  61. settingsDirectory,"perspective.xml"));
  62. if(!perspective.exists())
  63. return null;
  64. Log.log(Log.MESSAGE,PerspectiveManager.class,"Loading " + perspective);
  65. PerspectiveHandler handler = new PerspectiveHandler(restoreFiles);
  66. XmlParser parser = new XmlParser();
  67. parser.setHandler(handler);
  68. Reader in = null;
  69. try
  70. {
  71. in = new BufferedReader(new FileReader(perspective));
  72. parser.parse(null, null, in);
  73. }
  74. catch(XmlException xe)
  75. {
  76. int line = xe.getLine();
  77. String message = xe.getMessage();
  78. Log.log(Log.ERROR,PerspectiveManager.class,perspective
  79. + ":" + line + ": " + message);
  80. }
  81. catch(FileNotFoundException fnf)
  82. {
  83. }
  84. catch(Exception e)
  85. {
  86. Log.log(Log.ERROR,PerspectiveManager.class,e);
  87. }
  88. finally
  89. {
  90. try
  91. {
  92. if(in != null)
  93. in.close();
  94. }
  95. catch(IOException io)
  96. {
  97. Log.log(Log.ERROR,PerspectiveManager.class,io);
  98. }
  99. }
  100. return handler.view;
  101. } //}}}
  102. //{{{ savePerspective() method
  103. public static void savePerspective(boolean autosave)
  104. {
  105. String settingsDirectory = jEdit.getSettingsDirectory();
  106. if(settingsDirectory == null)
  107. return;
  108. // backgrounded
  109. if(jEdit.getBufferCount() == 0)
  110. return;
  111. if(!autosave)
  112. Log.log(Log.MESSAGE,PerspectiveManager.class,"Saving perspective.xml");
  113. File file1 = new File(MiscUtilities.constructPath(
  114. settingsDirectory,"#perspective.xml#save#"));
  115. File file2 = new File(MiscUtilities.constructPath(
  116. settingsDirectory,"perspective.xml"));
  117. String lineSep = System.getProperty("line.separator");
  118. BufferedWriter out = null;
  119. try
  120. {
  121. out = new BufferedWriter(new FileWriter(file1));
  122. out.write("<?xml version=\"1.0\"?>");
  123. out.write(lineSep);
  124. out.write("<!DOCTYPE PERSPECTIVE SYSTEM \"perspective.dtd\">");
  125. out.write(lineSep);
  126. out.write("<PERSPECTIVE>");
  127. out.write(lineSep);
  128. Buffer[] buffers = jEdit.getBuffers();
  129. for(int i = 0; i < buffers.length; i++)
  130. {
  131. Buffer buffer = buffers[i];
  132. if(buffer.isNewFile())
  133. continue;
  134. out.write("<BUFFER>");
  135. out.write(MiscUtilities.charsToEntities(buffer.getPath()));
  136. out.write("</BUFFER>");
  137. out.write(lineSep);
  138. }
  139. View[] views = jEdit.getViews();
  140. for(int i = 0; i < views.length; i++)
  141. {
  142. View view = views[i];
  143. // ensures that active view is saved last,
  144. // ie created last on next load, ie in front
  145. // on next load
  146. if(view == jEdit.getActiveView()
  147. && i != views.length - 1)
  148. {
  149. View last = views[views.length - 1];
  150. views[i] = last;
  151. views[views.length - 1] = view;
  152. view = last;
  153. }
  154. View.ViewConfig config = views[i].getViewConfig();
  155. out.write("<VIEW PLAIN=\"");
  156. out.write(config.plainView ? "TRUE" : "FALSE");
  157. out.write("\">");
  158. out.write("<PANES>");
  159. out.write(lineSep);
  160. out.write(config.splitConfig);
  161. out.write(lineSep);
  162. out.write("</PANES>");
  163. out.write(lineSep);
  164. out.write("<GEOMETRY X=\"");
  165. out.write(String.valueOf(config.x));
  166. out.write("\" Y=\"");
  167. out.write(String.valueOf(config.y));
  168. out.write("\" WIDTH=\"");
  169. out.write(String.valueOf(config.width));
  170. out.write("\" HEIGHT=\"");
  171. out.write(String.valueOf(config.height));
  172. out.write("\" EXT_STATE=\"");
  173. out.write(String.valueOf(config.extState));
  174. out.write("\" />");
  175. out.write(lineSep);
  176. out.write("<DOCKING LEFT=\"");
  177. out.write(config.left == null ? "" : config.left);
  178. out.write("\" TOP=\"");
  179. out.write(config.top == null ? "" : config.top);
  180. out.write("\" RIGHT=\"");
  181. out.write(config.right == null ? "" : config.right);
  182. out.write("\" BOTTOM=\"");
  183. out.write(config.bottom == null ? "" : config.bottom);
  184. out.write("\" LEFT_POS=\"");
  185. out.write(String.valueOf(config.leftPos));
  186. out.write("\" TOP_POS=\"");
  187. out.write(String.valueOf(config.topPos));
  188. out.write("\" RIGHT_POS=\"");
  189. out.write(String.valueOf(config.rightPos));
  190. out.write("\" BOTTOM_POS=\"");
  191. out.write(String.valueOf(config.bottomPos));
  192. out.write("\" />");
  193. out.write(lineSep);
  194. out.write("</VIEW>");
  195. out.write(lineSep);
  196. }
  197. out.write("</PERSPECTIVE>");
  198. out.write(lineSep);
  199. }
  200. catch(IOException io)
  201. {
  202. Log.log(Log.ERROR,PerspectiveManager.class,"Error saving " + file1);
  203. Log.log(Log.ERROR,PerspectiveManager.class,io);
  204. }
  205. finally
  206. {
  207. try
  208. {
  209. if(out != null)
  210. out.close();
  211. }
  212. catch(IOException e)
  213. {
  214. }
  215. }
  216. file2.delete();
  217. file1.renameTo(file2);
  218. } //}}}
  219. private static boolean dirty;
  220. //{{{ PerspectiveHandler class
  221. static class PerspectiveHandler extends HandlerBase
  222. {
  223. View view;
  224. String charData;
  225. View.ViewConfig config;
  226. boolean restoreFiles;
  227. PerspectiveHandler(boolean restoreFiles)
  228. {
  229. this.restoreFiles = restoreFiles;
  230. config = new View.ViewConfig();
  231. }
  232. public Object resolveEntity(String publicId, String systemId)
  233. {
  234. if("perspective.dtd".equals(systemId))
  235. {
  236. // this will result in a slight speed up, since we
  237. // don't need to read the DTD anyway, as AElfred is
  238. // non-validating
  239. return new StringReader("<!-- -->");
  240. /* try
  241. {
  242. return new BufferedReader(new InputStreamReader(
  243. getClass().getResourceAsStream("recent.dtd")));
  244. }
  245. catch(Exception e)
  246. {
  247. Log.log(Log.ERROR,this,"Error while opening"
  248. + " recent.dtd:");
  249. Log.log(Log.ERROR,this,e);
  250. } */
  251. }
  252. return null;
  253. }
  254. public void doctypeDecl(String name, String publicId,
  255. String systemId) throws Exception
  256. {
  257. if("PERSPECTIVE".equals(name))
  258. return;
  259. Log.log(Log.ERROR,this,"perspective.xml: DOCTYPE must be PERSPECTIVE");
  260. }
  261. public void attribute(String aname, String value, boolean specified)
  262. {
  263. if(!specified)
  264. return;
  265. if(aname.equals("X"))
  266. config.x = Integer.parseInt(value);
  267. else if(aname.equals("Y"))
  268. config.y = Integer.parseInt(value);
  269. else if(aname.equals("WIDTH"))
  270. config.width = Integer.parseInt(value);
  271. else if(aname.equals("HEIGHT"))
  272. config.height = Integer.parseInt(value);
  273. else if(aname.equals("EXT_STATE"))
  274. config.extState = Integer.parseInt(value);
  275. else if(aname.equals("PLAIN"))
  276. config.plainView = ("TRUE".equals(value));
  277. else if(aname.equals("TOP"))
  278. config.top = value;
  279. else if(aname.equals("LEFT"))
  280. config.left = value;
  281. else if(aname.equals("BOTTOM"))
  282. config.bottom = value;
  283. else if(aname.equals("RIGHT"))
  284. config.right = value;
  285. else if(aname.equals("TOP_POS"))
  286. config.topPos = Integer.parseInt(value);
  287. else if(aname.equals("LEFT_POS"))
  288. config.leftPos = Integer.parseInt(value);
  289. else if(aname.equals("BOTTOM_POS"))
  290. config.bottomPos = Integer.parseInt(value);
  291. else if(aname.equals("RIGHT_POS"))
  292. config.rightPos = Integer.parseInt(value);
  293. }
  294. public void endElement(String name)
  295. {
  296. if(name.equals("BUFFER"))
  297. {
  298. if(restoreFiles)
  299. jEdit.openFile(null,charData);
  300. }
  301. else if(name.equals("PANES"))
  302. config.splitConfig = charData;
  303. else if(name.equals("VIEW"))
  304. {
  305. if(jEdit.getBufferCount() == 0)
  306. jEdit.newFile(null);
  307. view = jEdit.newView(view,null,config);
  308. config = new View.ViewConfig();
  309. }
  310. }
  311. public void charData(char[] ch, int start, int length)
  312. {
  313. charData = new String(ch,start,length);
  314. }
  315. } //}}}
  316. }