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

Last change on this file since 12805 was 12802, checked in by mdewsnip, 18 years ago

Removed an unused variable.

  • Property svn:keywords set to Author Date Id Revision
File size: 21.7 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.*;
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.DebugStream;
50import org.greenstone.gatherer.Dictionary;
51import org.greenstone.gatherer.Gatherer;
52import org.greenstone.gatherer.LocalGreenstone;
53import org.greenstone.gatherer.file.WorkspaceTree;
54import org.greenstone.gatherer.util.StaticStrings;
55import org.greenstone.gatherer.util.Utility;
56import org.greenstone.gatherer.download.Download;
57import org.greenstone.gatherer.download.DownloadScrollPane;
58import org.greenstone.gatherer.download.ServerInfoDialog;
59import org.greenstone.gatherer.util.XMLTools;
60import org.greenstone.gatherer.cdm.*;
61import org.greenstone.gatherer.gui.*;
62import org.w3c.dom.*;
63import org.xml.sax.*;
64import org.greenstone.gatherer.GAuthenticator;
65
66/**
67 * @author John Thompson, Greenstone Digital Library, University of Waikato
68 * @version 2.1
69 */
70public class DownloadPane
71 extends JPanel {
72
73 /** The threshold for when the advanced options get added */
74 static private final int THRESHOLD = Configuration.SYSTEMS_MODE;
75 static final private Dimension LABEL_SIZE = new Dimension(225, 25);
76 static final private Dimension TREE_SIZE = new Dimension(150, 500);
77 static final private String CONTENTS[] = { "DOWNLOAD.MODE.WebDownload", "DOWNLOAD.MODE.OAIDownload", "DOWNLOAD.MODE.ZDownload" , "DOWNLOAD.MODE.SRWDownload"};
78
79 private boolean download_button_enabled = false;
80 private boolean ready = false;
81 private int current_mode;
82
83 private JPanel options_pane;
84 // TODO should use Vector to store all loaded downloads!!
85
86 private DesignTree tree;
87 private HashMap download_map;
88 private ServerInfoDialog server_info;
89 private JScrollPane list_scroll;
90 private DownloadScrollPane getter;
91 private String mode = null;
92 private TreePath previous_path;
93 private String proxy_url = "";
94
95 /** Main System code */
96 public DownloadPane() {
97 super();
98
99 // TODO: Download the WDownload and the download panel fixed!!
100 getter = new DownloadScrollPane();
101 getter.start();
102 list_scroll = getter.getDownloadJobList();
103
104 // TODO should use Vector to store all loaded downloads!!
105 String lang = Configuration.getLanguage();
106 download_map = new HashMap();
107 download_map.put("Web", loadDownload("WebDownload",lang));
108 download_map.put("OAI", loadDownload("OAIDownload",lang));
109 download_map.put("Z3950", loadDownload("Z3950Download",lang));
110 download_map.put("SRW", loadDownload("SRWDownload",lang));
111
112 // Creation
113 tree = new DesignTree();
114 options_pane = new JPanel();
115
116
117 JButton clear_cache_button = new GLIButton(Dictionary.get("Mirroring.ClearCache"), Dictionary.get("Mirroring.ClearCache_Tooltip"));
118 clear_cache_button.setEnabled(true);
119 clear_cache_button.setMnemonic(KeyEvent.VK_C);
120
121 JButton download_button = new GLIButton(Dictionary.get("Mirroring.Download"), Dictionary.get("Mirroring.Download_Tooltip"));
122 download_button.setEnabled(true);
123 download_button.setMnemonic(KeyEvent.VK_D);
124
125 JButton information_button = new GLIButton(Dictionary.get("Download.ServerInformation"), Dictionary.get("Download.ServerInformation_Tooltip"));
126 information_button.setEnabled(true);
127 information_button.setMnemonic(KeyEvent.VK_S);
128
129
130 JButton preferences_button = new GLIButton(Dictionary.get("Mirroring.Preferences"), Dictionary.get("Mirroring.Preferences_Tooltip"));
131 preferences_button.setEnabled(true);
132 preferences_button.setMnemonic(KeyEvent.VK_P);
133
134 // Connect
135 clear_cache_button.addActionListener(new ClearCacheListener());
136 download_button.addActionListener(new DownloadButtonListener());
137 preferences_button.addActionListener(new PreferencesButtonActionListener());
138 information_button.addActionListener(new InformationButtonActionListener());
139 tree.addTreeSelectionListener(new TreeListener());
140
141 // Add to Panel
142 JPanel button_pane = new JPanel();
143 button_pane.setLayout(new FlowLayout(FlowLayout.CENTER,20,5));
144 button_pane.setBorder(BorderFactory.createEtchedBorder());
145 button_pane.add(clear_cache_button);
146 button_pane.add(download_button);
147 button_pane.add(information_button);
148 button_pane.add(preferences_button);
149
150 JPanel tree_pane = new JPanel();
151 tree_pane.setLayout(new BorderLayout());
152 tree_pane.add(new JScrollPane(tree), BorderLayout.CENTER);
153 tree_pane.setPreferredSize(TREE_SIZE);
154
155
156 Color colour_two = Configuration.getColor("coloring.collection_tree_background", false);
157 options_pane.setBackground(colour_two);
158 options_pane.setBorder(BorderFactory.createEtchedBorder());
159
160
161 JScrollPane options_scroll_pane = new JScrollPane(options_pane);
162 JSplitPane mode_pane = new JSplitPane();
163 mode_pane.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
164 mode_pane.add(tree_pane,JSplitPane.LEFT);
165 mode_pane.add(options_scroll_pane,JSplitPane.RIGHT);
166 mode_pane.setDividerLocation(TREE_SIZE.width);
167
168 JPanel edit_pane = new JPanel();
169 edit_pane.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(2,0,0,0), BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Download Setting"), BorderFactory.createEmptyBorder(2,2,2,2))));
170 edit_pane.setLayout(new BorderLayout());
171 edit_pane.add(mode_pane,BorderLayout.CENTER);
172 edit_pane.add(button_pane,BorderLayout.PAGE_END);
173
174 // Add to "this"
175 setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
176 setLayout(new GridLayout(2,1));
177 add(edit_pane);
178 add(list_scroll);
179
180 mode = "Web";
181 generateOptions(options_pane,(Download)download_map.get(mode));
182 previous_path = tree.getSelectionPath();
183 }
184
185 /** System Utilities */
186 public void modeChanged(int mode) {
187 int old_mode = current_mode;
188 current_mode = mode;
189 if (old_mode >= THRESHOLD && current_mode >= THRESHOLD) {
190 return;
191 }
192 if (old_mode < THRESHOLD && current_mode < THRESHOLD) {
193 return;
194 }
195 if (current_mode >= THRESHOLD) {
196 System.out.println("current mode is greater then Threshold!!");
197 }
198 else {
199 System.out.println("current mode is smaller then Threshold!!");
200 }
201 }
202
203 private void addHeader(String name, Color color, JPanel target_pane) {
204 JPanel header = new JPanel();
205 header.setBackground(color);
206 JPanel inner_pane = new JPanel();
207 inner_pane.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(5,5,5,5), BorderFactory.createRaisedBevelBorder()));
208 inner_pane.setBackground(color);
209 JLabel header_label = new JLabel("<html><strong>" + name + "</strong></html>");
210 header_label.setBackground(Configuration.getColor("coloring.collection_heading_background", false));
211 header_label.setHorizontalAlignment(JLabel.CENTER);
212 header_label.setOpaque(true);
213
214 // Layout
215 inner_pane.setLayout(new BorderLayout());
216 inner_pane.add(header_label, BorderLayout.CENTER);
217
218 header.setLayout(new BorderLayout());
219 header.add(inner_pane, BorderLayout.CENTER);
220 target_pane.add(header);
221 }
222
223 /** Supporting Functions */
224 private Download loadDownload(String download_name, String lang) {
225 Document document = null;
226 InputStream input_stream = null;
227
228 try {
229 if (Gatherer.isGsdlRemote) {
230 StringBuffer launch_str = new StringBuffer();
231 launch_str.append(Gatherer.cgiBase);
232 launch_str.append("launch");
233 launch_str.append("?cmd=downloadinfo.pl&xml=&language=");
234 launch_str.append(lang);
235 launch_str.append("&plug=");
236 launch_str.append(download_name);
237
238 String launch = launch_str.toString();
239 System.err.println("*** launch = " + launch);
240
241 URL launch_url = new URL(launch);
242 URLConnection launch_connection = launch_url.openConnection();
243 input_stream = launch_connection.getInputStream();
244 }
245 else {
246 ArrayList args_list = new ArrayList();
247 String args[] = null;
248 if(Utility.isWindows()) {
249 if(Configuration.perl_path != null) {
250 args_list.add(Configuration.perl_path);
251 }
252 else {
253 args_list.add("Perl.exe");
254 }
255 }
256 args_list.add(LocalGreenstone.getBinScriptDirectoryPath()+"downloadinfo.pl");
257 args_list.add("-xml");
258 args_list.add("-language");
259 args_list.add(lang);
260 args_list.add(download_name);
261
262 // Create the process.
263 args = (String []) args_list.toArray(new String[0]);
264 Runtime runtime = Runtime.getRuntime();
265 DebugStream.println("Getting Download Info: "+args_list);
266 Process process = runtime.exec(args);
267
268 input_stream = process.getErrorStream();
269 }
270
271 document = XMLTools.parseXML(input_stream);
272 }
273 catch (Exception error) {
274 System.err.println("Failed when trying to parse: " + download_name);
275 error.printStackTrace();
276 }
277
278 if(document != null) {
279 return parseXML(document.getDocumentElement());
280 }
281
282 return null;
283 }
284
285 private Download parseXML(Node root) {
286 Download download = new Download();
287 String node_name = null;
288 for (Node node = root.getFirstChild(); node != null; node = node.getNextSibling()) {
289 node_name = node.getNodeName();
290 if(node_name.equalsIgnoreCase("Name")) {
291 String name = XMLTools.getValue(node);
292 download.setName(name);
293 }
294 else if (node_name.equalsIgnoreCase("Desc")) {
295 download.setDescription(XMLTools.getValue(node));
296 }
297 else if (node_name.equalsIgnoreCase("Abstract")) {
298 download.setIsAbstract(XMLTools.getValue(node).equalsIgnoreCase(StaticStrings.YES_STR));
299 }
300 else if(node_name.equalsIgnoreCase("Arguments")) {
301 for(Node arg = node.getFirstChild(); arg != null; arg = arg.getNextSibling()) {
302 node_name = arg.getNodeName();
303 if(node_name.equalsIgnoreCase("Option")) {
304 Argument argument = new Argument((Element)arg);
305 argument.parseXML((Element)arg);
306 argument.setValue(argument.getDefaultValue());
307 download.addArgument(argument);
308 }
309
310 }
311 }
312 else if(node_name.equalsIgnoreCase("DownloadInfo")) {
313 Download super_download = parseXML(node);
314 download.setSuper(super_download);
315 }
316 }
317
318 if(download.getName() != null) {
319 return download;
320 }
321 return null;
322 }
323
324 /** Update the previous setup */
325 private boolean updateArguments(boolean checkRequired)
326 {
327 boolean cont = true;
328 for(int i = 0; i < options_pane.getComponentCount(); i++) {
329
330 Component component = options_pane.getComponent(i);
331 if(component instanceof ArgumentControl) {
332 cont = cont && ((ArgumentControl)component).updateArgument(checkRequired);
333 }
334 }
335
336 if(cont){return true; }
337
338 return false;
339 }
340
341 /** Generate Controls for Options */
342 private void generateOptions(JPanel options_pane, ArgumentContainer data) {
343 options_pane.removeAll();
344 /** Create the current option panel */
345
346 ArrayList arguments = data.getArguments(true, false);
347 int mode = Configuration.getMode();
348 ArrayList added_arguments = new ArrayList();
349
350 for(int i = 0; i < arguments.size(); i++) {
351 Argument argument = (Argument) arguments.get(i);
352
353 if (argument.isHiddenGLI()) continue;
354 if(mode > Configuration.LIBRARIAN_MODE || !(argument.getType() == Argument.REGEXP)) {
355 ArgumentControl argument_control = new ArgumentControl(argument,false,null);
356 added_arguments.add(argument_control);
357 }
358 }
359
360 options_pane.setLayout(new GridLayout(added_arguments.size(),1));
361 for(int i = 0; i < added_arguments.size(); i++) {
362 options_pane.add((ArgumentControl)added_arguments.get(i));
363
364 }
365 }
366
367 /** Behaviour Functions */
368 public void afterDisplay() {
369 ready = true;
370 }
371
372
373 public void gainFocus() {
374 if(!ready) {
375 return;
376 }
377 // Lets see what warning message we should display, if any.
378 String wget_version_str = Configuration.getWGetVersion();
379 if(wget_version_str.equals(StaticStrings.NO_WGET_STR)) {
380 // If there was no WGet available then downloading is disabled entirely
381 download_button_enabled = false;
382 // And we tell the user why.
383 Gatherer.missingWGET();
384 }
385 else if(wget_version_str.equals(StaticStrings.WGET_OLD_STR)) {
386 // Downloading is enabled
387 download_button_enabled = true;
388 // But we display a preventable warning message about the path problems.
389 Gatherer.oldWGET();
390 }
391 // Otherwise version must be ok
392 else {
393 download_button_enabled = true;
394 }
395 // 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.
396 //download_button.setEnabled(download_button_enabled);
397 }
398
399
400
401 public void refresh(int refresh_reason, boolean ready)
402 {
403 }
404
405 /** Private classes */
406 /** This tree provides a 'table of contents' for the various components of the design process (collection configuration in more technical terms). */
407 private class DesignTree extends JTree {
408
409 private DesignNode root = null;
410 /** Constructor. Automatically generates all of the nodes, in the order of CONTENTS. */
411 public DesignTree() {
412 super();
413 resetModel(Configuration.getMode());
414 expandRow(0);
415 setRootVisible(false);
416 setSelectionRow(0);
417 }
418
419 /** Reset the model used by the design page contents tree. This is necessary to hide the partitions entry when in lower detail modes
420 * @param mode the current detail mode as an int
421 */
422 public void resetModel(int mode) {
423 root = new DesignNode("DOWNLOAD.MODE.Root");
424 // Now add the design categories.
425 for(int i = 0; i < CONTENTS.length; i++) {
426 root.add(new DesignNode(CONTENTS[i]));
427 }
428 this.setModel(new DefaultTreeModel(root));
429 updateUI();
430 }
431 /** Set the current view to the one specified.
432 * @param type the name of the desired view as a String
433 */
434 public void setSelectedView(String type) {
435 type = Dictionary.get(type);
436 for(int i = 0; i < root.getChildCount(); i++) {
437 DesignNode child = (DesignNode) root.getChildAt(i);
438 if(child.toString().equals(type)) {
439 TreePath path = new TreePath(child.getPath());
440 setSelectionPath(path);
441 }
442 }
443 }
444 }
445
446 /** A tree node that retains a reference to one of the possible design sub-views relating to the different sub-managers. */
447 private class DesignNode extends DefaultMutableTreeNode {
448 /** Constructor.
449 * @param object The <strong>Object</strong> assigned to this node.
450 */
451 public DesignNode(String object) {
452 super(object);
453 }
454 /** Retrieve a textual representation of the object.
455 * @return a String
456 */
457 public String toString() {
458 // return Dictionary.get("CDM.GUI." + (String)getUserObject());
459 return Dictionary.get((String) getUserObject());
460 }
461 }
462
463 /** Listens for selection changes in the 'contents' tree, and switches to the appropriate view. */
464 private class TreeListener
465 implements TreeSelectionListener {
466 /** Called whenever the selection changes, we must update the view so it matches the node selected.
467 * @param event A <strong>TreeSelectionEvent</strong> containing more information about the tree selection.
468 * @see org.greenstone.gatherer.cdm.ClassifierManager
469 * @see org.greenstone.gatherer.cdm.CollectionDesignManager
470 * @see org.greenstone.gatherer.cdm.CollectionMetaManager
471 * @see org.greenstone.gatherer.cdm.FormatManager
472 * @see org.greenstone.gatherer.cdm.LanguageManager
473 * @see org.greenstone.gatherer.cdm.MetadataSetView
474 * @see org.greenstone.gatherer.cdm.SubcollectionManager
475 * @see org.greenstone.gatherer.cdm.TranslationView
476 * @see org.greenstone.gatherer.cdm.PlugInManager
477 */
478 public void valueChanged(TreeSelectionEvent event) {
479 if(!tree.isSelectionEmpty()) {
480 TreePath path = tree.getSelectionPath();
481
482 DesignNode node = (DesignNode)path.getLastPathComponent();
483 String type = (String)node.getUserObject();
484 Gatherer.g_man.wait(true);
485 if(type == CONTENTS[0]) {
486 mode = "Web";
487 generateOptions(options_pane,(Download)download_map.get(mode));
488 }
489 else if(type == CONTENTS[1]) {
490 mode = "OAI";
491 generateOptions(options_pane,(Download)download_map.get(mode));
492 }
493 else if(type == CONTENTS[2]) {
494 mode = "Z3950";
495 generateOptions(options_pane,(Download)download_map.get(mode));
496 }
497 else if(type == CONTENTS[3]) {
498 mode = "SRW";
499 generateOptions(options_pane,(Download)download_map.get(mode));
500 }
501 tree.setSelectionPath(path);
502 previous_path = path;
503 repaint();
504
505 Gatherer.g_man.wait(false);
506 }
507 }
508 }
509
510 private class ClearCacheListener
511 implements ActionListener {
512 public void actionPerformed(ActionEvent event) {
513 // Retrieve the cache folder and delete it.
514 Utility.delete(Utility.getCacheDir());
515 // ...and refresh the node in the workspace tree to show it's all gone
516 Gatherer.g_man.refreshWorkspaceTree(WorkspaceTree.DOWNLOADED_FILES_CHANGED);
517 }
518 }
519
520 private class DownloadButtonListener
521 implements ActionListener {
522 public void actionPerformed(ActionEvent event) {
523
524 if(checkURL(true) && checkProxy() == true) {
525
526 getter.newDownloadJob((Download)download_map.get(mode) ,mode,proxy_url);
527 }
528 }
529 }
530
531
532 private boolean checkURL(boolean checkRequired){
533
534 if (!updateArguments(checkRequired)){
535 return false;
536 }
537
538 Download current_download = (Download)download_map.get(mode);
539 Argument arg_url = current_download.getArgument("url");
540
541 if (arg_url == null) return true;
542
543 String url_str = arg_url.getValue();
544 URL url = null;
545 try {
546 url = new URL(url_str);
547 }
548 catch(MalformedURLException error) {
549 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("Mirroring.Invalid_URL"), Dictionary.get("Mirroring.Invalid_URL_Title"), JOptionPane.ERROR_MESSAGE);
550 return false;
551 }
552
553 return true;
554 }
555
556
557 private boolean checkProxy(){
558
559 proxy_url = null;
560
561 Download current_download = (Download)download_map.get(mode);
562
563 Argument arg = current_download.getArgument("proxy_on");
564
565 if (arg == null) return true;
566
567 // Determine if we have to use a proxy.
568 if(Configuration.get("general.use_proxy", true)) {
569
570 String proxy_host = Configuration.getString("general.proxy_host", true);
571 String proxy_port = Configuration.getString("general.proxy_port", true);
572 // Find out whether the user has already authenticated themselves
573 String user_pass = "";
574 String address = proxy_host + ":" + proxy_port;
575
576 int count = 0;
577 while(count < 3 && (user_pass = (String) GAuthenticator.authentications.get(address)) == null) {
578 Authenticator.requestPasswordAuthentication(proxy_host, null, Integer.parseInt(proxy_port), "http://", Dictionary.get("WGet.Prompt"), "HTTP");
579 count++;
580 }
581
582 if(count >= 3) {
583 return false;
584 }
585
586 if(user_pass.indexOf("@") != -1) {
587
588 // Write the use proxy command - we don't do this anymore, instead we set environment variables - hopefully these can't be spied on like the follwoing can (using ps) - actually the environment stuff didn't work for windows, so lets go back to this
589 if (Utility.isWindows()) {
590
591 arg.setValue("true");
592 arg.setAssigned(true);
593
594 arg = current_download.getArgument("proxy_host");
595 arg.setValue(proxy_host);
596 arg.setAssigned(true);
597
598 arg = current_download.getArgument("proxy_port");
599 arg.setValue(proxy_port);
600 arg.setAssigned(true);
601
602 arg = current_download.getArgument("user_name");
603 arg.setValue(user_pass.substring(0, user_pass.indexOf("@")));
604 arg.setAssigned(true);
605
606 arg = current_download.getArgument("user_password");
607 arg.setValue(user_pass.substring(user_pass.indexOf("@") + 1));
608 arg.setAssigned(true);
609 }
610 else{
611 String user_name = user_pass.substring(0, user_pass.indexOf("@"));
612 String user_pwd = user_pass.substring(user_pass.indexOf("@") + 1);
613 proxy_url = user_name+":"+user_pwd+"@"+proxy_host+":"+proxy_port+"/";
614
615 }
616
617 return true;
618 }
619 else{
620 return false;
621 }
622
623 }
624
625 return true;
626 }
627
628 /*
629 private class PreferencesButtonActionListener
630 implements ActionListener {
631 public void actionPerformed(ActionEvent event) {
632 new Preferences(Preferences.CONNECTION_PREFS);
633 }
634 }*/
635
636 private class InformationButtonActionListener
637 implements ActionListener {
638 public void actionPerformed(ActionEvent event) {
639 //turn off the check for find argument
640 Download current_download = (Download)download_map.get(mode);
641
642 if (!checkProxy() || !checkURL(false) )return;
643
644
645 if(server_info != null) {
646 server_info.dispose();
647 }
648
649
650 Argument arg_url = current_download.getArgument("url");
651 String str_url = "";
652
653 if( arg_url!= null && arg_url.isAssigned()) {
654 str_url = arg_url.getValue();
655 }
656
657
658 server_info = new ServerInfoDialog(str_url ,proxy_url, mode,(Download)download_map.get(mode));
659
660 }
661 }
662
663 private class PreferencesButtonActionListener
664 implements ActionListener {
665 public void actionPerformed(ActionEvent event) {
666 new Preferences(Preferences.CONNECTION_PREFS);
667 }
668 }
669}
Note: See TracBrowser for help on using the repository browser.