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

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/PerspectiveManager.java

#
Java | 379 lines | 270 code | 42 blank | 67 comment | 60 complexity | 4da7afeb1537a032e2cfffaf4951bed4 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.StreamTokenizer;
  24. import java.io.StringReader;
  25. import java.io.IOException;
  26. import java.util.Collection;
  27. import java.util.LinkedList;
  28. import java.util.Stack;
  29. import org.gjt.sp.util.IOUtilities;
  30. import org.gjt.sp.util.Log;
  31. import org.gjt.sp.util.XMLUtilities;
  32. import org.xml.sax.Attributes;
  33. import org.xml.sax.InputSource;
  34. import org.xml.sax.helpers.DefaultHandler;
  35. /**
  36. * Manages persistence of open buffers and views across jEdit sessions.
  37. * @since jEdit 4.2pre1
  38. * @author Slava Pestov
  39. * @version $Id: PerspectiveManager.java 20108 2011-10-18 12:16:38Z evanpw $
  40. */
  41. public class PerspectiveManager
  42. {
  43. private static final String PERSPECTIVE_FILENAME = "perspective";
  44. //{{{ isPerspectiveDirty() 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 boolean isPerspectiveDirty()
  51. {
  52. return dirty;
  53. } //}}}
  54. //{{{ setPerspectiveDirty() method
  55. /**
  56. * We only autosave the perspective if it has changed, to avoid spinning
  57. * up the disk on laptops.
  58. * @since jEdit 4.2pre13
  59. */
  60. public static void setPerspectiveDirty(boolean dirty)
  61. {
  62. PerspectiveManager.dirty = dirty;
  63. } //}}}
  64. //{{{ isPerspectiveEnabled() method
  65. /**
  66. * We disable saving of the perspective while the 'close all' dialog is
  67. * showing.
  68. * @since jEdit 4.3pre2
  69. */
  70. public static boolean isPerspectiveEnabled()
  71. {
  72. return enabled;
  73. } //}}}
  74. //{{{ setPerspectiveEnabled() method
  75. /**
  76. * We disable saving of the perspective while the 'close all' dialog is
  77. * showing.
  78. * @since jEdit 4.3pre2
  79. */
  80. public static void setPerspectiveEnabled(boolean enabled)
  81. {
  82. PerspectiveManager.enabled = enabled;
  83. } //}}}
  84. //{{{ loadPerspective() method
  85. public static View loadPerspective(boolean restoreFiles)
  86. {
  87. if(perspectiveXML == null)
  88. return null;
  89. if(!perspectiveXML.fileExists())
  90. return null;
  91. Log.log(Log.MESSAGE,PerspectiveManager.class,"Loading " + perspectiveXML);
  92. PerspectiveHandler handler = new PerspectiveHandler(restoreFiles);
  93. try
  94. {
  95. perspectiveXML.load(handler);
  96. }
  97. catch(IOException e)
  98. {
  99. Log.log(Log.ERROR,PerspectiveManager.class,e);
  100. }
  101. return handler.view;
  102. } //}}}
  103. //{{{ savePerspective() method
  104. public static void savePerspective(boolean autosave)
  105. {
  106. if(!isPerspectiveEnabled() || !jEdit.isStartupDone())
  107. return;
  108. if(perspectiveXML == null)
  109. return;
  110. // backgrounded
  111. if(jEdit.getBufferCount() == 0)
  112. return;
  113. Buffer[] buffers = jEdit.getBuffers();
  114. Collection<Buffer> savedBuffers = new LinkedList<Buffer>();
  115. for (Buffer buffer: buffers)
  116. {
  117. if (!buffer.isNewFile())
  118. {
  119. savedBuffers.add(buffer);
  120. }
  121. }
  122. if(!autosave)
  123. Log.log(Log.MESSAGE,PerspectiveManager.class,"Saving " + perspectiveXML);
  124. String lineSep = System.getProperty("line.separator");
  125. SettingsXML.Saver out = null;
  126. try
  127. {
  128. out = perspectiveXML.openSaver();
  129. out.writeXMLDeclaration();
  130. out.write("<!DOCTYPE PERSPECTIVE SYSTEM \"perspective.dtd\">");
  131. out.write(lineSep);
  132. out.write("<PERSPECTIVE>");
  133. out.write(lineSep);
  134. for (Buffer buffer: savedBuffers)
  135. {
  136. out.write("<BUFFER AUTORELOAD=\"");
  137. out.write(buffer.getAutoReload() ? "TRUE" : "FALSE");
  138. out.write("\" AUTORELOAD_DIALOG=\"");
  139. out.write(buffer.getAutoReloadDialog() ? "TRUE" : "FALSE");
  140. out.write("\">");
  141. out.write(XMLUtilities.charsToEntities(buffer.getPath(), false));
  142. out.write("</BUFFER>");
  143. out.write(lineSep);
  144. }
  145. View[] views = jEdit.getViews();
  146. for(int i = 0; i < views.length; i++)
  147. {
  148. View view = views[i];
  149. // ensures that active view is saved last,
  150. // ie created last on next load, ie in front
  151. // on next load
  152. if(view == jEdit.getActiveView()
  153. && i != views.length - 1)
  154. {
  155. View last = views[views.length - 1];
  156. views[i] = last;
  157. views[views.length - 1] = view;
  158. view = last;
  159. }
  160. View.ViewConfig config = views[i].getViewConfig();
  161. out.write("<VIEW PLAIN=\"");
  162. out.write(config.plainView ? "TRUE" : "FALSE");
  163. out.write("\">");
  164. out.write(lineSep);
  165. if (config.title != null)
  166. {
  167. out.write(lineSep);
  168. out.write("<TITLE>");
  169. out.write(XMLUtilities.charsToEntities(config.title,false));
  170. out.write("</TITLE>");
  171. out.write(lineSep);
  172. }
  173. out.write("<PANES>");
  174. out.write(lineSep);
  175. out.write(XMLUtilities.charsToEntities(
  176. config.splitConfig,false));
  177. out.write(lineSep);
  178. out.write("</PANES>");
  179. out.write(lineSep);
  180. out.write("<GEOMETRY X=\"");
  181. out.write(String.valueOf(config.x));
  182. out.write("\" Y=\"");
  183. out.write(String.valueOf(config.y));
  184. out.write("\" WIDTH=\"");
  185. out.write(String.valueOf(config.width));
  186. out.write("\" HEIGHT=\"");
  187. out.write(String.valueOf(config.height));
  188. out.write("\" EXT_STATE=\"");
  189. out.write(String.valueOf(config.extState));
  190. out.write("\" />");
  191. out.write(lineSep);
  192. if (config.docking != null)
  193. config.docking.saveLayout(PERSPECTIVE_FILENAME, i);
  194. out.write("</VIEW>");
  195. out.write(lineSep);
  196. }
  197. out.write("</PERSPECTIVE>");
  198. out.write(lineSep);
  199. out.finish();
  200. }
  201. catch(IOException io)
  202. {
  203. Log.log(Log.ERROR,PerspectiveManager.class,"Error saving " + perspectiveXML);
  204. Log.log(Log.ERROR,PerspectiveManager.class,io);
  205. }
  206. finally
  207. {
  208. IOUtilities.closeQuietly(out);
  209. }
  210. } //}}}
  211. //{{{ Private members
  212. private static boolean dirty, enabled = true;
  213. private static SettingsXML perspectiveXML;
  214. //}}}
  215. //{{{ Class initializer
  216. static
  217. {
  218. String settingsDirectory = jEdit.getSettingsDirectory();
  219. if(settingsDirectory != null)
  220. {
  221. perspectiveXML = new SettingsXML(settingsDirectory, PERSPECTIVE_FILENAME);
  222. }
  223. } //}}}
  224. //{{{ PerspectiveHandler class
  225. private static class PerspectiveHandler extends DefaultHandler
  226. {
  227. View view;
  228. private StringBuilder charData;
  229. View.ViewConfig config;
  230. boolean restoreFiles;
  231. boolean restoreSplits;
  232. String autoReload, autoReloadDialog;
  233. PerspectiveHandler(boolean restoreFiles)
  234. {
  235. this.restoreFiles = restoreFiles;
  236. restoreSplits = jEdit.getBooleanProperty("restore.splits", true);
  237. config = new View.ViewConfig();
  238. charData = new StringBuilder();
  239. config.docking = View.getDockingFrameworkProvider().createDockingLayout();
  240. }
  241. @Override
  242. public InputSource resolveEntity(String publicId, String systemId)
  243. {
  244. return XMLUtilities.findEntity(systemId, "perspective.dtd", getClass());
  245. }
  246. @Override
  247. public void startElement(String uri, String localName,
  248. String qName, Attributes attrs)
  249. {
  250. charData.setLength(0);
  251. for (int i = 0; i < attrs.getLength(); i++)
  252. {
  253. String name = attrs.getQName(i);
  254. String value = attrs.getValue(i);
  255. attribute(name, value);
  256. }
  257. }
  258. private void attribute(String aname, String value)
  259. {
  260. if(aname.equals("X"))
  261. config.x = Integer.parseInt(value);
  262. else if(aname.equals("Y"))
  263. config.y = Integer.parseInt(value);
  264. else if(aname.equals("WIDTH"))
  265. config.width = Integer.parseInt(value);
  266. else if(aname.equals("HEIGHT"))
  267. config.height = Integer.parseInt(value);
  268. else if(aname.equals("EXT_STATE"))
  269. config.extState = Integer.parseInt(value);
  270. else if(aname.equals("PLAIN"))
  271. config.plainView = ("TRUE".equals(value));
  272. else if(aname.equals("AUTORELOAD"))
  273. autoReload = value;
  274. else if(aname.equals("AUTORELOAD_DIALOG"))
  275. autoReloadDialog = value;
  276. }
  277. /**
  278. * @return true if the uri points to a remote file
  279. */
  280. public static boolean skipRemote(String uri)
  281. {
  282. if (jEdit.getBooleanProperty("restore.remote"))
  283. return false;
  284. if(MiscUtilities.isURL(uri))
  285. {
  286. String protocol = MiscUtilities.getProtocolOfURL(uri);
  287. if (!protocol.equals("file")) return true;
  288. }
  289. return false;
  290. }
  291. @Override
  292. public void endElement(String uri, String localName, String name)
  293. {
  294. if(name.equals("BUFFER"))
  295. {
  296. if (restoreFiles && !skipRemote(charData.toString()))
  297. {
  298. Buffer restored = jEdit.openTemporary(null,null, charData.toString(), false);
  299. // if the autoReload attributes are not present, don't set anything
  300. // it's sufficient to check whether they are present on the first BUFFER element
  301. if (restored != null)
  302. {
  303. if(autoReload != null)
  304. restored.setAutoReload("TRUE".equals(autoReload));
  305. if(autoReloadDialog != null)
  306. restored.setAutoReloadDialog("TRUE".equals(autoReloadDialog));
  307. jEdit.commitTemporary(restored);
  308. }
  309. }
  310. }
  311. else if(name.equals("PANES"))
  312. {
  313. SplitConfigParser parser = new SplitConfigParser(charData.toString());
  314. parser.setIncludeSplits(restoreSplits);
  315. parser.setIncludeFiles(restoreFiles);
  316. parser.setIncludeRemoteFiles(jEdit.getBooleanProperty("restore.remote"));
  317. config.splitConfig = parser.parse();
  318. }
  319. else if(name.equals("VIEW"))
  320. {
  321. if (config.docking != null)
  322. config.docking.loadLayout(PERSPECTIVE_FILENAME, jEdit.getViewCount());
  323. view = jEdit.newView(view,null,config);
  324. config = new View.ViewConfig();
  325. config.docking = View.getDockingFrameworkProvider().createDockingLayout();
  326. }
  327. else if(name.equals("TITLE"))
  328. config.title = charData.toString();
  329. }
  330. @Override
  331. public void characters(char[] ch, int start, int length)
  332. {
  333. charData.append(ch,start,length);
  334. }
  335. }
  336. //}}}
  337. }