PageRenderTime 31ms CodeModel.GetById 16ms app.highlight 10ms RepoModel.GetById 2ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/org/gjt/sp/jedit/gui/HistoryModel.java

#
Java | 240 lines | 139 code | 27 blank | 74 comment | 25 complexity | b25484e34b3eae9efe62152548003da7 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 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.*;
 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.
 37 * @author Slava Pestov
 38 * @version $Id: HistoryModel.java 4299 2002-08-12 16:55:59Z spestov $
 39 */
 40public class HistoryModel
 41{
 42	//{{{ HistoryModel constructor
 43	/**
 44	 * Creates a new history list. Calling this is normally not
 45	 * necessary.
 46	 */
 47	public HistoryModel(String name)
 48	{
 49		this.name = name;
 50
 51		max = jEdit.getIntegerProperty("history",25);
 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		int index = data.indexOf(text);
 67		if(index != -1)
 68			data.removeElementAt(index);
 69
 70		data.insertElementAt(text,0);
 71
 72		while(getSize() > max)
 73			data.removeElementAt(data.size() - 1);
 74	} //}}}
 75
 76	//{{{ getItem() method
 77	/**
 78	 * Returns an item from the history list.
 79	 * @param index The index
 80	 */
 81	public String getItem(int index)
 82	{
 83		return (String)data.elementAt(index);
 84	} //}}}
 85
 86	//{{{ getSize() method
 87	/**
 88	 * Returns the number of elements in this history list.
 89	 */
 90	public int getSize()
 91	{
 92		return data.size();
 93	} //}}}
 94
 95	//{{{ getName() method
 96	/**
 97	 * Returns the name of this history list. This can be passed
 98	 * to the HistoryTextField constructor.
 99	 */
100	public String getName()
101	{
102		return name;
103	} //}}}
104
105	//{{{ getModel() method
106	/**
107	 * Returns a named model. If the specified model does not
108	 * already exist, it will be created.
109	 * @param name The model name
110	 */
111	public static HistoryModel getModel(String name)
112	{
113		if(models == null)
114			models = new Hashtable();
115
116		HistoryModel model = (HistoryModel)models.get(name);
117		if(model == null)
118		{
119			model = new HistoryModel(name);
120			models.put(name,model);
121		}
122
123		return model;
124	} //}}}
125
126	//{{{ loadHistory() method
127	/**
128	 * Loads the history from the specified file. jEdit calls this
129	 * on startup.
130	 * @param The file
131	 */
132	public static void loadHistory(File file)
133	{
134		if(models == null)
135			models = new Hashtable();
136
137		try
138		{
139			BufferedReader in = new BufferedReader(new FileReader(file));
140
141			HistoryModel currentModel = null;
142			String line;
143
144			while((line = in.readLine()) != null)
145			{
146				if(line.startsWith("[") && line.endsWith("]"))
147				{
148					if(currentModel != null)
149					{
150						models.put(currentModel.getName(),
151							currentModel);
152					}
153					currentModel = new HistoryModel(line
154						.substring(1,line.length() - 1));
155				}
156				else if(currentModel == null)
157				{
158					throw new IOException("History data starts"
159						+ " before model name");
160				}
161				else
162				{
163					currentModel.data.addElement(MiscUtilities
164						.escapesToChars(line));
165				}
166			}
167
168			if(currentModel != null)
169			{
170				models.put(currentModel.getName(),currentModel);
171			}
172
173			in.close();
174		}
175		catch(FileNotFoundException fnf)
176		{
177			Log.log(Log.DEBUG,HistoryModel.class,fnf);
178		}
179		catch(IOException io)
180		{
181			Log.log(Log.ERROR,HistoryModel.class,io);
182		}
183	} //}}}
184
185	//{{{ saveHistory() method
186	/**
187	 * Saves the history to the specified file. jEdit calls this when
188	 * it is exiting.
189	 * @param file The file
190	 */
191	public static void saveHistory(File file)
192	{
193		String lineSep = System.getProperty("line.separator");
194		try
195		{
196			BufferedWriter out = new BufferedWriter(
197				new FileWriter(file));
198
199			if(models == null)
200			{
201				out.close();
202				return;
203			}
204
205			Enumeration modelEnum = models.elements();
206			while(modelEnum.hasMoreElements())
207			{
208				HistoryModel model = (HistoryModel)modelEnum
209					.nextElement();
210
211				out.write('[');
212				out.write(model.getName());
213				out.write(']');
214				out.write(lineSep);
215
216				for(int i = 0; i < model.getSize(); i++)
217				{
218					out.write(MiscUtilities.charsToEscapes(
219						model.getItem(i),
220						TO_ESCAPE));
221					out.write(lineSep);
222				}
223			}
224
225			out.close();
226		}
227		catch(IOException io)
228		{
229			Log.log(Log.ERROR,HistoryModel.class,io);
230		}
231	} //}}}
232
233	//{{{ Private members
234	private static final String TO_ESCAPE = "\n\t\\\"'[]";
235	private String name;
236	private int max;
237	private Vector data;
238	private static Hashtable models;
239	//}}}
240}