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

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

Added a new LocalGreenstone class which contains functions that only make sense with a local Greenstone.

  • 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 command_parts_list.add(LocalGreenstone.getTmpDirectoryPath());
300
301 if (cd_title.equals("")) {
302 command_parts_list.add("exported_" + export_type);
303 }
304 else {
305 command_parts_list.add("exported_" + cd_title.replaceAll("\\s", ""));
306 }
307
308 for (int i = 0; i < num_collections; i++) {
309 command_parts_list.add(((BasicCollectionConfiguration) selected_collections.get(i)).getShortName());
310 }
311
312 DebugStream.print("export command = ");
313 for (int i = 0; i < command_parts_list.size(); i++) {
314 DebugStream.print(command_parts_list.get(i) + " ");
315 System.err.print(command_parts_list.get(i) + " ");
316 }
317 DebugStream.println("");
318 System.err.println("");
319
320 // Run the export.pl command
321 String[] command_parts = (String[]) command_parts_list.toArray(new String[0]);
322 GShell process = new GShell(command_parts, GShell.EXPORTAS, 3, this, null, GShell.GSHELL_EXPORTAS);
323 process.start();
324 //process.run();
325 DebugStream.println("ExportAsPrompt.exportAsCollections().return");
326
327 }
328
329 /** Shows an export complete prompt.
330 * @param success A <strong>boolean</strong> indicating if the collection was successfully deleted.
331 * @see org.greenstone.gatherer.collection.Collection
332 */
333 public void resultPrompt(boolean success, String extra) {
334 args = new String[2];
335 StringBuffer coll_names = new StringBuffer();
336 for (int i=0; i<selected_collections.size();i++) {
337 if (i>0) {
338 coll_names.append(", ");
339 }
340 BasicCollectionConfiguration complete_collection = (BasicCollectionConfiguration)selected_collections.get(i);
341 coll_names.append(complete_collection.getName() + StaticStrings.SPACE_CHARACTER + StaticStrings.OPEN_PARENTHESIS_CHARACTER + complete_collection.getShortName() + StaticStrings.CLOSE_PARENTHESIS_CHARACTER);
342 complete_collection = null;
343 }
344
345 args[0] = coll_names.toString();
346
347 args[1] = LocalGreenstone.getTmpDirectoryPath();
348 if(cd_title.equals("")) {
349 String export_type = (String)saveas_combobox.getSelectedItem();
350 args[1] += "exported_" + export_type;
351 } else {
352 args[1] += "exported_"+cd_title.replaceAll("\\s","");
353 }
354
355 String title;
356 String label;
357 String details;
358
359 if (success) {
360 String successMessage
361 = (selected_collections.size()==1)
362 ? "ExportAsPrompt.Successful_ExportOne"
363 : "ExportAsPrompt.Successful_ExportMany";
364
365 title = Dictionary.get("ExportAsPrompt.Successful_Title");
366 label = Dictionary.get(successMessage, args);
367 details = Dictionary.get("ExportAsPrompt.Successful_Details", args);
368 } else {
369 String failedMessage
370 = (selected_collections.size()==1)
371 ? "ExportAsPrompt.Failed_ExportOne"
372 : "ExportAsPrompt.Failed_ExportMany";
373
374 title = Dictionary.get("ExportAsPrompt.Failed_Title");
375 label = Dictionary.get(failedMessage, args);
376 details = Dictionary.get("ExportAsPrompt.Failed_Details", args);
377 }
378 SimpleResultDialog result_dialog = new SimpleResultDialog(title, label, details);
379 result_dialog.setVisible(true); // Blocks
380 result_dialog.dispose();
381 result_dialog = null;
382 }
383
384 /** Method to scan the collect directory retrieving and reloading each collection it finds, while building the list of known collections.
385 * @see org.greenstone.gatherer.Configuration
386 * @see org.greenstone.gatherer.Gatherer
387 * @see org.greenstone.gatherer.util.ArrayTools
388 * @see org.greenstone.gatherer.util.Utility
389 */
390 private void scanForCollections() {
391 // Start at the collect dir.
392 File collect_directory = new File(Gatherer.getCollectDirectoryPath());
393 if (collect_directory.exists()) {
394 // Now for each child directory see if it contains a .col file and
395 // if so try to load it..
396 File collections[] = collect_directory.listFiles();
397 ArrayTools.sort(collections);
398 for(int i = 0; collections != null && i < collections.length; i++) {
399 if(collections[i].isDirectory() && !collections[i].getName().equals(StaticStrings.MODEL_COLLECTION_NAME)) {
400 File config_file = new File(collections[i], Utility.CONFIG_FILE);
401 if (config_file.exists()) {
402 BasicCollectionConfiguration config = new BasicCollectionConfiguration(config_file);
403 all_collections.add(config);
404 config = null;
405 }
406 }
407 }
408 }
409 // Otherwise the collect directory doesn't actually exist, so there ain't much we can do.
410 }
411
412
413 /** All implementation of GShellListener must include this method so the listener can be informed of messages from the GShell.
414 * @param event A <strong>GShellEvent</strong> that contains, amoung other things, the message.
415 */
416 public synchronized void message(GShellEvent event) {
417 // Ignore the messages from RecPlug with 'show_progress' set (used for progress bars)
418 String message = event.getMessage();
419 if (message.startsWith("export.pl>")) {
420 message = message.substring(13);
421 //DebugStream.println("message = "+event.getMessage());
422 error_message.append(message);
423 error_message.append("\n");
424 }
425 }
426
427 /** 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.
428 * @param event A <strong>GShellEvent</strong> that contains details of the initial state of the <strong>GShell</strong> before task comencement.
429 */
430 public synchronized void processBegun(GShellEvent event) {
431 // We don't care.
432 }
433 /** All implementation of GShellListener must include this method so the listener can be informed when a GShell completes its task.
434 * @param event A <strong>GShellEvent</strong> that contains details of the final state of the <strong>GShell</strong> after task completion.
435 */
436 public synchronized void processComplete(GShellEvent event) {
437 successful = false;
438 if(event.getStatus() == GShell.OK) {
439 if(event.getType() == GShell.EXPORTAS) {
440 successful = true;
441 }
442 }
443 ok_button_listener.processComplete();
444 }
445
446 /** A button listener implementation, which listens for actions on the close button and disposes of the dialog when detected. */
447 private class CancelButtonListener
448 implements ActionListener {
449 /** Any implementation of ActionListener must include this method so we can be informed when the button is actioned.
450 * @param event An <strong>ActionEvent</strong> containing all the relevant information garnered from the event itself.
451 */
452 public void actionPerformed(ActionEvent event) {
453 prompt.dispose();
454 }
455 }
456
457 /** This private class listens for selection events in from the list and then displays the appropriate details for that collection.
458 */
459 private class CollectionListListener
460 implements ListSelectionListener
461 {
462 /** Any implementation of ListSelectionListener must include this method so we can be informed when the list selection changes.
463 * @param event a <strong>ListSelectionEvent</strong> containing all the relevant information garnered from the event itself
464 */
465 public void valueChanged(ListSelectionEvent event)
466 {
467 // Can only export when something is ticked
468 ok_button.setEnabled(!list.isNothingTicked());
469
470 if (list.isSelectionEmpty()) {
471 // This only happens when the dialog is first entered
472 Dictionary.setText(details_textarea, "DeleteCollectionPrompt.No_Collection");
473 return;
474 }
475
476 collection = (BasicCollectionConfiguration) ((CheckListEntry) list.getSelectedValue()).getObject();
477 args = new String[3];
478 args[0] = collection.getCreator();
479 args[1] = collection.getMaintainer();
480 args[2] = collection.getDescription();
481 Dictionary.setText(details_textarea, "DeleteCollectionPrompt.Details", args);
482 details_textarea.setCaretPosition(0);
483 }
484 }
485
486
487 /** The OK button listener implementation. */
488 private class OKButtonListener
489 implements ActionListener {
490 private Component glass_pane;
491 private MouseListener mouse_blocker_listener;
492 private ProgressDialog progress_dialog;
493
494 /** Any implementation of ActionListener must include this method so we can be informed when the button is actioned.
495 * @param event An <strong>ActionEvent</strong> containing all the relevant information garnered from the event itself.
496 * @see org.greenstone.gatherer.Configuration
497 * @see org.greenstone.gatherer.Gatherer
498 * @see org.greenstone.gatherer.util.Utility
499 */
500 public void actionPerformed(ActionEvent event) {
501 ///ystem.err.println("OK Clicked");
502 // Make sure there are some colls specified
503 selected_collections = list.getTicked();
504 error_message = new StringBuffer();
505
506 // Set the cursor to hourglass
507 glass_pane = getGlassPane();
508 mouse_blocker_listener = new MouseAdapter() {};
509 glass_pane.addMouseListener(mouse_blocker_listener);
510 glass_pane.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
511 glass_pane.setVisible(true);
512
513 // Export the selected collection.
514 ///ystem.err.println("Exporting As for named collections");
515 exportAsCollections();
516
517 // Show progress dialog
518 ///ystem.err.println("Showing progress dialog");
519 progress_dialog = new ProgressDialog();
520 progress_dialog.setVisible(true);
521 }
522
523 public void processComplete() {
524 ///ystem.err.println("Process complete");
525 // Dispose of progress dialog
526 progress_dialog.setVisible(false);
527 progress_dialog.dispose();
528 progress_dialog = null;
529
530 // unset the cursor
531 glass_pane.setVisible(false);
532 glass_pane.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
533 glass_pane.removeMouseListener(mouse_blocker_listener);
534 glass_pane = null;
535 mouse_blocker_listener= null;
536
537 if (successful) {
538 resultPrompt(true, error_message.toString());
539 } else {
540 resultPrompt(false, error_message.toString());
541 }
542 error_message = null;
543 }
544
545 private class ProgressDialog
546 extends ModalDialog {
547
548 private Dimension size = new Dimension(400,65);
549
550 public ProgressDialog() {
551 super(Gatherer.g_man, Dictionary.get("ExportAsPrompt.Title"), true);
552 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
553 setSize(size);
554 JPanel content_pane = (JPanel) getContentPane();
555 JLabel progress_label = new JLabel();
556 Dictionary.setText(progress_label, "ExportAsPrompt.Progress_Label");
557 JProgressBar progress_bar = new JProgressBar();
558 progress_bar.setIndeterminate(true);
559 content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
560 content_pane.setLayout(new BorderLayout());
561 content_pane.add(progress_label, BorderLayout.NORTH);
562 content_pane.add(progress_bar, BorderLayout.CENTER);
563 // Position
564 Rectangle frame_bounds = Gatherer.g_man.getBounds();
565 setLocation(frame_bounds.x + (frame_bounds.width - size.width) / 2, frame_bounds.y + (frame_bounds.height - size.height) / 2);
566 }
567 }
568 }
569
570
571}
572
573
574
575
Note: See TracBrowser for help on using the repository browser.