source: trunk/gli/src/org/greenstone/gatherer/gui/CollectionPane.java@ 4529

Last change on this file since 4529 was 4514, checked in by jmt12, 21 years ago

Hopefully the 'view assigned metadata for... now works properly.

  • Property svn:keywords set to Author Date Id Revision
File size: 34.5 KB
Line 
1package org.greenstone.gatherer.gui;
2/**
3 *#########################################################################
4 *
5 * A component of the Gatherer application, part of the Greenstone digital
6 * library suite from the New Zealand Digital Library Project at the
7 * University of Waikato, New Zealand.
8 *
9 * <BR><BR>
10 *
11 * Author: John Thompson, Greenstone Digital Library, University of Waikato
12 *
13 * <BR><BR>
14 *
15 * Copyright (C) 1999 New Zealand Digital Library Project
16 *
17 * <BR><BR>
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * <BR><BR>
25 *
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
30 *
31 * <BR><BR>
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
36 *########################################################################
37 */
38import java.awt.*;
39import java.awt.event.*;
40import java.io.*;
41import java.util.*;
42import javax.swing.*;
43import javax.swing.event.*;
44import javax.swing.tree.*;
45import org.greenstone.gatherer.Gatherer;
46import org.greenstone.gatherer.file.FileNode;
47import org.greenstone.gatherer.file.FileOpenActionListener;
48import org.greenstone.gatherer.file.FileQueue;
49import org.greenstone.gatherer.file.FileSystemModel;
50import org.greenstone.gatherer.gui.Filter;
51import org.greenstone.gatherer.gui.GComboBox;
52import org.greenstone.gatherer.gui.tree.DragTree;
53import org.greenstone.gatherer.undo.UndoManager;
54import org.greenstone.gatherer.util.DragComponent;
55import org.greenstone.gatherer.util.DragGroup;
56import org.greenstone.gatherer.util.TreeSynchronizer;
57import org.greenstone.gatherer.util.Utility;
58import org.greenstone.gatherer.util.WinRegistry;
59/** The collection pane is analogous with a file manager. It is there that the user chooses which files to include in their collection and what structure the file hierarchy should take. The later aspect is not important for the Greenstone Suite, but is usefull for grouping files for ease of metadata markup. The view essentially consists of two file trees, one denoting the entire source workspace and the other the files within your collection. The trees themselves have a title bar at the top, a filter control at the bottom, and are coloured to indicate activity (grey for disabled). The remainder of the screen is taken by a status area, to indicate current file job progress during copying etc, and three buttons for controlling features of the view.
60 * @author John Thompson, Greenstone Digital Library, University of Waikato
61 * @version 2.3
62 */
63public class CollectionPane
64 extends JPanel
65 implements ActionListener, FocusListener {
66 /** The group encompassing all of the components available as drop targets for drag and drop actions. Required so that only one component renders the ghost and higlights itself as a target, which the other members are restored to their original, pristine, condition. */
67 private DragGroup group = null;
68 /** The tree showing the files within the collection. */
69 private DragTree collection_tree = null;
70 /** The tree showing the available source workspace. */
71 private DragTree workspace_tree = null;
72 /** The threaded queue that handles the actually movement of files, so that the gui remains responsive. */
73 private FileQueue file_queue = null;
74 /** The filter currently applied to the collection tree. */
75 private Filter collection_filter = null;
76 /** The filter currently applied to the workspace tree. */
77 private Filter workspace_filter = null;
78 /** The collection model which is used to build, and hold the data of, the collection tree. */
79 private TreeModel collection = null;
80 /** The GTree model used as the data source for the workspace tree. */
81 private TreeModel workspace = null;
82 /** The button used to cancel all pending file queue jobs. */
83 private JButton cancel_action = null;
84 /** The button used to create a new folder in the collection tree. */
85 private JButton new_folder = null;
86 /** The label shown at the top of the collection tree. */
87 private JLabel collection_label = null;
88 /** The label shown in the status area explaining the file apon which action is taking place. */
89 private JLabel filename_label = null;
90 /** The label shown explaining the current state of the file queue thread. */
91 private JLabel status_label = null;
92 /** The label at the top of the workspace tree. */
93 private JLabel workspace_label = null;
94 /** The panel that contains the collection tree. */
95 private JPanel collection_pane = null;
96 /** The panel that contains the various controls including the status area. */
97 private JPanel control_pane = null;
98 /** The panel that contains the workspace tree. */
99 private JPanel workspace_pane = null;
100 /** The scrollable area into which the collection tree is placed. */
101 private JScrollPane collection_scroll = null;
102 /** The scrollable area into which the workspace tree is placed. */
103 private JScrollPane workspace_scroll = null;
104 /** A split pane seperating the two trees, allowing for the screen real-estate for each to be changed. */
105 private JSplitPane tree_pane = null;
106 /** Text fragment arguments used to fill in phrases returned from the dictionary. */
107 private String args[] = null;
108 /** Ensures that expansion and selection events between collection trees based on the same model are synchronized. */
109 private TreeSynchronizer collection_tree_sync = null;
110 /** Ensures that expansion and selection events between workspace trees based on the same model are synchronized. */
111 private TreeSynchronizer workspace_tree_sync = null;
112 /** The button used to delete files, which also doubles as a drop target for files from the Trees. */
113 private UndoManager bin_button = null;
114 /** The default size of a label in the interface. */
115 static final private Dimension LABEL_SIZE = new Dimension(100,30);
116 /** The default size of a special mapping dialog. */
117 static final Dimension DIALOG_SIZE = new Dimension(400, 120);
118 /** The minimum size a gui component can become. */
119 static private Dimension MIN_SIZE = new Dimension( 90, 90);
120 /** The default size of the status area. */
121 static private Dimension STATUS_SIZE = new Dimension(450, 120);
122 /** The initial size of the trees. */
123 static private Dimension TREE_SIZE = new Dimension(400, 430);
124 /* Constructor.
125 * @param tree_sync Ensures that expansion events between like trees are synchronized.
126 * @see org.greenstone.gatherer.file.FileManager
127 * @see org.greenstone.gatherer.file.FileQueue
128 */
129 public CollectionPane(TreeSynchronizer workspace_tree_sync, TreeSynchronizer collection_tree_sync) {
130 this.group = new DragGroup();
131 this.file_queue = Gatherer.f_man.getQueue();
132 this.collection_tree_sync = collection_tree_sync;
133 this.workspace_tree_sync = workspace_tree_sync;
134
135 // Create components.
136 cancel_action = new JButton(Utility.getImage("big_stop.gif"));
137 cancel_action.addActionListener(this);
138 cancel_action.setEnabled(false);
139 cancel_action.setMinimumSize(MIN_SIZE);
140 cancel_action.setPreferredSize(MIN_SIZE);
141
142 new_folder = new JButton(Utility.getImage("folder.gif"));
143 new_folder.addActionListener(this);
144 new_folder.setEnabled(false);
145 new_folder.setMinimumSize(MIN_SIZE);
146 new_folder.setPreferredSize(MIN_SIZE);
147 }
148 /** Any implementation of ActionListener requires this method so that when an action is performed the appropriate effect can occur. In this case there are three valid possibilities. If the action occured on the recycle bin, then delete the current selection from the collection tree. If the action instead occured on the new folder button, then create a new folder under the current (single) selection in the collection tree. And finally if the cancel button was pressed, cancel the current, and remaining, jobs on the file queue. */
149 public void actionPerformed(ActionEvent event) {
150 // If a user has clicked on the bin button directly remove whatever
151 // files are selected in the active tree.
152 if(event.getSource() == bin_button) {
153 if(!bin_button.ignore()) {
154 // Find the active tree (you've made selections in).
155 DragTree tree = (DragTree) group.getActive();
156 // Fudge things a bit
157 group.setSource(tree);
158 // Determine the selection.
159 TreePath paths[] = tree.getSelectionPaths();
160 if(paths != null) {
161 FileNode[] source_nodes = new FileNode[paths.length];
162 for(int i = 0; i < paths.length; i++) {
163 source_nodes[i] = (FileNode)(paths[i].getLastPathComponent());
164 }
165 Gatherer.f_man.action(tree, source_nodes, bin_button, null);
166 }
167 }
168 }
169 // If a user has clicked on new_folder create a new folder under
170 // whatever node is selected.
171 else if(event.getSource() == new_folder && collection_tree != null) {
172 int count = collection_tree.getSelectionCount();
173 boolean error = false;
174 if(count == 1) {
175 TreePath path = collection_tree.getSelectionPath();
176 FileNode node = (FileNode) path.getLastPathComponent();
177 if(node.getAllowsChildren()) {
178 Gatherer.f_man.newFolder(collection_tree, node);
179 }
180 else {
181 // try the parent
182 FileNode parent = (FileNode)node.getParent();
183 if (parent!=null && parent.getAllowsChildren()) {
184 Gatherer.f_man.newFolder(collection_tree, parent);
185 } else {
186 error = true;
187 }
188 }
189 }
190 else {
191 error = true;
192 }
193 if(error) {
194 // instead of an error, we now create a new folder at the root
195 FileNode node = (FileNode) collection_tree.getModel().getRoot();
196 Gatherer.f_man.newFolder(collection_tree, node);
197 //JOptionPane.showMessageDialog(Gatherer.g_man, Gatherer.dictionary.get("FileActions.No_Parent_For_New_Folder"), Gatherer.dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
198 }
199 }
200 else if(event.getSource() == cancel_action) {
201 file_queue.cancelAction();
202 }
203 }
204 /** Called whenever a significant change occurs in the current collections state, such as a new collection being loaded or the current one being closed. Several actions must occur in the GUI to indicate this change to the user, such as en/disabling the collection tree.
205 * @param ready <i>true</i> if a collection is loaded and ready to be modified, <i>false</i> otherwise.
206 * @see org.greenstone.gatherer.Configuration
207 * @see org.greenstone.gatherer.Gatherer
208 * @see org.greenstone.gatherer.collection.CollectionManager
209 * @see org.greenstone.gatherer.gui.Coloring
210 * @see org.greenstone.gatherer.gui.Filter
211 * @see org.greenstone.gatherer.util.TreeSynchronizer
212 */
213 public void collectionChanged(boolean ready) {
214 // Try to retrieve the collections record set.
215 collection = Gatherer.c_man.getRecordSet();
216 if(collection != null) {
217 args = new String[1];
218 args[0] = Gatherer.c_man.getCollection().getName();
219 collection_label.setText(get("Collection", args));
220 collection_tree.setModel(collection);
221 collection_tree.repaint();
222 collection_filter.setBackground(Gatherer.config.getColor("coloring.collection_heading_background", false));
223 }
224 else {
225 String args[] = new String[1];
226 args[0] = get("Collection.No_Collection");
227 collection_label.setText(get("Collection", args));
228 args = null;
229 collection_tree.setModel(new DefaultTreeModel(new DefaultMutableTreeNode("Error")));
230 collection_filter.setBackground(Color.lightGray);
231 }
232
233 collection_tree.setEnabled(ready);
234 collection_filter.setEnabled(ready);
235
236 // Change the label at the top of collection tree.
237 setEnabled(collection_label, ready, Gatherer.config.getColor("coloring.collection_heading_foreground", false), Gatherer.config.getColor("coloring.collection_heading_background", false));
238 // Ensure that this tree view of the collection record set is synchronized with any others.
239 collection_tree_sync.add(collection_tree);
240
241 workspace = Gatherer.c_man.getWorkspace();
242 workspace_tree.setModel(workspace);
243 workspace_tree_sync.add(workspace_tree);
244
245 // Enable or disable the control buttons
246 bin_button.setEnabled(ready);
247 cancel_action.setEnabled(ready);
248 new_folder.setEnabled(ready);
249 }
250 /** Generates the pane on controls used to 'collect' files into the collection. Resposible for creating, connecting and laying out these controls. */
251 public void display() {
252 // Create Components.
253 KeyListenerImpl key_listener = new KeyListenerImpl();
254 MouseListenerImpl mouse_listener = new MouseListenerImpl();
255 this.addKeyListener(key_listener);
256
257 // Workspace Tree
258 workspace_pane = new JPanel();
259 workspace_pane.setMinimumSize(MIN_SIZE);
260 workspace_pane.setPreferredSize(TREE_SIZE);
261 workspace_pane.setSize(TREE_SIZE);
262
263 workspace_label = new JLabel(get("Workspace"));
264 workspace_label.setOpaque(true);
265 workspace_label.setBackground(Gatherer.config.getColor("coloring.workspace_heading_background", false));
266 workspace_label.setForeground(Gatherer.config.getColor("coloring.workspace_heading_foreground", false));
267
268 workspace = Gatherer.c_man.getWorkspace();
269 workspace_tree = new DragTree(Utility.WORKSPACE_TREE, workspace, null);
270 group.add(workspace_tree);
271 workspace_tree.addFocusListener(this);
272 workspace_tree.addKeyListener(key_listener);
273 workspace_tree.addMouseListener(mouse_listener);
274 workspace_tree.addMouseListener(Gatherer.g_man.foa_listener);
275 workspace_tree.addTreeExpansionListener(Gatherer.g_man.foa_listener);
276 workspace_tree.addTreeSelectionListener(file_queue);
277 workspace_tree.putClientProperty("JTree.lineStyle", "Angled");
278 workspace_tree.setBackgroundNonSelectionColor(Gatherer.config.getColor("coloring.workspace_tree_background", false));
279 workspace_tree.setTextNonSelectionColor(Gatherer.config.getColor("coloring.workspace_tree_foreground", false));
280 workspace_tree.setBackgroundSelectionColor(Gatherer.config.getColor("coloring.workspace_selection_background", false));
281 workspace_tree.setTextSelectionColor(Gatherer.config.getColor("coloring.workspace_selection_foreground", false));
282 workspace_tree.setRootVisible(false);
283
284 workspace_scroll = new JScrollPane(workspace_tree);
285
286 workspace_filter = Gatherer.g_man.getFilter(workspace_tree);
287 workspace_filter.setBackground(Gatherer.config.getColor("coloring.workspace_heading_background", false));
288 // Change the default colours of this filters combobox.
289 GComboBox fcb = workspace_filter.getComboBox();
290 fcb.setBackgroundNonSelectionColor(Gatherer.config.getColor("coloring.workspace_tree_background", false));
291 fcb.setTextNonSelectionColor(Gatherer.config.getColor("coloring.workspace_tree_foreground", false));
292 fcb.setBackgroundSelectionColor(Gatherer.config.getColor("coloring.workspace_selection_background", false));
293 fcb.setTextSelectionColor(Gatherer.config.getColor("coloring.workspace_selection_foreground", false));
294 fcb = null;
295
296 // Collection Tree
297 collection_pane = new JPanel();
298 collection_pane.setMinimumSize(MIN_SIZE);
299 collection_pane.setPreferredSize(TREE_SIZE);
300 collection_pane.setSize(TREE_SIZE);
301
302 args = new String[1];
303 args[0] = get("Collection.No_Collection");
304 collection_label = new JLabel(get("Collection", args));
305 collection_label.setOpaque(true);
306
307 collection = Gatherer.c_man.getRecordSet();
308 if(collection != null) {
309 collection_tree = new DragTree(Utility.COLLECTION_TREE, collection, null);
310 collection_tree.setEnabled(true);
311 }
312 else {
313 collection_tree = new DragTree(Utility.COLLECTION_TREE, null);
314 collection_tree.setEnabled(false);
315 }
316 group.add(collection_tree);
317 collection_tree.addFocusListener(this);
318 collection_tree.addKeyListener(key_listener);
319 collection_tree.addMouseListener(mouse_listener);
320 collection_tree.addMouseListener(Gatherer.g_man.foa_listener);
321 collection_tree.addTreeSelectionListener(file_queue);
322 collection_tree.addTreeExpansionListener(Gatherer.g_man.foa_listener);
323 collection_tree.putClientProperty("JTree.lineStyle", "Angled");
324 collection_tree.setBackgroundNonSelectionColor(Gatherer.config.getColor("coloring.collection_tree_background", false));
325 collection_tree.setTextNonSelectionColor(Gatherer.config.getColor("coloring.collection_tree_foreground", false));
326 collection_tree.setBackgroundSelectionColor(Gatherer.config.getColor("coloring.collection_selection_background", false));
327 collection_tree.setTextSelectionColor(Gatherer.config.getColor("coloring.collection_selection_foreground", false));
328 collection_tree.setRootVisible(false);
329
330 collection_scroll = new JScrollPane(collection_tree);
331
332 collection_filter = Gatherer.g_man.getFilter(collection_tree);
333 if(collection != null) {
334 collection_filter.setBackground(Gatherer.config.getColor("coloring.collection_heading_background", false));
335 }
336 else {
337 collection_filter.setBackground(Color.lightGray);
338 }
339 // Change the default colours of this filters combobox.
340 fcb = collection_filter.getComboBox();
341 fcb.setBackgroundNonSelectionColor(Gatherer.config.getColor("coloring.collection_tree_background", false));
342 fcb.setTextNonSelectionColor(Gatherer.config.getColor("coloring.collection_tree_foreground", false));
343 fcb.setBackgroundSelectionColor(Gatherer.config.getColor("coloring.collection_selection_background", false));
344 fcb.setTextSelectionColor(Gatherer.config.getColor("coloring.collection_selection_foreground", false));
345 fcb = null;
346
347 tree_pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
348
349 // Status pane
350 control_pane = new JPanel();
351
352 JPanel inner_pane = new JPanel();
353 inner_pane.setSize(STATUS_SIZE);
354
355 JPanel file_pane = new JPanel();
356 file_pane.setBackground(Color.white);
357 //JPanel job_pane = new JPanel();
358 //job_pane.setBackground(Color.white);
359 JPanel progress_pane = new JPanel();
360 progress_pane.setBackground(Color.white);
361 JLabel file_status = file_queue.getFileStatus();
362 //JLabel job_status = job_queue.getJobStatus();
363
364 JProgressBar progress = file_queue.getProgress();
365
366 JPanel button_pane = new JPanel();
367
368 bin_button = Gatherer.c_man.undo;
369 bin_button.addActionListener(this);
370 bin_button.setEnabled(false);
371 bin_button.setMinimumSize(MIN_SIZE);
372 bin_button.setPreferredSize(MIN_SIZE);
373 group.add(bin_button);
374
375 // Layout Components.
376 workspace_pane.setLayout(new BorderLayout());
377 workspace_pane.add(workspace_label, BorderLayout.NORTH);
378 workspace_pane.add(workspace_scroll, BorderLayout.CENTER);
379 workspace_pane.add(workspace_filter, BorderLayout.SOUTH);
380
381 collection_pane.setLayout(new BorderLayout());
382 collection_pane.add(collection_label, BorderLayout.NORTH);
383 collection_pane.add(collection_scroll, BorderLayout.CENTER);
384 collection_pane.add(collection_filter, BorderLayout.SOUTH);
385
386 tree_pane.add(workspace_pane, JSplitPane.LEFT);
387 tree_pane.add(collection_pane, JSplitPane.RIGHT);
388 tree_pane.setDividerLocation(TREE_SIZE.width - 10);
389
390 //job_pane.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
391 //job_pane.setLayout(new BorderLayout());
392 //job_pane.add(job_status, BorderLayout.CENTER);
393
394 file_pane.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
395 file_pane.setLayout(new BorderLayout());
396 file_pane.add(file_status, BorderLayout.CENTER);
397
398 progress_pane.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
399 progress_pane.setLayout(new BorderLayout());
400 progress_pane.add(progress, BorderLayout.CENTER);
401
402 inner_pane.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(10,10,10,10), BorderFactory.createLoweredBevelBorder()));
403 inner_pane.setLayout(new GridLayout(2,1));
404 //inner_pane.add(job_pane);
405 inner_pane.add(file_pane);
406 inner_pane.add(progress_pane);
407
408 button_pane.add(cancel_action);
409 button_pane.add(new_folder);
410 button_pane.add(bin_button);
411
412 control_pane.setLayout(new BorderLayout());
413 //control_pane.add(new_folder, BorderLayout.WEST);
414 control_pane.add(inner_pane, BorderLayout.CENTER);
415 control_pane.add(button_pane, BorderLayout.EAST);
416
417 this.setLayout(new BorderLayout());
418 this.add(tree_pane, BorderLayout.CENTER);
419 this.add(control_pane, BorderLayout.SOUTH);
420 }
421 /** This method ensures that a certain tree path is visible and selected within the collection tree, expanding nodes if necessary. If the method is successful the bounds of the new selection are returned. */
422 public Rectangle expandPath(TreePath path) {
423 collection_tree.setImmediate(true);
424 collection_tree.scrollPathToVisible(path);
425 collection_tree.setSelectionPath(path);
426 collection_tree.setImmediate(false);
427 return collection_tree.getRowBounds(collection_tree.getRowForPath(path));
428 }
429 /** Called whenever this pane gains focus, this method ensures that the various tree renderers are correctly colouring the tree (as these settings sometimes get lost).
430 * @param event A <strong>FocusEvent</strong> containing details about the focus action performed.
431 */
432 public void focusGained(FocusEvent event) {
433 DefaultTreeCellRenderer def = new DefaultTreeCellRenderer();
434 DefaultTreeCellRenderer w = (DefaultTreeCellRenderer)workspace_tree.getCellRenderer();
435 DefaultTreeCellRenderer c = (DefaultTreeCellRenderer)collection_tree.getCellRenderer();
436 if(event.getSource() == workspace_tree) {
437 w.setBackgroundSelectionColor(def.getBackgroundSelectionColor());
438 c.setBackgroundSelectionColor(Color.lightGray);
439 }
440 else if(event.getSource() == collection_tree) {
441 c.setBackgroundSelectionColor(def.getBackgroundSelectionColor());
442 w.setBackgroundSelectionColor(Color.lightGray);
443 }
444 repaint();
445 // Update the menubars idea of whats been selected
446 if(collection_tree != null) {
447 if(collection_tree.isSelectionEmpty()) {
448 Gatherer.g_man.menu_bar.setMetaAuditSuffix(null);
449 }
450 else {
451 Gatherer.g_man.menu_bar.setMetaAuditSuffix(collection_tree.getSelectionDetails());
452 }
453 }
454 }
455 /** Implementation side-effect, not used in any way.
456 * @param event A <strong>FocusEvent</strong> containing details about the focus action performed.
457 */
458 public void focusLost(FocusEvent event) {
459 }
460 /** Retrieve a list of the currently selected file records in the active tree. */
461 public FileNode[] getSelected() {
462 TreePath paths[] = collection_tree.getSelectionPaths();
463 FileNode records[] = null;
464 if(paths != null) {
465 records = new FileNode[paths.length];
466 for(int i = 0; i < records.length; i++) {
467 records[i] = (FileNode) paths[i].getLastPathComponent();
468 }
469 }
470 return records;
471 }
472
473 public String getSelectionDetails() {
474 return collection_tree.getSelectionDetails();
475 }
476
477 public DragTree getWorkspaceTree() {
478 return workspace_tree;
479 }
480
481 public void refreshTrees() {
482 collection_tree.refresh(null);
483 }
484
485 /** Retrieve a phrase from the dictionary based on the key.
486 * @param key A <strong>String</strong> used to uniquely identify the phrase to be retrieved.
487 */
488 private String get(String key) {
489 return get(key, null);
490 }
491 /** Retrieve a phrase from the dictionary based on the key, and filled in using text fragments from an arguments array.
492 * @param key A <strong>String</strong> used to uniquely identify the phrase to be retrieved.
493 * @param args A <strong>String[]</strong> used as parameters to be inserted in the phrase retrieved.
494 * @see org.greenstone.gatherer.Dictionary
495 * @see org.greenstone.gatherer.Gatherer
496 */
497 private String get(String key, String args[]) {
498 if(key.indexOf('.') == -1) {
499 key = "Collection." + key;
500 }
501 return Gatherer.dictionary.get(key, args);
502 }
503 /** Used to set the enabled state, and hence the colouring, of the two tree labels.
504 * @param label The <strong>JLabel</strong> to be affected.
505 * @param state <i>true</i> for enabled, i.e. when a collection is ready, <i>false</i> otherwise.
506 * @param foreground The <strong>Color</strong> to make the foreground text of the label when enabled.
507 * @param background The <strong>Color</strong> to make the background of the label when enabled.
508 */
509 private void setEnabled(JLabel label, boolean state, Color foreground, Color background) {
510 ///ystem.err.println("Setting the label color to state " + state);
511 if(state) {
512 label.setBackground(background);
513 label.setForeground(foreground);
514 }
515 else {
516 label.setBackground(Color.lightGray);
517 label.setForeground(Color.black);
518 }
519 label.repaint();
520 ///ystem.err.println("Color is now " + label.getBackground());
521 }
522 /** When a user right-clicks within the trees on the collection pane view they are presented with a small popup menu of context based options. This class provides such functionality.
523 */
524 private class GPopupMenu
525 extends JPopupMenu
526 implements ActionListener {
527 /** The tree over which the right click action occured. */
528 private DragTree tree = null;
529 /** The file record over which the right click action occured, if any. */
530 private FileNode node = null;
531 /** A menu item enabled if a delete action is appropriate in the menus current context. */
532 private JMenuItem delete = null;
533 /** A menu item enabled if a special directory mapping is appropriate in the menus current context. */
534 private JMenuItem map = null;
535 /** A menu item enabled if a new folder action is appropriate in the menus current context. */
536 private JMenuItem new_folder = null;
537 /** A menu item enabled if a show meta-audit dialog action is appropriate in the menus current context. */
538 private JMenuItem show_metaaudit = null;
539 /** A menu item allowing a user to unmap a special directory, if and only if a special directory is selected. */
540 private JMenuItem unmap = null;
541 /** Constructor. */
542 public GPopupMenu(DragTree tree, MouseEvent event) {
543 super();
544 this.tree = tree;
545 TreePath path = tree.getClosestPathForLocation(event.getX(), event.getY());
546 node = (FileNode)path.getLastPathComponent();
547 // Set Options based on selection and tree
548 if(tree.getSelectionCount() != 0 && tree == collection_tree) {
549 show_metaaudit = new JMenuItem(get("Menu.Metadata_View") + " " + collection_tree.getSelectionDetails(), KeyEvent.VK_V);
550 show_metaaudit.addActionListener(this);
551 add(show_metaaudit);
552 }
553 if(tree == collection_tree && node != null && node.getFile() != null && node.getFile().isDirectory() && !node.isReadOnly()) {
554 new_folder = new JMenuItem(get("New_Folder"), KeyEvent.VK_N);
555 new_folder.addActionListener(this);
556 add(new_folder);
557 add(new JSeparator());
558 }
559 if(node == null || (node != null && !node.isReadOnly())) {
560 delete = new JMenuItem(get("Delete"), KeyEvent.VK_D);
561 delete.addActionListener(this);
562 add(delete);
563 }
564 if(tree == workspace_tree && node != null && !node.isLeaf()) {
565 String node_name = node.toString();
566 FileNode root = (FileNode) tree.getModel().getRoot();
567 if(!node_name.equals(get("Tree.World")) && !node_name.equals(get("Tree.Root")) && !node_name.equals(get("Tree.Public")) && !node_name.equals(get("Tree.Private"))) {
568 // You can unmap 1st level nodes.
569 if(root.getIndex(node) != -1) {
570 unmap = new JMenuItem(get("MappingPrompt.Unmap"), KeyEvent.VK_U);
571 unmap.addActionListener(this);
572 add(unmap);
573 }
574 // Or map any other level directories.
575 else {
576 map = new JMenuItem(get("MappingPrompt.Map"), KeyEvent.VK_M);
577 map.addActionListener(this);
578 add(map);
579 }
580 }
581 }
582 show(tree, event.getX(), event.getY());
583 }
584 /** Called whenever one of the menu items is actioned apon, this method then causes the appropriate effect. */
585 public void actionPerformed(ActionEvent event) {
586 if(event.getSource() == delete) {
587 // Retrieve the selection. Of course this gets a bit tricky as the user may have right clicked over a node not in the current selection, in which case we remove only that node.
588 TreePath[] selection_paths = tree.getSelectionPaths();
589 if(selection_paths != null) {
590 FileNode[] source_nodes = new FileNode[selection_paths.length];
591 boolean found = false;
592 for(int i = 0; i < selection_paths.length; i++) {
593 source_nodes[i] = (FileNode) selection_paths[i].getLastPathComponent();
594 if(node != null) {
595 found = found || source_nodes[i].equals(node);
596 }
597 }
598 if(node != null && !found) {
599 source_nodes = null;
600 source_nodes = new FileNode[1];
601 source_nodes[0] = node;
602 }
603 // Fire a delete action
604 Gatherer.f_man.action(tree, source_nodes, bin_button, null);
605 source_nodes = null;
606 }
607 selection_paths = null;
608 }
609 else if(event.getSource() == map && node != null) {
610 MappingPrompt mp = new MappingPrompt(node.getFile());
611 mp.destroy();
612 mp = null;
613 }
614 else if(event.getSource() == new_folder && node != null) {
615 Gatherer.f_man.newFolder(tree, node);
616 }
617 else if(event.getSource() == show_metaaudit) {
618 Gatherer.g_man.showMetaAuditBox();
619 }
620 else if(event.getSource() == unmap && node != null) {
621 Gatherer.c_man.removeDirectoryMapping(node);
622 }
623 }
624 /** Retrieve a phrase from the dictionary based on the key.
625 * @param key A <strong>String</strong> used to uniquely identify the phrase to be retrieved.
626 */
627 private String get(String key) {
628 return get(key, null);
629 }
630 /** Retrieve a phrase from the dictionary based on the key, and filled in using text fragments from an arguments array.
631 * @param key A <strong>String</strong> used to uniquely identify the phrase to be retrieved.
632 * @param args A <strong>String[]</strong> used as parameters to be inserted in the phrase retrieved.
633 * @see org.greenstone.gatherer.Dictionary
634 * @see org.greenstone.gatherer.Gatherer
635 */
636 private String get(String key, String args[]) {
637 if(key.indexOf('.') == -1) {
638 key = "CollectionPopupMenu." + key;
639 }
640 return Gatherer.dictionary.get(key, args);
641 }
642 }
643 /** This class listens for certain key presses, such as [Enter] or [Delete], and responds appropriately. */
644 private class KeyListenerImpl
645 extends KeyAdapter {
646 /** Called whenever a key that was pressed is released, it is this action that will cause the desired effects (this allows for the key event itself to be processed prior to this listener dealing with it). */
647 public void keyReleased(KeyEvent event) {
648 ///ystem.err.println("Key Release detected. " + event.getKeyCode());
649 if(event.getKeyCode() == KeyEvent.VK_DELETE) {
650 // Get the selected files from the tree and removal them using the default dnd removal method.
651 // Find the active tree (you've made selections in).
652 DragTree tree = (DragTree) group.getActive();
653 // Fudge things a bit
654 group.setSource(tree);
655 // Determine the selection.
656 TreePath paths[] = tree.getSelectionPaths();
657 if(paths != null) {
658 FileNode[] source_nodes = new FileNode[paths.length];
659 for(int i = 0; i < source_nodes.length; i++) {
660 source_nodes[i] = (FileNode) paths[i].getLastPathComponent();
661 }
662 Gatherer.f_man.action(tree, source_nodes, bin_button, null);
663 source_nodes = null;
664 }
665 }
666 else if(event.getKeyCode() == KeyEvent.VK_ENTER) {
667 // Get the first selected file.
668 DragTree tree = (DragTree)event.getSource();
669 TreePath path = tree.getSelectionPath();
670 if(path != null) {
671 File file = ((FileNode)path.getLastPathComponent()).getFile();
672 if(file.isFile()) {
673 Gatherer.self.spawnApplication(file);
674 }
675 }
676 }
677 }
678 }
679
680 /** This provides a small prompt for gathering addition details about a special directory mapping such as its symbolic name. */
681 private class MappingPrompt
682 extends JDialog
683 implements ActionListener, KeyListener {
684 private boolean cancelled = false;
685 private JButton cancel_button = null;
686 private JButton ok_button = null;
687 private JTextField name_field = null;
688 public MappingPrompt(File file) {
689 super(Gatherer.g_man);
690 setModal(true);
691 setSize(DIALOG_SIZE);
692 setTitle(get("MappingPrompt.Title"));
693 // Creation
694 JPanel content_pane = (JPanel) getContentPane();
695 JPanel center_pane = new JPanel();
696 JPanel file_pane = new JPanel();
697 JLabel file_label = new JLabel(get("MappingPrompt.File"));
698 file_label.setPreferredSize(LABEL_SIZE);
699 JLabel file_field = new JLabel(file.getAbsolutePath());
700 JPanel name_pane = new JPanel();
701 JLabel name_label = new JLabel(get("MappingPrompt.Name"));
702 name_label.setPreferredSize(LABEL_SIZE);
703 name_field = new JTextField(file.getName());
704 JPanel button_pane = new JPanel();
705 ok_button = new JButton(get("General.OK"));
706 ok_button.setEnabled(name_field.getText().length() > 0);
707 cancel_button = new JButton(get("General.Cancel"));
708 // Connection
709 cancel_button.addActionListener(this);
710 ok_button.addActionListener(this);
711 name_field.addKeyListener(this);
712 // Layout
713 file_pane.setLayout(new BorderLayout());
714 file_pane.add(file_label, BorderLayout.WEST);
715 file_pane.add(file_field, BorderLayout.CENTER);
716
717 name_pane.setLayout(new BorderLayout());
718 name_pane.add(name_label, BorderLayout.WEST);
719 name_pane.add(name_field, BorderLayout.CENTER);
720
721 center_pane.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
722 center_pane.setLayout(new GridLayout(2,1,5,5));
723 center_pane.add(file_pane);
724 center_pane.add(name_pane);
725
726 button_pane.setLayout(new GridLayout(1,2,5,5));
727 button_pane.add(ok_button);
728 button_pane.add(cancel_button);
729
730 content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
731 content_pane.setLayout(new BorderLayout());
732 content_pane.add(center_pane, BorderLayout.CENTER);
733 content_pane.add(button_pane, BorderLayout.SOUTH);
734 // Display
735 Dimension screen_size = Gatherer.config.screen_size;
736 setLocation((screen_size.width - DIALOG_SIZE.width) / 2, (screen_size.height - DIALOG_SIZE.height) / 2);
737 show();
738 // If not cancelled create mapping.
739 if(!cancelled) {
740 Gatherer.c_man.addDirectoryMapping(name_field.getText(), file);
741 }
742 }
743 public void actionPerformed(ActionEvent event) {
744 if(event.getSource() == cancel_button) {
745 cancelled = true;
746 }
747 dispose();
748 }
749 public void destroy() {
750 cancel_button = null;
751 ok_button = null;
752 name_field = null;
753 }
754 public void keyPressed(KeyEvent event) {
755 }
756 public void keyReleased(KeyEvent event) {
757 ok_button.setEnabled(name_field.getText().length() > 0);
758
759 }
760 public void keyTyped(KeyEvent event) {
761 }
762 }
763 /** This class listens for mouse clicks and responds right mouse button clicks (popup menu). */
764 private class MouseListenerImpl
765 extends MouseAdapter {
766 /** Any subclass of MouseAdapter can override this method to respond to mouse click events. In this case we want to open a pop-up menu if we detect a right mouse click over one of our registered components, and start an external application if someone double clicks on a certain file record. */
767 public void mouseClicked(MouseEvent event) {
768 if(SwingUtilities.isRightMouseButton(event)) {
769 new GPopupMenu((DragTree)event.getSource(), event);
770 }
771 }
772 }
773}
Note: See TracBrowser for help on using the repository browser.