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

Last change on this file since 12142 was 12119, checked in by kjdon, 18 years ago

Changed text handling to use Dictionary.get rather than Dictionary.setText or Dictionary.registerBoth etc. also removed mnemonics cos they suck for other languages

  • Property svn:keywords set to Author Date Id Revision
File size: 13.1 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(Dictionary.get("Mirroring.Source_URL"));
99 url_field = new JTextField();
100 url_field.setToolTipText(Dictionary.get("Mirroring.Source_URL_Tooltip"));
101
102 depth_model = new Vector();
103 depth_model.add(new DepthEntry(0, Dictionary.get("Mirroring.Download_Depth.Zero")));
104 /* @todo - add to dictionary */
105 depth_model.add(new DepthEntry(1, String.valueOf(1)));
106 /* @todo - add to dictionary */
107 depth_model.add(new DepthEntry(2, String.valueOf(2)));
108 /* @todo - add to dictionary */
109 depth_model.add(new DepthEntry(3, String.valueOf(3)));
110 /* @todo - add to dictionary */
111 depth_model.add(new DepthEntry(4, String.valueOf(4)));
112 /* @todo - add to dictionary */
113 depth_model.add(new DepthEntry(5, String.valueOf(5)));
114 depth_model.add(new DepthEntry(-1, Dictionary.get("Mirroring.Download_Depth.Unlimited")));
115 JPanel depth_pane = new JPanel();
116 JLabel depth_label = new JLabel(Dictionary.get("Mirroring.Download_Depth"));
117 depth_combobox = new JComboBox(depth_model);
118 depth_combobox.setToolTipText(Dictionary.get("Mirroring.Download_Depth_Tooltip"));
119
120 requisite_checkbox = new JCheckBox(Dictionary.get("Mirroring.Download_OnlyHTML"));
121
122 higher_checkbox = new JCheckBox(Dictionary.get("Mirroring.Higher_Directories"));
123 higher_checkbox.addActionListener(new CheckboxClickListener());
124
125 same_host_checkbox = new JCheckBox(Dictionary.get("Mirroring.Same_Host"));
126 same_host_checkbox.setSelected(true);
127
128 JPanel button_pane = new JPanel();
129
130 JButton preferences_button = new GLIButton(Dictionary.get("Mirroring.Preferences"), Dictionary.get("Mirroring.Preferences_Tooltip"));
131 preferences_button.setEnabled(true);
132
133 clear_cache_button = new GLIButton(Dictionary.get("Mirroring.ClearCache"), Dictionary.get("Mirroring.ClearCache_Tooltip"));
134 clear_cache_button.setEnabled(true);
135
136 download_button = new GLIButton(Dictionary.get("Mirroring.Download"), Dictionary.get("Mirroring.Download_Tooltip"));
137 download_button.setEnabled(true);
138
139 // Connect
140 clear_cache_button.addActionListener(new ClearCacheListener());
141 download_button.addActionListener(new DownloadButtonListener());
142 preferences_button.addActionListener(new PreferencesButtonActionListener());
143
144 // Layout
145 url_pane.setLayout(new BorderLayout(5,0));
146 url_pane.add(url_label, BorderLayout.WEST);
147 url_pane.add(url_field, BorderLayout.CENTER);
148
149 depth_pane.setLayout(new GridLayout(1,3));
150 depth_pane.add(depth_label);
151 depth_pane.add(new JPanel());
152 depth_pane.add(depth_combobox);
153
154 details_pane.setLayout(new GridLayout(2,1,0,5));
155 details_pane.setBorder(BorderFactory.createEmptyBorder(2,2,5,2));
156 details_pane.add(url_pane);
157 details_pane.add(depth_pane);
158
159 options_pane.setLayout(new GridLayout(3,1,0,5));
160
161 options_pane.add(higher_checkbox);
162 options_pane.add(same_host_checkbox);
163 //if (current_mode >= THRESHOLD) {
164 options_pane.add(requisite_checkbox);
165 //}
166
167 button_pane.setLayout(new GridLayout(3,1));
168 button_pane.add(preferences_button);
169 button_pane.add(clear_cache_button);
170 button_pane.add(download_button);
171
172 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))));
173 edit_pane.setLayout(new BorderLayout());
174 edit_pane.add(details_pane, BorderLayout.NORTH);
175 edit_pane.add(button_pane, BorderLayout.EAST);
176 edit_pane.add(options_pane, BorderLayout.CENTER);
177
178 setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
179 setLayout(new BorderLayout());
180 add(edit_pane, BorderLayout.NORTH);
181 add(list_scroll, BorderLayout.CENTER);
182 }
183
184 public void afterDisplay() {
185 ready = true;
186 }
187
188
189 /** 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.
190 */
191 public void gainFocus() {
192 if(!ready) {
193 return;
194 }
195 // Lets see what warning message we should display, if any.
196 String wget_version_str = Configuration.getWGetVersion();
197 if(wget_version_str.equals(StaticStrings.NO_WGET_STR)) {
198 // If there was no WGet available then downloading is disabled entirely
199 download_button_enabled = false;
200 // And we tell the user why.
201 Gatherer.missingWGET();
202 }
203 else if(wget_version_str.equals(StaticStrings.WGET_OLD_STR)) {
204 // Downloading is enabled
205 download_button_enabled = true;
206 // But we display a preventable warning message about the path problems.
207 Gatherer.oldWGET();
208 }
209 // Otherwise version must be ok
210 else {
211 download_button_enabled = true;
212 }
213 // 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.
214 download_button.setEnabled(download_button_enabled);
215 }
216
217 public void modeChanged(int mode) {
218 int old_mode = current_mode;
219 current_mode = mode;
220 if (old_mode >= THRESHOLD && current_mode >= THRESHOLD) {
221 return;
222 }
223 if (old_mode < THRESHOLD && current_mode < THRESHOLD) {
224 return;
225 }
226
227 //if (current_mode >= THRESHOLD) {
228 // options_pane.add(requisite_checkbox);
229 //}
230 //else {
231 // options_pane.remove(requisite_checkbox);
232 // requisite_checkbox.setSelected(false);
233 //}
234 return;
235 }
236
237
238 public void refresh(int refresh_reason, boolean ready)
239 {
240 }
241
242
243 /** 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. */
244 private class DepthEntry
245 implements Comparable {
246 private int value;
247 private String text;
248 /** Default constructor.
249 * @param value the depth value as an int
250 * @param text how this entry show represent itself as a String
251 */
252 public DepthEntry(int value, String text) {
253 this.text = text;
254 this.value = value;
255 }
256 /** Determines the natural ordering of this DepthEntry and some other object
257 * @param object the Object to test for ordering
258 * @return >0 if the object is before this one, 0 if they are equal, or <0 if the object is after this one
259 */
260 public int compareTo(Object object) {
261 // If the object is a DepthEntry then this is easy pesy.
262 if(object instanceof DepthEntry) {
263 int target_value = ((DepthEntry) object).getValue();
264 return value - target_value;
265 }
266 // If the object is a String, then we should try parsing an int from it
267 if(object instanceof String) {
268 try {
269 int target_value = Integer.parseInt((String)object);
270 return value - target_value;
271 }
272 catch(Exception exception) {
273 }
274 }
275 // Otherwise we'll try a string comparison between our text and the toString of the object
276 return text.compareTo(object.toString());
277 }
278
279 /** 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.
280 * @param object the Object to test for equality
281 * @return true if the object is equal to this depth entry, false otherwise
282 */
283 public boolean equals(Object object) {
284 return (compareTo(object) == 0);
285 }
286
287 public int getValue() {
288 return value;
289 }
290
291 public String toString() {
292 return text;
293 }
294 }
295
296 private class ClearCacheListener
297 implements ActionListener {
298 public void actionPerformed(ActionEvent event) {
299 // Retrieve the cache folder and delete it.
300 Utility.delete(Gatherer.getGLIUserCacheDirectoryPath());
301 // ...and refresh the node in the workspace tree to show it's all gone
302 Gatherer.g_man.refreshWorkspaceTree(WorkspaceTree.DOWNLOADED_FILES_CHANGED);
303 }
304 }
305
306 private class CheckboxClickListener
307 implements ActionListener {
308 public void actionPerformed(ActionEvent event) {
309 if (higher_checkbox.isSelected()) {
310 same_host_checkbox.setEnabled(false);
311 } else {
312 same_host_checkbox.setEnabled(true);
313 }
314 }
315 }
316
317 private class DownloadButtonListener
318 implements ActionListener {
319
320 public void actionPerformed(ActionEvent event) {
321 // Retrieve the current url and confirm it is valid
322 String url_str = url_field.getText();
323 URL url = null;
324 try {
325 url = new URL(url_str);
326 }
327 catch(MalformedURLException error) {
328 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("Mirroring.Invalid_URL"), Dictionary.get("Mirroring.Invalid_URL_Title"), JOptionPane.ERROR_MESSAGE);
329 }
330
331 // Otherwise construct a new download job
332 Object depth_object = depth_combobox.getSelectedItem();
333 int depth_value = 0;
334 if(depth_object instanceof DepthEntry) {
335 depth_value = ((DepthEntry)depth_object).getValue();
336 }
337 else if(depth_object instanceof String) {
338 String depth_string = (String) depth_object;
339 try {
340 depth_value = Integer.parseInt(depth_string);
341 // Create a new entry for the purposes of comparison, and if needs be, addition
342 DepthEntry new_entry = new DepthEntry(depth_value, depth_string);
343 // Try to select the correct item from the combobox
344 depth_combobox.setSelectedItem(null);
345 depth_combobox.setSelectedItem(new_entry);
346 // We can see if that worked, and if it didn't add the entry
347 if(depth_combobox.getSelectedItem() == null) {
348 depth_combobox.addItem(new_entry);
349 depth_combobox.setSelectedItem(new_entry);
350 }
351 new_entry = null;
352 }
353 catch(Exception exception) {
354 /* @todo - add to dictionary */
355 JOptionPane.showMessageDialog(Gatherer.g_man, "Mirroring.Mirror_Depth.Invalid_Depth", "Mirroring.Mirror_Depth.Invalid_Depth_Title", JOptionPane.ERROR_MESSAGE);
356 return;
357 }
358 depth_string = null;
359 }
360 depth_object = null;
361
362 String destination_file_path = Gatherer.getGLIUserCacheDirectoryPath();
363 (new File(destination_file_path)).mkdirs(); // If they aren't already
364
365 if(url != null) {
366 getter.newDownloadJob(higher_checkbox.isSelected(), (higher_checkbox.isSelected()? false : !same_host_checkbox.isSelected()), !requisite_checkbox.isSelected(), url, depth_value, destination_file_path);
367 }
368 }
369 }
370
371 private class PreferencesButtonActionListener
372 implements ActionListener {
373 public void actionPerformed(ActionEvent event) {
374 new Preferences(Preferences.CONNECTION_PREFS);
375 }
376 }
377}
Note: See TracBrowser for help on using the repository browser.