KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > bufferio > JEditKillRing


1 /*
2  * KillRing.java - Stores deleted text
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 2003, 2005 Slava Pestov
7  * Portions copyright (C) 2006 Matthieu Casanova
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22  */

23 package org.gjt.sp.jedit.bufferio;
24
25 import org.gjt.sp.jedit.buffer.KillRing;
26 import org.gjt.sp.jedit.buffer.UndoManager;
27 import org.gjt.sp.jedit.jEdit;
28 import org.gjt.sp.jedit.MiscUtilities;
29 import org.gjt.sp.util.Log;
30 import org.gjt.sp.util.XMLUtilities;
31
32 import java.io.*;
33
34 /**
35  * @author Matthieu Casanova
36  * @version $Id: ParserRuleSet.java 5471 2006-06-22 06:31:23Z kpouer $
37  */

38 public class JEditKillRing extends KillRing
39 {
40     //{{{ load() method
41
public void load()
42     {
43         String JavaDoc settingsDirectory = jEdit.getSettingsDirectory();
44         if(settingsDirectory == null)
45             return;
46
47         File killRing = new File(MiscUtilities.constructPath(
48             settingsDirectory,"killring.xml"));
49         if(!killRing.exists())
50             return;
51
52         killRingModTime = killRing.lastModified();
53         Log.log(Log.MESSAGE,KillRing.class,"Loading killring.xml");
54
55         KillRingHandler handler = new KillRingHandler();
56         try
57         {
58             XMLUtilities.parseXML(new FileInputStream(killRing), handler);
59         }
60         catch (IOException ioe)
61         {
62             Log.log(Log.ERROR, this, ioe);
63         }
64
65         ring = handler.list.toArray(
66             new UndoManager.Remove[handler.list.size()]);
67         count = 0;
68         wrap = true;
69     } //}}}
70

71     //{{{ save() method
72
public void save()
73     {
74         String JavaDoc settingsDirectory = jEdit.getSettingsDirectory();
75         if(settingsDirectory == null)
76             return;
77
78         File file1 = new File(MiscUtilities.constructPath(
79             settingsDirectory, "#killring.xml#save#"));
80         File file2 = new File(MiscUtilities.constructPath(
81             settingsDirectory, "killring.xml"));
82         if(file2.exists() && file2.lastModified() != killRingModTime)
83         {
84             Log.log(Log.WARNING,KillRing.class,file2
85                 + " changed on disk; will not save recent"
86                 + " files");
87             return;
88         }
89
90         jEdit.backupSettingsFile(file2);
91
92         Log.log(Log.MESSAGE,KillRing.class,"Saving killring.xml");
93
94         String JavaDoc lineSep = System.getProperty("line.separator");
95
96         BufferedWriter out = null;
97
98         try
99         {
100             out = new BufferedWriter(
101                 new OutputStreamWriter(
102                     new FileOutputStream(file1),
103                     "UTF-8"));
104
105             out.write("<?xml version=\"1.1\" encoding=\"UTF-8\"?>");
106             out.write(lineSep);
107             out.write("<!DOCTYPE KILLRING SYSTEM \"killring.dtd\">");
108             out.write(lineSep);
109             out.write("<KILLRING>");
110             out.write(lineSep);
111
112             int size = getSize();
113             for(int i = size - 1; i >=0; i--)
114             {
115                 out.write("<ENTRY>");
116                 out.write(XMLUtilities.charsToEntities(
117                     getElementAt(i).toString(),true));
118                 out.write("</ENTRY>");
119                 out.write(lineSep);
120             }
121
122             out.write("</KILLRING>");
123             out.write(lineSep);
124
125             out.close();
126
127             /* to avoid data loss, only do this if the above
128              * completed successfully */

129             file2.delete();
130             file1.renameTo(file2);
131         }
132         catch(Exception JavaDoc e)
133         {
134             Log.log(Log.ERROR,KillRing.class,e);
135         }
136         finally
137         {
138             try
139             {
140                 if(out != null)
141                     out.close();
142             }
143             catch(IOException e)
144             {
145             }
146         }
147
148         killRingModTime = file2.lastModified();
149     } //}}}
150
}
151
Popular Tags