1 package org.astrogrid.ui.help;
2
3 import java.util.Vector;
4 import javax.swing.tree.*;
5 import java.awt.event.*;
6 import org.astrogrid.log.Log;
7
8 /***
9 * History (from the navigator package)
10 *
11 * Provides a way of tracking a history, such as may be used by browsers.
12 * This takes & returns Object types for each instance; it's up to the
13 * using class to check for typesafety.
14 *
15 * Is an action listener, so can listen for (eg) button events - which
16 * should have the commands given as final constants below
17 *
18 * Similarly, it generates History events, so that other components can tell if
19 * it has changed
20 *
21 * @author M Hill
22 */
23
24 public class History implements ActionListener
25 {
26 private Vector history = new Vector();
27 private int historyCursor = -1;
28
29 private Vector listeners = new Vector();
30
31
32 public static final String HISTORY_CMD_BACK = "BackHistory";
33 public static final String HISTORY_CMD_FWD = "FwdHistory";
34 public static final String HISTORY_CMD_CLEAR = "Clear History";
35
36 /***
37 * Add item to history at current index. ie, mmove forward,
38 * ie trim at current index, add new item to end, and move
39 * cursor forward to that new item
40 */
41 public void add(Object item)
42 {
43 trim();
44 history.add(item);
45 historyCursor++;
46 fireHistoryChange();
47 }
48
49 /***
50 * Chops history so it contains only all those before and
51 * <b>including</b> the historyCursor
52 */
53 private void trim()
54 {
55 if (historyCursor>-1)
56 while (history.size()>(historyCursor+1))
57 history.removeElementAt(historyCursor+1);
58 else
59 history.clear();
60
61 }
62
63 /***
64 * Gets current history item - returns null if no history
65 */
66 public Object getCurrentItem()
67 {
68 if (historyCursor<0)
69 return null;
70 else
71 return history.elementAt(historyCursor);
72 }
73
74 public Object getPreviousItem()
75 {
76 if (historyCursor<1)
77 return null;
78 else
79 return history.elementAt(historyCursor-1);
80 }
81
82 /***
83 * Clears history - resets to empty
84 */
85 public void clear()
86 {
87 historyCursor = -1;
88 trim();
89 }
90
91 /***
92 * move to previous item in history and return it.
93 * Returns null if no previous
94 */
95 public Object moveBack()
96 {
97 if (historyCursor >0)
98 {
99 historyCursor--;
100 fireHistoryChange();
101 return getCurrentItem();
102 }
103 return null;
104 }
105
106 /***
107 * Move to next item in history and return it
108 * Returns null if no next
109 */
110 public Object moveForward()
111 {
112 if (historyCursor+1<history.size())
113 {
114 historyCursor++;
115 fireHistoryChange();
116 return getCurrentItem();
117 }
118 return null;
119 }
120
121
122 /***
123 * Register listener to listen to history cursor changes
124 *
125 public void addHistoryListener(HistoryListener newListener)
126 {
127 listeners.add(newListener);
128 }
129
130 /**
131 * Let listeners know of change
132 */
133 private void fireHistoryChange()
134 {
135 dump();
136
137
138
139
140
141
142 }
143
144 /***
145 * Returns true if cursor is on last history item
146 */
147 public boolean isLast()
148 {
149 return (historyCursor == history.size()-1);
150 }
151
152 /***
153 * Returns true if cursor is on first history item
154 */
155 public boolean isFirst()
156 {
157 return (historyCursor == 0) || (historyCursor == -1);
158 }
159
160 /***
161 * Returns true if there is a previous item
162 */
163 public boolean isPrevious()
164 {
165 return (historyCursor > 0);
166 }
167
168 /***
169 * Returns true if there is a next item
170 */
171 public boolean isNext()
172 {
173 return (historyCursor < history.size()-1);
174 }
175 /***
176 * For debug - dump history to Log
177 */
178 public void dump()
179 {
180
181 String s = "History: ";
182 for (int i=0;i<history.size();i++)
183 {
184 if (i == historyCursor)
185 s = s + ">>"+history.elementAt(i)+"<<, ";
186 else
187 s = s + ""+history.elementAt(i)+", ";
188 }
189 Log.trace(s);
190 }
191
192 /***
193 * Although the History class exposes its action methods
194 * (moveBack(), etc), it can also be given instructions via
195 * ActionEvents. in other words, you can create a "Back" Button,
196 * set its 'actionCommand' to a History command (see HISTORY_CMD_*),
197 * and register a History instance with it, and Robert is your
198 * father's brother.
199 */
200 public void actionPerformed(ActionEvent anEvent)
201 {
202 if (anEvent.getActionCommand() == HISTORY_CMD_BACK)
203 moveBack();
204 else if (anEvent.getActionCommand() == HISTORY_CMD_FWD)
205 moveForward();
206 else if (anEvent.getActionCommand() == HISTORY_CMD_CLEAR)
207 clear();
208 }
209
210 }
211