PageRenderTime 60ms CodeModel.GetById 32ms app.highlight 25ms RepoModel.GetById 0ms app.codeStats 1ms

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