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

Last change on this file since 13531 was 13060, checked in by kjdon, 18 years ago

got rid of unnecessary gli mode stuff

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