01: package jimm.datavision.gui;
02:
03: import jimm.datavision.ErrorHandler;
04: import jimm.util.I18N;
05: import java.io.IOException;
06: import java.net.URL;
07: import java.net.MalformedURLException;
08: import java.util.Stack;
09: import javax.swing.JTextField;
10: import javax.swing.JEditorPane;
11:
12: /**
13: * A URL stack manages the browser-like behaviour of having a current URL
14: * and a list of previous and next URLs. It also updates a URL text field
15: * and gives URLs to the <code>JEditorPane</code> that displays them.
16: * <p>
17: * We define the home page to be the first page loaded.
18: */
19: class HelpURLStack {
20:
21: protected Stack back;
22: protected URL home;
23: protected URL current;
24: protected Stack forward;
25: protected JEditorPane contentField;
26: protected JTextField urlField;
27:
28: HelpURLStack(JEditorPane htmlField, JTextField textField) {
29: contentField = htmlField;
30: urlField = textField;
31: back = new Stack();
32: current = null;
33: forward = new Stack();
34: }
35:
36: boolean hasPrevious() {
37: return !back.empty();
38: }
39:
40: boolean hasNext() {
41: return !forward.empty();
42: }
43:
44: void goTo(String urlString) {
45: try {
46: URL url = new URL(urlString);
47: goTo(url);
48: } catch (MalformedURLException e) {
49: ErrorHandler.error(I18N.get("HelpURLStack.error_parsing")
50: + ' ' + urlString, e);
51: }
52: }
53:
54: void goTo(URL url) {
55: if (current != null)
56: back.push(current);
57: if (home == null)
58: home = url;
59: current = url;
60: forward.clear();
61: updateGUI();
62: }
63:
64: /** The home page is the same as the first page we loaded. */
65: void goToHomePage() {
66: goTo(home);
67: }
68:
69: void goToPreviousPage() {
70: if (hasPrevious()) {
71: if (current != null)
72: forward.push(current);
73: current = (URL) back.pop();
74: }
75: updateGUI();
76: }
77:
78: void goToNextPage() {
79: if (hasNext()) {
80: if (current != null)
81: back.push(current);
82: current = (URL) forward.pop();
83: }
84: updateGUI();
85: }
86:
87: protected void updateGUI() {
88: urlField.setText(current == null ? "" : current.toString());
89: try {
90: contentField.setPage(current);
91: } catch (IOException e) {
92: ErrorHandler.error(I18N.get("HelpURLStack.error_loading")
93: + ' ' + current, e);
94: }
95:
96: }
97:
98: }
|