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

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

2030084: Fixed problem where preview pane was incorrectly attempting to reload the displayed page during GLI exit.

  • Property svn:keywords set to Author Date Id Revision
File size: 9.0 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 URL homepage = null;
60
61 static final public String BLANK_PAGE = "<html><head><title>No Page</title></head><body></body></html>";
62
63 public PreviewPane() {
64 // Create components
65 back = new JButton(get("Back"), Utility.getImage("back.gif"));
66 back.addActionListener(new BackListener());
67 back.setEnabled(false);
68
69 home = new JButton(get("Home"), Utility.getImage("home.gif"));
70 home.addActionListener(new HomeListener());
71 home.setEnabled(false);
72
73 forward = new JButton(get("Forward"), Utility.getImage("forward.gif"));
74 forward.addActionListener(new ForwardListener());
75 forward.setEnabled(false);
76
77 reload = new JButton(get("Reload"), Utility.getImage("reload.gif"));
78 reload.addActionListener(new ReloadListener());
79 reload.setEnabled(false);
80
81 CalHTMLPreferences prefs = new CalHTMLPreferences();
82 observer = new Observer();
83 view = new CalHTMLPane(prefs, observer, "Default");
84
85 status = new JLabel(get("Ready"));
86 }
87
88 public void collectionChanged(boolean ready) {
89 // If we were showing a page, but that page is no longer available, then return to a blank page
90 if(!ready && homepage != null) {
91 homepage = null;
92 view.showHTMLDocument(BLANK_PAGE);
93 }
94 }
95
96 public void display() {
97 JPanel control_pane = new JPanel();
98 control_pane.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
99 control_pane.setLayout(new GridLayout(1,4));
100 control_pane.add(back);
101 control_pane.add(reload);
102 control_pane.add(home);
103 control_pane.add(forward);
104
105 JPanel a_panel = new JPanel(new BorderLayout());
106 a_panel.setSize(new Dimension(770, 440));
107 a_panel.add(view, BorderLayout.CENTER);
108
109 setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
110 setLayout(new BorderLayout());
111 add(control_pane, BorderLayout.NORTH);
112 add(a_panel, BorderLayout.CENTER);
113 add(status, BorderLayout.SOUTH);
114 }
115
116 /** 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?)
117 */
118 public void gainFocus() {
119 if(Gatherer.c_man.ready() && Gatherer.c_man.built()) {
120 if(homepage == null) {
121 try {
122 // Now load the collection
123 homepage = new URL(Gatherer.config.exec_address.toString() + "?a=p&p=about&c=" + Gatherer.c_man.getCollection().getName());
124 ///atherer.println("Loading " + url);
125 status.setText(get("Loading", homepage.toString()));
126 view.showHTMLDocument(homepage);
127 }
128 catch (MalformedURLException exception) {
129 Gatherer.printStackTrace(exception);
130 }
131 }
132 }
133 validate();
134 }
135
136 /** Used to retrieve the phrase that matches the given key from the loaded dictionary.
137 */
138 public String get(String key) {
139 return get(key, null);
140 }
141
142 public String get(String key, String arg) {
143 if(key.indexOf(".") == -1) {
144 key = "Browser." + key;
145 }
146 return Gatherer.dictionary.get(key, arg);
147 }
148
149 public void configServer(String command) {
150 try {
151 String url = Gatherer.config.exec_address.toString() + command;
152 ///ystem.err.println("Action: " + url);
153 view.setLoadSynchronously(true);
154 view.showHTMLDocument(new URL(url));
155 view.setLoadSynchronously(false);
156 ///ystem.err.println("Complete.");
157 url = null;
158 }
159 catch(MalformedURLException error) {
160 ///ystem.err.println("Bad URL.");
161 }
162 }
163
164 public void validate() {
165 back.setEnabled(back_count > 0);
166 home.setEnabled(homepage != null);
167 forward.setEnabled(forward_count > 0);
168 reload.setEnabled(homepage != null);
169 }
170
171 private class BackListener
172 implements ActionListener {
173 public void actionPerformed(ActionEvent event) {
174 view.goBack();
175 back_count--;
176 forward_count++;
177 validate();
178 }
179 }
180
181 private class ForwardListener
182 implements ActionListener {
183 public void actionPerformed(ActionEvent event) {
184 view.goForward();
185 back_count++;
186 forward_count--;
187 validate();
188 }
189 }
190
191 private class HomeListener
192 implements ActionListener {
193 public void actionPerformed(ActionEvent event) {
194 view.showHTMLDocument(homepage);
195 back_count++;
196 forward_count = 0;
197 validate();
198 }
199 }
200
201 private class ReloadListener
202 implements ActionListener {
203 public void actionPerformed(ActionEvent event) {
204 view.reloadDocument();
205 }
206 }
207
208 private class Observer
209 extends DefaultCalHTMLObserver {
210 public int state = CalCons.DOC_LOADED;
211 public void linkActivatedUpdate(CalHTMLPane pane, URL url, String target_frame, String j_name) {
212 back_count++;
213 forward_count = 0;
214 validate();
215 }
216 public void linkFocusUpdate(CalHTMLPane pane, URL url) {
217 ///ystem.err.println("Link in focus " + url.toString());
218 status.setText(get("Follow", url.toString()));
219 }
220 public void statusUpdate(CalHTMLPane pane, int state, URL url, int value, String message) {
221 this.state = state;
222 switch(state) {
223 // The Pane is attempting to connect to the given URL to receive data.
224 case CalCons.PRE_CONNECT:
225 ///ystem.err.println("Preconnect: " + state + " - " + message);
226 break;
227 // 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.
228 case CalCons.PARSE_FAILED:
229 ///ystem.err.println("Parse Failed: " + state + " - " + message);
230 status.setText(get("CannotConnect", url.toString()));
231 break;
232 // The Pane has established a connection to the given URL and is receiving any content.
233 case CalCons.CONNECTED:
234 ///ystem.err.println("Connected: " + state + " - " + message);
235 status.setText(get("Loading", url.toString()));
236 break;
237 // The size of the content at the given URL is known and is contained in the value argument.
238 case CalCons.DOC_LENGTH:
239 ///ystem.err.println("Doc Length: " + state + " - " + message);
240 break;
241 // 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.
242 case CalCons.TITLE:
243 ///ystem.err.println("Title: " + state + " - " + message);
244 break;
245 // 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.
246 case CalCons.PARSE_FAILED_POST_CONNECT:
247 ///ystem.err.println("Parse Failed Post Connect: " + state + " - " + message);
248 status.setText(get("TimedOut", url.toString()));
249 break;
250 // 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.
251 case CalCons.WAITING_FOR_IMAGES:
252 ///ystem.err.println("Waiting For Images: " + state + " - " + message + " - " + url.toString());
253 break;
254 // All text and image data has been received, parsed and the document structure determined.
255 case CalCons.DOC_LOADED:
256 ///ystem.err.println("Doc Loaded: " + state + " - " + message);
257 status.setText(get("Ready"));
258 break;
259 }
260 }
261 }
262}
Note: See TracBrowser for help on using the repository browser.