KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > gui > HistoryModel


1 /*
2  * HistoryModel.java - History list model
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 1999, 2005 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
23 package org.gjt.sp.jedit.gui;
24
25 //{{{ Imports
26
import javax.swing.DefaultListModel JavaDoc;
27 import java.util.*;
28 //}}}
29

30 /**
31  * A history list. One history list can be used by several history text
32  * fields. Note that the list model implementation is incomplete; no events
33  * are fired when the history model changes.
34  *
35  * @author Slava Pestov
36  * @version $Id: HistoryModel.java 8265 2006-12-27 18:12:29Z vampire0 $
37  */

38 public class HistoryModel extends DefaultListModel JavaDoc
39     implements MutableListModel
40 {
41     //{{{ HistoryModel constructor
42
/**
43      * Creates a new history list. Calling this is normally not
44      * necessary.
45      */

46     public HistoryModel(String JavaDoc name)
47     {
48         this.name = name;
49     } //}}}
50

51     //{{{ addItem() method
52
/**
53      * Adds an item to the end of this history list, trimming the list
54      * to the maximum number of items if necessary.
55      * @param text The item
56      */

57     public void addItem(String JavaDoc text)
58     {
59         if(text == null || text.length() == 0)
60             return;
61
62         int index = indexOf(text);
63         if(index != -1)
64             removeElementAt(index);
65
66         insertElementAt(text,0);
67
68         while(getSize() > max)
69             removeElementAt(getSize() - 1);
70     } //}}}
71

72     //{{{ insertElementAt() method
73
public void insertElementAt(Object JavaDoc obj, int index)
74     {
75         modified = true;
76         super.insertElementAt(obj,index);
77     } //}}}
78

79     //{{{ getItem() method
80
/**
81      * Returns an item from the history list.
82      * @param index The index
83      */

84     public String JavaDoc getItem(int index)
85     {
86         return (String JavaDoc)elementAt(index);
87     } //}}}
88

89     //{{{ removeElement() method
90
public boolean removeElement(Object JavaDoc obj)
91     {
92         modified = true;
93         return super.removeElement(obj);
94     } //}}}
95

96     //{{{ clear() method
97
/**
98      * @deprecated Call <code>removeAllElements()</code> instead.
99      */

100     public void clear()
101     {
102         removeAllElements();
103     } //}}}
104

105     //{{{ removeAllElements() method
106
public void removeAllElements()
107     {
108         modified = true;
109         super.removeAllElements();
110     } //}}}
111

112     //{{{ getName() method
113
/**
114      * Returns the name of this history list. This can be passed
115      * to the HistoryTextField constructor.
116      */

117     public String JavaDoc getName()
118     {
119         return name;
120     } //}}}
121

122     //{{{ getModel() method
123
/**
124      * Returns a named model. If the specified model does not
125      * already exist, it will be created.
126      * @param name The model name
127      */

128     public static HistoryModel getModel(String JavaDoc name)
129     {
130         if(models == null)
131             models = Collections.synchronizedMap(new HashMap<String JavaDoc, HistoryModel>());
132
133         HistoryModel model = models.get(name);
134         if(model == null)
135         {
136             model = new HistoryModel(name);
137             models.put(name,model);
138         }
139
140         return model;
141     } //}}}
142

143     //{{{ loadHistory() method
144
public static void loadHistory()
145     {
146         if (saver != null)
147             models = saver.load(models);
148     } //}}}
149

150     //{{{ saveHistory() method
151
public static void saveHistory()
152     {
153         if (saver != null && modified && saver.save(models))
154             modified = false;
155     } //}}}
156

157     //{{{ setMax() method
158
public static void setMax(int max)
159     {
160         HistoryModel.max = max;
161     } //}}}
162

163     //{{{ setSaver() method
164
public static void setSaver(HistoryModelSaver saver)
165     {
166         HistoryModel.saver = saver;
167     } //}}}
168

169     //{{{ Private members
170
private static int max;
171
172     private String JavaDoc name;
173     private static Map<String JavaDoc, HistoryModel> models;
174
175     private static boolean modified;
176     private static HistoryModelSaver saver;
177     //}}}
178
}
179
Popular Tags