PageRenderTime 44ms CodeModel.GetById 26ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

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