source: trunk/gli/src/org/greenstone/gatherer/gui/PreviewPane.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: 10.4 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.net.*;
43import javax.swing.*;
44import org.greenstone.gatherer.Dictionary;
45import org.greenstone.gatherer.collection.Collection;
46import org.greenstone.gatherer.Gatherer;
47import org.greenstone.gatherer.gui.GLIButton;
48import org.greenstone.gatherer.util.GURL;
49import org.greenstone.gatherer.util.Utility;
50
51public class PreviewPane
52 extends JPanel {
53
54 private CalHTMLPane view = null;
55 private int back_count = 0;
56 private int forward_count = 0;
57 private JButton back = null;
58 private JButton forward = null;
59 private JButton home = null;
60 private JButton reload = null;
61 private JLabel status = null;
62 private Observer observer = null;
63 private URL homepage = null;
64
65 static final public String BLANK_PAGE = "<html><head><title>No Page</title></head><body></body></html>";
66
67 public PreviewPane() {
68 super();
69
70 CalHTMLManager.setCacheDocuments(false);
71
72 // Create components
73 back = new GLIButton(Utility.getImage("back.gif"));
74 back.addActionListener(new BackListener());
75 back.setEnabled(false);
76 back.setMnemonic(KeyEvent.VK_BACK_SPACE);
77 Dictionary.registerBoth(back, "Browser.Back", "Browser.Back_Tooltip");
78
79 home = new GLIButton(Utility.getImage("home.gif"));
80 home.addActionListener(new HomeListener());
81 home.setEnabled(false);
82 home.setMnemonic(KeyEvent.VK_H);
83 Dictionary.registerBoth(home, "Browser.Home", "Browser.Home_Tooltip");
84
85 forward = new GLIButton(Utility.getImage("forward.gif"));
86 forward.addActionListener(new ForwardListener());
87 forward.setEnabled(false);
88 forward.setMnemonic(KeyEvent.VK_F);
89 Dictionary.registerBoth(forward, "Browser.Forward", "Browser.Forward_Tooltip");
90
91 reload = new GLIButton(Utility.getImage("reload.gif"));
92 reload.addActionListener(new ReloadListener());
93 reload.setEnabled(false);
94 reload.setMnemonic(KeyEvent.VK_R);
95 Dictionary.registerBoth(reload, "Browser.Reload", "Browser.Reload_Tooltip");
96
97 CalHTMLPreferences prefs = new CalHTMLPreferences();
98 observer = new Observer();
99 view = new CalHTMLPane(prefs, observer, "Default");
100
101 status = new JLabel();
102 Dictionary.registerText(status, "Browser.Ready");
103 }
104
105 public void collectionChanged(boolean ready) {
106 // If we were showing a page, but that page is no longer available, then return to a blank page
107 if(!ready && homepage != null) {
108 homepage = null;
109 view.showHTMLDocument(BLANK_PAGE);
110 }
111 if (ready && Gatherer.config.exec_address != null) {
112 // reload the home page for the current collection
113 try {
114 // Now load the collection
115 Collection this_collection = Gatherer.c_man.getCollection();
116 String search_types = this_collection.cdm.searchtype_manager.getSearchTypes();
117 String extra_args = "";
118 if (search_types.length() > 0) {
119 // we need some more args on the url
120 if (search_types.equals("")) {
121 extra_args = "&ct=1&qt=0&qto=3";
122 } else if (search_types.equals("plain")) {
123 extra_args = "&ct=1&qt=0&qto=1";
124 } else if (search_types.equals("form")) {
125 extra_args = "&ct=1&qt=1&qto=2";
126 } else if (search_types.equals("plain,form")) {
127 extra_args = "&ct=1&qt=0&qto=3";
128 } else if (search_types.equals("form,plain")) {
129 extra_args = "&ct=1&qt=1&qto=3";
130 }
131 }
132
133 homepage = new URL(Gatherer.config.exec_address.toString() + "?a=p&p=about&c=" + this_collection.getName() + extra_args);
134 String[] args = new String[1];
135 args[0] = homepage.toString();
136 Dictionary.registerText(status, "Browser.Loading", args);
137 view.showHTMLDocument(homepage, null, true);
138 }
139 catch (MalformedURLException exception) {
140 Gatherer.printStackTrace(exception);
141 }
142 }
143 }
144
145 public void display() {
146 JPanel control_pane = new JPanel();
147 control_pane.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
148 control_pane.setLayout(new GridLayout(1,4));
149 control_pane.add(back);
150 control_pane.add(reload);
151 control_pane.add(home);
152 control_pane.add(forward);
153
154 JPanel a_panel = new JPanel(new BorderLayout());
155 a_panel.setSize(new Dimension(770, 440));
156 a_panel.add(view, BorderLayout.CENTER);
157
158 setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
159 setLayout(new BorderLayout());
160 add(control_pane, BorderLayout.NORTH);
161 add(a_panel, BorderLayout.CENTER);
162 add(status, BorderLayout.SOUTH);
163 }
164
165 /** 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?)
166 */
167 public void gainFocus() {
168 if(Gatherer.c_man.ready() && Gatherer.c_man.built()) {
169 if(homepage == null && Gatherer.config.exec_address != null) {
170 try {
171 // Now load the collection
172 homepage = new URL(Gatherer.config.exec_address.toString() + "?a=p&p=about&c=" + Gatherer.c_man.getCollection().getName());
173 String[] args = new String[1];
174 args[0] = homepage.toString();
175 Dictionary.registerText(status, "Browser.Loading", args);
176 view.showHTMLDocument(homepage);
177 }
178 catch (MalformedURLException exception) {
179 Gatherer.printStackTrace(exception);
180 }
181 }
182 }
183 validate();
184 }
185
186 public void configServer(String command) {
187 try {
188 String url = Gatherer.config.exec_address.toString() + command;
189 ///ystem.err.println("Action: " + url);
190 view.setLoadSynchronously(true);
191 view.showHTMLDocument(new URL(url));
192 view.setLoadSynchronously(false);
193 ///ystem.err.println("Complete.");
194 url = null;
195 }
196 catch(Exception error) {
197 ///ystem.err.println("Bad URL.");
198 }
199 }
200
201 public void validate() {
202 back.setEnabled(back_count > 0);
203 home.setEnabled(homepage != null);
204 forward.setEnabled(forward_count > 0);
205 reload.setEnabled(homepage != null);
206 }
207
208 private class BackListener
209 implements ActionListener {
210 public void actionPerformed(ActionEvent event) {
211 view.goBack();
212 back_count--;
213 forward_count++;
214 validate();
215 }
216 }
217
218 private class ForwardListener
219 implements ActionListener {
220 public void actionPerformed(ActionEvent event) {
221 view.goForward();
222 back_count++;
223 forward_count--;
224 validate();
225 }
226 }
227
228 private class HomeListener
229 implements ActionListener {
230 public void actionPerformed(ActionEvent event) {
231 view.showHTMLDocument(homepage);
232 back_count++;
233 forward_count = 0;
234 validate();
235 }
236 }
237
238 private class ReloadListener
239 implements ActionListener {
240 public void actionPerformed(ActionEvent event) {
241 view.reloadDocument();
242 }
243 }
244
245 private class Observer
246 extends DefaultCalHTMLObserver {
247 public int state = CalCons.DOC_LOADED;
248 public void linkActivatedUpdate(CalHTMLPane pane, URL url, String target_frame, String j_name) {
249 back_count++;
250 forward_count = 0;
251 validate();
252 }
253
254 public void linkFocusUpdate(CalHTMLPane pane, URL url) {
255 String[] args = new String[1];
256 args[0] = url.toString();
257 Dictionary.registerText(status, "Browser.Follow", args);
258 }
259
260 public void statusUpdate(CalHTMLPane pane, int state, URL url, int value, String message) {
261 this.state = state;
262 String[] args = new String[1];
263 args[0] = url.toString();
264
265 switch(state) {
266 // The Pane is attempting to connect to the given URL to receive data.
267 case CalCons.PRE_CONNECT:
268 ///ystem.err.println("Preconnect: " + state + " - " + message);
269 break;
270 // 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.
271 case CalCons.PARSE_FAILED:
272 ///ystem.err.println("Parse Failed: " + state + " - " + message);
273 Dictionary.registerText(status, "Browser.CannotConnect", args);
274 break;
275 // The Pane has established a connection to the given URL and is receiving any content.
276 case CalCons.CONNECTED:
277 ///ystem.err.println("Connected: " + state + " - " + message);
278 Dictionary.registerText(status, "Browser.Loading", args);
279 break;
280 // The size of the content at the given URL is known and is contained in the value argument.
281 case CalCons.DOC_LENGTH:
282 ///ystem.err.println("Doc Length: " + state + " - " + message);
283 break;
284 // 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.
285 case CalCons.TITLE:
286 ///ystem.err.println("Title: " + state + " - " + message);
287 break;
288 // 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.
289 case CalCons.PARSE_FAILED_POST_CONNECT:
290 ///ystem.err.println("Parse Failed Post Connect: " + state + " - " + message);
291 Dictionary.registerText(status, "Browser.TimedOut", args);
292 break;
293 // 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.
294 case CalCons.WAITING_FOR_IMAGES:
295 ///ystem.err.println("Waiting For Images: " + state + " - " + message + " - " + url.toString());
296 break;
297 // All text and image data has been received, parsed and the document structure determined.
298 case CalCons.DOC_LOADED:
299 ///ystem.err.println("Doc Loaded: " + state + " - " + message);
300 Dictionary.registerText(status, "Browser.Ready");
301 break;
302 }
303 }
304 }
305}
Note: See TracBrowser for help on using the repository browser.