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

Last change on this file since 12762 was 12620, checked in by mdewsnip, 18 years ago

Removed the unnecessary CollectionDesignManager.XMLStringToDOM() function.

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