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

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

Changed JButtons for GLIButtons, which know whether they should paint their background depending on what platform they are run on, and finished keyboard shortcuts

  • Property svn:keywords set to Author Date Id Revision
File size: 14.1 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * <BR><BR>
9 *
10 * Author: John Thompson, Greenstone Digital Library, University of Waikato
11 *
12 * <BR><BR>
13 *
14 * Copyright (C) 1999 New Zealand Digital Library Project
15 *
16 * <BR><BR>
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * <BR><BR>
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * <BR><BR>
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 *########################################################################
36 */
37package org.greenstone.gatherer.gui;
38
39import calpa.html.*;
40import java.awt.*;
41import java.awt.event.*;
42import java.io.*;
43import java.net.*;
44import javax.swing.*;
45import org.greenstone.gatherer.Dictionary;
46import org.greenstone.gatherer.Gatherer;
47import org.greenstone.gatherer.gui.GLIButton;
48import org.greenstone.gatherer.util.GURL;
49import org.greenstone.gatherer.util.Utility;
50/** This class provides browser-like functionality, with all the standard buttons. The only tricky bit is that the bookmarks are collection specific.
51 * @author John Thompson, Greenstone Digital Library, University of Waikato
52 * @version 2.3
53 */
54public class BrowsingPane
55 extends JPanel {
56 private Bookmarks bookmarks = null;
57 private CalHTMLPane view_pane = null;
58 private CalHTMLPreferences preferences = null;
59 private int back_count = 0;
60 private int forward_count = 0;
61 private JButton back_button = null;
62 private JButton bookmarks_button = null; // Collection bookmarks.
63 private JButton forward_button = null;
64 private JButton go_button = null;
65 private JButton home_button = null;
66 private JButton refresh_button = null;
67 private JButton reload_button = null;
68 private JButton stop_button = null;
69 private JLabel status_label = null;
70 private JTextField address_field = null;
71 private Observer observer = null;
72 private String args[] = null;
73 static final String BOOKMARKS_FILE = "bookmarks.txt";
74
75 public BrowsingPane() {
76 super();
77
78 CalHTMLManager.setCacheDocuments(false);
79
80 // Initialization
81 bookmarks = new Bookmarks(); // Empty until collection loaded.
82 observer = new Observer();
83 preferences = new CalHTMLPreferences();
84 preferences.setHomeURL((new GURL(Gatherer.config.getString("general.home_page", false))).getURL());
85 // Creation
86 // Controls
87 JPanel button_pane = new JPanel();
88 JPanel left_button_pane = new JPanel();
89
90 back_button = new GLIButton(Utility.getImage("back.gif"));
91 back_button.setEnabled(false);
92 back_button.setMnemonic(KeyEvent.VK_BACK_SPACE);
93 Dictionary.registerTooltip(back_button, "Browser.Back_Tooltip");
94
95 // refresh_button = new GLIButton(Utility.getImage("reload.gif"));
96 // refresh_button.setEnabled(false);
97 // refresh_button.setMnemonic(KeyEvent.VK_R);
98 // Dictionary.registerTooltip(refresh_button, "Browser.Reload");
99
100 bookmarks_button = new GLIButton(Utility.getImage("bookmark.gif"));
101 bookmarks_button.setEnabled(false);
102 bookmarks_button.setMnemonic(KeyEvent.VK_M);
103 Dictionary.registerTooltip(bookmarks_button, "Browser.Bookmarks");
104
105 home_button = new GLIButton(Utility.getImage("home.gif"));
106 home_button.setMnemonic(KeyEvent.VK_H);
107 Dictionary.registerTooltip(home_button, "Browser.Home_Tooltip");
108
109 JPanel address_pane = new JPanel();
110 address_field = new JTextField(Gatherer.config.getString("general.home_page", false));
111 Dictionary.registerTooltip(address_field, "Browser.Address_Tooltip");
112
113 JPanel right_button_pane = new JPanel();
114 go_button = new GLIButton(Utility.getImage("go.gif"));
115 go_button.setMnemonic(KeyEvent.VK_ENTER);
116 Dictionary.registerTooltip(go_button, "Browser.Go_Tooltip");
117
118 stop_button = new GLIButton(Utility.getImage("stop.gif"));
119 stop_button.setEnabled(false);
120 stop_button.setMnemonic(KeyEvent.VK_S);
121 Dictionary.registerTooltip(stop_button, "Browser.Stop_Tooltip");
122
123 forward_button = new GLIButton(Utility.getImage("forward.gif"));
124 forward_button.setEnabled(false);
125 forward_button.setMnemonic(KeyEvent.VK_F);
126 Dictionary.registerTooltip(forward_button, "Browser.Forward_Tooltip");
127
128 // Calpa Pane stuff
129 view_pane = new CalHTMLPane(preferences, observer, "VIEW_PANE");
130
131 // Status bar
132 status_label = new JLabel();
133 Dictionary.registerText(status_label, "Browser.Ready");
134
135 // Connection
136 address_field.addActionListener(new AddressFieldListener());
137 back_button.addActionListener(new BackButtonListener());
138 bookmarks_button.addActionListener(new BookMarksButtonListener());
139 forward_button.addActionListener(new ForwardButtonListener());
140 go_button.addActionListener(new GoButtonListener());
141 home_button.addActionListener(new HomeButtonListener());
142 // refresh_button.addActionListener(new RefreshButtonListener());
143 stop_button.addActionListener(new StopButtonListener());
144
145 // Layout
146 left_button_pane.setLayout(new GridLayout(1,3));
147 left_button_pane.add(back_button);
148 left_button_pane.add(bookmarks_button);
149 left_button_pane.add(home_button);
150
151 address_pane.setBorder(BorderFactory.createEmptyBorder(10,5,10,5));
152 address_pane.setLayout(new BorderLayout());
153 address_pane.add(address_field, BorderLayout.CENTER);
154
155 right_button_pane.setLayout(new GridLayout(1,3));
156 right_button_pane.add(go_button);
157 right_button_pane.add(stop_button);
158 right_button_pane.add(forward_button);
159
160 button_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
161 button_pane.setLayout(new BorderLayout());
162 button_pane.add(left_button_pane, BorderLayout.WEST);
163 button_pane.add(address_pane, BorderLayout.CENTER);
164 button_pane.add(right_button_pane, BorderLayout.EAST);
165
166 setLayout(new BorderLayout());
167 add(button_pane, BorderLayout.NORTH);
168 add(new JScrollPane(view_pane), BorderLayout.CENTER);
169 add(status_label, BorderLayout.SOUTH);
170 }
171
172 /** 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.
173 */
174 public void afterDisplay() {
175 view_pane.goHome();
176 }
177
178 /** Called when a significant change occurs to the currently loaded collection.
179 * @param ready <i>true</i> if there is a collection ready to be accessed.
180 */
181 public void collectionChanged(boolean ready) {
182 // Reload the bookmarks associated with this collection.
183 if (ready) {
184 bookmarks.load();
185 }
186 else {
187 bookmarks.clear();
188 }
189 }
190
191 /** 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.
192 */
193 public void controlsChanged() {
194 }
195
196 /** Retrieves the current address entered in the address Textfield.
197 * @return A <strong>String</strong> representing the currently entered address.
198 */
199 public String getCurrentURL() {
200 return address_field.getText();
201 }
202
203
204 /** 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.
205 * @param update_address <i>true</i> if this pollControls call should update the address bar, <i>false</i> otherwise.
206 */
207 private void validateControls() {
208 back_button.setEnabled(back_count > 0);
209 forward_button.setEnabled(forward_count > 0);
210 }
211
212 private class AddressFieldListener
213 implements ActionListener {
214 public void actionPerformed(ActionEvent event) {
215 // Attempt to create a GURL
216 GURL url = new GURL(address_field.getText());
217 address_field.setText(url.getURL().toString());
218 if(url.valid()) {
219 // Now load document
220 view_pane.showHTMLDocument(url.getURL());
221 back_count++;
222 forward_count = 0;
223 }
224 validateControls();
225 }
226 }
227
228 private class BackButtonListener
229 implements ActionListener {
230 public void actionPerformed(ActionEvent event) {
231 view_pane.goBack();
232 back_count--;
233 forward_count++;
234 validateControls();
235 }
236 }
237
238 private class Bookmarks
239 extends JDialog {
240
241 private DefaultListModel data = null;
242
243 public Bookmarks() {
244 data = new DefaultListModel();
245 }
246
247 public void clear() {
248 data.removeAllElements();
249 // Re-add the "Add Bookmark" entry.
250 //add
251 }
252
253 public void load() {
254 // Clear existing
255 data.removeAllElements();
256 // Determine the file to load from...
257 File bookmarks = new File(Gatherer.c_man.getCollectionDirectory() + BOOKMARKS_FILE);
258 if(bookmarks.exists()) {
259 // Read in file line at a time adding to bookmarks.
260 }
261 }
262
263 public int count() {
264 return data.size();
265 }
266
267 private class Entry {
268 public String title = null;
269 public URL url = null;
270 public Entry(String title, String url_raw) {
271 this.title = title;
272 try {
273 url = new URL(url_raw);
274 }
275 catch(Exception error) {
276
277 }
278 }
279 public String toString() {
280 return title;
281 }
282 }
283 }
284
285 private class BookMarksButtonListener
286 implements ActionListener {
287 public void actionPerformed(ActionEvent event) {
288
289 }
290 }
291
292 private class ForwardButtonListener
293 implements ActionListener {
294 public void actionPerformed(ActionEvent event) {
295 view_pane.goForward();
296 back_count++;
297 forward_count--;
298 validateControls();
299 }
300 }
301
302 private class GoButtonListener
303 implements ActionListener {
304 public void actionPerformed(ActionEvent event) {
305 // Attempt to create a GURL
306 GURL url = new GURL(address_field.getText());
307 address_field.setText(url.getURL().toString());
308 if(url.valid()) {
309 // Now load document
310 view_pane.showHTMLDocument(url.getURL());
311 back_count++;
312 forward_count = 0;
313 }
314 validateControls();
315 }
316 }
317
318 private class HomeButtonListener
319 implements ActionListener {
320 public void actionPerformed(ActionEvent event) {
321 address_field.setText(Gatherer.config.getString("general.home_page", false));
322 view_pane.goHome();
323 back_count++;
324 forward_count = 0;
325 validateControls();
326 }
327 }
328
329 private class RefreshButtonListener
330 implements ActionListener {
331 public void actionPerformed(ActionEvent event) {
332 view_pane.reloadDocument();
333 validateControls();
334 }
335 }
336
337 private class StopButtonListener
338 implements ActionListener {
339 public void actionPerformed(ActionEvent event) {
340 view_pane.stopAll();
341 validateControls();
342 }
343 }
344
345 private class Observer
346 extends DefaultCalHTMLObserver {
347
348 /** Notification that a form submission has been initiated. */
349 public void formSubmitUpdate(CalHTMLPane pane, URL docBaseURL, int method, String action, String data) {
350 ///ystem.err.println("Form submitted:" + docBaseURL.toString());
351 ///ystem.err.println("Data: " + data);
352 }
353
354
355 public void linkActivatedUpdate(CalHTMLPane pane, URL url, String target_frame, String j_name) {
356 ///ystem.err.println("Link clicked: " + url.toString());
357 address_field.setText(url.toString());
358 back_count++;
359 validateControls();
360 }
361
362 public void linkFocusUpdate(CalHTMLPane pane, URL url) {
363 ///ystem.err.println("Link in focus " + url.toString());
364 args = new String[1];
365 args[0] = url.toString();
366 Dictionary.registerText(status_label, "Browser.Follow");
367 args = null;
368 }
369 public void statusUpdate(CalHTMLPane pane, int state, URL url, int value, String message) {
370 switch(state) {
371 // The Pane is attempting to connect to the given URL to receive data.
372 case CalCons.PRE_CONNECT:
373 ///ystem.err.println("Preconnect: " + url.toString());
374 break;
375 // The Pane was unable to connect to the given URL or was unable to parse the content. Most likely this will be due to an incorrectly specified URL. The message argument may contain further details of the reason for failure.
376 case CalCons.PARSE_FAILED:
377 ///ystem.err.println("Parse Failed: " + url.toString());
378 ///ystem.err.println("Message: " + message);
379 break;
380 // The Pane has established a connection to the given URL and is receiving any content.
381 case CalCons.CONNECTED:
382 // Can only stop if somethings happening.
383 stop_button.setEnabled(true);
384 args = new String[1];
385 args[0] = url.toString();
386 Dictionary.registerText(status_label, "Browser.Loading", args);
387 args = null;
388 break;
389 // The size of the content at the given URL is known and is contained in the value argument.
390 case CalCons.DOC_LENGTH:
391 break;
392 // The title of the document for the given URL is known and is contained in the message argument. Only the title of a document in the Pane's top level frame will be sent to this method. If the document has no name, the message will be null, unless the document is a Frameset document in which case the message "Frameset" will be sent.
393 case CalCons.TITLE:
394 break;
395 // An exception has been thrown during parsing of the data from the URL. This will most likely be an IOException such as a server time-out.
396 case CalCons.PARSE_FAILED_POST_CONNECT:
397 ///ystem.err.println("Parse Failed Post Connect: " + url.toString());
398 break;
399 // The document has been parsed but formatting cannot be completed because the document contains images of unspecified size. The parsing thread is waiting for image updates to give it the information it needs to format and display the document.
400 case CalCons.WAITING_FOR_IMAGES:
401 break;
402 // All text and image data has been received, parsed and the document structure determined.
403 case CalCons.DOC_LOADED:
404 // All done.
405 stop_button.setEnabled(false);
406 // Rendering complete.
407 Dictionary.registerText(status_label, "Browser.Ready");
408 go_button.setEnabled(true);
409 stop_button.setEnabled(false);
410 // refresh_button.setEnabled(true);
411 // Update mirror pane
412 Gatherer.g_man.mirror_pane.setURL(url.toString());
413 break;
414 }
415 }
416 }
417}
Note: See TracBrowser for help on using the repository browser.