KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > PerspectiveManager


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
23 package org.gjt.sp.jedit;
24
25 import java.io.*;
26
27 import org.xml.sax.Attributes JavaDoc;
28 import org.xml.sax.InputSource JavaDoc;
29 import org.xml.sax.helpers.DefaultHandler JavaDoc;
30
31 import org.gjt.sp.util.Log;
32 import org.gjt.sp.util.XMLUtilities;
33
34 /**
35  * Manages persistence of open buffers and views across jEdit sessions.
36  * @since jEdit 4.2pre1
37  * @author Slava Pestov
38  * @version $Id: PerspectiveManager.java 8028 2006-11-15 05:51:22Z vanza $
39  */

40 public class PerspectiveManager
41 {
42     //{{{ isPerspectiveDirty() method
43
/**
44      * We only autosave the perspective if it has changed, to avoid spinning
45      * up the disk on laptops.
46      * @since jEdit 4.2pre13
47      */

48     public static boolean isPerspectiveDirty()
49     {
50         return dirty;
51     } //}}}
52

53     //{{{ setPerspectiveDirty() method
54
/**
55      * We only autosave the perspective if it has changed, to avoid spinning
56      * up the disk on laptops.
57      * @since jEdit 4.2pre13
58      */

59     public static void setPerspectiveDirty(boolean dirty)
60     {
61         PerspectiveManager.dirty = dirty;
62     } //}}}
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

75     //{{{ setPerspectiveEnabled() method
76
/**
77      * We disable saving of the perspective while the 'close all' dialog is
78      * showing.
79      * @since jEdit 4.3pre2
80      */

81     public static void setPerspectiveEnabled(boolean enabled)
82     {
83         PerspectiveManager.enabled = enabled;
84     } //}}}
85

86     //{{{ loadPerspective() method
87
public static View loadPerspective(boolean restoreFiles)
88     {
89         String JavaDoc settingsDirectory = jEdit.getSettingsDirectory();
90         if(settingsDirectory == null)
91             return null;
92
93         File perspective = new File(MiscUtilities.constructPath(
94             settingsDirectory,"perspective.xml"));
95
96         if(!perspective.exists())
97             return null;
98
99         Log.log(Log.MESSAGE,PerspectiveManager.class,"Loading " + perspective);
100
101         PerspectiveHandler handler = new PerspectiveHandler(restoreFiles);
102         try
103         {
104             XMLUtilities.parseXML(new FileInputStream(perspective), handler);
105         }
106         catch(IOException e)
107         {
108             Log.log(Log.ERROR,PerspectiveManager.class,e);
109         }
110         return handler.view;
111     } //}}}
112

113     //{{{ savePerspective() method
114
public static void savePerspective(boolean autosave)
115     {
116         if(!isPerspectiveEnabled())
117             return;
118
119         String JavaDoc settingsDirectory = jEdit.getSettingsDirectory();
120         if(settingsDirectory == null)
121             return;
122
123         // backgrounded
124
if(jEdit.getBufferCount() == 0)
125             return;
126
127         if(!autosave)
128             Log.log(Log.MESSAGE,PerspectiveManager.class,"Saving perspective.xml");
129
130         File file1 = new File(MiscUtilities.constructPath(
131             settingsDirectory,"#perspective.xml#save#"));
132         File file2 = new File(MiscUtilities.constructPath(
133             settingsDirectory,"perspective.xml"));
134
135         String JavaDoc lineSep = System.getProperty("line.separator");
136
137         BufferedWriter out = null;
138
139         try
140         {
141             out = new BufferedWriter(new FileWriter(file1));
142
143             out.write("<?xml version=\"1.0\"?>");
144             out.write(lineSep);
145             out.write("<!DOCTYPE PERSPECTIVE SYSTEM \"perspective.dtd\">");
146             out.write(lineSep);
147             out.write("<PERSPECTIVE>");
148             out.write(lineSep);
149
150             Buffer[] buffers = jEdit.getBuffers();
151             for(int i = 0; i < buffers.length; i++)
152             {
153                 Buffer buffer = buffers[i];
154                 if(buffer.isNewFile())
155                     continue;
156                 out.write("<BUFFER AUTORELOAD=\"");
157                 out.write(buffer.getAutoReload() ? "TRUE" : "FALSE");
158                 out.write("\" AUTORELOAD_DIALOG=\"");
159                 out.write(buffer.getAutoReloadDialog() ? "TRUE" : "FALSE");
160                 out.write("\">");
161                 out.write(XMLUtilities.charsToEntities(buffer.getPath(), false));
162                 out.write("</BUFFER>");
163                 out.write(lineSep);
164             }
165
166             View[] views = jEdit.getViews();
167             for(int i = 0; i < views.length; i++)
168             {
169                 View view = views[i];
170                 // ensures that active view is saved last,
171
// ie created last on next load, ie in front
172
// on next load
173
if(view == jEdit.getActiveView()
174                     && i != views.length - 1)
175                 {
176                     View last = views[views.length - 1];
177                     views[i] = last;
178                     views[views.length - 1] = view;
179                     view = last;
180                 }
181
182                 View.ViewConfig config = views[i].getViewConfig();
183                 out.write("<VIEW PLAIN=\"");
184                 out.write(config.plainView ? "TRUE" : "FALSE");
185                 out.write("\">");
186
187                 out.write("<PANES>");
188                 out.write(lineSep);
189                 out.write(XMLUtilities.charsToEntities(
190                     config.splitConfig,false));
191                 out.write(lineSep);
192                 out.write("</PANES>");
193                 out.write(lineSep);
194
195                 out.write("<GEOMETRY X=\"");
196                 out.write(String.valueOf(config.x));
197                 out.write("\" Y=\"");
198                 out.write(String.valueOf(config.y));
199                 out.write("\" WIDTH=\"");
200                 out.write(String.valueOf(config.width));
201                 out.write("\" HEIGHT=\"");
202                 out.write(String.valueOf(config.height));
203                 out.write("\" EXT_STATE=\"");
204                 out.write(String.valueOf(config.extState));
205                 out.write("\" />");
206                 out.write(lineSep);
207
208                 out.write("<DOCKING LEFT=\"");
209                 out.write(config.left == null ? "" : config.left);
210                 out.write("\" TOP=\"");
211                 out.write(config.top == null ? "" : config.top);
212                 out.write("\" RIGHT=\"");
213                 out.write(config.right == null ? "" : config.right);
214                 out.write("\" BOTTOM=\"");
215                 out.write(config.bottom == null ? "" : config.bottom);
216                 out.write("\" LEFT_POS=\"");
217                 out.write(String.valueOf(config.leftPos));
218                 out.write("\" TOP_POS=\"");
219                 out.write(String.valueOf(config.topPos));
220                 out.write("\" RIGHT_POS=\"");
221                 out.write(String.valueOf(config.rightPos));
222                 out.write("\" BOTTOM_POS=\"");
223                 out.write(String.valueOf(config.bottomPos));
224                 out.write("\" />");
225                 out.write(lineSep);
226
227                 out.write("</VIEW>");
228                 out.write(lineSep);
229             }
230
231             out.write("</PERSPECTIVE>");
232             out.write(lineSep);
233         }
234         catch(IOException io)
235         {
236             Log.log(Log.ERROR,PerspectiveManager.class,"Error saving " + file1);
237             Log.log(Log.ERROR,PerspectiveManager.class,io);
238         }
239         finally
240         {
241             try
242             {
243                 if(out != null)
244                     out.close();
245             }
246             catch(IOException e)
247             {
248             }
249         }
250
251         file2.delete();
252         file1.renameTo(file2);
253     } //}}}
254

255     private static boolean dirty, enabled = true;
256
257     //{{{ PerspectiveHandler class
258
static class PerspectiveHandler extends DefaultHandler JavaDoc
259     {
260         View view;
261         Buffer currentBuffer;
262         StringBuffer JavaDoc charData;
263         View.ViewConfig config;
264         boolean restoreFiles;
265         String JavaDoc autoReload, autoReloadDialog;
266
267         PerspectiveHandler(boolean restoreFiles)
268         {
269             this.restoreFiles = restoreFiles;
270             config = new View.ViewConfig();
271             charData = new StringBuffer JavaDoc();
272         }
273
274         public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId)
275         {
276             return XMLUtilities.findEntity(systemId, "perspective.dtd", getClass());
277         }
278
279         public void startElement(String JavaDoc uri, String JavaDoc localName,
280                      String JavaDoc qName, Attributes JavaDoc attrs)
281         {
282             charData.setLength(0);
283             for (int i = 0; i < attrs.getLength(); i++)
284             {
285                 String JavaDoc name = attrs.getQName(i);
286                 String JavaDoc value = attrs.getValue(i);
287                 attribute(name, value);
288             }
289         }
290
291         private void attribute(String JavaDoc aname, String JavaDoc value)
292         {
293             if(aname.equals("X"))
294                 config.x = Integer.parseInt(value);
295             else if(aname.equals("Y"))
296                 config.y = Integer.parseInt(value);
297             else if(aname.equals("WIDTH"))
298                 config.width = Integer.parseInt(value);
299             else if(aname.equals("HEIGHT"))
300                 config.height = Integer.parseInt(value);
301             else if(aname.equals("EXT_STATE"))
302                 config.extState = Integer.parseInt(value);
303             else if(aname.equals("PLAIN"))
304                 config.plainView = ("TRUE".equals(value));
305             else if(aname.equals("TOP"))
306                 config.top = value;
307             else if(aname.equals("LEFT"))
308                 config.left = value;
309             else if(aname.equals("BOTTOM"))
310                 config.bottom = value;
311             else if(aname.equals("RIGHT"))
312                 config.right = value;
313             else if(aname.equals("TOP_POS"))
314                 config.topPos = Integer.parseInt(value);
315             else if(aname.equals("LEFT_POS"))
316                 config.leftPos = Integer.parseInt(value);
317             else if(aname.equals("BOTTOM_POS"))
318                 config.bottomPos = Integer.parseInt(value);
319             else if(aname.equals("RIGHT_POS"))
320                 config.rightPos = Integer.parseInt(value);
321             else if(aname.equals("AUTORELOAD"))
322                 autoReload = value;
323             else if(aname.equals("AUTORELOAD_DIALOG"))
324                 autoReloadDialog = value;
325         }
326
327         public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc name)
328         {
329             if(name.equals("BUFFER"))
330             {
331                 if(restoreFiles)
332                 {
333                     currentBuffer = jEdit.openFile(null,charData.toString());
334                     // if the autoReload attributes are not present, don't set anything
335
// it's sufficient to check whether they are present on the first BUFFER element
336
if (currentBuffer != null)
337                     {
338                         if(autoReload != null)
339                             currentBuffer.setAutoReload("TRUE".equals(autoReload));
340                         if(autoReloadDialog != null)
341                             currentBuffer.setAutoReloadDialog("TRUE".equals(autoReloadDialog));
342                     }
343                 }
344             }
345             else if(name.equals("PANES"))
346                 config.splitConfig = charData.toString();
347             else if(name.equals("VIEW"))
348             {
349                 view = jEdit.newView(view,null,config);
350                 config = new View.ViewConfig();
351             }
352         }
353
354         public void characters(char[] ch, int start, int length)
355         {
356             charData.append(ch,start,length);
357         }
358     } //}}}
359
}
360
Popular Tags