source: trunk/gli/src/org/greenstone/gatherer/gui/PreviewPane.java@ 4311

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

modified for new local library stuff

  • Property svn:keywords set to Author Date Id Revision
File size: 10.5 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.net.*;
42import javax.swing.*;
43import org.greenstone.gatherer.Gatherer;
44import org.greenstone.gatherer.util.GURL;
45import org.greenstone.gatherer.util.Utility;
46
47public class PreviewPane
48 extends JPanel {
49
50 private CalHTMLPane view = null;
51 private int back_count = 0;
52 private int forward_count = 0;
53 private JButton back = null;
54 private JButton forward = null;
55 private JButton home = null;
56 private JButton reload = null;
57 private JLabel status = null;
58 private Observer observer = null;
59 private String args[] = null;
60 private String current_url = null;
61 private String page_showing = null;
62 private URL homepage = null;
63
64 public PreviewPane() {
65 // Create components
66 back = new JButton(get("Back"), Utility.getImage("back.gif"));
67 back.addActionListener(new BackListener());
68 back.setEnabled(false);
69
70 home = new JButton(get("Home"), Utility.getImage("home.gif"));
71 home.addActionListener(new HomeListener());
72 home.setEnabled(false);
73
74 forward = new JButton(get("Forward"), Utility.getImage("forward.gif"));
75 forward.addActionListener(new ForwardListener());
76 forward.setEnabled(false);
77
78 reload = new JButton(get("Reload"), Utility.getImage("reload.gif"));
79 reload.addActionListener(new ReloadListener());
80 reload.setEnabled(false);
81
82 CalHTMLPreferences prefs = new CalHTMLPreferences();
83 observer = new Observer();
84 view = new CalHTMLPane(prefs, observer, "Default");
85 //view.setSize(new Dimension(770, 440));
86
87 status = new JLabel(get("Ready"));
88 }
89
90 public void collectionChanged(boolean ready) {
91 /**
92 if(ready && Gatherer.c_man.built()) {
93 // If no address is set yet then its time to load the local library.
94
95 // Ask library to rebuild if necessary.
96 String url = Gatherer.config.exec_address + "?a=config&cmd=add-collection&c=" + Gatherer.c_man.getCollection().getName();
97 Gatherer.println("Reset library: " + url);
98 observer.state = CalCons.PRE_CONNECT;
99 view.showHTMLDocument((new GURL(url)).getURL());
100 // Display page.
101 url = Gatherer.config.exec_path + "?a=p&p=about&c=" + Gatherer.c_man.getCollection().getName();
102 Gatherer.println("Loading " + url);
103 args = new String[1];
104 args[0] = url;
105 status.setText(get("Loading", args));
106 args = null;
107 view.showHTMLDocument((new GURL(url)).getURL());
108 }
109 validate();
110 */
111 }
112
113 public void display() {
114 JPanel control_pane = new JPanel();
115 control_pane.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
116 control_pane.setLayout(new GridLayout(1,4));
117 control_pane.add(back);
118 control_pane.add(reload);
119 control_pane.add(home);
120 control_pane.add(forward);
121
122 JPanel a_panel = new JPanel(new BorderLayout());
123 a_panel.setSize(new Dimension(770, 440));
124 a_panel.add(view, BorderLayout.CENTER);
125
126 setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
127 setLayout(new BorderLayout());
128 add(control_pane, BorderLayout.NORTH);
129 add(a_panel, BorderLayout.CENTER);
130 add(status, BorderLayout.SOUTH);
131 }
132
133 /** This method is called when the user selects the 'Preview' tab from the views bar. Can be used to display a particular page or perform some other relevant tests (ensure internet connection to specified server maybe?)
134 */
135 public void gainFocus() {
136 if(Gatherer.c_man.ready() && Gatherer.c_man.built()) {
137 String url = Gatherer.config.exec_path + "?a=p&p=about&c=" + Gatherer.c_man.getCollection().getName();
138 if(page_showing != null && page_showing.equals(url)) {
139 view.reloadDocument();
140 }
141 else {
142 String url = Gatherer.config.exec_address + "?a=config&cmd=add-collection&c=" + Gatherer.c_man.getCollection().getName();
143
144 Gatherer.println("Loading " + url);
145 args = new String[1];
146 args[0] = url;
147 status.setText(get("Loading", args));
148 args = null;
149 view.showHTMLDocument((new GURL(url)).getURL());
150 page_showing = url;
151 homepage = (new GURL(url)).getURL();
152 }
153 }
154 validate();
155 }
156
157 /** Used to retrieve the phrase that matches the given key from the loaded dictionary.
158 */
159 public String get(String key) {
160 return get(key, null);
161 }
162
163 public String get(String key, String args[]) {
164 if(key.indexOf(".") == -1) {
165 key = "Browser." + key;
166 }
167 return Gatherer.dictionary.get(key, args);
168 }
169
170 public void validate() {
171 if(back_count > 0) {
172 back.setEnabled(true);
173 }
174 else {
175 back.setEnabled(false);
176 }
177 if(homepage != null) {
178 home.setEnabled(true);
179 }
180 else {
181 home.setEnabled(false);
182 }
183 if(forward_count > 0) {
184 forward.setEnabled(true);
185 }
186 else {
187 forward.setEnabled(false);
188 }
189 reload.setEnabled(true);
190 }
191
192 /** This method performs nominally the same as a showHTMLDocument call expect that it blocks until some final loading state is reached, good or bad. */
193 private void showAndWait(URL url) {
194 try {
195 // Send url to CalPane
196
197 // Wait until state is preconnect
198 while(observer.state != CalCons.PRE_CONNECT) {
199 wait(100); // 10th of a second
200 }
201 // Now wait until state is final state
202 while(observer.state != CalCons.DOC_LOADED) {
203 wait(100); // 10th of a second
204 }
205 }
206 catch (InterruptedException error) {
207 }
208 }
209
210 private class BackListener
211 implements ActionListener {
212 public void actionPerformed(ActionEvent event) {
213 view.goBack();
214 back_count--;
215 forward_count++;
216 validate();
217 }
218 }
219
220 private class ForwardListener
221 implements ActionListener {
222 public void actionPerformed(ActionEvent event) {
223 view.goForward();
224 back_count++;
225 forward_count--;
226 validate();
227 }
228 }
229
230 private class HomeListener
231 implements ActionListener {
232 public void actionPerformed(ActionEvent event) {
233 if(homepage != null) {
234 view.showHTMLDocument(homepage);
235 back_count++;
236 validate();
237 }
238 }
239 }
240
241 private class ReloadListener
242 implements ActionListener {
243 public void actionPerformed(ActionEvent event) {
244 view.reloadDocument();
245 }
246 }
247
248 private class Observer
249 extends DefaultCalHTMLObserver {
250 public int state = CalCons.DOC_LOADED;
251 public void linkActivatedUpdate(CalHTMLPane pane, URL url, String target_frame, String j_name) {
252 if(!url.toString().equals(current_url)) {
253 back_count++;
254 forward_count = 0;
255 validate();
256 }
257 }
258 public void linkFocusUpdate(CalHTMLPane pane, URL url) {
259 ///ystem.err.println("Link in focus " + url.toString());
260 args = new String[1];
261 args[0] = url.toString();
262 status.setText(get("Follow"));
263 args = null;
264 }
265 public void statusUpdate(CalHTMLPane pane, int state, URL url, int value, String message) {
266 this.state = state;
267 switch(state) {
268 // The Pane is attempting to connect to the given URL to receive data.
269 case CalCons.PRE_CONNECT:
270 System.err.println("Preconnect: " + value + " - " + message);
271 break;
272 // 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.
273 case CalCons.PARSE_FAILED:
274 System.err.println("Parse Failed: " + value + " - " + message);
275 status.setText(get("CannotConnect") + message);
276 break;
277 // The Pane has established a connection to the given URL and is receiving any content.
278 case CalCons.CONNECTED:
279 System.err.println("Connected: " + value + " - " + message);
280 args = new String[1];
281 args[0] = url.toString();
282 status.setText(get("Loading", args));
283 args = null;
284 break;
285 // The size of the content at the given URL is known and is contained in the value argument.
286 case CalCons.DOC_LENGTH:
287 System.err.println("Doc Length: " + value + " - " + message);
288 break;
289 // 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.
290 case CalCons.TITLE:
291 System.err.println("Title: " + value + " - " + message);
292 break;
293 // 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.
294 case CalCons.PARSE_FAILED_POST_CONNECT:
295 System.err.println("Parse Failed Post Connect: " + value + " - " + message);
296 status.setText(get("TimedOut") + message);
297 break;
298 // 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.
299 case CalCons.WAITING_FOR_IMAGES:
300 System.err.println("Waiting For Images: " + value + " - " + message);
301 break;
302 // All text and image data has been received, parsed and the document structure determined.
303 case CalCons.DOC_LOADED:
304 System.err.println("Doc Loaded: " + value + " - " + message);
305 status.setText(get("Ready"));
306 break;
307 }
308 }
309 }
310}
Note: See TracBrowser for help on using the repository browser.