PageRenderTime 175ms CodeModel.GetById 152ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 1ms

/jEdit/tags/jedit-4-2-pre4/org/gjt/sp/jedit/gui/HistoryModel.java

#
Java | 314 lines | 186 code | 46 blank | 82 comment | 36 complexity | b79218cd7f830c3663e00380189be982 MD5 | raw file
  1/*
  2 * HistoryModel.java - History list model
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 1999, 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.gui;
 24
 25//{{{ Imports
 26import javax.swing.AbstractListModel;
 27import java.io.*;
 28import java.util.*;
 29import org.gjt.sp.jedit.jEdit;
 30import org.gjt.sp.jedit.MiscUtilities;
 31import org.gjt.sp.util.Log;
 32//}}}
 33
 34/**
 35 * A history list. One history list can be used by several history text
 36 * fields. Note that the list model implementation is incomplete; no events
 37 * are fired when the history model changes.
 38 * @author Slava Pestov
 39 * @version $Id: HistoryModel.java 4841 2003-07-28 21:08:04Z spestov $
 40 */
 41public class HistoryModel extends AbstractListModel
 42{
 43	//{{{ HistoryModel constructor
 44	/**
 45	 * Creates a new history list. Calling this is normally not
 46	 * necessary.
 47	 */
 48	public HistoryModel(String name)
 49	{
 50		this.name = name;
 51
 52		data = new Vector(max);
 53	} //}}}
 54
 55	//{{{ addItem() method
 56	/**
 57	 * Adds an item to the end of this history list, trimming the list
 58	 * to the maximum number of items if necessary.
 59	 * @param text The item
 60	 */
 61	public void addItem(String text)
 62	{
 63		if(text == null || text.length() == 0)
 64			return;
 65
 66		modified = true;
 67
 68		int index = data.indexOf(text);
 69		if(index != -1)
 70			data.removeElementAt(index);
 71
 72		data.insertElementAt(text,0);
 73
 74		while(getSize() > max)
 75			data.removeElementAt(data.size() - 1);
 76	} //}}}
 77
 78	//{{{ getItem() method
 79	/**
 80	 * Returns an item from the history list.
 81	 * @param index The index
 82	 */
 83	public String getItem(int index)
 84	{
 85		return (String)data.elementAt(index);
 86	} //}}}
 87
 88	//{{{ getElementAt() method
 89	/**
 90	 * Returns an item from the history list. This method returns the
 91	 * same thing as {@link #getItem(int)} and only exists so that
 92	 * <code>HistoryModel</code> instances can be used as list models.
 93	 * @param index The index
 94	 * @since jEdit 4.2pre2
 95	 */
 96	public Object getElementAt(int index)
 97	{
 98		return getItem(index);
 99	} //}}}
100
101	//{{{ clear() method
102	/**
103	 * Removes all entries from this history model.
104	 * @since jEdit 4.2pre2
105	 */
106	public void clear()
107	{
108		data.removeAllElements();
109	} //}}}
110
111	//{{{ getSize() method
112	/**
113	 * Returns the number of elements in this history list.
114	 */
115	public int getSize()
116	{
117		return data.size();
118	} //}}}
119
120	//{{{ getName() method
121	/**
122	 * Returns the name of this history list. This can be passed
123	 * to the HistoryTextField constructor.
124	 */
125	public String getName()
126	{
127		return name;
128	} //}}}
129
130	//{{{ getModel() method
131	/**
132	 * Returns a named model. If the specified model does not
133	 * already exist, it will be created.
134	 * @param name The model name
135	 */
136	public static HistoryModel getModel(String name)
137	{
138		if(!loaded)
139			loadHistory();
140
141		if(models == null)
142			models = new Hashtable();
143
144		HistoryModel model = (HistoryModel)models.get(name);
145		if(model == null)
146		{
147			model = new HistoryModel(name);
148			models.put(name,model);
149		}
150
151		return model;
152	} //}}}
153
154	//{{{ saveHistory() method
155	public static void saveHistory()
156	{
157		if(!loaded || !modified)
158			return;
159
160		Log.log(Log.MESSAGE,HistoryModel.class,"Saving history");
161		File file1 = new File(MiscUtilities.constructPath(
162			jEdit.getSettingsDirectory(), "#history#save#"));
163		File file2 = new File(MiscUtilities.constructPath(
164			jEdit.getSettingsDirectory(), "history"));
165		if(file2.exists() && file2.lastModified() != historyModTime)
166		{
167			Log.log(Log.WARNING,HistoryModel.class,file2
168				+ " changed on disk; will not save history");
169			return;
170		}
171
172		jEdit.backupSettingsFile(file2);
173
174		String lineSep = System.getProperty("line.separator");
175
176		try
177		{
178			BufferedWriter out = new BufferedWriter(
179				new FileWriter(file1));
180
181			if(models == null)
182			{
183				out.close();
184				return;
185			}
186
187			Enumeration modelEnum = models.elements();
188			while(modelEnum.hasMoreElements())
189			{
190				HistoryModel model = (HistoryModel)modelEnum
191					.nextElement();
192				if(model.getSize() == 0)
193					continue;
194
195				out.write('[');
196				out.write(model.getName());
197				out.write(']');
198				out.write(lineSep);
199
200				for(int i = 0; i < model.getSize(); i++)
201				{
202					out.write(MiscUtilities.charsToEscapes(
203						model.getItem(i),
204						TO_ESCAPE));
205					out.write(lineSep);
206				}
207			}
208
209			out.close();
210
211			/* to avoid data loss, only do this if the above
212			 * completed successfully */
213			file2.delete();
214			file1.renameTo(file2);
215			modified = false;
216		}
217		catch(IOException io)
218		{
219			Log.log(Log.ERROR,HistoryModel.class,io);
220		}
221
222		historyModTime = file2.lastModified();
223	} //}}}
224
225	//{{{ propertiesChanged() method
226	public static void propertiesChanged()
227	{
228		max = jEdit.getIntegerProperty("history",25);
229	} //}}}
230
231	//{{{ Private members
232	private static final String TO_ESCAPE = "\n\t\\\"'[]";
233	private static int max;
234
235	private String name;
236	private Vector data;
237	private static Hashtable models;
238
239	private static boolean loaded;
240	private static boolean modified;
241	private static File history;
242	private static long historyModTime;
243
244	//{{{ loadHistory() method
245	private static void loadHistory()
246	{
247		String settingsDirectory = jEdit.getSettingsDirectory();
248		if(settingsDirectory == null)
249			return;
250
251		history = new File(MiscUtilities.constructPath(
252			settingsDirectory,"history"));
253		if(!history.exists())
254			return;
255
256		historyModTime = history.lastModified();
257
258		Log.log(Log.MESSAGE,HistoryModel.class,"Loading history.xml");
259
260		loaded = true;
261
262		if(models == null)
263			models = new Hashtable();
264
265		try
266		{
267			BufferedReader in = new BufferedReader(new FileReader(history));
268
269			HistoryModel currentModel = null;
270			String line;
271
272			while((line = in.readLine()) != null)
273			{
274				if(line.startsWith("[") && line.endsWith("]"))
275				{
276					if(currentModel != null)
277					{
278						models.put(currentModel.getName(),
279							currentModel);
280					}
281					currentModel = new HistoryModel(line
282						.substring(1,line.length() - 1));
283				}
284				else if(currentModel == null)
285				{
286					throw new IOException("History data starts"
287						+ " before model name");
288				}
289				else
290				{
291					currentModel.data.addElement(MiscUtilities
292						.escapesToChars(line));
293				}
294			}
295
296			if(currentModel != null)
297			{
298				models.put(currentModel.getName(),currentModel);
299			}
300
301			in.close();
302		}
303		catch(FileNotFoundException fnf)
304		{
305			//Log.log(Log.DEBUG,HistoryModel.class,fnf);
306		}
307		catch(IOException io)
308		{
309			Log.log(Log.ERROR,HistoryModel.class,io);
310		}
311	} //}}}
312
313	//}}}
314}