source: trunk/gli/src/org/greenstone/gatherer/gui/GUIManager.java@ 6665

Last change on this file since 6665 was 6665, checked in by jmt12, 20 years ago

Forgot to check if there was a cdm loaded before asking if it could save -> NPE

  • Property svn:keywords set to Author Date Id Revision
File size: 40.0 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.datatransfer.*;
41import java.awt.event.*;
42import java.io.*;
43import java.lang.*;
44import java.net.*;
45import java.util.*;
46import javax.swing.*;
47import javax.swing.event.*;
48import javax.swing.plaf.*;
49import javax.swing.text.*;
50import org.greenstone.gatherer.Configuration;
51import org.greenstone.gatherer.Dictionary;
52import org.greenstone.gatherer.Gatherer;
53import org.greenstone.gatherer.collection.Collection;
54import org.greenstone.gatherer.collection.DeleteCollectionPrompt;
55import org.greenstone.gatherer.collection.ExportCollectionPrompt;
56import org.greenstone.gatherer.collection.SaveCollectionBox;
57import org.greenstone.gatherer.file.FileNode;
58import org.greenstone.gatherer.file.FileOpenActionListener;
59import org.greenstone.gatherer.gui.AboutDialog;
60//import org.greenstone.gatherer.gui.BrowsingPane;
61import org.greenstone.gatherer.gui.CollectionPane;
62import org.greenstone.gatherer.gui.CreatePane;
63import org.greenstone.gatherer.gui.Filter;
64import org.greenstone.gatherer.gui.GConfigPane;
65import org.greenstone.gatherer.gui.MenuBar;
66import org.greenstone.gatherer.gui.MetaEditPane;
67import org.greenstone.gatherer.gui.MirrorPane;
68import org.greenstone.gatherer.gui.OpenCollectionDialog;
69import org.greenstone.gatherer.gui.Preferences;
70//import org.greenstone.gatherer.gui.PreviewPane;
71import org.greenstone.gatherer.gui.SimpleOpenCollectionDialog;
72import org.greenstone.gatherer.gui.metaaudit.MetaAuditFrame;
73import org.greenstone.gatherer.gui.tree.DragTree;
74import org.greenstone.gatherer.gui.tree.WorkspaceTree;
75import org.greenstone.gatherer.help.HelpFrame;
76import org.greenstone.gatherer.mem.MetadataEditorManager;
77import org.greenstone.gatherer.msm.ElementWrapper;
78import org.greenstone.gatherer.msm.Metadata;
79import org.greenstone.gatherer.shell.GShell;
80import org.greenstone.gatherer.util.StaticStrings;
81import org.greenstone.gatherer.util.TreeSynchronizer;
82import org.greenstone.gatherer.util.Utility;
83
84/** The GUIManager is in charge of creating the Gatherer window frame then filling it with the goodness of the view panes. GUIManager not only creates these panes, but allows some messaging between them. Furthermore GUIManager includes functionality from menu driven choices, simply as it was easier to put it here once and have it accessible from all pane children. */
85public class GUIManager
86 extends JFrame
87 implements ActionListener, ChangeListener {
88 /** The browsing pane behaves much like an internet browser, or at least will some day. */
89 //public BrowsingPane browser_pane = null;
90 /** The collection pane is more like a file manager where you drag files from one tree to another. */
91 public CollectionPane collection_pane = null;
92 /** The create pane contains scripting options for importing and building collections into libraries. */
93 public CreatePane create_pane = null;
94
95 public FileOpenActionListener foa_listener = new FileOpenActionListener();
96
97 /** The configuration pane allows you to edit the design of the library in terms of the collection configuration file. */
98 public GConfigPane config_pane = null;
99 /** A reference to the currently instantiated help window, if any. */
100 public HelpFrame help = null;
101 /** The menu bar. */
102 public MenuBar menu_bar = null;
103 public MetaAuditFrame meta_audit;
104 /** The metaedit pane is used to assign, edit and remove metadata from files within the collection. */
105 public MetaEditPane metaedit_pane = null;
106 /** The mirror pane contains controls for mirroring internet sites. */
107 public MirrorPane mirror_pane = null;
108 /** The preview pane contains a preview of your build collection. */
109 //public PreviewPane preview_pane = null;
110 /** Are certain panes currently locked? */
111 private boolean locked = false;
112 /** The size of the Gatherer window. */
113 private Dimension size = null;
114 /** The filters used to dynamically filter the trees at user request. */
115 private HashMap filters = new HashMap();
116 /** The panel within the window that other components are placed on. */
117 private JPanel content_pane = null;
118 /** The dummy export pane. */
119 private JPanel export_pane = new JPanel();
120 /** The last view pane selected. */
121 private JPanel previous_pane;
122 /** The main tab pane containing the different views, available here to trap view change events. */
123 private JTabbedPane tab_pane = null;
124 /** A reference to any existing search and replace module/dialog. */
125 // private SearchAndReplace sar = null;
126 /** A threaded tab changer to try and avoid NPE on exit. */
127 private TabUpdater tab_updater = null;
128 /** The thread group this manager, and hence its child graphical rendering threads, belong to. In a vain attempts to make the Dictionary work across threads.
129 * @see org.greenstone.gatherer.Dictionary
130 */
131 private ThreadGroup thread_group = null;
132 /** Ensures that expansion events between like collection trees are synchronized. */
133 private TreeSynchronizer collection_tree_sync = null;
134 /** Ensures that expansion events between like workspace trees are synchronized. */
135 private TreeSynchronizer workspace_tree_sync = null;
136 /**Constructor. Enable window events and arranges all other components.
137 * @param size The intial <strong>Dimension</strong> of the screen.
138 * @param graphic The default <strong>GraphicsConfiguration</strong> for this platform.
139 */
140 public GUIManager(Dimension size) {
141 super();
142 // Initialization
143 this.help = new HelpFrame();
144 this.size = size;
145 this.collection_tree_sync = new TreeSynchronizer();
146 this.meta_audit = new MetaAuditFrame(collection_tree_sync, null);
147 this.workspace_tree_sync = new TreeSynchronizer();
148
149 this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
150
151 // Add a focus listener to ourselves. Thus if we gain focus when a Modal Dialog should instead have it, we can try to bring the modal dialog to the fore.
152 this.addFocusListener(new GLIGUIFocusListener());
153
154 // Make the Tool tip hang around for a rediculous amount of time.
155 ToolTipManager.sharedInstance().setDismissDelay(10000);
156 // Get a reference to the main thread group. Create a new thread, which defaults into the same thread group, then get its thread group. Easy.
157 Thread bob = new Thread();
158 thread_group = bob.getThreadGroup();
159 // Set up some other UI stuff. (fonts handled in Gatherer.main())
160 UIManager.put("FileChooser.lookInLabelText", Dictionary.get("SaveCollectionBox.Look_In"));
161 UIManager.put("FileChooser.filesOfTypeLabelText", Dictionary.get("SaveCollectionBox.Files_Of_Type"));
162 UIManager.put("FileChooser.fileNameLabelText", Dictionary.get("SaveCollectionBox.File_Name"));
163 }
164
165 private class GLIGUIFocusListener
166 extends FocusAdapter {
167 public void focusGained(FocusEvent e) {
168 if(Gatherer.current_modal != null) {
169 Gatherer.current_modal.makeVisible();
170 Gatherer.current_modal.toFront();
171 }
172 }
173 }
174
175 /** Any implementation of <i>ActionListener</i> must include this method so that we can be informed when an action has occured. In this case we are listening to actions from the menu-bar, and should react appropriately.
176 * @param event An <strong>ActionEvent</strong> containing information about the action that has occured.
177 */
178 public void actionPerformed(ActionEvent event) {
179 boolean cont = true;
180 Object esrc = event.getSource();
181 // *************
182 // File Options.
183 // *************
184 if(esrc == menu_bar.file_associations) {
185 Gatherer.assoc_man.edit();
186 }
187 else if(esrc == menu_bar.file_close) {
188 // if(!Gatherer.c_man.saved()) {
189 cont = showSaveCollectionBox(true, false);
190 // }
191 if(cont) {
192 tab_pane.setSelectedComponent(collection_pane);
193 }
194 }
195 else if(esrc == menu_bar.file_delete) {
196 DeleteCollectionPrompt dcp = new DeleteCollectionPrompt();
197 if(dcp.display()) {
198 Gatherer.c_man.closeCollection();
199 }
200 dcp.destroy();
201 dcp = null;
202 System.gc();
203 }
204 else if(esrc == menu_bar.file_export) {
205 ExportCollectionPrompt ecp = new ExportCollectionPrompt();
206 ecp.display();
207 ecp.destroy();
208 ecp = null;
209 }
210 else if(esrc == menu_bar.file_exit) {
211 //menu_bar.exit();
212 exit();
213 }
214 else if(esrc == menu_bar.file_new) {
215 showNewCollectionPrompt();
216 }
217 else if(esrc == menu_bar.file_open) {
218 if (showLoadCollectionBox()) {
219 tab_pane.setSelectedComponent(collection_pane);
220 }
221 }
222 else if(esrc == menu_bar.file_options) {
223 // Just incase the user has edited the GeneralSettings of a collection without losing focus afterwards. Well I'm forever losing foc... ooh shiney.
224 config_pane.loseFocus();
225 // And spawn a new preferences.
226 new Preferences();
227 }
228 else if(esrc == menu_bar.file_save) {
229 Gatherer.c_man.saveCollection(false, false);
230 }
231// else if(esrc == menu_bar.file_save_as) {
232// String name = JOptionPane.showInputDialog(this, "Enter new collection filename.");
233// if(name != null) {
234// Gatherer.c_man.saveCollectionAs(name);
235// }
236// }
237 // *************
238 // Edit Options.
239 // *************
240 else if(esrc == menu_bar.edit_copy) {
241 try {
242 KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
243 // Get the component with selected text as a JTextComponent
244 JTextComponent text = (JTextComponent) kfm.getPermanentFocusOwner();//getFocusOwner();
245 text.copy();
246 }
247 catch (Exception cce) {
248 // If the component is not a text component ignore the copy command
249 Gatherer.println(cce.toString());
250 }
251 }
252 else if(esrc == menu_bar.edit_cut) {
253 try {
254 KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
255 // Get the component with selected text as a JTextComponent
256 JTextComponent text = (JTextComponent) kfm.getPermanentFocusOwner();
257 // Cut the text to the clipboard
258 text.cut();
259 }
260 catch (ClassCastException cce) {
261 // If the component is not a text component ignore the cut command
262 Gatherer.println(cce.toString());
263 }
264 }
265 else if(esrc == menu_bar.edit_paste) {
266 try {
267 KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
268 // Get the component with selected text as a JTextComponent
269 JTextComponent text = (JTextComponent) kfm.getPermanentFocusOwner();
270 // Cut the text to the clipboard
271 text.paste();
272 }
273 catch (ClassCastException cce) {
274 // If the component is not a text component ignore the paste command
275 Gatherer.println(cce.toString());
276 }
277 }
278// else if(esrc == menu_bar.edit_search) {
279// if(sar != null) {
280// sar.dispose();
281// }
282// sar = new SearchAndReplace(false);
283// }
284// else if(esrc == menu_bar.edit_replace) {
285// if(sar != null) {
286// sar.dispose();
287// }
288// sar = new SearchAndReplace(true);
289// }
290
291 // *************
292 // Tools Options.
293 // *************
294// else if(esrc == menu_bar.tools_size) {
295// FileNode records[] = metaedit_pane.getSelectedNode();
296// if(records != null && records.length > 0) {
297// ArrayList metadatum = Gatherer.c_man.getCollection().gdm.getMetadata(records[0].getFile());
298// if(metadatum.size() >= 4) {
299// Metadata metadata = (Metadata) metadatum.get(0);
300// ElementWrapper ew = metadata.getElement();
301
302// int SIZE = 5000;
303// Object[] array = new Object[SIZE];
304// Runtime.getRuntime().gc();
305// long start = Runtime.getRuntime().freeMemory();
306// for (int i = 0; i < SIZE; i++) {
307// array[i] = ew.copy();
308// }
309// Runtime.getRuntime().gc();
310// long end = Runtime.getRuntime().freeMemory();
311// long difference = (( start - end ) / SIZE);
312// ///ystem.out.println(difference + " bytes used." );
313// }
314// }
315// }
316
317 // *************
318 // Help Options.
319 // *************
320 else if(esrc == menu_bar.help_about) {
321 new AboutDialog(this);
322 }
323// else if(esrc == menu_bar.help_browse) {
324// help.setView("huntingforfiles");
325// }
326 else if(esrc == menu_bar.help_build) {
327 help.setView("producingthecollection");
328 }
329 else if(esrc == menu_bar.help_collect) {
330 help.setView("collectingfiles");
331 }
332 else if(esrc == menu_bar.help_design) {
333 help.setView("designingacollection");
334 }
335 else if(esrc == menu_bar.help_general) {
336 help.setView("introduction");
337 }
338 else if(esrc == menu_bar.help_metaedit) {
339 help.setView("enrichingacollection");
340 }
341 else if(esrc == menu_bar.help_mirror) {
342 help.setView("downloadingfiles");
343 }
344 else if(esrc == menu_bar.help_preview) {
345 help.setView("previewingthecollection");
346 }
347 // *****************
348 // Metadata Options.
349 // *****************
350 else if(esrc == menu_bar.metadata_import) {
351 Gatherer.c_man.getCollection().msm.importMDS();
352 }
353 else if(esrc == menu_bar.metadata_edit) {
354 showEditMetadataBox();
355 }
356 else if(esrc == menu_bar.metadata_export) {
357 Gatherer.c_man.getCollection().msm.exportMDS();
358 }
359 else if(esrc == menu_bar.metadata_view) {
360 showMetaAuditBox();
361 }
362 }
363 /** Any actions that should happen after the display of the Gatherer window can be called here. Currently only updates the browser pane if it is active to work around bug in Mozilla renderer implementation.
364 */
365 public void afterDisplay() {
366// if(Gatherer.config.get("workflow.browse", true)) {
367// browser_pane.afterDisplay();
368// }
369 if(mirror_pane != null) {
370 mirror_pane.afterDisplay();
371 }
372 metaedit_pane.afterDisplay();
373 }
374 /** Once a collection has been made available to Gatherer, either by its creation or by it being reloaded, we need to inform all the visual components to update necessary data components (such as tree models), enable certain controls that depend on a collection being accessible, and refresh themselves.
375 * @param ready <i>true</i> if the collection is ready for editing, <i>false</i> otherwise.
376 */
377 public void collectionChanged(final boolean ready) {
378 if(Gatherer.config.get("workflow.mirror", true)) {
379 mirror_pane.collectionChanged(ready);
380 }
381 menu_bar.collectionChanged(ready); // Inform the menu bar that the collections changed.
382 collection_pane.collectionChanged(ready); // Used to update the collection workspace.
383 metaedit_pane.collectionChanged(ready); // Very important that metaedit pane shows current collection and is listening to latest msm.
384 config_pane.collectionChanged(ready); // Also important config pane is listening to latest msm.
385 create_pane.collectionChanged(ready); // Used to indicate a new BuildOptions model should be loaded.
386 //preview_pane.collectionChanged(ready); // preview should know when teh coll has changed so it can reload the home page
387
388 if(!locked) {
389 // Now enable tabs as necessary. Do this on event queue to prevent crazy NPEs
390 if(tab_updater == null) {
391 tab_updater = new TabUpdater(tab_pane, ready);
392 }
393 else {
394 tab_updater.setReady(ready);
395 }
396 SwingUtilities.invokeLater(tab_updater);
397 }
398 // Set the title
399 String collection_title = null;
400 String collection_name = null;
401 if (ready) {
402 Collection collection = Gatherer.c_man.getCollection();
403 collection_title = collection.getTitle();
404 collection_name = collection.getName();
405 collection = null;
406 }
407 setTitle(collection_title, collection_name);
408 collection_title = null;
409 collection_name = null;
410 // Now is a good time to force a garbage collect.
411 ///ystem.err.println("Calling garbage collection.");
412 System.gc();
413 }
414
415 public void destroy() {
416 // Destroying create pane ensures the latest log has been closed
417 if(create_pane != null) {
418 create_pane.destroy();
419 }
420 }
421
422 /** Enabled events on the window to be trapped, creates all the visual components, then builds the tab and other layouts.
423 */
424 public void display() {
425 content_pane = (JPanel) this.getContentPane();
426 // Enable window-type events to be fired.
427 enableEvents(AWTEvent.WINDOW_EVENT_MASK);
428 // Initialise and layout sub-components, plus other window dressing.
429 try {
430 this.setSize(size);
431
432 // Set the title
433 String collection_title = null;
434 String collection_name = null;
435 if (Gatherer.c_man.ready()) {
436 Collection collection = Gatherer.c_man.getCollection();
437 collection_title = collection.getTitle();
438 collection_name = collection.getName();
439 collection = null;
440 }
441 setTitle(collection_title, collection_name);
442 collection_title = null;
443 collection_name = null;
444
445 // Pretty corner icon
446 this.setIconImage(Utility.getImage("gatherer_small.gif").getImage());
447 // BorderLayout for the main screen. I'll try my best to avoid these in subcomponents as they're space greedy.
448 content_pane.setLayout(new BorderLayout());
449 // Create the menu-bar and stick it up the top.
450 menu_bar = new MenuBar(new MenuListenerImpl());
451 content_pane.add(menu_bar, BorderLayout.NORTH);
452 // Create the tabbed pane and plop it in the center where it will
453 // expand to consume all available space like any good gas would.
454 tab_pane = new JTabbedPane();
455 tab_pane.addChangeListener(this);
456 tab_pane.setFont(Gatherer.config.getFont("general.font", false));
457
458// // May have to play with the order in which tabs are added.
459// if(Gatherer.config.get("workflow.browse", true)) {
460// browser_pane = new BrowsingPane();
461// tab_pane.addTab("GUI.Hunt", Utility.getImage("browsing.gif"), browser_pane);
462// tab_pane.setEnabledAt(tab_pane.indexOfComponent(browser_pane), Gatherer.config.get("workflow.browse", false));
463// }
464
465 if(Gatherer.config.get(StaticStrings.WORKFLOW_MIRROR, true)) {
466 mirror_pane = new MirrorPane();
467 tab_pane.addTab("GUI.Mirror", Utility.getImage("mirroring.gif"), mirror_pane);
468 tab_pane.setEnabledAt(tab_pane.indexOfComponent(mirror_pane), Gatherer.config.get(StaticStrings.WORKFLOW_MIRROR, false));
469 }
470
471 collection_pane = new CollectionPane(workspace_tree_sync, collection_tree_sync);
472 collection_pane.display();
473 if(Gatherer.config.get("workflow.gather", true)) {
474 tab_pane.addTab("GUI.Gather", Utility.getImage("collection.gif"), collection_pane);
475 tab_pane.setEnabledAt(tab_pane.indexOfComponent(collection_pane), Gatherer.config.get("workflow.gather", false));
476 }
477
478 metaedit_pane = new MetaEditPane(collection_tree_sync);
479 metaedit_pane.display();
480 if(Gatherer.config.get("workflow.enrich", true)) {
481 tab_pane.addTab("GUI.Enrich", Utility.getImage("metaedit.gif"), metaedit_pane);
482 tab_pane.setEnabledAt(tab_pane.indexOfComponent(metaedit_pane), false);
483 }
484
485 config_pane = new GConfigPane();
486 config_pane.display();
487 if(Gatherer.config.get("workflow.design", true)) {
488 tab_pane.addTab("GUI.Design", Utility.getImage("build.gif"), config_pane);
489 tab_pane.setEnabledAt(tab_pane.indexOfComponent(config_pane), false);
490 }
491
492 if(Gatherer.config.get("workflow.export", true)) {
493 tab_pane.addTab("GUI.Export", Utility.getImage("export.gif"), export_pane);
494 tab_pane.setEnabledAt(tab_pane.indexOfComponent(export_pane), false);
495 }
496
497 create_pane = new CreatePane();
498 create_pane.display();
499 if(Gatherer.config.get("workflow.create", true)) {
500 tab_pane.addTab("GUI.Create", Utility.getImage("build session.gif"), create_pane);
501 tab_pane.setEnabledAt(tab_pane.indexOfComponent(create_pane), false);
502 }
503
504 //preview_pane = new PreviewPane();
505 //preview_pane.display();
506 //if(Gatherer.config.get("workflow.preview", true)) {
507 //tab_pane.addTab("GUI.Preview", Utility.getImage("final.gif"), preview_pane);
508 //tab_pane.setEnabledAt(tab_pane.indexOfComponent(preview_pane), false);
509 //}
510
511 // Select the collect pane if it is available
512 if(Gatherer.config.get("workflow.gather", false)) {
513 tab_pane.setSelectedComponent(collection_pane);
514 }
515 // Otherwise find the first tab that is enabled and select that.
516 else {
517 boolean found = false;
518 for(int i = 0; !found && i < tab_pane.getTabCount(); i++) {
519 if(tab_pane.isEnabledAt(i)) {
520 tab_pane.setSelectedIndex(i);
521 found = true;
522 }
523 }
524 }
525
526 Dictionary.register(tab_pane);
527 content_pane.add(tab_pane, BorderLayout.CENTER);
528 // Drive a sessionReady event to update all controls to reflect current collection status.
529 collectionChanged(Gatherer.c_man.ready());
530 }
531 catch (Exception e) {
532 Gatherer.printStackTrace(e);
533 // The GUI failing to build is a app killer
534 e.printStackTrace();
535 System.exit(1);
536 }
537 }
538 /** When called this method ensures that all the things needing saving are saved before Gatherer.exit() is called. This includes a save collection prompt if necessary.
539 */
540 public void exit() {
541 // Tell everyone who cares that they are losing focus
542 if(!Gatherer.c_man.ready() || config_pane.canSave()) {
543 boolean cont = true;
544 if(Gatherer.c_man.ready() && !Gatherer.c_man.saved()) {
545 cont = showSaveCollectionBox(false, true);
546 }
547 else {
548 // Deal to help
549 if(help != null) {
550 help.destroy();
551 help = null;
552 }
553 Gatherer.self.exit();
554 }
555 }
556 }
557 /** Retrieve the filter, or if one already exists, spawn a linked copy. */
558 public Filter getFilter(DragTree tree) {
559 Filter filter = (Filter) filters.get(tree.getModel());
560 if (filter == null) {
561 filter = new Filter(tree, null);
562 filters.put(tree.getModel(), filter);
563 return filter;
564 }
565 return filter.spawn(tree);
566 }
567
568 public Component getSelectedView() {
569 return tab_pane.getSelectedComponent();
570 }
571 /** This method is called when the collection is being built, and is used to disable all controls in all pane which could change the state of the collection.
572 */
573 public void lockCollection(boolean import_stage, boolean lock) {
574 locked = lock;
575 if(import_stage) {
576 int collection_pos = tab_pane.indexOfComponent(collection_pane);
577 int metaedit_pos = tab_pane.indexOfComponent(metaedit_pane);
578 int config_pos = tab_pane.indexOfComponent(config_pane);
579 tab_pane.setEnabledAt(collection_pos, !lock);
580 tab_pane.setEnabledAt(metaedit_pos, !lock);
581 tab_pane.setEnabledAt(config_pos, !lock);
582 }
583 else {
584 int config_pos = tab_pane.indexOfComponent(config_pane);
585 tab_pane.setEnabledAt(config_pos, !lock);
586 }
587 }
588
589 public void modeChanged(int mode) {
590 // Set the title
591 String collection_title = null;
592 String collection_name = null;
593 if (Gatherer.c_man.ready()) {
594 Collection collection = Gatherer.c_man.getCollection();
595 collection_title = collection.getTitle();
596 collection_name = collection.getName();
597 collection = null;
598 }
599 setTitle(collection_title, collection_name);
600 collection_title = null;
601 collection_name = null;
602 // Now pass on the message to anyone who cares
603 if(collection_pane != null) {
604 collection_pane.modeChanged(mode);
605 }
606 if(config_pane != null) {
607 config_pane.modeChanged(mode);
608 }
609 if(create_pane != null) {
610 create_pane.modeChanged(mode);
611 }
612 if(metaedit_pane != null) {
613 metaedit_pane.modeChanged(mode);
614 }
615 }
616
617 public void refreshTrees(int refresh_reason)
618 {
619 if(collection_pane != null) {
620 collection_pane.refreshWorkspaceTree(refresh_reason);
621 collection_pane.refreshCollectionTree(refresh_reason);
622 }
623 if(metaedit_pane != null) {
624 metaedit_pane.refreshTrees();
625 }
626 }
627
628 /** Allows the system to programatically set the selected tab.
629 * @param component The view you wish to make visable in the tab pane as a <strong>Component</strong>.
630 */
631 public void setSelectedView(Component component) {
632 tab_pane.setSelectedComponent(component);
633 }
634
635 /** Specifies whether a certain tab is enabled or not. */
636 public void setTabEnabled(String rawname, boolean state) {
637 // Retrieve the dictionary based name.
638 String name = Dictionary.get("GUI." + rawname);
639 int index = tab_pane.indexOfTab(name);
640 // Of course we may not have this tab available.
641 if(index != -1) {
642 // Some tabs are also dependant on if a collection is ready
643 Component component = tab_pane.getComponentAt(index);
644 //if(component == preview_pane) {
645 //tab_pane.setEnabledAt(index, state && Gatherer.c_man != null && Gatherer.c_man.ready() && Gatherer.c_man.built());
646 //}
647 //else
648 if(component == metaedit_pane || component == config_pane || component == export_pane || component == create_pane) {
649 tab_pane.setEnabledAt(index, state && Gatherer.c_man != null && Gatherer.c_man.ready());
650 }
651 else {
652 tab_pane.setEnabledAt(index, state);
653 }
654 // If this was the currently selected tab and it is now disabled, change the view to the first enabled tab.
655 if(tab_pane.getSelectedIndex() == index && !state) {
656 boolean found = false;
657 for(int i = 0; !found && i < tab_pane.getTabCount(); i++) {
658 if(tab_pane.isEnabledAt(i)) {
659 tab_pane.setSelectedIndex(i);
660 found = true;
661 }
662 }
663 // If there are no tabs enabled, which should be impossible, then select the first tab
664 if(!found) {
665 tab_pane.setSelectedIndex(0);
666 }
667 }
668 }
669 // If the rawname was mirror then rebuild workspace tree to remove caches.
670 if (rawname.equals("Mirror")) {
671 collection_pane.refreshWorkspaceTree(WorkspaceTree.MAPPED_DIRECTORIES_CHANGED);
672 }
673 }
674
675 /** Change the string shown in the title bar of the main gui frame. If either value is null, the 'No Collection' string is shown instead.
676 * @param title
677 * @param name
678 */
679 public void setTitle(String title, String name) {
680 // Finally display the collection name in the title bar.
681 StringBuffer title_buffer = new StringBuffer(Utility.PROGRAM_NAME);
682 title_buffer.append(StaticStrings.SPACE_CHARACTER);
683 title_buffer.append(StaticStrings.SPACE_CHARACTER);
684 // Describe the current user mode
685 title_buffer.append(StaticStrings.MODE_STR);
686 title_buffer.append(Gatherer.config.getModeAsString());
687 title_buffer.append(StaticStrings.SPACE_CHARACTER);
688 title_buffer.append(StaticStrings.SPACE_CHARACTER);
689 // Now for the current collection
690 title_buffer.append(StaticStrings.COLLECTION_STR);
691 if (title != null && name != null) {
692 title_buffer.append(title);
693 title_buffer.append(StaticStrings.SPACE_CHARACTER);
694 title_buffer.append(StaticStrings.OPEN_PARENTHESIS_CHARACTER);
695 title_buffer.append(name);
696 title_buffer.append(StaticStrings.CLOSE_PARENTHESIS_CHARACTER);
697 }
698 else {
699 title_buffer.append(Dictionary.get("Collection.No_Collection"));
700 }
701 this.setTitle(title_buffer.toString());
702 title_buffer = null;
703 }
704
705 /** When the edit metadata option is choosen from the menu, this method is called to ensure we only edit the metadata if there is metadata loaded.
706 */
707 public void showEditMetadataBox() {
708 if(Gatherer.c_man.getCollection() != null) {
709 Gatherer.c_man.getCollection().msm.editMDS(null, MetadataEditorManager.NORMAL);
710 }
711 }
712 /** When the load collection option is choosen this method is called to produce the modal file load prompt.
713 */
714 public boolean showLoadCollectionBox() {
715 boolean result = false;
716 // We first try the simple open collection dialog
717 SimpleOpenCollectionDialog dialog = new SimpleOpenCollectionDialog();
718 int user_choice = dialog.display();
719 String filename = null;
720 // The user may choose to go to the advanced 'browse' dialog
721 if(user_choice == SimpleOpenCollectionDialog.OK_OPTION) {
722 filename = dialog.getFileName();
723 }
724 else if(user_choice == SimpleOpenCollectionDialog.BROWSE_OPTION) {
725 File file;
726 if(Gatherer.config.gsdl_path != null) {
727 file = new File(Utility.getCollectionDir(Gatherer.config.gsdl_path));
728 }
729 else {
730 file = new File(Utility.BASE_DIR);
731 }
732 OpenCollectionDialog chooser = new OpenCollectionDialog(file);
733 file = null;
734 filename = chooser.getFileName();
735 chooser.destroy();
736 chooser = null;
737 }
738 dialog.destroy();
739 dialog = null;
740 // User can cancel action.
741 if(filename != null) {
742 // If there is already a collection open, save and close it.
743 if(Gatherer.c_man.ready()) {
744 showSaveCollectionBox(true, false);
745 // Wait until it is closed.
746 try {
747 synchronized(this) {
748 while(Gatherer.c_man.reallyReady()) {
749 wait(10);
750 }
751 }
752 }
753 catch(Exception error) {
754 Gatherer.println("Exception: " + error);
755 Gatherer.printStackTrace(error);
756 }
757 }
758 result = Gatherer.c_man.loadCollection(filename);
759 filename = null;
760 }
761 return result;
762 }
763
764 /** When called this method causes the MetaAuditBox class in CollectionManager to display a nice dialog box which contains all the metadata assigned in the collection.
765 */
766 public void showMetaAuditBox() {
767 wait(true);
768 meta_audit.display();
769 wait(false);
770 }
771 /** This method is used to open the new collection box on the screen.
772 */
773 public void showNewCollectionPrompt() {
774 NewCollectionMetadataPrompt ncm_prompt = null;
775 // Create the collection details prompt from new collection prompt
776 NewCollectionDetailsPrompt ncd_prompt = new NewCollectionDetailsPrompt();
777 // If no previous collection was indicated as a model design, then show the metadata selection prompt from new collection prompt
778 if(!ncd_prompt.isCancelled() && (ncd_prompt.getBase() == null)) {
779 ncm_prompt = new NewCollectionMetadataPrompt();
780 }
781 // Create the new collection (if not cancelled) in a new thread.
782 if(!ncd_prompt.isCancelled() && (ncm_prompt == null || !ncm_prompt.isCancelled())) {
783 // If there is already a collection open, save and close it.
784 if(Gatherer.c_man.ready()) {
785 showSaveCollectionBox(true, false);
786 // Wait until it is closed.
787 try {
788 synchronized(this) {
789 while(Gatherer.c_man.reallyReady()) {
790 wait(10);
791 }
792 }
793 }
794 catch(Exception error) {
795 Gatherer.println("Exception: " + error);
796 Gatherer.printStackTrace(error);
797 }
798 }
799
800 // Create new collection.
801 CreationTask task = new CreationTask(ncd_prompt, ncm_prompt);
802 // SwingUtilities.invokeLater(task);
803 task.start();
804 // Close prompt.
805 }
806 // Done
807 ncd_prompt = null;
808 ncm_prompt = null;
809 }
810 private class CreationTask
811 extends Thread {
812 private NewCollectionDetailsPrompt ncd_prompt = null;
813 private NewCollectionMetadataPrompt ncm_prompt = null;
814 public CreationTask(NewCollectionDetailsPrompt ncd_prompt, NewCollectionMetadataPrompt ncm_prompt) {
815 this.ncd_prompt = ncd_prompt;
816 this.ncm_prompt = ncm_prompt;
817 }
818
819 public void run() {
820 ///ystem.err.println("Running CreationTask...");
821 if(ncm_prompt == null) {
822 Gatherer.c_man.createCollection(ncd_prompt.getDescription(), Gatherer.config.getEmail(), ncd_prompt.getName(), ncd_prompt.getTitle(), ncd_prompt.getBase(), null);
823 }
824 else {
825 Gatherer.c_man.createCollection(ncd_prompt.getDescription(), Gatherer.config.getEmail(), ncd_prompt.getName(), ncd_prompt.getTitle(), null, ncm_prompt.getSets());
826 }
827 // Now that the collection specific settings are loaded we can set the 'view extracted metadata' property
828 // do we want to have this in here???
829 //Gatherer.config.set("general.view_extracted_metadata", Configuration.COLLECTION_SPECIFIC, true);
830 ncd_prompt.dispose();
831 ncd_prompt = null;
832 if(ncm_prompt != null) {
833 ncm_prompt.dispose();
834 ncm_prompt = null;
835 }
836 }
837 }
838
839
840 /** This method is used to open the save collection box/prompt on the screen.
841 * @return A <i>boolean</i> which is <i>true</i> if the collection was saved successfully, <i>false</i> otherwise.
842 */
843 public boolean showSaveCollectionBox(boolean close_after, boolean exit_after) {
844 //SaveCollectionBox save_collection_box = new SaveCollectionBox();
845 //Rectangle bounds = save_collection_box.getBounds();
846 //int result = save_collection_box.getUserOption(Gatherer.c_man.getCollection().getName());
847 //switch(result) {
848 //case SaveCollectionBox.SAVE_YES:
849 Gatherer.c_man.setClosingThread(true);
850 Gatherer.c_man.saveCollection(close_after, exit_after);
851 // Wait until it is closed.
852 try {
853 synchronized(this) {
854 while(Gatherer.c_man.reallyReady()) {
855 wait(10);
856 }
857 }
858 }
859 catch(Exception error) {
860 Gatherer.println("Exception: " + error);
861 Gatherer.printStackTrace(error);
862 }
863
864 Gatherer.c_man.setClosingThread(false);
865
866 //content_pane.paintImmediately(bounds);
867 return true;
868 //case SaveCollectionBox.SAVE_NO:
869 // Close collection.
870 // if(close_after) {
871 // tab_pane.setSelectedComponent(collection_pane);
872 // Gatherer.c_man.closeCollection();
873 // }
874 // if(exit_after) {
875 // Gatherer.self.exit();
876 // }
877 // return true;
878 //default:
879 // return false;
880 //}
881 }
882 /** Any implementation of ChangeListener must include this method so we can be informed when the state of one of the registered objects changes. In this case we are listening to view changes within the tabbed pane.
883 * @param event A ChangeEvent containing information about the event that fired this call.
884 */
885 public void stateChanged(ChangeEvent event) {
886 if(previous_pane != null) {
887 if(previous_pane == create_pane) {
888 create_pane.loseFocus();
889 }
890 else if(previous_pane == config_pane) {
891 config_pane.loseFocus();
892 }
893 }
894 // "View assigned metadata" menu item is disabled by default
895 menu_bar.metadata_view.setCanEnable(false);
896 menu_bar.setMetaAuditSuffix(null);
897
898 menu_bar.tabSelected(tab_pane.getSelectedIndex());
899 int selected_index = tab_pane.getSelectedIndex();
900 if( selected_index == tab_pane.indexOfComponent(collection_pane)) {
901 collection_pane.gainFocus();
902 // "View assigned metadata" menu item is enabled for the "Gather" pane
903 menu_bar.metadata_view.setCanEnable(true);
904 }
905 else if(selected_index == tab_pane.indexOfComponent(metaedit_pane)) {
906 metaedit_pane.gainFocus();
907 // "View assigned metadata" menu item is enabled for the "Enrich" pane
908 menu_bar.metadata_view.setCanEnable(true);
909 }
910 else if(selected_index == tab_pane.indexOfComponent(config_pane)) {
911 config_pane.gainFocus();
912 }
913 else if(selected_index == tab_pane.indexOfComponent(create_pane)) {
914 create_pane.gainFocus();
915 }
916 else if(selected_index == tab_pane.indexOfComponent(mirror_pane)) {
917 mirror_pane.gainFocus();
918 }
919 //else if(tab_pane.getSelectedIndex() == tab_pane.indexOfComponent(preview_pane)) {
920 //config_pane.saveConfiguration();
921 //preview_pane.gainFocus();
922 //}
923
924 previous_pane = (JPanel) tab_pane.getSelectedComponent();
925 }
926
927 private MouseListener mouse_blocker_listener = new MouseAdapter() {};
928
929 public void wait(boolean waiting) {
930 Component glass_pane = getGlassPane();
931 if(waiting) {
932 // Show wait cursor.
933 glass_pane.addMouseListener(mouse_blocker_listener);
934 glass_pane.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
935 glass_pane.setVisible(true);
936 }
937 else {
938 // Hide wait cursor.
939 glass_pane.setVisible(false);
940 glass_pane.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
941 glass_pane.removeMouseListener(mouse_blocker_listener);
942 }
943 glass_pane = null;
944 }
945
946 public void workflowUpdate(String raw, boolean state) {
947 WorkflowUpdater task = new WorkflowUpdater(raw, state);
948 SwingUtilities.invokeLater(task);
949 task = null;
950 }
951
952 /** Called to determine if we should wait for a thread to finish before continuing. We wait for threads if they are named: GSHELL_BUILD, GSHELL_IMPORT, or GSHELL_NEW.
953 * @return <i>true</i> if we should wait for a thread, <i>false</i> if it is safe to continue.
954 */
955 private boolean waitForThread() {
956 Thread active[] = new Thread[thread_group.activeCount()];
957 int size = thread_group.enumerate(active);
958 for(int i = 0; i < size; i++) {
959 if(active[i].getName().equals(GShell.GSHELL_BUILD) ||
960 active[i].getName().equals(GShell.GSHELL_IMPORT) ||
961 active[i].getName().equals(GShell.GSHELL_NEW)) {
962 return true;
963 }
964 }
965 return false;
966 }
967
968
969 /**Overridden from JFrame so we can exit safely when window is closed (or destroyed).
970 * @param event A <strong>WindowEvent</strong> containing information about the event that fired this call.
971 */
972 protected void processWindowEvent(WindowEvent event) {
973 if(event.getID() == WindowEvent.WINDOW_CLOSING) {
974 exit();
975 }
976 }
977 /** Listens to actions upon the menu bar, and if it detects a click over the help menu brings the help window to the front if it has become hidden.
978 */
979 private class MenuListenerImpl
980 implements MenuListener {
981 /** Called whenever a popup menu is hidden, but we don't care.
982 * @param e Some <strong>MenuEvent</strong> that we could care less about.
983 */
984 public void menuCanceled(MenuEvent e) {
985 }
986 /** Called whenever a menu header (ie button) becomes unselected, but we don't care.
987 * @param e Some <strong>MenuEvent</strong> that we could care less about.
988 */
989 public void menuDeselected(MenuEvent e) {
990 }
991 /** This method, when a menu is first opened, is the only one we respond to by bringing the help window to the front if possible, but only if there is a help window and the help menu is the one opening.
992 * @param e The <strong>MenuEvent</strong> whose source is checked.
993 */
994 public void menuSelected(MenuEvent e) {
995 if(e.getSource() == menu_bar.help) {
996 if(menu_bar.help.isSelected()) {
997 menu_bar.help.doClick(10);
998 }
999 }
1000 }
1001 }
1002 private class TabUpdater
1003 implements Runnable {
1004 private boolean ready = false;
1005 //private int browse_pos = -1;
1006 private int mirror_pos = -1;
1007 private int config_pos = -1;
1008 private int create_pos = -1;
1009 private int export_pos = -1;
1010 private int metaedit_pos = -1;
1011 //private int preview_pos = -1;
1012 private JTabbedPane tab_pane = null;
1013 public TabUpdater(JTabbedPane tab_pane, boolean ready) {
1014 this.ready = ready;
1015 this.tab_pane = tab_pane;
1016 //browse_pos = tab_pane.indexOfComponent(browser_pane);
1017 mirror_pos = tab_pane.indexOfComponent(mirror_pane);
1018 metaedit_pos = tab_pane.indexOfComponent(metaedit_pane);
1019 config_pos = tab_pane.indexOfComponent(config_pane);
1020 export_pos = tab_pane.indexOfComponent(export_pane);
1021 create_pos = tab_pane.indexOfComponent(create_pane);
1022 //preview_pos = tab_pane.indexOfComponent(preview_pane);
1023 }
1024
1025 public void run() {
1026// if(browse_pos != -1) {
1027// if(ready) {
1028// tab_pane.setEnabledAt(browse_pos, Gatherer.config.get("workflow.browse", false));
1029// }
1030// else {
1031// tab_pane.setEnabledAt(browse_pos, Gatherer.config.get("workflow.browse", true));
1032// }
1033// }
1034 if(mirror_pos != -1) {
1035 if(ready) {
1036 tab_pane.setEnabledAt(mirror_pos, Gatherer.config.get("workflow.mirror", false));
1037 }
1038 else {
1039 tab_pane.setEnabledAt(mirror_pos, Gatherer.config.get("workflow.mirror", true));
1040 }
1041 }
1042 if(metaedit_pos != -1) {
1043 tab_pane.setEnabledAt(metaedit_pos, ready && Gatherer.config.get("workflow.enrich", false));
1044 }
1045 if(config_pos != -1) {
1046 tab_pane.setEnabledAt(config_pos, ready && Gatherer.config.get("workflow.design", false) && Gatherer.config.getMode() > Configuration.ASSISTANT_MODE);
1047 }
1048 if(export_pos != -1) {
1049 tab_pane.setEnabledAt(export_pos, ready && Gatherer.config.get("workflow.export", false));
1050 }
1051 if(create_pos != -1) {
1052 tab_pane.setEnabledAt(create_pos, ready && Gatherer.config.get("workflow.create", false));
1053 }
1054// if(preview_pos != -1) {
1055// tab_pane.setEnabledAt(preview_pos, Gatherer.c_man != null && Gatherer.c_man.built() && Gatherer.config.get("workflow.preview", false));
1056// }
1057 }
1058 public void setReady(boolean ready) {
1059 this.ready = ready;
1060 }
1061 }
1062
1063 private class WorkflowUpdater
1064 implements Runnable {
1065 private boolean state;
1066 private String raw;
1067 public WorkflowUpdater(String raw, boolean state) {
1068 this.raw = raw;
1069 this.state = state;
1070 }
1071 public void run() {
1072 setTabEnabled(raw, state);
1073 }
1074 }
1075}
1076
1077
Note: See TracBrowser for help on using the repository browser.