PageRenderTime 51ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

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

#
Java | 313 lines | 231 code | 34 blank | 48 comment | 58 complexity | 5e8ba726fb6ccc2bf2080fb8e72de9f9 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 4695 2003-05-09 23:42:25Z spestov $
  31. */
  32. public class PerspectiveManager
  33. {
  34. //{{{ loadPerspective() method
  35. public static View loadPerspective(boolean restoreFiles)
  36. {
  37. String settingsDirectory = jEdit.getSettingsDirectory();
  38. if(settingsDirectory == null)
  39. return null;
  40. File perspective = new File(MiscUtilities.constructPath(
  41. settingsDirectory,"perspective.xml"));
  42. if(!perspective.exists())
  43. return null;
  44. Log.log(Log.MESSAGE,PerspectiveManager.class,"Loading " + perspective);
  45. PerspectiveHandler handler = new PerspectiveHandler(restoreFiles);
  46. XmlParser parser = new XmlParser();
  47. parser.setHandler(handler);
  48. Reader in = null;
  49. try
  50. {
  51. in = new BufferedReader(new FileReader(perspective));
  52. parser.parse(null, null, in);
  53. }
  54. catch(XmlException xe)
  55. {
  56. int line = xe.getLine();
  57. String message = xe.getMessage();
  58. Log.log(Log.ERROR,PerspectiveManager.class,perspective
  59. + ":" + line + ": " + message);
  60. }
  61. catch(FileNotFoundException fnf)
  62. {
  63. }
  64. catch(Exception e)
  65. {
  66. Log.log(Log.ERROR,PerspectiveManager.class,e);
  67. }
  68. finally
  69. {
  70. try
  71. {
  72. if(in != null)
  73. in.close();
  74. }
  75. catch(IOException io)
  76. {
  77. Log.log(Log.ERROR,PerspectiveManager.class,io);
  78. }
  79. }
  80. return handler.view;
  81. } //}}}
  82. //{{{ savePerspective() method
  83. public static void savePerspective(boolean autosave)
  84. {
  85. String settingsDirectory = jEdit.getSettingsDirectory();
  86. if(settingsDirectory == null)
  87. return;
  88. // backgrounded
  89. if(jEdit.getBufferCount() == 0)
  90. return;
  91. File perspective = new File(MiscUtilities.constructPath(
  92. settingsDirectory,"perspective.xml"));
  93. if(!autosave)
  94. Log.log(Log.MESSAGE,PerspectiveManager.class,"Saving " + perspective);
  95. try
  96. {
  97. String lineSep = System.getProperty("line.separator");
  98. BufferedWriter out = new BufferedWriter(new FileWriter(
  99. perspective));
  100. out.write("<?xml version=\"1.0\"?>");
  101. out.write(lineSep);
  102. out.write("<!DOCTYPE PERSPECTIVE SYSTEM \"perspective.dtd\">");
  103. out.write(lineSep);
  104. out.write("<PERSPECTIVE>");
  105. out.write(lineSep);
  106. Buffer[] buffers = jEdit.getBuffers();
  107. for(int i = 0; i < buffers.length; i++)
  108. {
  109. Buffer buffer = buffers[i];
  110. out.write("<BUFFER>");
  111. out.write(MiscUtilities.charsToEntities(buffer.getPath()));
  112. out.write("</BUFFER>");
  113. out.write(lineSep);
  114. }
  115. View[] views = jEdit.getViews();
  116. for(int i = 0; i < views.length; i++)
  117. {
  118. View view = views[i];
  119. // ensures that active view is saved last,
  120. // ie created last on next load, ie in front
  121. // on next load
  122. if(view == jEdit.getActiveView()
  123. && i != views.length - 1)
  124. {
  125. View last = views[views.length - 1];
  126. views[i] = last;
  127. views[views.length - 1] = view;
  128. view = last;
  129. }
  130. View.ViewConfig config = views[i].getViewConfig();
  131. out.write("<VIEW PLAIN=\"");
  132. out.write(config.plainView ? "TRUE" : "FALSE");
  133. out.write("\">");
  134. out.write("<PANES>");
  135. out.write(lineSep);
  136. out.write(config.splitConfig);
  137. out.write(lineSep);
  138. out.write("</PANES>");
  139. out.write(lineSep);
  140. out.write("<GEOMETRY X=\"");
  141. out.write(String.valueOf(config.x));
  142. out.write("\" Y=\"");
  143. out.write(String.valueOf(config.y));
  144. out.write("\" WIDTH=\"");
  145. out.write(String.valueOf(config.width));
  146. out.write("\" HEIGHT=\"");
  147. out.write(String.valueOf(config.height));
  148. out.write("\" EXT_STATE=\"");
  149. out.write(String.valueOf(config.extState));
  150. out.write("\" />");
  151. out.write(lineSep);
  152. out.write("<DOCKING LEFT=\"");
  153. out.write(config.left == null ? "" : config.left);
  154. out.write("\" TOP=\"");
  155. out.write(config.top == null ? "" : config.top);
  156. out.write("\" RIGHT=\"");
  157. out.write(config.right == null ? "" : config.right);
  158. out.write("\" BOTTOM=\"");
  159. out.write(config.bottom == null ? "" : config.bottom);
  160. out.write("\" LEFT_POS=\"");
  161. out.write(String.valueOf(config.leftPos));
  162. out.write("\" TOP_POS=\"");
  163. out.write(String.valueOf(config.topPos));
  164. out.write("\" RIGHT_POS=\"");
  165. out.write(String.valueOf(config.rightPos));
  166. out.write("\" BOTTOM_POS=\"");
  167. out.write(String.valueOf(config.bottomPos));
  168. out.write("\" />");
  169. out.write(lineSep);
  170. out.write("</VIEW>");
  171. out.write(lineSep);
  172. }
  173. out.write("</PERSPECTIVE>");
  174. out.write(lineSep);
  175. out.close();
  176. }
  177. catch(IOException io)
  178. {
  179. Log.log(Log.ERROR,PerspectiveManager.class,"Error saving " + perspective);
  180. Log.log(Log.ERROR,PerspectiveManager.class,io);
  181. }
  182. } //}}}
  183. //{{{ PerspectiveHandler class
  184. static class PerspectiveHandler extends HandlerBase
  185. {
  186. View view;
  187. String charData;
  188. View.ViewConfig config;
  189. boolean restoreFiles;
  190. PerspectiveHandler(boolean restoreFiles)
  191. {
  192. this.restoreFiles = restoreFiles;
  193. config = new View.ViewConfig();
  194. }
  195. public Object resolveEntity(String publicId, String systemId)
  196. {
  197. if("perspective.dtd".equals(systemId))
  198. {
  199. // this will result in a slight speed up, since we
  200. // don't need to read the DTD anyway, as AElfred is
  201. // non-validating
  202. return new StringReader("<!-- -->");
  203. /* try
  204. {
  205. return new BufferedReader(new InputStreamReader(
  206. getClass().getResourceAsStream("recent.dtd")));
  207. }
  208. catch(Exception e)
  209. {
  210. Log.log(Log.ERROR,this,"Error while opening"
  211. + " recent.dtd:");
  212. Log.log(Log.ERROR,this,e);
  213. } */
  214. }
  215. return null;
  216. }
  217. public void doctypeDecl(String name, String publicId,
  218. String systemId) throws Exception
  219. {
  220. if("PERSPECTIVE".equals(name))
  221. return;
  222. Log.log(Log.ERROR,this,"perspective.xml: DOCTYPE must be PERSPECTIVE");
  223. }
  224. public void attribute(String aname, String value, boolean specified)
  225. {
  226. if(!specified)
  227. return;
  228. if(aname.equals("X"))
  229. config.x = Integer.parseInt(value);
  230. else if(aname.equals("Y"))
  231. config.y = Integer.parseInt(value);
  232. else if(aname.equals("WIDTH"))
  233. config.width = Integer.parseInt(value);
  234. else if(aname.equals("HEIGHT"))
  235. config.height = Integer.parseInt(value);
  236. else if(aname.equals("EXT_STATE"))
  237. config.extState = Integer.parseInt(value);
  238. else if(aname.equals("PLAIN"))
  239. config.plainView = ("TRUE".equals(value));
  240. else if(aname.equals("TOP"))
  241. config.top = value;
  242. else if(aname.equals("LEFT"))
  243. config.left = value;
  244. else if(aname.equals("BOTTOM"))
  245. config.bottom = value;
  246. else if(aname.equals("RIGHT"))
  247. config.right = value;
  248. else if(aname.equals("TOP_POS"))
  249. config.topPos = Integer.parseInt(value);
  250. else if(aname.equals("LEFT_POS"))
  251. config.leftPos = Integer.parseInt(value);
  252. else if(aname.equals("BOTTOM_POS"))
  253. config.bottomPos = Integer.parseInt(value);
  254. else if(aname.equals("RIGHT_POS"))
  255. config.rightPos = Integer.parseInt(value);
  256. }
  257. public void endElement(String name)
  258. {
  259. if(name.equals("BUFFER"))
  260. {
  261. if(restoreFiles)
  262. jEdit.openFile(null,charData);
  263. }
  264. else if(name.equals("PANES"))
  265. config.splitConfig = charData;
  266. else if(name.equals("VIEW"))
  267. {
  268. if(jEdit.getBufferCount() == 0)
  269. jEdit.newFile(null);
  270. view = jEdit.newView(view,null,config);
  271. config = new View.ViewConfig();
  272. }
  273. }
  274. public void charData(char[] ch, int start, int length)
  275. {
  276. charData = new String(ch,start,length);
  277. }
  278. } //}}}
  279. }