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

Last change on this file since 5350 was 5347, checked in by mdewsnip, 21 years ago

Changed dictionary get()s to have the whole key.

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