PageRenderTime 33ms CodeModel.GetById 20ms app.highlight 11ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Java | 229 lines | 141 code | 28 blank | 60 comment | 26 complexity | 357936784c97550d1b785b1153e842d8 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1/*
  2 * HistoryModel.java - History list model
  3 * Copyright (C) 1999 Slava Pestov
  4 *
  5 * This program is free software; you can redistribute it and/or
  6 * modify it under the terms of the GNU General Public License
  7 * as published by the Free Software Foundation; either version 2
  8 * of the License, or any later version.
  9 *
 10 * This program is distributed in the hope that it will be useful,
 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13 * GNU General Public License for more details.
 14 *
 15 * You should have received a copy of the GNU General Public License
 16 * along with this program; if not, write to the Free Software
 17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 18 */
 19
 20package org.gjt.sp.jedit.gui;
 21
 22import javax.swing.*;
 23import java.io.*;
 24import java.util.*;
 25import org.gjt.sp.jedit.jEdit;
 26import org.gjt.sp.jedit.MiscUtilities;
 27import org.gjt.sp.util.Log;
 28
 29/**
 30 * A history list. One history list can be used by several history text
 31 * fields.
 32 * @author Slava Pestov
 33 * @version $Id: HistoryModel.java 3805 2001-09-10 08:46:23Z spestov $
 34 */
 35public class HistoryModel
 36{
 37	/**
 38	 * Creates a new history list. Calling this is normally not
 39	 * necessary.
 40	 */
 41	public HistoryModel(String name)
 42	{
 43		this.name = name;
 44
 45		max = jEdit.getIntegerProperty("history",25);
 46		data = new Vector(max);
 47	}
 48
 49	/**
 50	 * Adds an item to the end of this history list, trimming the list
 51	 * to the maximum number of items if necessary.
 52	 * @param text The item
 53	 */
 54	public void addItem(String text)
 55	{
 56		if(text == null || text.length() == 0)
 57			return;
 58
 59		int index = data.indexOf(text);
 60		if(index != -1)
 61			data.removeElementAt(index);
 62
 63		data.insertElementAt(text,0);
 64
 65		if(getSize() > max)
 66			data.removeElementAt(getSize() - 1);
 67	}
 68
 69	/**
 70	 * Returns an item from the history list.
 71	 * @param index The index
 72	 */
 73	public String getItem(int index)
 74	{
 75		return (String)data.elementAt(index);
 76	}
 77
 78	/**
 79	 * Returns the number of elements in this history list.
 80	 */
 81	public int getSize()
 82	{
 83		return data.size();
 84	}
 85
 86	/**
 87	 * Returns the name of this history list. This can be passed
 88	 * to the HistoryTextField constructor.
 89	 */
 90	public String getName()
 91	{
 92		return name;
 93	}
 94
 95	/**
 96	 * Returns a named model. If the specified model does not
 97	 * already exist, it will be created.
 98	 * @param name The model name
 99	 */
100	public static HistoryModel getModel(String name)
101	{
102		if(models == null)
103			models = new Hashtable();
104
105		HistoryModel model = (HistoryModel)models.get(name);
106		if(model == null)
107		{
108			model = new HistoryModel(name);
109			models.put(name,model);
110		}
111
112		return model;
113	}
114
115	/**
116	 * Loads the history from the specified file. jEdit calls this
117	 * on startup.
118	 * @param The file
119	 */
120	public static void loadHistory(File file)
121	{
122		if(models == null)
123			models = new Hashtable();
124
125		try
126		{
127			BufferedReader in = new BufferedReader(new FileReader(file));
128
129			HistoryModel currentModel = null;
130			String line;
131
132			while((line = in.readLine()) != null)
133			{
134				if(line.startsWith("[") && line.endsWith("]"))
135				{
136					if(currentModel != null)
137					{
138						models.put(currentModel.getName(),
139							currentModel);
140					}
141					currentModel = new HistoryModel(line
142						.substring(1,line.length() - 1));
143				}
144				else if(currentModel == null)
145				{
146					throw new IOException("History data starts"
147						+ " before model name");
148				}
149				else
150				{
151					currentModel.addItemToEnd(MiscUtilities
152						.escapesToChars(line));
153				}
154			}
155
156			if(currentModel != null)
157			{
158				models.put(currentModel.getName(),currentModel);
159			}
160
161			in.close();
162		}
163		catch(FileNotFoundException fnf)
164		{
165			Log.log(Log.DEBUG,HistoryModel.class,fnf);
166		}
167		catch(IOException io)
168		{
169			Log.log(Log.ERROR,HistoryModel.class,io);
170		}
171	}
172
173	/**
174	 * Saves the history to the specified file. jEdit calls this when
175	 * it is exiting.
176	 * @param file The file
177	 */
178	public static void saveHistory(File file)
179	{
180		String lineSep = System.getProperty("line.separator");
181		try
182		{
183			BufferedWriter out = new BufferedWriter(
184				new FileWriter(file));
185
186			if(models == null)
187			{
188				out.close();
189				return;
190			}
191
192			Enumeration modelEnum = models.elements();
193			while(modelEnum.hasMoreElements())
194			{
195				HistoryModel model = (HistoryModel)modelEnum
196					.nextElement();
197
198				out.write('[');
199				out.write(model.getName());
200				out.write(']');
201				out.write(lineSep);
202
203				for(int i = 0; i < model.getSize(); i++)
204				{
205					out.write(MiscUtilities.charsToEscapes(
206						model.getItem(i),true));
207					out.write(lineSep);
208				}
209			}
210
211			out.close();
212		}
213		catch(IOException io)
214		{
215			Log.log(Log.ERROR,HistoryModel.class,io);
216		}
217	}
218
219	// private members
220	private String name;
221	private int max;
222	private Vector data;
223	private static Hashtable models;
224
225	private void addItemToEnd(String item)
226	{
227		data.addElement(item);
228	}
229}