source: trunk/gli/src/org/greenstone/gatherer/gui/WriteCDImagePrompt.java@ 9889

Last change on this file since 9889 was 9889, checked in by davidb, 19 years ago

exportcol.pl minus argument -nonetscape can't be last argument before
collections (or any other no-argument minus option for that matter) as
it gets confused and things the first collection is the argument to
the flag. Shifting further up the order avoids the problem.

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