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

Last change on this file since 4560 was 4560, checked in by jmt12, 21 years ago

2030123: Have now (successfully) told CalPane not to cache pages. After the first 'a=config&cmd=release-collection' the page returned was being cached and so the index files weren't released for subsequent builds.

  • 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
74 CalHTMLManager.setCacheDocuments(false);
75
76 // Initialization
77 bookmarks = new Bookmarks(); // Empty until collection loaded.
78 observer = new Observer();
79 preferences = new CalHTMLPreferences();
80 preferences.setHomeURL((new GURL(Gatherer.config.getString("general.home_page", false))).getURL());
81 // Creation
82 // Controls
83 JPanel button_pane = new JPanel();
84 JPanel left_button_pane = new JPanel();
85 back_button = new JButton("", Utility.getImage("back.gif"));
86 back_button.setEnabled(false);
87 back_button.setToolTipText(get("Back"));
88 refresh_button = new JButton("", Utility.getImage("reload.gif"));
89 refresh_button.setEnabled(false);
90 refresh_button.setToolTipText(get("Reload"));
91 bookmarks_button = new JButton("", Utility.getImage("bookmark.gif"));
92 bookmarks_button.setToolTipText(get("Bookmarks"));
93 home_button = new JButton("", Utility.getImage("home.gif"));
94 home_button.setToolTipText(get("Home"));
95 JPanel address_pane = new JPanel();
96 address_field = new JTextField(Gatherer.config.getString("general.home_page", false));
97 JPanel right_button_pane = new JPanel();
98 go_button = new JButton("", Utility.getImage("go.gif"));
99 go_button.setToolTipText(get("Go"));
100 stop_button = new JButton("", Utility.getImage("stop.gif"));
101 stop_button.setEnabled(false);
102 stop_button.setToolTipText(get("Stop"));
103 forward_button = new JButton("", Utility.getImage("forward.gif"));
104 forward_button.setEnabled(false);
105 forward_button.setToolTipText(get("Forward"));
106 // Calpa Pane stuff
107 view_pane = new CalHTMLPane(preferences, observer, "VIEW_PANE");
108 // Status bar
109 status_label = new JLabel(get("Ready"));
110 // Connection
111 address_field.addActionListener(new AddressFieldListener());
112 back_button.addActionListener(new BackButtonListener());
113 bookmarks_button.addActionListener(new BookMarksButtonListener());
114 forward_button.addActionListener(new ForwardButtonListener());
115 go_button.addActionListener(new GoButtonListener());
116 home_button.addActionListener(new HomeButtonListener());
117 refresh_button.addActionListener(new RefreshButtonListener());
118 stop_button.addActionListener(new StopButtonListener());
119 // Layout
120 left_button_pane.setLayout(new GridLayout(1,3));
121 left_button_pane.add(back_button);
122 left_button_pane.add(bookmarks_button);
123 left_button_pane.add(home_button);
124
125 address_pane.setBorder(BorderFactory.createEmptyBorder(10,5,10,5));
126 address_pane.setLayout(new BorderLayout());
127 address_pane.add(address_field, BorderLayout.CENTER);
128
129 right_button_pane.setLayout(new GridLayout(1,3));
130 right_button_pane.add(go_button);
131 right_button_pane.add(stop_button);
132 right_button_pane.add(forward_button);
133
134 button_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
135 button_pane.setLayout(new BorderLayout());
136 button_pane.add(left_button_pane, BorderLayout.WEST);
137 button_pane.add(address_pane, BorderLayout.CENTER);
138 button_pane.add(right_button_pane, BorderLayout.EAST);
139
140 setLayout(new BorderLayout());
141 add(button_pane, BorderLayout.NORTH);
142 add(new JScrollPane(view_pane), BorderLayout.CENTER);
143 add(status_label, BorderLayout.SOUTH);
144 }
145 /** 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.
146 */
147 public void afterDisplay() {
148 view_pane.goHome();
149 }
150
151 /** Called when a significant change occurs to the currently loaded collection.
152 * @param ready <i>true</i> if there is a collection ready to be accessed.
153 */
154 public void collectionChanged(boolean ready) {
155 // Reload the bookmarks associated with this collection.
156 if(ready) {
157 bookmarks.load();
158 }
159 else {
160 bookmarks.clear();
161 }
162 }
163
164 /** 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.
165 */
166 public void controlsChanged() {
167 }
168
169 /** Retrieves the current address entered in the address Textfield.
170 * @return A <strong>String</strong> representing the currently entered address.
171 */
172 public String getCurrentURL() {
173 return address_field.getText();
174 }
175
176
177 /** Retrieves a key from the Dictionary, using no extra arguments.
178 * @param key A <strong>String</strong> which maps to a certain phrase from the Dictionary.
179 * @return The <strong>String</strong> that matches the key or an error message if no match was found.
180 */
181 private String get(String key) {
182 return get(key, null);
183 }
184
185 /** Retrieves a key from the Dictionary, providing extra arguments to be inserted using a String array.
186 * @param key A <strong>String</strong> which maps to a certain phrase from the Dictionary.
187 * @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.
188 * @return The <strong>String</strong> that matches the key or an error message if no match was found.
189 */
190 private String get(String key, String args[]) {
191 if(key.indexOf('.') == -1) {
192 key = "Browser." + key;
193 }
194 return Gatherer.dictionary.get(key,args);
195 }
196 /** 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.
197 * @param update_address <i>true</i> if this pollControls call should update the address bar, <i>false</i> otherwise.
198 */
199 private void validateControls() {
200 back_button.setEnabled(back_count > 0);
201 forward_button.setEnabled(forward_count > 0);
202 }
203
204 private class AddressFieldListener
205 implements ActionListener {
206 public void actionPerformed(ActionEvent event) {
207 // Attempt to create a GURL
208 GURL url = new GURL(address_field.getText());
209 address_field.setText(url.getURL().toString());
210 if(url.valid()) {
211 // Now load document
212 view_pane.showHTMLDocument(url.getURL());
213 back_count++;
214 forward_count = 0;
215 }
216 validateControls();
217 }
218 }
219
220 private class BackButtonListener
221 implements ActionListener {
222 public void actionPerformed(ActionEvent event) {
223 view_pane.goBack();
224 back_count--;
225 forward_count++;
226 validateControls();
227 }
228 }
229
230 private class Bookmarks
231 extends JDialog {
232
233 private DefaultListModel data = null;
234
235 public Bookmarks() {
236 data = new DefaultListModel();
237 }
238
239 public void clear() {
240 data.removeAllElements();
241 // Re-add the "Add Bookmark" entry.
242 //add
243 }
244
245 public void load() {
246 // Clear existing
247 data.removeAllElements();
248 // Determine the file to load from...
249 File bookmarks = new File(Gatherer.c_man.getCollectionDirectory() + BOOKMARKS_FILE);
250 if(bookmarks.exists()) {
251 // Read in file line at a time adding to bookmarks.
252 }
253 }
254
255 public int count() {
256 return data.size();
257 }
258
259 private class Entry {
260 public String title = null;
261 public URL url = null;
262 public Entry(String title, String url_raw) {
263 this.title = title;
264 try {
265 url = new URL(url_raw);
266 }
267 catch(Exception error) {
268
269 }
270 }
271 public String toString() {
272 return title;
273 }
274 }
275 }
276
277 private class BookMarksButtonListener
278 implements ActionListener {
279 public void actionPerformed(ActionEvent event) {
280
281 }
282 }
283
284 private class ForwardButtonListener
285 implements ActionListener {
286 public void actionPerformed(ActionEvent event) {
287 view_pane.goForward();
288 back_count++;
289 forward_count--;
290 validateControls();
291 }
292 }
293
294 private class GoButtonListener
295 implements ActionListener {
296 public void actionPerformed(ActionEvent event) {
297 // Attempt to create a GURL
298 GURL url = new GURL(address_field.getText());
299 address_field.setText(url.getURL().toString());
300 if(url.valid()) {
301 // Now load document
302 view_pane.showHTMLDocument(url.getURL());
303 back_count++;
304 forward_count = 0;
305 }
306 validateControls();
307 }
308 }
309
310 private class HomeButtonListener
311 implements ActionListener {
312 public void actionPerformed(ActionEvent event) {
313 address_field.setText(Gatherer.config.getString("general.home_page", false));
314 view_pane.goHome();
315 back_count++;
316 forward_count = 0;
317 validateControls();
318 }
319 }
320
321 private class RefreshButtonListener
322 implements ActionListener {
323 public void actionPerformed(ActionEvent event) {
324 view_pane.reloadDocument();
325 validateControls();
326 }
327 }
328
329 private class StopButtonListener
330 implements ActionListener {
331 public void actionPerformed(ActionEvent event) {
332 view_pane.stopAll();
333 validateControls();
334 }
335 }
336
337 private class Observer
338 extends DefaultCalHTMLObserver {
339 public void linkActivatedUpdate(CalHTMLPane pane, URL url, String target_frame, String j_name) {
340 address_field.setText(url.toString());
341 back_count++;
342 validateControls();
343 }
344 public void linkFocusUpdate(CalHTMLPane pane, URL url) {
345 ///ystem.err.println("Link in focus " + url.toString());
346 args = new String[1];
347 args[0] = url.toString();
348 status_label.setText(get("Follow"));
349 args = null;
350 }
351 public void statusUpdate(CalHTMLPane pane, int state, URL url, int value, String message) {
352 switch(state) {
353 case 11:
354 // Can only stop if somethings happening.
355 stop_button.setEnabled(true);
356 args = new String[1];
357 args[0] = url.toString();
358 status_label.setText(get("Loading", args));
359 args = null;
360 break;
361 case 14:
362 // All done.
363 stop_button.setEnabled(false);
364 // Rendering complete.
365 status_label.setText(get("Ready"));
366 go_button.setEnabled(true);
367 stop_button.setEnabled(false);
368 refresh_button.setEnabled(true);
369 // Update mirror pane
370 Gatherer.g_man.mirror_pane.setURL(url.toString());
371 break;
372 case 74:
373 address_field.setText(url.toString());
374 default:
375 ///ystem.err.println(state + ": " + url + " \"" + message + "\"");
376 }
377 }
378 }
379}
Note: See TracBrowser for help on using the repository browser.