source: trunk/gli/src/org/greenstone/gatherer/gui/DownloadPane.java@ 12064

Last change on this file since 12064 was 11508, checked in by shaoqun, 18 years ago

changed the string for download only html files

  • Property svn:keywords set to Author Date Id Revision
File size: 13.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 java.awt.*;
40import java.awt.event.*;
41import java.io.File;
42import java.net.*;
43import java.util.*;
44import javax.swing.*;
45import javax.swing.border.*;
46import javax.swing.event.*;
47import javax.swing.tree.*;
48import org.greenstone.gatherer.Configuration;
49import org.greenstone.gatherer.Dictionary;
50import org.greenstone.gatherer.Gatherer;
51import org.greenstone.gatherer.WGet;
52import org.greenstone.gatherer.file.WorkspaceTree;
53import org.greenstone.gatherer.util.StaticStrings;
54import org.greenstone.gatherer.util.Utility;
55
56/**
57 * @author John Thompson, Greenstone Digital Library, University of Waikato
58 * @version 2.1
59 */
60public class DownloadPane
61 extends JPanel {
62
63 /** The threshold for when the advanced options get added */
64 static private final int THRESHOLD = Configuration.SYSTEMS_MODE;
65
66 private boolean download_button_enabled = false;
67 private boolean ready = false;
68 private int current_mode;
69 private JButton clear_cache_button;
70 private JButton download_button;
71 private JCheckBox higher_checkbox;
72 private JCheckBox requisite_checkbox;
73 private JCheckBox same_host_checkbox;
74 private JScrollPane list_scroll;
75 private JComboBox depth_combobox;
76 private JPanel options_pane;
77 private JTextField url_field;
78 private Vector depth_model;
79 private WGet getter;
80
81 public DownloadPane() {
82 super();
83 // Create a new wget thread
84 getter = new WGet();
85 getter.start();
86 // And retrieve its list of pending jobs
87 list_scroll = getter.getDownloadJobList();
88
89 current_mode = Configuration.getMode();
90 // String user_dir = System.getProperty("user.dir"); // ****
91 // System.err.println("**** user dir = "+user_dir);
92 // Create
93 JPanel edit_pane = new JPanel();
94 JPanel details_pane = new JPanel();
95 options_pane = new JPanel();
96
97 JPanel url_pane = new JPanel();
98 JLabel url_label = new JLabel();
99 Dictionary.registerText(url_label, "Mirroring.Source_URL");
100 url_field = new JTextField();
101 Dictionary.registerTooltip(url_field, "Mirroring.Source_URL_Tooltip");
102
103 depth_model = new Vector();
104 depth_model.add(new DepthEntry(0, Dictionary.get("Mirroring.Download_Depth.Zero")));
105 /* @todo - add to dictionary */
106 depth_model.add(new DepthEntry(1, String.valueOf(1)));
107 /* @todo - add to dictionary */
108 depth_model.add(new DepthEntry(2, String.valueOf(2)));
109 /* @todo - add to dictionary */
110 depth_model.add(new DepthEntry(3, String.valueOf(3)));
111 /* @todo - add to dictionary */
112 depth_model.add(new DepthEntry(4, String.valueOf(4)));
113 /* @todo - add to dictionary */
114 depth_model.add(new DepthEntry(5, String.valueOf(5)));
115 depth_model.add(new DepthEntry(-1, Dictionary.get("Mirroring.Download_Depth.Unlimited")));
116 JPanel depth_pane = new JPanel();
117 JLabel depth_label = new JLabel();
118 Dictionary.registerText(depth_label, "Mirroring.Download_Depth");
119 depth_combobox = new JComboBox(depth_model);
120 Dictionary.registerTooltip(depth_combobox, "Mirroring.Download_Depth_Tooltip");
121
122 requisite_checkbox = new JCheckBox();
123 Dictionary.registerText(requisite_checkbox, "Mirroring.Download_OnlyHTML");
124
125
126 higher_checkbox = new JCheckBox();
127 Dictionary.registerText(higher_checkbox, "Mirroring.Higher_Directories");
128 higher_checkbox.addActionListener(new CheckboxClickListener());
129
130 same_host_checkbox = new JCheckBox();
131 same_host_checkbox.setSelected(true);
132 Dictionary.registerText(same_host_checkbox, "Mirroring.Same_Host");
133
134 JPanel button_pane = new JPanel();
135
136 JButton preferences_button = new GLIButton();
137 preferences_button.setEnabled(true);
138 preferences_button.setMnemonic(KeyEvent.VK_P);
139 Dictionary.registerBoth(preferences_button, "Mirroring.Preferences", "Mirroring.Preferences_Tooltip");
140
141 clear_cache_button = new GLIButton();
142 clear_cache_button.setEnabled(true);
143 clear_cache_button.setMnemonic(KeyEvent.VK_C);
144 Dictionary.registerBoth(clear_cache_button, "Mirroring.ClearCache", "Mirroring.ClearCache_Tooltip");
145
146 download_button = new GLIButton();
147 download_button.setEnabled(true);
148 download_button.setMnemonic(KeyEvent.VK_D);
149 Dictionary.registerBoth(download_button, "Mirroring.Download", "Mirroring.Download_Tooltip");
150 // Connect
151 clear_cache_button.addActionListener(new ClearCacheListener());
152 download_button.addActionListener(new DownloadButtonListener());
153 preferences_button.addActionListener(new PreferencesButtonActionListener());
154
155 // Layout
156 url_pane.setLayout(new BorderLayout(5,0));
157 url_pane.add(url_label, BorderLayout.WEST);
158 url_pane.add(url_field, BorderLayout.CENTER);
159
160 depth_pane.setLayout(new GridLayout(1,3));
161 depth_pane.add(depth_label);
162 depth_pane.add(new JPanel());
163 depth_pane.add(depth_combobox);
164
165 details_pane.setLayout(new GridLayout(2,1,0,5));
166 details_pane.setBorder(BorderFactory.createEmptyBorder(2,2,5,2));
167 details_pane.add(url_pane);
168 details_pane.add(depth_pane);
169
170 options_pane.setLayout(new GridLayout(3,1,0,5));
171
172 options_pane.add(higher_checkbox);
173 options_pane.add(same_host_checkbox);
174 //if (current_mode >= THRESHOLD) {
175 options_pane.add(requisite_checkbox);
176 //}
177
178 button_pane.setLayout(new GridLayout(3,1));
179 button_pane.add(preferences_button);
180 button_pane.add(clear_cache_button);
181 button_pane.add(download_button);
182
183 edit_pane.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(2,0,0,0), BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder(Dictionary.get("Mirroring.Download_Controls")), BorderFactory.createEmptyBorder(2,2,2,2))));
184 edit_pane.setLayout(new BorderLayout());
185 edit_pane.add(details_pane, BorderLayout.NORTH);
186 edit_pane.add(button_pane, BorderLayout.EAST);
187 edit_pane.add(options_pane, BorderLayout.CENTER);
188
189 setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
190 setLayout(new BorderLayout());
191 add(edit_pane, BorderLayout.NORTH);
192 add(list_scroll, BorderLayout.CENTER);
193 }
194
195 public void afterDisplay() {
196 ready = true;
197 }
198
199
200 /** This method is called whenever the Download pane is brought into focus and is a good time to display a warning message if WGet is not available or of an older, problematic, version.
201 */
202 public void gainFocus() {
203 if(!ready) {
204 return;
205 }
206 // Lets see what warning message we should display, if any.
207 String wget_version_str = Configuration.getWGetVersion();
208 if(wget_version_str.equals(StaticStrings.NO_WGET_STR)) {
209 // If there was no WGet available then downloading is disabled entirely
210 download_button_enabled = false;
211 // And we tell the user why.
212 Gatherer.missingWGET();
213 }
214 else if(wget_version_str.equals(StaticStrings.WGET_OLD_STR)) {
215 // Downloading is enabled
216 download_button_enabled = true;
217 // But we display a preventable warning message about the path problems.
218 Gatherer.oldWGET();
219 }
220 // Otherwise version must be ok
221 else {
222 download_button_enabled = true;
223 }
224 // It is also a good time to determine if the download should be enabled - ie if its allowed to be enabled and a valid URL is present in the field.
225 download_button.setEnabled(download_button_enabled);
226 }
227
228 public void modeChanged(int mode) {
229 int old_mode = current_mode;
230 current_mode = mode;
231 if (old_mode >= THRESHOLD && current_mode >= THRESHOLD) {
232 return;
233 }
234 if (old_mode < THRESHOLD && current_mode < THRESHOLD) {
235 return;
236 }
237
238 //if (current_mode >= THRESHOLD) {
239 // options_pane.add(requisite_checkbox);
240 //}
241 //else {
242 // options_pane.remove(requisite_checkbox);
243 // requisite_checkbox.setSelected(false);
244 //}
245 return;
246 }
247
248
249 public void refresh(int refresh_reason, boolean ready)
250 {
251 }
252
253
254 /** A DepthEntry contains a depth value, as an int, and a representitive text string. This allows for a more meaningful appearance than '0' for instance. */
255 private class DepthEntry
256 implements Comparable {
257 private int value;
258 private String text;
259 /** Default constructor.
260 * @param value the depth value as an int
261 * @param text how this entry show represent itself as a String
262 */
263 public DepthEntry(int value, String text) {
264 this.text = text;
265 this.value = value;
266 }
267 /** Determines the natural ordering of this DepthEntry and some other object
268 * @param object the Object to test for ordering
269 * @return >0 if the object is before this one, 0 if they are equal, or <0 if the object is after this one
270 */
271 public int compareTo(Object object) {
272 // If the object is a DepthEntry then this is easy pesy.
273 if(object instanceof DepthEntry) {
274 int target_value = ((DepthEntry) object).getValue();
275 return value - target_value;
276 }
277 // If the object is a String, then we should try parsing an int from it
278 if(object instanceof String) {
279 try {
280 int target_value = Integer.parseInt((String)object);
281 return value - target_value;
282 }
283 catch(Exception exception) {
284 }
285 }
286 // Otherwise we'll try a string comparison between our text and the toString of the object
287 return text.compareTo(object.toString());
288 }
289
290 /** Determine if this DepthEntry is equal to some other object. Given that there is significant processing involved we push the resposiblility for this onto compareTo, so we only have the complex code once.
291 * @param object the Object to test for equality
292 * @return true if the object is equal to this depth entry, false otherwise
293 */
294 public boolean equals(Object object) {
295 return (compareTo(object) == 0);
296 }
297
298 public int getValue() {
299 return value;
300 }
301
302 public String toString() {
303 return text;
304 }
305 }
306
307 private class ClearCacheListener
308 implements ActionListener {
309 public void actionPerformed(ActionEvent event) {
310 // Retrieve the cache folder and delete it.
311 Utility.delete(Gatherer.getGLIUserCacheDirectoryPath());
312 // ...and refresh the node in the workspace tree to show it's all gone
313 Gatherer.g_man.refreshWorkspaceTree(WorkspaceTree.DOWNLOADED_FILES_CHANGED);
314 }
315 }
316
317 private class CheckboxClickListener
318 implements ActionListener {
319 public void actionPerformed(ActionEvent event) {
320 if (higher_checkbox.isSelected()) {
321 same_host_checkbox.setEnabled(false);
322 } else {
323 same_host_checkbox.setEnabled(true);
324 }
325 }
326 }
327
328 private class DownloadButtonListener
329 implements ActionListener {
330
331 public void actionPerformed(ActionEvent event) {
332 // Retrieve the current url and confirm it is valid
333 String url_str = url_field.getText();
334 URL url = null;
335 try {
336 url = new URL(url_str);
337 }
338 catch(MalformedURLException error) {
339 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("Mirroring.Invalid_URL"), Dictionary.get("Mirroring.Invalid_URL_Title"), JOptionPane.ERROR_MESSAGE);
340 }
341
342 // Otherwise construct a new download job
343 Object depth_object = depth_combobox.getSelectedItem();
344 int depth_value = 0;
345 if(depth_object instanceof DepthEntry) {
346 depth_value = ((DepthEntry)depth_object).getValue();
347 }
348 else if(depth_object instanceof String) {
349 String depth_string = (String) depth_object;
350 try {
351 depth_value = Integer.parseInt(depth_string);
352 // Create a new entry for the purposes of comparison, and if needs be, addition
353 DepthEntry new_entry = new DepthEntry(depth_value, depth_string);
354 // Try to select the correct item from the combobox
355 depth_combobox.setSelectedItem(null);
356 depth_combobox.setSelectedItem(new_entry);
357 // We can see if that worked, and if it didn't add the entry
358 if(depth_combobox.getSelectedItem() == null) {
359 depth_combobox.addItem(new_entry);
360 depth_combobox.setSelectedItem(new_entry);
361 }
362 new_entry = null;
363 }
364 catch(Exception exception) {
365 /* @todo - add to dictionary */
366 JOptionPane.showMessageDialog(Gatherer.g_man, "Mirroring.Mirror_Depth.Invalid_Depth", "Mirroring.Mirror_Depth.Invalid_Depth_Title", JOptionPane.ERROR_MESSAGE);
367 return;
368 }
369 depth_string = null;
370 }
371 depth_object = null;
372
373 String destination_file_path = Gatherer.getGLIUserCacheDirectoryPath();
374 (new File(destination_file_path)).mkdirs(); // If they aren't already
375
376 if(url != null) {
377 getter.newDownloadJob(higher_checkbox.isSelected(), (higher_checkbox.isSelected()? false : !same_host_checkbox.isSelected()), !requisite_checkbox.isSelected(), url, depth_value, destination_file_path);
378 }
379 }
380 }
381
382 private class PreferencesButtonActionListener
383 implements ActionListener {
384 public void actionPerformed(ActionEvent event) {
385 new Preferences(Preferences.CONNECTION_PREFS);
386 }
387 }
388}
Note: See TracBrowser for help on using the repository browser.