001: /*
002: * HistoryModel.java - History list model
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 1999, 2005 Slava Pestov
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package org.gjt.sp.jedit.gui;
024:
025: //{{{ Imports
026: import javax.swing.DefaultListModel;
027: import java.util.*;
028:
029: //}}}
030:
031: /**
032: * A history list. One history list can be used by several history text
033: * fields. Note that the list model implementation is incomplete; no events
034: * are fired when the history model changes.
035: *
036: * @author Slava Pestov
037: * @version $Id: HistoryModel.java 8265 2006-12-27 18:12:29Z vampire0 $
038: */
039: public class HistoryModel extends DefaultListModel implements
040: MutableListModel {
041: //{{{ HistoryModel constructor
042: /**
043: * Creates a new history list. Calling this is normally not
044: * necessary.
045: */
046: public HistoryModel(String name) {
047: this .name = name;
048: } //}}}
049:
050: //{{{ addItem() method
051: /**
052: * Adds an item to the end of this history list, trimming the list
053: * to the maximum number of items if necessary.
054: * @param text The item
055: */
056: public void addItem(String text) {
057: if (text == null || text.length() == 0)
058: return;
059:
060: int index = indexOf(text);
061: if (index != -1)
062: removeElementAt(index);
063:
064: insertElementAt(text, 0);
065:
066: while (getSize() > max)
067: removeElementAt(getSize() - 1);
068: } //}}}
069:
070: //{{{ insertElementAt() method
071: public void insertElementAt(Object obj, int index) {
072: modified = true;
073: super .insertElementAt(obj, index);
074: } //}}}
075:
076: //{{{ getItem() method
077: /**
078: * Returns an item from the history list.
079: * @param index The index
080: */
081: public String getItem(int index) {
082: return (String) elementAt(index);
083: } //}}}
084:
085: //{{{ removeElement() method
086: public boolean removeElement(Object obj) {
087: modified = true;
088: return super .removeElement(obj);
089: } //}}}
090:
091: //{{{ clear() method
092: /**
093: * @deprecated Call <code>removeAllElements()</code> instead.
094: */
095: public void clear() {
096: removeAllElements();
097: } //}}}
098:
099: //{{{ removeAllElements() method
100: public void removeAllElements() {
101: modified = true;
102: super .removeAllElements();
103: } //}}}
104:
105: //{{{ getName() method
106: /**
107: * Returns the name of this history list. This can be passed
108: * to the HistoryTextField constructor.
109: */
110: public String getName() {
111: return name;
112: } //}}}
113:
114: //{{{ getModel() method
115: /**
116: * Returns a named model. If the specified model does not
117: * already exist, it will be created.
118: * @param name The model name
119: */
120: public static HistoryModel getModel(String name) {
121: if (models == null)
122: models = Collections
123: .synchronizedMap(new HashMap<String, HistoryModel>());
124:
125: HistoryModel model = models.get(name);
126: if (model == null) {
127: model = new HistoryModel(name);
128: models.put(name, model);
129: }
130:
131: return model;
132: } //}}}
133:
134: //{{{ loadHistory() method
135: public static void loadHistory() {
136: if (saver != null)
137: models = saver.load(models);
138: } //}}}
139:
140: //{{{ saveHistory() method
141: public static void saveHistory() {
142: if (saver != null && modified && saver.save(models))
143: modified = false;
144: } //}}}
145:
146: //{{{ setMax() method
147: public static void setMax(int max) {
148: HistoryModel.max = max;
149: } //}}}
150:
151: //{{{ setSaver() method
152: public static void setSaver(HistoryModelSaver saver) {
153: HistoryModel.saver = saver;
154: } //}}}
155:
156: //{{{ Private members
157: private static int max;
158:
159: private String name;
160: private static Map<String, HistoryModel> models;
161:
162: private static boolean modified;
163: private static HistoryModelSaver saver;
164: //}}}
165: }
|