PageRenderTime 134ms CodeModel.GetById 25ms app.highlight 72ms 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
 23package org.gjt.sp.jedit;
 24
 25import com.microstar.xml.*;
 26import java.io.*;
 27import org.gjt.sp.util.Log;
 28
 29/**
 30 * Manages persistence of open buffers and views across jEdit sessions.
 31 * @since jEdit 4.2pre1
 32 * @author Slava Pestov
 33 * @version $Id: PerspectiveManager.java 5053 2004-05-29 01:55:26Z spestov $
 34 */
 35public class PerspectiveManager
 36{
 37	//{{{ isPerspectiveDirty() method
 38	/**
 39	 * We only autosave the perspective if it has changed, to avoid spinning
 40	 * up the disk on laptops.
 41	 * @since jEdit 4.2pre13
 42	 */
 43	public static boolean isPerspectiveDirty()
 44	{
 45		return dirty;
 46	} //}}}
 47
 48	//{{{ setPerspectiveDirty() method
 49	/**
 50	 * We only autosave the perspective if it has changed, to avoid spinning
 51	 * up the disk on laptops.
 52	 * @since jEdit 4.2pre13
 53	 */
 54	public static void setPerspectiveDirty(boolean dirty)
 55	{
 56		PerspectiveManager.dirty = dirty;
 57	} //}}}
 58
 59	//{{{ loadPerspective() method
 60	public static View loadPerspective(boolean restoreFiles)
 61	{
 62		String settingsDirectory = jEdit.getSettingsDirectory();
 63		if(settingsDirectory == null)
 64			return null;
 65
 66		File perspective = new File(MiscUtilities.constructPath(
 67			settingsDirectory,"perspective.xml"));
 68
 69		if(!perspective.exists())
 70			return null;
 71
 72		Log.log(Log.MESSAGE,PerspectiveManager.class,"Loading " + perspective);
 73
 74		PerspectiveHandler handler = new PerspectiveHandler(restoreFiles);
 75		XmlParser parser = new XmlParser();
 76		parser.setHandler(handler);
 77		Reader in = null;
 78		try
 79		{
 80			in = new BufferedReader(new FileReader(perspective));
 81			parser.parse(null, null, in);
 82		}
 83		catch(XmlException xe)
 84		{
 85			int line = xe.getLine();
 86			String message = xe.getMessage();
 87			Log.log(Log.ERROR,PerspectiveManager.class,perspective
 88				+ ":" + line + ": " + message);
 89		}
 90		catch(FileNotFoundException fnf)
 91		{
 92		}
 93		catch(Exception e)
 94		{
 95			Log.log(Log.ERROR,PerspectiveManager.class,e);
 96		}
 97		finally
 98		{
 99			try
100			{
101				if(in != null)
102					in.close();
103			}
104			catch(IOException io)
105			{
106				Log.log(Log.ERROR,PerspectiveManager.class,io);
107			}
108		}
109
110		return handler.view;
111	} //}}}
112
113	//{{{ savePerspective() method
114	public static void savePerspective(boolean autosave)
115	{
116		String settingsDirectory = jEdit.getSettingsDirectory();
117		if(settingsDirectory == null)
118			return;
119
120		// backgrounded
121		if(jEdit.getBufferCount() == 0)
122			return;
123
124		if(!autosave)
125			Log.log(Log.MESSAGE,PerspectiveManager.class,"Saving perspective.xml");
126
127		File file1 = new File(MiscUtilities.constructPath(
128			settingsDirectory,"#perspective.xml#save#"));
129		File file2 = new File(MiscUtilities.constructPath(
130			settingsDirectory,"perspective.xml"));
131
132		String lineSep = System.getProperty("line.separator");
133
134		BufferedWriter out = null;
135
136		try
137		{
138			out = new BufferedWriter(new FileWriter(file1));
139
140			out.write("<?xml version=\"1.0\"?>");
141			out.write(lineSep);
142			out.write("<!DOCTYPE PERSPECTIVE SYSTEM \"perspective.dtd\">");
143			out.write(lineSep);
144			out.write("<PERSPECTIVE>");
145			out.write(lineSep);
146
147			Buffer[] buffers = jEdit.getBuffers();
148			for(int i = 0; i < buffers.length; i++)
149			{
150				Buffer buffer = buffers[i];
151				if(buffer.isNewFile())
152					continue;
153				out.write("<BUFFER>");
154				out.write(MiscUtilities.charsToEntities(buffer.getPath()));
155				out.write("</BUFFER>");
156				out.write(lineSep);
157			}
158
159			View[] views = jEdit.getViews();
160			for(int i = 0; i < views.length; i++)
161			{
162				View view = views[i];
163				// ensures that active view is saved last,
164				// ie created last on next load, ie in front
165				// on next load
166				if(view == jEdit.getActiveView()
167					&& i != views.length - 1)
168				{
169					View last = views[views.length - 1];
170					views[i] = last;
171					views[views.length - 1] = view;
172					view = last;
173				}
174
175				View.ViewConfig config = views[i].getViewConfig();
176				out.write("<VIEW PLAIN=\"");
177				out.write(config.plainView ? "TRUE" : "FALSE");
178				out.write("\">");
179
180				out.write("<PANES>");
181				out.write(lineSep);
182				out.write(config.splitConfig);
183				out.write(lineSep);
184				out.write("</PANES>");
185				out.write(lineSep);
186
187				out.write("<GEOMETRY X=\"");
188				out.write(String.valueOf(config.x));
189				out.write("\" Y=\"");
190				out.write(String.valueOf(config.y));
191				out.write("\" WIDTH=\"");
192				out.write(String.valueOf(config.width));
193				out.write("\" HEIGHT=\"");
194				out.write(String.valueOf(config.height));
195				out.write("\" EXT_STATE=\"");
196				out.write(String.valueOf(config.extState));
197				out.write("\" />");
198				out.write(lineSep);
199
200				out.write("<DOCKING LEFT=\"");
201				out.write(config.left == null ? "" : config.left);
202				out.write("\" TOP=\"");
203				out.write(config.top == null ? "" : config.top);
204				out.write("\" RIGHT=\"");
205				out.write(config.right == null ? "" : config.right);
206				out.write("\" BOTTOM=\"");
207				out.write(config.bottom == null ? "" : config.bottom);
208				out.write("\" LEFT_POS=\"");
209				out.write(String.valueOf(config.leftPos));
210				out.write("\" TOP_POS=\"");
211				out.write(String.valueOf(config.topPos));
212				out.write("\" RIGHT_POS=\"");
213				out.write(String.valueOf(config.rightPos));
214				out.write("\" BOTTOM_POS=\"");
215				out.write(String.valueOf(config.bottomPos));
216				out.write("\" />");
217				out.write(lineSep);
218
219				out.write("</VIEW>");
220				out.write(lineSep);
221			}
222
223			out.write("</PERSPECTIVE>");
224			out.write(lineSep);
225		}
226		catch(IOException io)
227		{
228			Log.log(Log.ERROR,PerspectiveManager.class,"Error saving " + file1);
229			Log.log(Log.ERROR,PerspectiveManager.class,io);
230		}
231		finally
232		{
233			try
234			{
235				if(out != null)
236					out.close();
237			}
238			catch(IOException e)
239			{
240			}
241		}
242
243		file2.delete();
244		file1.renameTo(file2);
245	} //}}}
246
247	private static boolean dirty;
248
249	//{{{ PerspectiveHandler class
250	static class PerspectiveHandler extends HandlerBase
251	{
252		View view;
253		String charData;
254		View.ViewConfig config;
255		boolean restoreFiles;
256
257		PerspectiveHandler(boolean restoreFiles)
258		{
259			this.restoreFiles = restoreFiles;
260			config = new View.ViewConfig();
261		}
262
263		public Object resolveEntity(String publicId, String systemId)
264		{
265			if("perspective.dtd".equals(systemId))
266			{
267				// this will result in a slight speed up, since we
268				// don't need to read the DTD anyway, as AElfred is
269				// non-validating
270				return new StringReader("<!-- -->");
271
272				/* try
273				{
274					return new BufferedReader(new InputStreamReader(
275						getClass().getResourceAsStream("recent.dtd")));
276				}
277				catch(Exception e)
278				{
279					Log.log(Log.ERROR,this,"Error while opening"
280						+ " recent.dtd:");
281					Log.log(Log.ERROR,this,e);
282				} */
283			}
284
285			return null;
286		}
287
288		public void doctypeDecl(String name, String publicId,
289			String systemId) throws Exception
290		{
291			if("PERSPECTIVE".equals(name))
292				return;
293
294			Log.log(Log.ERROR,this,"perspective.xml: DOCTYPE must be PERSPECTIVE");
295		}
296
297		public void attribute(String aname, String value, boolean specified)
298		{
299			if(!specified)
300				return;
301
302			if(aname.equals("X"))
303				config.x = Integer.parseInt(value);
304			else if(aname.equals("Y"))
305				config.y = Integer.parseInt(value);
306			else if(aname.equals("WIDTH"))
307				config.width = Integer.parseInt(value);
308			else if(aname.equals("HEIGHT"))
309				config.height = Integer.parseInt(value);
310			else if(aname.equals("EXT_STATE"))
311				config.extState = Integer.parseInt(value);
312			else if(aname.equals("PLAIN"))
313				config.plainView = ("TRUE".equals(value));
314			else if(aname.equals("TOP"))
315				config.top = value;
316			else if(aname.equals("LEFT"))
317				config.left = value;
318			else if(aname.equals("BOTTOM"))
319				config.bottom = value;
320			else if(aname.equals("RIGHT"))
321				config.right = value;
322			else if(aname.equals("TOP_POS"))
323				config.topPos = Integer.parseInt(value);
324			else if(aname.equals("LEFT_POS"))
325				config.leftPos = Integer.parseInt(value);
326			else if(aname.equals("BOTTOM_POS"))
327				config.bottomPos = Integer.parseInt(value);
328			else if(aname.equals("RIGHT_POS"))
329				config.rightPos = Integer.parseInt(value);
330		}
331
332		public void endElement(String name)
333		{
334			if(name.equals("BUFFER"))
335			{
336				if(restoreFiles)
337					jEdit.openFile(null,charData);
338			}
339			else if(name.equals("PANES"))
340				config.splitConfig = charData;
341			else if(name.equals("VIEW"))
342			{
343				if(jEdit.getBufferCount() == 0)
344					jEdit.newFile(null);
345				view = jEdit.newView(view,null,config);
346				config = new View.ViewConfig();
347			}
348		}
349
350		public void charData(char[] ch, int start, int length)
351		{
352			charData = new String(ch,start,length);
353		}
354	} //}}}
355}