source: trunk/gli/src/org/greenstone/gatherer/gui/ExportAsPrompt.java@ 10368

Last change on this file since 10368 was 10368, checked in by mdewsnip, 19 years ago

Removed some occurrences of "gsdl_path", and added some checks so it is only used when a local Greenstone exists.

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