PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/org/gjt/sp/jedit/PerspectiveManager.java

#
Java | 337 lines | 240 code | 39 blank | 58 comment | 56 complexity | af35a245d18c2912a9b7bc509422fd46 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 java.io.*;
  24. import org.xml.sax.Attributes;
  25. import org.xml.sax.InputSource;
  26. import org.xml.sax.helpers.DefaultHandler;
  27. import org.gjt.sp.util.Log;
  28. /**
  29. * Manages persistence of open buffers and views across jEdit sessions.
  30. * @since jEdit 4.2pre1
  31. * @author Slava Pestov
  32. * @version $Id: PerspectiveManager.java 5443 2006-06-18 18:51:40Z vanza $
  33. */
  34. public class PerspectiveManager
  35. {
  36. //{{{ isPerspectiveDirty() method
  37. /**
  38. * We only autosave the perspective if it has changed, to avoid spinning
  39. * up the disk on laptops.
  40. * @since jEdit 4.2pre13
  41. */
  42. public static boolean isPerspectiveDirty()
  43. {
  44. return dirty;
  45. } //}}}
  46. //{{{ setPerspectiveDirty() method
  47. /**
  48. * We only autosave the perspective if it has changed, to avoid spinning
  49. * up the disk on laptops.
  50. * @since jEdit 4.2pre13
  51. */
  52. public static void setPerspectiveDirty(boolean dirty)
  53. {
  54. PerspectiveManager.dirty = dirty;
  55. } //}}}
  56. //{{{ isPerspectiveEnabled() method
  57. /**
  58. * We disable saving of the perspective while the 'close all' dialog is
  59. * showing.
  60. * @since jEdit 4.3pre2
  61. */
  62. public static boolean isPerspectiveEnabled()
  63. {
  64. return enabled;
  65. } //}}}
  66. //{{{ setPerspectiveEnabled() method
  67. /**
  68. * We disable saving of the perspective while the 'close all' dialog is
  69. * showing.
  70. * @since jEdit 4.3pre2
  71. */
  72. public static void setPerspectiveEnabled(boolean enabled)
  73. {
  74. PerspectiveManager.enabled = enabled;
  75. } //}}}
  76. //{{{ loadPerspective() method
  77. public static View loadPerspective(boolean restoreFiles)
  78. {
  79. String settingsDirectory = jEdit.getSettingsDirectory();
  80. if(settingsDirectory == null)
  81. return null;
  82. File perspective = new File(MiscUtilities.constructPath(
  83. settingsDirectory,"perspective.xml"));
  84. if(!perspective.exists())
  85. return null;
  86. Log.log(Log.MESSAGE,PerspectiveManager.class,"Loading " + perspective);
  87. PerspectiveHandler handler = new PerspectiveHandler(restoreFiles);
  88. try
  89. {
  90. MiscUtilities.parseXML(new FileInputStream(perspective), handler);
  91. }
  92. catch(IOException e)
  93. {
  94. Log.log(Log.ERROR,PerspectiveManager.class,e);
  95. }
  96. return handler.view;
  97. } //}}}
  98. //{{{ savePerspective() method
  99. public static void savePerspective(boolean autosave)
  100. {
  101. if(!isPerspectiveEnabled())
  102. return;
  103. String settingsDirectory = jEdit.getSettingsDirectory();
  104. if(settingsDirectory == null)
  105. return;
  106. // backgrounded
  107. if(jEdit.getBufferCount() == 0)
  108. return;
  109. if(!autosave)
  110. Log.log(Log.MESSAGE,PerspectiveManager.class,"Saving perspective.xml");
  111. File file1 = new File(MiscUtilities.constructPath(
  112. settingsDirectory,"#perspective.xml#save#"));
  113. File file2 = new File(MiscUtilities.constructPath(
  114. settingsDirectory,"perspective.xml"));
  115. String lineSep = System.getProperty("line.separator");
  116. BufferedWriter out = null;
  117. try
  118. {
  119. out = new BufferedWriter(new FileWriter(file1));
  120. out.write("<?xml version=\"1.0\"?>");
  121. out.write(lineSep);
  122. out.write("<!DOCTYPE PERSPECTIVE SYSTEM \"perspective.dtd\">");
  123. out.write(lineSep);
  124. out.write("<PERSPECTIVE>");
  125. out.write(lineSep);
  126. Buffer[] buffers = jEdit.getBuffers();
  127. for(int i = 0; i < buffers.length; i++)
  128. {
  129. Buffer buffer = buffers[i];
  130. if(buffer.isNewFile())
  131. continue;
  132. out.write("<BUFFER>");
  133. out.write(MiscUtilities.charsToEntities(buffer.getPath()));
  134. out.write("</BUFFER>");
  135. out.write(lineSep);
  136. }
  137. View[] views = jEdit.getViews();
  138. for(int i = 0; i < views.length; i++)
  139. {
  140. View view = views[i];
  141. // ensures that active view is saved last,
  142. // ie created last on next load, ie in front
  143. // on next load
  144. if(view == jEdit.getActiveView()
  145. && i != views.length - 1)
  146. {
  147. View last = views[views.length - 1];
  148. views[i] = last;
  149. views[views.length - 1] = view;
  150. view = last;
  151. }
  152. View.ViewConfig config = views[i].getViewConfig();
  153. out.write("<VIEW PLAIN=\"");
  154. out.write(config.plainView ? "TRUE" : "FALSE");
  155. out.write("\">");
  156. out.write("<PANES>");
  157. out.write(lineSep);
  158. out.write(MiscUtilities.charsToEntities(
  159. config.splitConfig));
  160. out.write(lineSep);
  161. out.write("</PANES>");
  162. out.write(lineSep);
  163. out.write("<GEOMETRY X=\"");
  164. out.write(String.valueOf(config.x));
  165. out.write("\" Y=\"");
  166. out.write(String.valueOf(config.y));
  167. out.write("\" WIDTH=\"");
  168. out.write(String.valueOf(config.width));
  169. out.write("\" HEIGHT=\"");
  170. out.write(String.valueOf(config.height));
  171. out.write("\" EXT_STATE=\"");
  172. out.write(String.valueOf(config.extState));
  173. out.write("\" />");
  174. out.write(lineSep);
  175. out.write("<DOCKING LEFT=\"");
  176. out.write(config.left == null ? "" : config.left);
  177. out.write("\" TOP=\"");
  178. out.write(config.top == null ? "" : config.top);
  179. out.write("\" RIGHT=\"");
  180. out.write(config.right == null ? "" : config.right);
  181. out.write("\" BOTTOM=\"");
  182. out.write(config.bottom == null ? "" : config.bottom);
  183. out.write("\" LEFT_POS=\"");
  184. out.write(String.valueOf(config.leftPos));
  185. out.write("\" TOP_POS=\"");
  186. out.write(String.valueOf(config.topPos));
  187. out.write("\" RIGHT_POS=\"");
  188. out.write(String.valueOf(config.rightPos));
  189. out.write("\" BOTTOM_POS=\"");
  190. out.write(String.valueOf(config.bottomPos));
  191. out.write("\" />");
  192. out.write(lineSep);
  193. out.write("</VIEW>");
  194. out.write(lineSep);
  195. }
  196. out.write("</PERSPECTIVE>");
  197. out.write(lineSep);
  198. }
  199. catch(IOException io)
  200. {
  201. Log.log(Log.ERROR,PerspectiveManager.class,"Error saving " + file1);
  202. Log.log(Log.ERROR,PerspectiveManager.class,io);
  203. }
  204. finally
  205. {
  206. try
  207. {
  208. if(out != null)
  209. out.close();
  210. }
  211. catch(IOException e)
  212. {
  213. }
  214. }
  215. file2.delete();
  216. file1.renameTo(file2);
  217. } //}}}
  218. private static boolean dirty, enabled = true;
  219. //{{{ PerspectiveHandler class
  220. static class PerspectiveHandler extends DefaultHandler
  221. {
  222. View view;
  223. StringBuffer charData;
  224. View.ViewConfig config;
  225. boolean restoreFiles;
  226. PerspectiveHandler(boolean restoreFiles)
  227. {
  228. this.restoreFiles = restoreFiles;
  229. config = new View.ViewConfig();
  230. charData = new StringBuffer();
  231. }
  232. public InputSource resolveEntity(String publicId, String systemId)
  233. {
  234. return MiscUtilities.findEntity(systemId, "perspective.dtd", getClass());
  235. }
  236. public void startElement(String uri, String localName,
  237. String qName, Attributes attrs)
  238. {
  239. charData.setLength(0);
  240. for (int i = 0; i < attrs.getLength(); i++)
  241. {
  242. String name = attrs.getQName(i);
  243. String value = attrs.getValue(i);
  244. attribute(name, value);
  245. }
  246. }
  247. private void attribute(String aname, String value)
  248. {
  249. if(aname.equals("X"))
  250. config.x = Integer.parseInt(value);
  251. else if(aname.equals("Y"))
  252. config.y = Integer.parseInt(value);
  253. else if(aname.equals("WIDTH"))
  254. config.width = Integer.parseInt(value);
  255. else if(aname.equals("HEIGHT"))
  256. config.height = Integer.parseInt(value);
  257. else if(aname.equals("EXT_STATE"))
  258. config.extState = Integer.parseInt(value);
  259. else if(aname.equals("PLAIN"))
  260. config.plainView = ("TRUE".equals(value));
  261. else if(aname.equals("TOP"))
  262. config.top = value;
  263. else if(aname.equals("LEFT"))
  264. config.left = value;
  265. else if(aname.equals("BOTTOM"))
  266. config.bottom = value;
  267. else if(aname.equals("RIGHT"))
  268. config.right = value;
  269. else if(aname.equals("TOP_POS"))
  270. config.topPos = Integer.parseInt(value);
  271. else if(aname.equals("LEFT_POS"))
  272. config.leftPos = Integer.parseInt(value);
  273. else if(aname.equals("BOTTOM_POS"))
  274. config.bottomPos = Integer.parseInt(value);
  275. else if(aname.equals("RIGHT_POS"))
  276. config.rightPos = Integer.parseInt(value);
  277. }
  278. public void endElement(String uri, String localName, String name)
  279. {
  280. if(name.equals("BUFFER"))
  281. {
  282. if(restoreFiles)
  283. jEdit.openFile(null,charData.toString());
  284. }
  285. else if(name.equals("PANES"))
  286. config.splitConfig = charData.toString();
  287. else if(name.equals("VIEW"))
  288. {
  289. view = jEdit.newView(view,null,config);
  290. config = new View.ViewConfig();
  291. }
  292. }
  293. public void characters(char[] ch, int start, int length)
  294. {
  295. charData.append(ch,start,length);
  296. }
  297. } //}}}
  298. }