KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > apps > svgbrowser > LocalHistory


1 /*
2
3    Copyright 2001-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.apps.svgbrowser;
19
20 import java.awt.event.ActionEvent JavaDoc;
21 import java.awt.event.ActionListener JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24
25 import javax.swing.ButtonGroup JavaDoc;
26 import javax.swing.JMenu JavaDoc;
27 import javax.swing.JMenuBar JavaDoc;
28 import javax.swing.JMenuItem JavaDoc;
29 import javax.swing.JRadioButtonMenuItem JavaDoc;
30
31 /**
32  * This class represents an history of the files visited by a single
33  * browser frame.
34  *
35  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
36  * @version $Id: LocalHistory.java,v 1.12 2004/08/18 07:12:27 vhardy Exp $
37  */

38 public class LocalHistory {
39     /**
40      * The frame to manage.
41      */

42     protected JSVGViewerFrame svgFrame;
43
44     /**
45      * The menu which contains the history.
46      */

47     protected JMenu JavaDoc menu;
48
49     /**
50      * The index of the first history item in this menu.
51      */

52     protected int index;
53
54     /**
55      * The visited URIs.
56      */

57     protected List JavaDoc visitedURIs = new ArrayList JavaDoc();
58
59     /**
60      * The index of the current URI.
61      */

62     protected int currentURI = -1;
63
64     /**
65      * The button group for the menu items.
66      */

67     protected ButtonGroup JavaDoc group = new ButtonGroup JavaDoc();
68
69     /**
70      * The action listener.
71      */

72     protected ActionListener JavaDoc actionListener = new RadioListener();
73
74     /**
75      * The current state.
76      */

77     protected int state;
78
79     // States
80
protected final static int STABLE_STATE = 0;
81     protected final static int BACK_PENDING_STATE = 1;
82     protected final static int FORWARD_PENDING_STATE = 2;
83     protected final static int RELOAD_PENDING_STATE = 3;
84
85     /**
86      * Creates a new local history.
87      * @param mb The menubar used to display the history. It must
88      * contains one '@@@' item used as marker to place the
89      * history items.
90      * @param svgFrame The frame to manage.
91      */

92     public LocalHistory(JMenuBar JavaDoc mb, JSVGViewerFrame svgFrame) {
93         this.svgFrame = svgFrame;
94
95         // Find the marker.
96
int mc = mb.getMenuCount();
97         for (int i = 0; i < mc; i++) {
98             JMenu JavaDoc m = mb.getMenu(i);
99             int ic = m.getItemCount();
100             for (int j = 0; j < ic; j++) {
101                 JMenuItem JavaDoc mi = m.getItem(j);
102                 if (mi != null) {
103                     String JavaDoc s = mi.getText();
104                     if ("@@@".equals(s)) {
105                         menu = m;
106                         index = j;
107                         m.remove(j);
108                         return;
109                     }
110                 }
111             }
112         }
113         throw new IllegalArgumentException JavaDoc("No '@@@' marker found");
114     }
115
116     /**
117      * Goes back of one position in the history.
118      * Assumes that <tt>canGoBack()</tt> is true.
119      */

120     public void back() {
121         update();
122         state = BACK_PENDING_STATE;
123         currentURI -= 2;
124         svgFrame.showSVGDocument((String JavaDoc)visitedURIs.get(currentURI + 1));
125     }
126
127     /**
128      * Whether it is possible to go back.
129      */

130     public boolean canGoBack() {
131         return currentURI > 0;
132     }
133
134     /**
135      * Goes forward of one position in the history.
136      * Assumes that <tt>canGoForward()</tt> is true.
137      */

138     public void forward() {
139         update();
140         state = FORWARD_PENDING_STATE;
141         svgFrame.showSVGDocument((String JavaDoc)visitedURIs.get(currentURI + 1));
142     }
143
144     /**
145      * Whether it is possible to go forward.
146      */

147     public boolean canGoForward() {
148         return currentURI < visitedURIs.size() - 1;
149     }
150
151     /**
152      * Reloads the current document.
153      */

154     public void reload() {
155         update();
156         state = RELOAD_PENDING_STATE;
157         currentURI--;
158         svgFrame.showSVGDocument((String JavaDoc)visitedURIs.get(currentURI + 1));
159     }
160
161     /**
162      * Updates the history.
163      * @param uri The URI of the document just loaded.
164      */

165     public void update(String JavaDoc uri) {
166         if (currentURI < -1) {
167             throw new InternalError JavaDoc();
168         }
169         state = STABLE_STATE;
170         if (++currentURI < visitedURIs.size()) {
171             if (!visitedURIs.get(currentURI).equals(uri)) {
172                 int len = menu.getItemCount();
173                 for (int i = len - 1; i >= index + currentURI + 1; i--) {
174                     JMenuItem JavaDoc mi = menu.getItem(i);
175                     group.remove(mi);
176                     menu.remove(i);
177                 }
178                 visitedURIs = visitedURIs.subList(0, currentURI + 1);
179             }
180             JMenuItem JavaDoc mi = menu.getItem(index + currentURI);
181             group.remove(mi);
182             menu.remove(index + currentURI);
183             visitedURIs.set(currentURI, uri);
184         } else {
185             if (visitedURIs.size() >= 15) {
186                 visitedURIs.remove(0);
187                 JMenuItem JavaDoc mi = menu.getItem(index);
188                 group.remove(mi);
189                 menu.remove(index);
190                 currentURI--;
191             }
192             visitedURIs.add(uri);
193         }
194
195         // Computes the button text.
196
String JavaDoc text = uri;
197         int i = uri.lastIndexOf("/");
198         if (i == -1) {
199             i = uri.lastIndexOf("\\");
200             if (i != -1) {
201                 text = uri.substring(i + 1);
202             }
203         } else {
204             text = uri.substring(i + 1);
205         }
206
207         JMenuItem JavaDoc mi = new JRadioButtonMenuItem JavaDoc(text);
208         mi.setActionCommand(uri);
209         mi.addActionListener(actionListener);
210         group.add(mi);
211         mi.setSelected(true);
212         menu.insert(mi, index + currentURI);
213     }
214
215     /**
216      * Updates the state of this history.
217      */

218     protected void update() {
219         switch (state) {
220         case BACK_PENDING_STATE:
221             currentURI += 2;
222             break;
223         case RELOAD_PENDING_STATE:
224             currentURI++;
225         case FORWARD_PENDING_STATE:
226         case STABLE_STATE:
227         }
228     }
229
230     /**
231      * To listen to the radio buttons.
232      */

233     protected class RadioListener implements ActionListener JavaDoc {
234         public RadioListener() {}
235     public void actionPerformed(ActionEvent JavaDoc e) {
236         String JavaDoc uri = e.getActionCommand();
237             currentURI = getItemIndex((JMenuItem JavaDoc)e.getSource()) - 1;
238         svgFrame.showSVGDocument(uri);
239     }
240         public int getItemIndex(JMenuItem JavaDoc item) {
241             int ic = menu.getItemCount();
242             for (int i = index; i < ic; i++) {
243                 if (menu.getItem(i) == item) {
244                     return i - index;
245                 }
246             }
247             throw new InternalError JavaDoc();
248         }
249     }
250 }
251
Popular Tags