source: trunk/gli/src/org/greenstone/gatherer/gui/BrowsingPane.java@ 4367

Last change on this file since 4367 was 4367, checked in by mdewsnip, 21 years ago

Fixed tabbing.

  • Property svn:keywords set to Author Date Id Revision
File size: 12.3 KB
Line 
1package org.greenstone.gatherer.gui;
2/**
3 *#########################################################################
4 *
5 * A component of the Gatherer application, part of the Greenstone digital
6 * library suite from the New Zealand Digital Library Project at the
7 * University of Waikato, New Zealand.
8 *
9 * <BR><BR>
10 *
11 * Author: John Thompson, Greenstone Digital Library, University of Waikato
12 *
13 * <BR><BR>
14 *
15 * Copyright (C) 1999 New Zealand Digital Library Project
16 *
17 * <BR><BR>
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * <BR><BR>
25 *
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
30 *
31 * <BR><BR>
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
36 *########################################################################
37 */
38import calpa.html.*;
39import java.awt.*;
40import java.awt.event.*;
41import java.io.*;
42import java.net.*;
43import javax.swing.*;
44import org.greenstone.gatherer.Gatherer;
45import org.greenstone.gatherer.util.GURL;
46import org.greenstone.gatherer.util.Utility;
47/** This class provides browser-like functionality, with all the standard buttons. The only tricky bit is that the bookmarks are collection specific.
48 * @author John Thompson, Greenstone Digital Library, University of Waikato
49 * @version 2.3
50 */
51public class BrowsingPane
52 extends JPanel {
53 private Bookmarks bookmarks = null;
54 private CalHTMLPane view_pane = null;
55 private CalHTMLPreferences preferences = null;
56 private int back_count = 0;
57 private int forward_count = 0;
58 private JButton back_button = null;
59 private JButton bookmarks_button = null; // Collection bookmarks.
60 private JButton forward_button = null;
61 private JButton go_button = null;
62 private JButton home_button = null;
63 private JButton refresh_button = null;
64 private JButton reload_button = null;
65 private JButton stop_button = null;
66 private JLabel status_label = null;
67 private JTextField address_field = null;
68 private Observer observer = null;
69 private String args[] = null;
70 static final String BOOKMARKS_FILE = "bookmarks.txt";
71 public BrowsingPane() {
72 super();
73 // Initialization
74 bookmarks = new Bookmarks(); // Empty until collection loaded.
75 observer = new Observer();
76 preferences = new CalHTMLPreferences();
77 preferences.setHomeURL((new GURL(Gatherer.config.getString("general.home_page", false))).getURL());
78 // Creation
79 // Controls
80 JPanel button_pane = new JPanel();
81 JPanel left_button_pane = new JPanel();
82 back_button = new JButton("", Utility.getImage("back.gif"));
83 back_button.setEnabled(false);
84 back_button.setToolTipText(get("Back"));
85 refresh_button = new JButton("", Utility.getImage("reload.gif"));
86 refresh_button.setEnabled(false);
87 refresh_button.setToolTipText(get("Reload"));
88 bookmarks_button = new JButton("", Utility.getImage("bookmark.gif"));
89 bookmarks_button.setToolTipText(get("Bookmarks"));
90 home_button = new JButton("", Utility.getImage("home.gif"));
91 home_button.setToolTipText(get("Home"));
92 JPanel address_pane = new JPanel();
93 address_field = new JTextField(Gatherer.config.getString("general.home_page", false));
94 JPanel right_button_pane = new JPanel();
95 go_button = new JButton("", Utility.getImage("go.gif"));
96 go_button.setToolTipText(get("Go"));
97 stop_button = new JButton("", Utility.getImage("stop.gif"));
98 stop_button.setEnabled(false);
99 stop_button.setToolTipText(get("Stop"));
100 forward_button = new JButton("", Utility.getImage("forward.gif"));
101 forward_button.setEnabled(false);
102 forward_button.setToolTipText(get("Forward"));
103 // Calpa Pane stuff
104 view_pane = new CalHTMLPane(preferences, observer, "VIEW_PANE");
105 // Status bar
106 status_label = new JLabel(get("Ready"));
107 // Connection
108 address_field.addActionListener(new AddressFieldListener());
109 back_button.addActionListener(new BackButtonListener());
110 bookmarks_button.addActionListener(new BookMarksButtonListener());
111 forward_button.addActionListener(new ForwardButtonListener());
112 go_button.addActionListener(new GoButtonListener());
113 home_button.addActionListener(new HomeButtonListener());
114 refresh_button.addActionListener(new RefreshButtonListener());
115 stop_button.addActionListener(new StopButtonListener());
116 // Layout
117 left_button_pane.setLayout(new GridLayout(1,3));
118 left_button_pane.add(back_button);
119 left_button_pane.add(bookmarks_button);
120 left_button_pane.add(home_button);
121
122 address_pane.setBorder(BorderFactory.createEmptyBorder(10,5,10,5));
123 address_pane.setLayout(new BorderLayout());
124 address_pane.add(address_field, BorderLayout.CENTER);
125
126 right_button_pane.setLayout(new GridLayout(1,3));
127 right_button_pane.add(go_button);
128 right_button_pane.add(stop_button);
129 right_button_pane.add(forward_button);
130
131 button_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
132 button_pane.setLayout(new BorderLayout());
133 button_pane.add(left_button_pane, BorderLayout.WEST);
134 button_pane.add(address_pane, BorderLayout.CENTER);
135 button_pane.add(right_button_pane, BorderLayout.EAST);
136
137 setLayout(new BorderLayout());
138 add(button_pane, BorderLayout.NORTH);
139 add(new JScrollPane(view_pane), BorderLayout.CENTER);
140 add(status_label, BorderLayout.SOUTH);
141 }
142 /** Some actions, such as getting the HTML to render properly without throwing sixty bazillion NPE's, can only occur after the frame has been drawn to screen. To this end we have this method so we can action things -after- the frame draw.
143 */
144 public void afterDisplay() {
145 view_pane.goHome();
146 }
147
148 /** Called when a significant change occurs to the currently loaded collection.
149 * @param ready <i>true</i> if there is a collection ready to be accessed.
150 */
151 public void collectionChanged(boolean ready) {
152 // Reload the bookmarks associated with this collection.
153 if(ready) {
154 bookmarks.load();
155 }
156 else {
157 bookmarks.clear();
158 }
159 }
160
161 /** If some external class has possibly changed the state of the browsing pane then the controls should be repolled to determine which are still valid.
162 */
163 public void controlsChanged() {
164 }
165
166 /** Retrieves the current address entered in the address Textfield.
167 * @return A <strong>String</strong> representing the currently entered address.
168 */
169 public String getCurrentURL() {
170 return address_field.getText();
171 }
172
173
174 /** Retrieves a key from the Dictionary, using no extra arguments.
175 * @param key A <strong>String</strong> which maps to a certain phrase from the Dictionary.
176 * @return The <strong>String</strong> that matches the key or an error message if no match was found.
177 */
178 private String get(String key) {
179 return get(key, null);
180 }
181
182 /** Retrieves a key from the Dictionary, providing extra arguments to be inserted using a String array.
183 * @param key A <strong>String</strong> which maps to a certain phrase from the Dictionary.
184 * @param args A <strong>String[]</strong> containing further arguments (such as formatting instructions and variable values) to be taken into account when Dictionary creates the return String.
185 * @return The <strong>String</strong> that matches the key or an error message if no match was found.
186 */
187 private String get(String key, String args[]) {
188 if(key.indexOf('.') == -1) {
189 key = "Browser." + key;
190 }
191 return Gatherer.dictionary.get(key,args);
192 }
193 /** Called to validate the status of the controls. Disables those that are no longer applicable or restores those that have come back into scope of usage.
194 * @param update_address <i>true</i> if this pollControls call should update the address bar, <i>false</i> otherwise.
195 */
196 private void validateControls() {
197 back_button.setEnabled(back_count > 0);
198 forward_button.setEnabled(forward_count > 0);
199 }
200
201 private class AddressFieldListener
202 implements ActionListener {
203 public void actionPerformed(ActionEvent event) {
204 // Attempt to create a GURL
205 GURL url = new GURL(address_field.getText());
206 address_field.setText(url.getURL().toString());
207 if(url.valid()) {
208 // Now load document
209 view_pane.showHTMLDocument(url.getURL());
210 back_count++;
211 forward_count = 0;
212 }
213 validateControls();
214 }
215 }
216
217 private class BackButtonListener
218 implements ActionListener {
219 public void actionPerformed(ActionEvent event) {
220 view_pane.goBack();
221 back_count--;
222 forward_count++;
223 validateControls();
224 }
225 }
226
227 private class Bookmarks
228 extends JDialog {
229
230 private DefaultListModel data = null;
231
232 public Bookmarks() {
233 data = new DefaultListModel();
234 }
235
236 public void clear() {
237 data.removeAllElements();
238 // Re-add the "Add Bookmark" entry.
239 //add
240 }
241
242 public void load() {
243 // Clear existing
244 data.removeAllElements();
245 // Determine the file to load from...
246 File bookmarks = new File(Gatherer.c_man.getCollectionDirectory() + BOOKMARKS_FILE);
247 if(bookmarks.exists()) {
248 // Read in file line at a time adding to bookmarks.
249 }
250 }
251
252 public int count() {
253 return data.size();
254 }
255
256 private class Entry {
257 public String title = null;
258 public URL url = null;
259 public Entry(String title, String url_raw) {
260 this.title = title;
261 try {
262 url = new URL(url_raw);
263 }
264 catch(Exception error) {
265
266 }
267 }
268 public String toString() {
269 return title;
270 }
271 }
272 }
273
274 private class BookMarksButtonListener
275 implements ActionListener {
276 public void actionPerformed(ActionEvent event) {
277
278 }
279 }
280
281 private class ForwardButtonListener
282 implements ActionListener {
283 public void actionPerformed(ActionEvent event) {
284 view_pane.goForward();
285 back_count++;
286 forward_count--;
287 validateControls();
288 }
289 }
290
291 private class GoButtonListener
292 implements ActionListener {
293 public void actionPerformed(ActionEvent event) {
294 // Attempt to create a GURL
295 GURL url = new GURL(address_field.getText());
296 address_field.setText(url.getURL().toString());
297 if(url.valid()) {
298 // Now load document
299 view_pane.showHTMLDocument(url.getURL());
300 back_count++;
301 forward_count = 0;
302 }
303 validateControls();
304 }
305 }
306
307 private class HomeButtonListener
308 implements ActionListener {
309 public void actionPerformed(ActionEvent event) {
310 address_field.setText(Gatherer.config.getString("general.home_page", false));
311 view_pane.goHome();
312 back_count++;
313 forward_count = 0;
314 validateControls();
315 }
316 }
317
318 private class RefreshButtonListener
319 implements ActionListener {
320 public void actionPerformed(ActionEvent event) {
321 view_pane.reloadDocument();
322 validateControls();
323 }
324 }
325
326 private class StopButtonListener
327 implements ActionListener {
328 public void actionPerformed(ActionEvent event) {
329 view_pane.stopAll();
330 validateControls();
331 }
332 }
333
334 private class Observer
335 extends DefaultCalHTMLObserver {
336 public void linkActivatedUpdate(CalHTMLPane pane, URL url, String target_frame, String j_name) {
337 address_field.setText(url.toString());
338 back_count++;
339 validateControls();
340 }
341 public void linkFocusUpdate(CalHTMLPane pane, URL url) {
342 ///ystem.err.println("Link in focus " + url.toString());
343 args = new String[1];
344 args[0] = url.toString();
345 status_label.setText(get("Follow"));
346 args = null;
347 }
348 public void statusUpdate(CalHTMLPane pane, int state, URL url, int value, String message) {
349 switch(state) {
350 case 11:
351 // Can only stop if somethings happening.
352 stop_button.setEnabled(true);
353 args = new String[1];
354 args[0] = url.toString();
355 status_label.setText(get("Loading", args));
356 args = null;
357 break;
358 case 14:
359 // All done.
360 stop_button.setEnabled(false);
361 // Rendering complete.
362 status_label.setText(get("Ready"));
363 go_button.setEnabled(true);
364 stop_button.setEnabled(false);
365 refresh_button.setEnabled(true);
366 // Update mirror pane
367 Gatherer.g_man.mirror_pane.setURL(url.toString());
368 break;
369 case 74:
370 address_field.setText(url.toString());
371 default:
372 ///ystem.err.println(state + ": " + url + " \"" + message + "\"");
373 }
374 }
375 }
376}
Note: See TracBrowser for help on using the repository browser.