source: branches/gsdl-2_70-distribution-branch/gli/src/org/greenstone/gatherer/gui/ExportAsPrompt.java@ 11734

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

fixed a bug in the arguments passed to export.pl

  • Property svn:keywords set to Author Date Id Revision
File size: 22.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.*;
42import java.util.ArrayList;
43import javax.swing.*;
44import javax.swing.event.*;
45import org.greenstone.gatherer.Configuration;
46import org.greenstone.gatherer.DebugStream;
47import org.greenstone.gatherer.Dictionary;
48import org.greenstone.gatherer.Gatherer;
49import org.greenstone.gatherer.LocalGreenstone;
50import org.greenstone.gatherer.collection.BasicCollectionConfiguration;
51import org.greenstone.gatherer.shell.GShell;
52import org.greenstone.gatherer.shell.GShellEvent;
53import org.greenstone.gatherer.shell.GShellListener;
54import org.greenstone.gatherer.util.ArrayTools;
55import org.greenstone.gatherer.util.CheckList;
56import org.greenstone.gatherer.util.CheckListEntry;
57import org.greenstone.gatherer.util.StaticStrings;
58import org.greenstone.gatherer.util.Utility;
59
60/** This class provides the functionality to export a set of current
61 * collections from the GSDLHOME/collect/ directory to various formats
62 * (hence ExportAs) using export.pl. The user chooses the collection from a
63 * list, where each entry also displays details about itself, confirms the
64 * delete of a collection by checking a checkbox then presses the ok button
65 * to actually delete the collection.
66 * Copied from WriteCDImagePrompt
67 * @author John Thompson, Greenstone Digital Library, University of Waikato
68 * @version 2.3
69 */
70public class ExportAsPrompt
71 extends ModalDialog
72 implements GShellListener {
73
74 static final private Dimension LABEL_SIZE = new Dimension(120, 25);
75
76 private OKButtonListener ok_button_listener;
77
78 private JLabel saveas_label = null;
79 private JComboBox saveas_combobox = null;
80
81 private ArrayList all_collections = null;
82 private ArrayList selected_collections = null;
83 /** The list of collections to include in exported cd-rom/dvd image */
84 private CheckList list = null;
85 /** The currently selected collection for deletion. */
86 private BasicCollectionConfiguration collection = null;
87 /** A reference to ourself so any inner-classes can dispose of us. */
88 private ExportAsPrompt prompt = null;
89 /** The close button, which exits the prompt without deleting anything. */
90 private JButton cancel_button = null;
91 /** The ok button which causes the selected collection to be deleted. */
92 private JButton ok_button = null;
93 /** The label above details. */
94 private JLabel details_label = null;
95 /** The label above the list. */
96 private JLabel list_label = null;
97 /** The text area used to display details about the collection selected. */
98 private JTextArea details_textarea = null;
99 /** The text area used to display instructions for the cd-rom/dvd export */
100 private JTextArea instructions_textarea;
101 /** A string array used to pass arguments to the phrase retrieval method. */
102 private JTextField title_field = null;
103 private JLabel title_label = null;
104 private String args[] = null;
105 private String cd_title = null;
106 /** whether the exporting was successful or not */
107 private boolean successful = false;
108 /** whether we are trying to export or not */
109 private boolean exporting = false;
110 /** the error message if any */
111 private StringBuffer error_message = null;
112 /** The size of the export prompt screen. */
113 public static final Dimension SIZE = new Dimension(500, 500);
114
115 /** Constructor.
116 * @see org.greenstone.gatherer.collection.ExportAsPrompt.CancelButtonListener
117 * @see org.greenstone.gatherer.collection.ExportAsPrompt.CollectionListListener
118 * @see org.greenstone.gatherer.collection.ExportAsPrompt.OKButtonListener
119 */
120 public ExportAsPrompt() {
121 super(Gatherer.g_man, true);
122 cancel_button = new GLIButton();
123 cancel_button.setMnemonic(KeyEvent.VK_C);
124 Dictionary.setBoth(cancel_button, "General.Close", "General.Close_Tooltip");
125 details_textarea = new JTextArea();
126 details_textarea.setEditable(false);
127 Dictionary.setText(details_textarea, "DeleteCollectionPrompt.No_Collection");
128 details_label = new JLabel();
129 Dictionary.setText(details_label, "DeleteCollectionPrompt.Collection_Details");
130
131 instructions_textarea = new JTextArea();
132 instructions_textarea.setCaretPosition(0);
133 instructions_textarea.setEditable(false);
134 instructions_textarea.setLineWrap(true);
135 instructions_textarea.setRows(4);
136 instructions_textarea.setWrapStyleWord(true);
137 Dictionary.registerText(instructions_textarea, "ExportAsPrompt.Instructions");
138
139 // Save As
140 ArrayList saveas_formats = new ArrayList();
141 saveas_formats.add("METS");
142 saveas_formats.add("DSpace");
143
144 saveas_label = new JLabel();
145 //saveas_label.setPreferredSize(LABEL_SIZE);
146 Dictionary.registerText(saveas_label, "ExportAsPrompt.SaveAs");
147 saveas_combobox = new JComboBox(saveas_formats.toArray());
148 Dictionary.registerTooltip(saveas_combobox, "ExportAsPrompt.SaveAs_Tooltip");
149
150 all_collections = new ArrayList();
151 list = new CheckList(true);
152 list_label = new JLabel();
153 Dictionary.setText(list_label, "DeleteCollectionPrompt.Collection_List");
154 ok_button = new GLIButton();
155 ok_button.setMnemonic(KeyEvent.VK_D);
156 Dictionary.setBoth(ok_button, "ExportAsPrompt.Export", "ExportAsPrompt.Export_Tooltip");
157
158 title_field = new JTextField();
159 // Dictionary.setTooltip(title_field, "ExportAsPrompt.Export_Name_Tooltip");
160 title_label = new JLabel();
161 Dictionary.setText(title_label, "ExportAsPrompt.Export_Name");
162 scanForCollections();
163 list.setListData(all_collections);
164
165 prompt = this;
166 setSize(SIZE);
167 Dictionary.setText(this, "ExportAsPrompt.Title");
168
169 setJMenuBar(new SimpleMenuBar("0")); // need to find an appropriate help page to open at
170 cancel_button.addActionListener(new CancelButtonListener());
171 list.addListSelectionListener(new CollectionListListener());
172 list.clearSelection();
173 list.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
174 ok_button_listener = new OKButtonListener();
175 ok_button.addActionListener(ok_button_listener);
176 ok_button.setEnabled(false);
177 //title.getDocument().addDocumentListener(new DocumentListener());
178 }
179
180 /** Destructor. */
181 public void destroy() {
182 saveas_label = null;
183 saveas_combobox = null;
184 all_collections.clear();
185 all_collections = null;
186 cancel_button = null;
187 details_textarea = null;
188 details_label = null;
189 list = null;
190 ok_button = null;
191 prompt = null;
192 if (selected_collections!=null) {
193 selected_collections.clear();
194 selected_collections = null;
195 }
196 title_field = null;
197 title_label = null;
198 }
199
200 /** This method causes the modal prompt to be displayed.
201 * returns true if it has exported the collections that are currently selected */
202 public boolean display() {
203 // Top pane
204 JPanel instructions_pane = new JPanel(new BorderLayout());
205 instructions_pane.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
206 instructions_pane.add(new JScrollPane(instructions_textarea), BorderLayout.CENTER);
207
208 title_label.setBorder(BorderFactory.createEmptyBorder(0,5,0,15));
209
210 JPanel title_pane = new JPanel(new BorderLayout());
211 title_pane.add(title_label, BorderLayout.WEST);
212 title_pane.add(title_field, BorderLayout.CENTER);
213 instructions_pane.add(title_pane, BorderLayout.CENTER);
214
215 // Save as pane
216 JPanel saveas_pane = new JPanel(new BorderLayout());
217 saveas_pane.setLayout(new BorderLayout());
218 saveas_pane.add(saveas_label, BorderLayout.WEST);
219 saveas_pane.add(saveas_combobox, BorderLayout.CENTER);
220 instructions_pane.add(saveas_pane, BorderLayout.SOUTH);
221
222 // Central pane
223 JPanel list_pane = new JPanel(new BorderLayout());
224 list_pane.add(list_label, BorderLayout.NORTH);
225 list_pane.add(new JScrollPane(list), BorderLayout.CENTER);
226 list_pane.setBorder(BorderFactory.createEmptyBorder(0, 0, 5, 0));
227
228 JPanel details_pane = new JPanel(new BorderLayout());
229 details_pane.add(details_label, BorderLayout.NORTH);
230 details_pane.add(new JScrollPane(details_textarea), BorderLayout.CENTER);
231 details_pane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
232
233 JPanel central_pane = new JPanel(new GridLayout(2, 1));
234 central_pane.add(list_pane);
235 central_pane.add(details_pane);
236 central_pane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
237
238 // Lower pane
239 JPanel button_pane = new JPanel(new GridLayout(1, 2));
240 button_pane.add(ok_button);
241 button_pane.add(cancel_button);
242 button_pane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
243
244 JPanel lower_pane = new JPanel(new BorderLayout());
245 lower_pane.add(button_pane, BorderLayout.SOUTH);
246 lower_pane.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));
247
248 // Final.
249 JPanel content_pane = (JPanel)this.getContentPane();
250 content_pane.setLayout(new BorderLayout());
251 content_pane.add(instructions_pane, BorderLayout.NORTH);
252 content_pane.add(central_pane, BorderLayout.CENTER);
253 content_pane.add(lower_pane, BorderLayout.SOUTH);
254
255 // Center and display.
256 Dimension screen_size = Configuration.screen_size;
257 this.setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);
258 this.setVisible(true); // blocks until the dialog is killed
259 return true;
260
261 }
262
263
264 /** This method calls the builcol.pl scripts via a GShell so as to not lock up the processor.
265 * @see org.greenstone.gatherer.Configuration
266 * @see org.greenstone.gatherer.Gatherer
267 * @see org.greenstone.gatherer.collection.Collection
268 * @see org.greenstone.gatherer.gui.BuildOptions
269 * @see org.greenstone.gatherer.shell.GShell
270 * @see org.greenstone.gatherer.shell.GShellListener
271 * @see org.greenstone.gatherer.shell.GShellProgressMonitor
272 * @see org.greenstone.gatherer.util.Utility
273 */
274 public void exportAsCollections() {
275 DebugStream.println("ExportAsPrompt.exportAsCollections()");
276
277 int num_collections = selected_collections.size();
278 if (num_collections == 0) return;
279 cd_title = title_field.getText();
280 cd_title = cd_title.trim();
281 cd_title = cd_title.replaceAll("\"", "");
282
283 String export_type = (String) saveas_combobox.getSelectedItem();
284
285 // Generate the export.pl command
286 ArrayList command_parts_list = new ArrayList();
287 if (Utility.isWindows() && (!Gatherer.isGsdlRemote)) {
288 command_parts_list.add(Configuration.perl_path);
289 command_parts_list.add("-S");
290 }
291 command_parts_list.add(LocalGreenstone.getBinScriptDirectoryPath() + "export.pl");
292 command_parts_list.add("-gli");
293 command_parts_list.add("-language");
294 command_parts_list.add(Configuration.getLanguage());
295 command_parts_list.add("-removeold");
296 command_parts_list.add("-saveas");
297 command_parts_list.add(export_type);
298 command_parts_list.add("-exportdir");
299 String export_dir = LocalGreenstone.getTmpDirectoryPath();
300 if (cd_title.equals("")) {
301 export_dir += "exported_" + export_type;
302 }
303 else {
304 export_dir += "exported_" + cd_title.replaceAll("\\s", "");
305 }
306 command_parts_list.add(export_dir);
307
308
309 for (int i = 0; i < num_collections; i++) {
310 command_parts_list.add(((BasicCollectionConfiguration) selected_collections.get(i)).getShortName());
311 }
312
313 DebugStream.print("export command = ");
314 for (int i = 0; i < command_parts_list.size(); i++) {
315 DebugStream.print(command_parts_list.get(i) + " ");
316 System.err.print(command_parts_list.get(i) + " ");
317 }
318 DebugStream.println("");
319 System.err.println("");
320
321 // Run the export.pl command
322 String[] command_parts = (String[]) command_parts_list.toArray(new String[0]);
323 GShell process = new GShell(command_parts, GShell.EXPORTAS, 3, this, null, GShell.GSHELL_EXPORTAS);
324 process.start();
325 //process.run();
326 DebugStream.println("ExportAsPrompt.exportAsCollections().return");
327
328 }
329
330 /** Shows an export complete prompt.
331 * @param success A <strong>boolean</strong> indicating if the collection was successfully deleted.
332 * @see org.greenstone.gatherer.collection.Collection
333 */
334 public void resultPrompt(boolean success, String extra) {
335 args = new String[2];
336 StringBuffer coll_names = new StringBuffer();
337 for (int i=0; i<selected_collections.size();i++) {
338 if (i>0) {
339 coll_names.append(", ");
340 }
341 BasicCollectionConfiguration complete_collection = (BasicCollectionConfiguration)selected_collections.get(i);
342 coll_names.append(complete_collection.getName() + StaticStrings.SPACE_CHARACTER + StaticStrings.OPEN_PARENTHESIS_CHARACTER + complete_collection.getShortName() + StaticStrings.CLOSE_PARENTHESIS_CHARACTER);
343 complete_collection = null;
344 }
345
346 args[0] = coll_names.toString();
347
348 args[1] = LocalGreenstone.getTmpDirectoryPath();
349 if(cd_title.equals("")) {
350 String export_type = (String)saveas_combobox.getSelectedItem();
351 args[1] += "exported_" + export_type;
352 } else {
353 args[1] += "exported_"+cd_title.replaceAll("\\s","");
354 }
355
356 String title;
357 String label;
358 String details;
359
360 if (success) {
361 String successMessage
362 = (selected_collections.size()==1)
363 ? "ExportAsPrompt.Successful_ExportOne"
364 : "ExportAsPrompt.Successful_ExportMany";
365
366 title = Dictionary.get("ExportAsPrompt.Successful_Title");
367 label = Dictionary.get(successMessage, args);
368 details = Dictionary.get("ExportAsPrompt.Successful_Details", args);
369 } else {
370 String failedMessage
371 = (selected_collections.size()==1)
372 ? "ExportAsPrompt.Failed_ExportOne"
373 : "ExportAsPrompt.Failed_ExportMany";
374
375 title = Dictionary.get("ExportAsPrompt.Failed_Title");
376 label = Dictionary.get(failedMessage, args);
377 details = Dictionary.get("ExportAsPrompt.Failed_Details", args);
378 }
379 SimpleResultDialog result_dialog = new SimpleResultDialog(title, label, details);
380 result_dialog.setVisible(true); // Blocks
381 result_dialog.dispose();
382 result_dialog = null;
383 }
384
385 /** Method to scan the collect directory retrieving and reloading each collection it finds, while building the list of known collections.
386 * @see org.greenstone.gatherer.Configuration
387 * @see org.greenstone.gatherer.Gatherer
388 * @see org.greenstone.gatherer.util.ArrayTools
389 * @see org.greenstone.gatherer.util.Utility
390 */
391 private void scanForCollections() {
392 // Start at the collect dir.
393 File collect_directory = new File(Gatherer.getCollectDirectoryPath());
394 if (collect_directory.exists()) {
395 // Now for each child directory see if it contains a .col file and
396 // if so try to load it..
397 File collections[] = collect_directory.listFiles();
398 ArrayTools.sort(collections);
399 for(int i = 0; collections != null && i < collections.length; i++) {
400 if(collections[i].isDirectory() && !collections[i].getName().equals(StaticStrings.MODEL_COLLECTION_NAME)) {
401 File config_file = new File(collections[i], Utility.CONFIG_FILE);
402 if (config_file.exists()) {
403 BasicCollectionConfiguration config = new BasicCollectionConfiguration(config_file);
404 all_collections.add(config);
405 config = null;
406 }
407 }
408 }
409 }
410 // Otherwise the collect directory doesn't actually exist, so there ain't much we can do.
411 }
412
413
414 /** All implementation of GShellListener must include this method so the listener can be informed of messages from the GShell.
415 * @param event A <strong>GShellEvent</strong> that contains, amoung other things, the message.
416 */
417 public synchronized void message(GShellEvent event) {
418 // Ignore the messages from RecPlug with 'show_progress' set (used for progress bars)
419 String message = event.getMessage();
420 if (message.startsWith("export.pl>")) {
421 message = message.substring(13);
422 //DebugStream.println("message = "+event.getMessage());
423 error_message.append(message);
424 error_message.append("\n");
425 }
426 }
427
428 /** All implementation of GShellListener must include this method so the listener can be informed when a GShell begins its task. Implementation side-effect, not actually used.
429 * @param event A <strong>GShellEvent</strong> that contains details of the initial state of the <strong>GShell</strong> before task comencement.
430 */
431 public synchronized void processBegun(GShellEvent event) {
432 // We don't care.
433 }
434 /** All implementation of GShellListener must include this method so the listener can be informed when a GShell completes its task.
435 * @param event A <strong>GShellEvent</strong> that contains details of the final state of the <strong>GShell</strong> after task completion.
436 */
437 public synchronized void processComplete(GShellEvent event) {
438 successful = false;
439 if(event.getStatus() == GShell.OK) {
440 if(event.getType() == GShell.EXPORTAS) {
441 successful = true;
442 }
443 }
444 ok_button_listener.processComplete();
445 }
446
447 /** A button listener implementation, which listens for actions on the close button and disposes of the dialog when detected. */
448 private class CancelButtonListener
449 implements ActionListener {
450 /** Any implementation of ActionListener must include this method so we can be informed when the button is actioned.
451 * @param event An <strong>ActionEvent</strong> containing all the relevant information garnered from the event itself.
452 */
453 public void actionPerformed(ActionEvent event) {
454 prompt.dispose();
455 }
456 }
457
458 /** This private class listens for selection events in from the list and then displays the appropriate details for that collection.
459 */
460 private class CollectionListListener
461 implements ListSelectionListener
462 {
463 /** Any implementation of ListSelectionListener must include this method so we can be informed when the list selection changes.
464 * @param event a <strong>ListSelectionEvent</strong> containing all the relevant information garnered from the event itself
465 */
466 public void valueChanged(ListSelectionEvent event)
467 {
468 // Can only export when something is ticked
469 ok_button.setEnabled(!list.isNothingTicked());
470
471 if (list.isSelectionEmpty()) {
472 // This only happens when the dialog is first entered
473 Dictionary.setText(details_textarea, "DeleteCollectionPrompt.No_Collection");
474 return;
475 }
476
477 collection = (BasicCollectionConfiguration) ((CheckListEntry) list.getSelectedValue()).getObject();
478 args = new String[3];
479 args[0] = collection.getCreator();
480 args[1] = collection.getMaintainer();
481 args[2] = collection.getDescription();
482 Dictionary.setText(details_textarea, "DeleteCollectionPrompt.Details", args);
483 details_textarea.setCaretPosition(0);
484 }
485 }
486
487
488 /** The OK button listener implementation. */
489 private class OKButtonListener
490 implements ActionListener {
491 private Component glass_pane;
492 private MouseListener mouse_blocker_listener;
493 private ProgressDialog progress_dialog;
494
495 /** Any implementation of ActionListener must include this method so we can be informed when the button is actioned.
496 * @param event An <strong>ActionEvent</strong> containing all the relevant information garnered from the event itself.
497 * @see org.greenstone.gatherer.Configuration
498 * @see org.greenstone.gatherer.Gatherer
499 * @see org.greenstone.gatherer.util.Utility
500 */
501 public void actionPerformed(ActionEvent event) {
502 ///ystem.err.println("OK Clicked");
503 // Make sure there are some colls specified
504 selected_collections = list.getTicked();
505 error_message = new StringBuffer();
506
507 // Set the cursor to hourglass
508 glass_pane = getGlassPane();
509 mouse_blocker_listener = new MouseAdapter() {};
510 glass_pane.addMouseListener(mouse_blocker_listener);
511 glass_pane.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
512 glass_pane.setVisible(true);
513
514 // Export the selected collection.
515 ///ystem.err.println("Exporting As for named collections");
516 exportAsCollections();
517
518 // Show progress dialog
519 ///ystem.err.println("Showing progress dialog");
520 progress_dialog = new ProgressDialog();
521 progress_dialog.setVisible(true);
522 }
523
524 public void processComplete() {
525 ///ystem.err.println("Process complete");
526 // Dispose of progress dialog
527 progress_dialog.setVisible(false);
528 progress_dialog.dispose();
529 progress_dialog = null;
530
531 // unset the cursor
532 glass_pane.setVisible(false);
533 glass_pane.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
534 glass_pane.removeMouseListener(mouse_blocker_listener);
535 glass_pane = null;
536 mouse_blocker_listener= null;
537
538 if (successful) {
539 resultPrompt(true, error_message.toString());
540 } else {
541 resultPrompt(false, error_message.toString());
542 }
543 error_message = null;
544 }
545
546 private class ProgressDialog
547 extends ModalDialog {
548
549 private Dimension size = new Dimension(400,65);
550
551 public ProgressDialog() {
552 super(Gatherer.g_man, Dictionary.get("ExportAsPrompt.Title"), true);
553 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
554 setSize(size);
555 JPanel content_pane = (JPanel) getContentPane();
556 JLabel progress_label = new JLabel();
557 Dictionary.setText(progress_label, "ExportAsPrompt.Progress_Label");
558 JProgressBar progress_bar = new JProgressBar();
559 progress_bar.setIndeterminate(true);
560 content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
561 content_pane.setLayout(new BorderLayout());
562 content_pane.add(progress_label, BorderLayout.NORTH);
563 content_pane.add(progress_bar, BorderLayout.CENTER);
564 // Position
565 Rectangle frame_bounds = Gatherer.g_man.getBounds();
566 setLocation(frame_bounds.x + (frame_bounds.width - size.width) / 2, frame_bounds.y + (frame_bounds.height - size.height) / 2);
567 }
568 }
569 }
570
571
572}
573
574
575
576
Note: See TracBrowser for help on using the repository browser.