KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > gui > HelpURLStack


1 package jimm.datavision.gui;
2 import jimm.datavision.ErrorHandler;
3 import jimm.util.I18N;
4 import java.io.IOException JavaDoc;
5 import java.net.URL JavaDoc;
6 import java.net.MalformedURLException JavaDoc;
7 import java.util.Stack JavaDoc;
8 import javax.swing.JTextField JavaDoc;
9 import javax.swing.JEditorPane JavaDoc;
10
11 /**
12  * A URL stack manages the browser-like behaviour of having a current URL
13  * and a list of previous and next URLs. It also updates a URL text field
14  * and gives URLs to the <code>JEditorPane</code> that displays them.
15  * <p>
16  * We define the home page to be the first page loaded.
17  */

18 class HelpURLStack {
19
20 protected Stack JavaDoc back;
21 protected URL JavaDoc home;
22 protected URL JavaDoc current;
23 protected Stack JavaDoc forward;
24 protected JEditorPane JavaDoc contentField;
25 protected JTextField JavaDoc urlField;
26
27 HelpURLStack(JEditorPane JavaDoc htmlField, JTextField JavaDoc textField) {
28     contentField = htmlField;
29     urlField = textField;
30     back = new Stack JavaDoc();
31     current = null;
32     forward = new Stack JavaDoc();
33 }
34
35 boolean hasPrevious() {
36     return !back.empty();
37 }
38 boolean hasNext() {
39     return !forward.empty();
40 }
41
42 void goTo(String JavaDoc urlString) {
43     try {
44     URL JavaDoc url = new URL JavaDoc(urlString);
45     goTo(url);
46     }
47     catch (MalformedURLException JavaDoc e) {
48     ErrorHandler.error(I18N.get("HelpURLStack.error_parsing")
49                + ' ' + urlString, e);
50     }
51 }
52
53 void goTo(URL JavaDoc url) {
54     if (current != null)
55     back.push(current);
56     if (home == null)
57     home = url;
58     current = url;
59     forward.clear();
60     updateGUI();
61 }
62
63 /** The home page is the same as the first page we loaded. */
64 void goToHomePage() {
65     goTo(home);
66 }
67
68 void goToPreviousPage() {
69     if (hasPrevious()) {
70     if (current != null)
71         forward.push(current);
72     current = (URL JavaDoc)back.pop();
73     }
74     updateGUI();
75 }
76
77 void goToNextPage() {
78     if (hasNext()) {
79     if (current != null)
80         back.push(current);
81     current = (URL JavaDoc)forward.pop();
82     }
83     updateGUI();
84 }
85
86 protected void updateGUI() {
87     urlField.setText(current == null ? "" : current.toString());
88     try {
89     contentField.setPage(current);
90     }
91     catch (IOException JavaDoc e) {
92     ErrorHandler.error(I18N.get("HelpURLStack.error_loading")
93                + ' ' + current, e);
94     }
95
96 }
97
98 }
99
Popular Tags