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