KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * BufferHistory.java - Remembers caret positions
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 2000, 2005 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 //{{{ Imports
26
import java.io.*;
27 import java.util.*;
28
29 import org.xml.sax.InputSource JavaDoc;
30 import org.xml.sax.helpers.DefaultHandler JavaDoc;
31
32 import org.gjt.sp.jedit.msg.DynamicMenuChanged;
33 import org.gjt.sp.jedit.textarea.*;
34 import org.gjt.sp.util.Log;
35 import org.gjt.sp.util.XMLUtilities;
36 //}}}
37

38 /**
39  * Recent file list.
40  * @author Slava Pestov
41  * @version $Id: BufferHistory.java 5573 2006-07-13 04:37:32Z vanza $
42  */

43 public class BufferHistory
44 {
45     //{{{ getEntry() method
46
public static Entry getEntry(String JavaDoc path)
47     {
48         Iterator iter = history.iterator();
49         while(iter.hasNext())
50         {
51             Entry entry = (Entry)iter.next();
52             if(MiscUtilities.pathsEqual(entry.path,path))
53                 return entry;
54         }
55
56         return null;
57     } //}}}
58

59     //{{{ setEntry() method
60
public static void setEntry(String JavaDoc path, int caret, Selection[] selection,
61         String JavaDoc encoding)
62     {
63         removeEntry(path);
64         addEntry(new Entry(path,caret,selectionToString(selection),
65             encoding));
66         EditBus.send(new DynamicMenuChanged("recent-files"));
67     } //}}}
68

69     //{{{ clear() method
70
/**
71      * Clear the BufferHistory.
72      * @since 4.3pre6
73      */

74     public static void clear()
75     {
76         history.clear();
77         EditBus.send(new DynamicMenuChanged("recent-files"));
78     } //}}}
79

80     //{{{ getHistory() method
81
/**
82      * @since jEdit 4.2pre2
83      */

84     public static List getHistory()
85     {
86         return history;
87     } //}}}
88

89     //{{{ load() method
90
public static void load()
91     {
92         String JavaDoc settingsDirectory = jEdit.getSettingsDirectory();
93         if(settingsDirectory == null)
94             return;
95
96         File recent = new File(MiscUtilities.constructPath(
97             settingsDirectory,"recent.xml"));
98         if(!recent.exists())
99             return;
100
101         recentModTime = recent.lastModified();
102
103         Log.log(Log.MESSAGE,BufferHistory.class,"Loading recent.xml");
104
105         RecentHandler handler = new RecentHandler();
106         try
107         {
108             XMLUtilities.parseXML(new FileInputStream(recent), handler);
109         }
110         catch(IOException e)
111         {
112             Log.log(Log.ERROR,BufferHistory.class,e);
113         }
114     } //}}}
115

116     //{{{ save() method
117
public static void save()
118     {
119         String JavaDoc settingsDirectory = jEdit.getSettingsDirectory();
120         if(settingsDirectory == null)
121             return;
122
123         File file1 = new File(MiscUtilities.constructPath(
124             settingsDirectory, "#recent.xml#save#"));
125         File file2 = new File(MiscUtilities.constructPath(
126             settingsDirectory, "recent.xml"));
127         if(file2.exists() && file2.lastModified() != recentModTime)
128         {
129             Log.log(Log.WARNING,BufferHistory.class,file2
130                 + " changed on disk; will not save recent"
131                 + " files");
132             return;
133         }
134
135         jEdit.backupSettingsFile(file2);
136
137         Log.log(Log.MESSAGE,BufferHistory.class,"Saving " + file1);
138
139         String JavaDoc lineSep = System.getProperty("line.separator");
140
141         boolean ok = false;
142
143         BufferedWriter out = null;
144
145         try
146         {
147             out = new BufferedWriter(new FileWriter(file1));
148
149             out.write("<?xml version=\"1.0\"?>");
150             out.write(lineSep);
151             out.write("<!DOCTYPE RECENT SYSTEM \"recent.dtd\">");
152             out.write(lineSep);
153             out.write("<RECENT>");
154             out.write(lineSep);
155
156             Iterator iter = history.iterator();
157             while(iter.hasNext())
158             {
159                 out.write("<ENTRY>");
160                 out.write(lineSep);
161
162                 Entry entry = (Entry)iter.next();
163
164                 out.write("<PATH>");
165                 out.write(XMLUtilities.charsToEntities(entry.path,false));
166                 out.write("</PATH>");
167                 out.write(lineSep);
168
169                 out.write("<CARET>");
170                 out.write(String.valueOf(entry.caret));
171                 out.write("</CARET>");
172                 out.write(lineSep);
173
174                 if(entry.selection != null
175                     && entry.selection.length() > 0)
176                 {
177                     out.write("<SELECTION>");
178                     out.write(entry.selection);
179                     out.write("</SELECTION>");
180                     out.write(lineSep);
181                 }
182
183                 if(entry.encoding != null)
184                 {
185                     out.write("<ENCODING>");
186                     out.write(entry.encoding);
187                     out.write("</ENCODING>");
188                     out.write(lineSep);
189                 }
190
191                 out.write("</ENTRY>");
192                 out.write(lineSep);
193             }
194
195             out.write("</RECENT>");
196             out.write(lineSep);
197
198             out.close();
199
200             ok = true;
201         }
202         catch(Exception JavaDoc e)
203         {
204             Log.log(Log.ERROR,BufferHistory.class,e);
205         }
206         finally
207         {
208             try
209             {
210                 if(out != null)
211                     out.close();
212             }
213             catch(IOException e)
214             {
215             }
216         }
217
218         if(ok)
219         {
220             /* to avoid data loss, only do this if the above
221              * completed successfully */

222             file2.delete();
223             file1.renameTo(file2);
224         }
225
226         recentModTime = file2.lastModified();
227     } //}}}
228

229     //{{{ Private members
230
private static LinkedList history;
231     private static long recentModTime;
232
233     //{{{ Class initializer
234
static
235     {
236         history = new LinkedList();
237     } //}}}
238

239     //{{{ addEntry() method
240
/* private */ static void addEntry(Entry entry)
241     {
242         history.addFirst(entry);
243         int max = jEdit.getIntegerProperty("recentFiles",50);
244         while(history.size() > max)
245             history.removeLast();
246     } //}}}
247

248     //{{{ removeEntry() method
249
/* private */ static void removeEntry(String JavaDoc path)
250     {
251         Iterator iter = history.iterator();
252         while(iter.hasNext())
253         {
254             Entry entry = (Entry)iter.next();
255             if(MiscUtilities.pathsEqual(path,entry.path))
256             {
257                 iter.remove();
258                 return;
259             }
260         }
261     } //}}}
262

263     //{{{ selectionToString() method
264
private static String JavaDoc selectionToString(Selection[] s)
265     {
266         if(s == null)
267             return null;
268
269         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
270
271         for(int i = 0; i < s.length; i++)
272         {
273             if(i != 0)
274                 buf.append(' ');
275
276             Selection sel = s[i];
277             if(sel instanceof Selection.Range)
278                 buf.append("range ");
279             else //if(sel instanceof Selection.Rect)
280
buf.append("rect ");
281             buf.append(sel.getStart());
282             buf.append(' ');
283             buf.append(sel.getEnd());
284         }
285
286         return buf.toString();
287     } //}}}
288

289     //{{{ stringToSelection() method
290
private static Selection[] stringToSelection(String JavaDoc s)
291     {
292         if(s == null)
293             return null;
294
295         Vector selection = new Vector();
296         StringTokenizer st = new StringTokenizer(s);
297
298         while(st.hasMoreTokens())
299         {
300             String JavaDoc type = st.nextToken();
301             int start = Integer.parseInt(st.nextToken());
302             int end = Integer.parseInt(st.nextToken());
303             if(end < start)
304             {
305                 // I'm not sure when this can happen,
306
// but it does sometimes, witness the
307
// jEdit bug tracker.
308
continue;
309             }
310
311             Selection sel;
312             if(type.equals("range"))
313                 sel = new Selection.Range(start,end);
314             else //if(type.equals("rect"))
315
sel = new Selection.Rect(start,end);
316
317             selection.addElement(sel);
318         }
319
320         Selection[] returnValue = new Selection[selection.size()];
321         selection.copyInto(returnValue);
322         return returnValue;
323     } //}}}
324

325     //}}}
326

327     //{{{ Entry class
328
/**
329      * Recent file list entry.
330      */

331     public static class Entry
332     {
333         public String JavaDoc path;
334         public int caret;
335         public String JavaDoc selection;
336         public String JavaDoc encoding;
337
338         public Selection[] getSelection()
339         {
340             return stringToSelection(selection);
341         }
342
343         public Entry(String JavaDoc path, int caret, String JavaDoc selection, String JavaDoc encoding)
344         {
345             this.path = path;
346             this.caret = caret;
347             this.selection = selection;
348             this.encoding = encoding;
349         }
350
351         public String JavaDoc toString()
352         {
353             return path + ": " + caret;
354         }
355     } //}}}
356

357     //{{{ RecentHandler class
358
static class RecentHandler extends DefaultHandler JavaDoc
359     {
360         public void endDocument()
361         {
362             int max = jEdit.getIntegerProperty("recentFiles",50);
363             while(history.size() > max)
364                 history.removeLast();
365         }
366
367         public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId)
368         {
369             return XMLUtilities.findEntity(systemId, "recent.dtd", getClass());
370         }
371
372         public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc name)
373         {
374             if(name.equals("ENTRY"))
375             {
376                 history.addLast(new Entry(
377                     path,caret,selection,
378                     encoding));
379                 path = null;
380                 caret = 0;
381                 selection = null;
382                 encoding = null;
383             }
384             else if(name.equals("PATH"))
385                 path = charData.toString();
386             else if(name.equals("CARET"))
387                 caret = Integer.parseInt(charData.toString());
388             else if(name.equals("SELECTION"))
389                 selection = charData.toString();
390             else if(name.equals("ENCODING"))
391                 encoding = charData.toString();
392             charData.setLength(0);
393         }
394
395         public void characters(char[] ch, int start, int length)
396         {
397             charData.append(ch,start,length);
398         }
399
400         // end HandlerBase implementation
401

402         // private members
403
private String JavaDoc path;
404         private int caret;
405         private String JavaDoc selection;
406         private String JavaDoc encoding;
407         private StringBuffer JavaDoc charData = new StringBuffer JavaDoc();
408     } //}}}
409
}
410
Popular Tags