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

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