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

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

Support added for running remote commands (GLI applet) that specify multiple
collections, for instance when exporting to CD-ROM

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