KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > help > HelpHistoryModel


1 /*
2  * HelpHistoryModel.java - History Model for Help GUI
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 2005 Nicholas O'Leary
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.help;
24
25 /**
26  * History model used by the help browser
27  * @author Nicholas O'Leary
28  * @version $Id: HelpHistoryModel.java 5295 2005-11-05 06:42:54Z ezust $
29  */

30 public class HelpHistoryModel
31 {
32     //{{{ HelpHistoryModel
33
public HelpHistoryModel(int size)
34     {
35         int
36         historyPos = 0;
37         history = new HistoryEntry[size];
38         listeners = new java.util.Vector JavaDoc();
39     } //}}}
40

41     //{{{ forward
42
public String JavaDoc forward()
43     {
44         if(history.length - historyPos <= 1)
45             return null;
46         if (history[historyPos] == null)
47             return null;
48         String JavaDoc url = history[historyPos].url;
49         historyPos++;
50         fireUpdate();
51         return url;
52     } //}}}
53

54     //{{{ hasNext
55
public boolean hasNext()
56     {
57         return !(history.length - historyPos <= 1 ||
58             history[historyPos] == null);
59     } //}}}
60

61     //{{{ back
62
public String JavaDoc back()
63     {
64         if (historyPos<=1)
65             return null;
66         String JavaDoc url = history[--historyPos - 1].url;
67         fireUpdate();
68         return url;
69     } //}}}
70

71     //{{{ hasPrevious
72
public boolean hasPrevious()
73     {
74         return (historyPos>1);
75     } //}}}
76

77     //{{{ addToHistory
78
public void addToHistory(String JavaDoc url)
79     {
80         history[historyPos] = new HistoryEntry(url,url);
81         if(historyPos + 1 == history.length)
82         {
83             System.arraycopy(history,1,history,
84                 0,history.length - 1);
85             history[historyPos] = null;
86         }
87         else
88         {
89             historyPos++;
90             for (int i = historyPos;i<history.length;i++)
91             {
92                 history[i] = null;
93             }
94         }
95         fireUpdate();
96     } //}}}
97

98     //{{{ setCurrentEntry
99
public void setCurrentEntry(HistoryEntry entry)
100     {
101         for (int i=0;i<history.length;i++)
102         {
103             if (history[i]!=null && history[i].equals(entry))
104             {
105                 historyPos = i+1;
106                 fireUpdate();
107                 break;
108             }
109         }
110         // Do nothing?
111
} //}}}
112

113     //{{{ updateTitle
114
public void updateTitle(String JavaDoc url, String JavaDoc title)
115     {
116         for (int i=0;i<history.length;i++)
117         {
118             if (history[i]!=null && history[i].url.equals(url))
119             {
120                 history[i].title = title;
121             }
122         }
123         fireUpdate();
124     }//}}}
125

126     //{{{ getPreviousURLs
127
public HistoryEntry[] getPreviousURLs()
128     {
129         if (historyPos<=1)
130             return new HelpHistoryModel.HistoryEntry[0];
131         HistoryEntry[] previous = new HistoryEntry[historyPos-1];
132         System.arraycopy(history,0,previous,0,historyPos-1);
133         return previous;
134     } //}}}
135

136     //{{{ getNextURLs
137
public HistoryEntry[] getNextURLs()
138     {
139         if (history.length - historyPos <= 1)
140             return new HelpHistoryModel.HistoryEntry[0];
141         if (history[historyPos] == null)
142             return new HelpHistoryModel.HistoryEntry[0];
143         HistoryEntry[] next = new HistoryEntry[history.length-historyPos];
144         System.arraycopy(history,historyPos,next,0,history.length-historyPos);
145         return next;
146     } //}}}
147

148     //{{{ addHelpHistoryModelListener
149
public void addHelpHistoryModelListener(HelpHistoryModelListener hhml)
150     {
151         listeners.add(hhml);
152     } //}}}
153

154     //{{{ removeHelpHistoryModelListener
155
public void removeHelpHistoryModelListener(HelpHistoryModelListener hhml)
156     {
157         listeners.remove(hhml);
158     } //}}}
159

160     //{{{ fireUpdate
161
public void fireUpdate()
162     {
163         for (int i=0;i<listeners.size();i++)
164             ((HelpHistoryModelListener)listeners.elementAt(i)).historyUpdated();
165     } //}}}
166

167     //{{{ Private members
168
private int historyPos;
169     private HistoryEntry[] history;
170     private java.util.Vector JavaDoc listeners;
171     //}}}
172

173     //{{{ Inner Classes
174
class HistoryEntry
175     {
176         String JavaDoc url;
177         String JavaDoc title;
178         HistoryEntry(String JavaDoc url,String JavaDoc title)
179         {
180             this.url = url;
181             this.title = title;
182         }
183         public boolean equals(HistoryEntry he)
184         {
185             return he.url.equals(this.url) &&
186                 he.title.equals(this.title);
187         }
188     }
189     //}}}
190
}
191
192
193
Popular Tags