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

Last change on this file since 4448 was 4375, checked in by kjdon, 21 years ago

and now if a file is selected, it tries to create a folder as a child of the parent. if that is unsuccessful, it goes back to teh root

  • Property svn:keywords set to Author Date Id Revision
File size: 34.2 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 }
446 /** Implementation side-effect, not used in any way.
447 * @param event A <strong>FocusEvent</strong> containing details about the focus action performed.
448 */
449 public void focusLost(FocusEvent event) {
450 }
451 /** Retrieve a list of the currently selected file records in the active tree. */
452 public FileNode[] getSelected() {
453 TreePath paths[] = collection_tree.getSelectionPaths();
454 FileNode records[] = null;
455 if(paths != null) {
456 records = new FileNode[paths.length];
457 for(int i = 0; i < records.length; i++) {
458 records[i] = (FileNode) paths[i].getLastPathComponent();
459 }
460 }
461 return records;
462 }
463
464 public String getSelectionDetails() {
465 return collection_tree.getSelectionDetails();
466 }
467
468 public DragTree getWorkspaceTree() {
469 return workspace_tree;
470 }
471
472 public void refreshTrees() {
473 collection_tree.refresh(null);
474 }
475
476 /** Retrieve a phrase from the dictionary based on the key.
477 * @param key A <strong>String</strong> used to uniquely identify the phrase to be retrieved.
478 */
479 private String get(String key) {
480 return get(key, null);
481 }
482 /** Retrieve a phrase from the dictionary based on the key, and filled in using text fragments from an arguments array.
483 * @param key A <strong>String</strong> used to uniquely identify the phrase to be retrieved.
484 * @param args A <strong>String[]</strong> used as parameters to be inserted in the phrase retrieved.
485 * @see org.greenstone.gatherer.Dictionary
486 * @see org.greenstone.gatherer.Gatherer
487 */
488 private String get(String key, String args[]) {
489 if(key.indexOf('.') == -1) {
490 key = "Collection." + key;
491 }
492 return Gatherer.dictionary.get(key, args);
493 }
494 /** Used to set the enabled state, and hence the colouring, of the two tree labels.
495 * @param label The <strong>JLabel</strong> to be affected.
496 * @param state <i>true</i> for enabled, i.e. when a collection is ready, <i>false</i> otherwise.
497 * @param foreground The <strong>Color</strong> to make the foreground text of the label when enabled.
498 * @param background The <strong>Color</strong> to make the background of the label when enabled.
499 */
500 private void setEnabled(JLabel label, boolean state, Color foreground, Color background) {
501 ///ystem.err.println("Setting the label color to state " + state);
502 if(state) {
503 label.setBackground(background);
504 label.setForeground(foreground);
505 }
506 else {
507 label.setBackground(Color.lightGray);
508 label.setForeground(Color.black);
509 }
510 label.repaint();
511 ///ystem.err.println("Color is now " + label.getBackground());
512 }
513 /** 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.
514 */
515 private class GPopupMenu
516 extends JPopupMenu
517 implements ActionListener {
518 /** The tree over which the right click action occured. */
519 private DragTree tree = null;
520 /** The file record over which the right click action occured, if any. */
521 private FileNode node = null;
522 /** A menu item enabled if a delete action is appropriate in the menus current context. */
523 private JMenuItem delete = null;
524 /** A menu item enabled if a special directory mapping is appropriate in the menus current context. */
525 private JMenuItem map = null;
526 /** A menu item enabled if a new folder action is appropriate in the menus current context. */
527 private JMenuItem new_folder = null;
528 /** A menu item enabled if a show meta-audit dialog action is appropriate in the menus current context. */
529 private JMenuItem show_metaaudit = null;
530 /** A menu item allowing a user to unmap a special directory, if and only if a special directory is selected. */
531 private JMenuItem unmap = null;
532 /** Constructor. */
533 public GPopupMenu(DragTree tree, MouseEvent event) {
534 super();
535 this.tree = tree;
536 TreePath path = tree.getClosestPathForLocation(event.getX(), event.getY());
537 node = (FileNode)path.getLastPathComponent();
538 // Set Options based on selection and tree
539 if(tree.getSelectionCount() != 0 && tree == collection_tree) {
540 show_metaaudit = new JMenuItem(get("Menu.Metadata_View") + " " + collection_tree.getSelectionDetails(), KeyEvent.VK_V);
541 show_metaaudit.addActionListener(this);
542 add(show_metaaudit);
543 }
544 if(tree == collection_tree && node != null && node.getFile() != null && node.getFile().isDirectory() && !node.isReadOnly()) {
545 new_folder = new JMenuItem(get("New_Folder"), KeyEvent.VK_N);
546 new_folder.addActionListener(this);
547 add(new_folder);
548 add(new JSeparator());
549 }
550 if(node == null || (node != null && !node.isReadOnly())) {
551 delete = new JMenuItem(get("Delete"), KeyEvent.VK_D);
552 delete.addActionListener(this);
553 add(delete);
554 }
555 if(tree == workspace_tree && node != null && !node.isLeaf()) {
556 String node_name = node.toString();
557 FileNode root = (FileNode) tree.getModel().getRoot();
558 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"))) {
559 // You can unmap 1st level nodes.
560 if(root.getIndex(node) != -1) {
561 unmap = new JMenuItem(get("MappingPrompt.Unmap"), KeyEvent.VK_U);
562 unmap.addActionListener(this);
563 add(unmap);
564 }
565 // Or map any other level directories.
566 else {
567 map = new JMenuItem(get("MappingPrompt.Map"), KeyEvent.VK_M);
568 map.addActionListener(this);
569 add(map);
570 }
571 }
572 }
573 show(tree, event.getX(), event.getY());
574 }
575 /** Called whenever one of the menu items is actioned apon, this method then causes the appropriate effect. */
576 public void actionPerformed(ActionEvent event) {
577 if(event.getSource() == delete) {
578 // 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.
579 TreePath[] selection_paths = tree.getSelectionPaths();
580 if(selection_paths != null) {
581 FileNode[] source_nodes = new FileNode[selection_paths.length];
582 boolean found = false;
583 for(int i = 0; i < selection_paths.length; i++) {
584 source_nodes[i] = (FileNode) selection_paths[i].getLastPathComponent();
585 if(node != null) {
586 found = found || source_nodes[i].equals(node);
587 }
588 }
589 if(node != null && !found) {
590 source_nodes = null;
591 source_nodes = new FileNode[1];
592 source_nodes[0] = node;
593 }
594 // Fire a delete action
595 Gatherer.f_man.action(tree, source_nodes, bin_button, null);
596 source_nodes = null;
597 }
598 selection_paths = null;
599 }
600 else if(event.getSource() == map && node != null) {
601 MappingPrompt mp = new MappingPrompt(node.getFile());
602 mp.destroy();
603 mp = null;
604 }
605 else if(event.getSource() == new_folder && node != null) {
606 Gatherer.f_man.newFolder(tree, node);
607 }
608 else if(event.getSource() == show_metaaudit) {
609 Gatherer.g_man.showMetaAuditBox();
610 }
611 else if(event.getSource() == unmap && node != null) {
612 Gatherer.c_man.removeDirectoryMapping(node);
613 }
614 }
615 /** Retrieve a phrase from the dictionary based on the key.
616 * @param key A <strong>String</strong> used to uniquely identify the phrase to be retrieved.
617 */
618 private String get(String key) {
619 return get(key, null);
620 }
621 /** Retrieve a phrase from the dictionary based on the key, and filled in using text fragments from an arguments array.
622 * @param key A <strong>String</strong> used to uniquely identify the phrase to be retrieved.
623 * @param args A <strong>String[]</strong> used as parameters to be inserted in the phrase retrieved.
624 * @see org.greenstone.gatherer.Dictionary
625 * @see org.greenstone.gatherer.Gatherer
626 */
627 private String get(String key, String args[]) {
628 if(key.indexOf('.') == -1) {
629 key = "CollectionPopupMenu." + key;
630 }
631 return Gatherer.dictionary.get(key, args);
632 }
633 }
634 /** This class listens for certain key presses, such as [Enter] or [Delete], and responds appropriately. */
635 private class KeyListenerImpl
636 extends KeyAdapter {
637 /** 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). */
638 public void keyReleased(KeyEvent event) {
639 ///ystem.err.println("Key Release detected. " + event.getKeyCode());
640 if(event.getKeyCode() == KeyEvent.VK_DELETE) {
641 // Get the selected files from the tree and removal them using the default dnd removal method.
642 // Find the active tree (you've made selections in).
643 DragTree tree = (DragTree) group.getActive();
644 // Fudge things a bit
645 group.setSource(tree);
646 // Determine the selection.
647 TreePath paths[] = tree.getSelectionPaths();
648 if(paths != null) {
649 FileNode[] source_nodes = new FileNode[paths.length];
650 for(int i = 0; i < source_nodes.length; i++) {
651 source_nodes[i] = (FileNode) paths[i].getLastPathComponent();
652 }
653 Gatherer.f_man.action(tree, source_nodes, bin_button, null);
654 source_nodes = null;
655 }
656 }
657 else if(event.getKeyCode() == KeyEvent.VK_ENTER) {
658 // Get the first selected file.
659 DragTree tree = (DragTree)event.getSource();
660 TreePath path = tree.getSelectionPath();
661 if(path != null) {
662 File file = ((FileNode)path.getLastPathComponent()).getFile();
663 if(file.isFile()) {
664 Gatherer.self.spawnApplication(file);
665 }
666 }
667 }
668 }
669 }
670
671 /** This provides a small prompt for gathering addition details about a special directory mapping such as its symbolic name. */
672 private class MappingPrompt
673 extends JDialog
674 implements ActionListener, KeyListener {
675 private boolean cancelled = false;
676 private JButton cancel_button = null;
677 private JButton ok_button = null;
678 private JTextField name_field = null;
679 public MappingPrompt(File file) {
680 super(Gatherer.g_man);
681 setModal(true);
682 setSize(DIALOG_SIZE);
683 setTitle(get("MappingPrompt.Title"));
684 // Creation
685 JPanel content_pane = (JPanel) getContentPane();
686 JPanel center_pane = new JPanel();
687 JPanel file_pane = new JPanel();
688 JLabel file_label = new JLabel(get("MappingPrompt.File"));
689 file_label.setPreferredSize(LABEL_SIZE);
690 JLabel file_field = new JLabel(file.getAbsolutePath());
691 JPanel name_pane = new JPanel();
692 JLabel name_label = new JLabel(get("MappingPrompt.Name"));
693 name_label.setPreferredSize(LABEL_SIZE);
694 name_field = new JTextField(file.getName());
695 JPanel button_pane = new JPanel();
696 ok_button = new JButton(get("General.OK"));
697 ok_button.setEnabled(name_field.getText().length() > 0);
698 cancel_button = new JButton(get("General.Cancel"));
699 // Connection
700 cancel_button.addActionListener(this);
701 ok_button.addActionListener(this);
702 name_field.addKeyListener(this);
703 // Layout
704 file_pane.setLayout(new BorderLayout());
705 file_pane.add(file_label, BorderLayout.WEST);
706 file_pane.add(file_field, BorderLayout.CENTER);
707
708 name_pane.setLayout(new BorderLayout());
709 name_pane.add(name_label, BorderLayout.WEST);
710 name_pane.add(name_field, BorderLayout.CENTER);
711
712 center_pane.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
713 center_pane.setLayout(new GridLayout(2,1,5,5));
714 center_pane.add(file_pane);
715 center_pane.add(name_pane);
716
717 button_pane.setLayout(new GridLayout(1,2,5,5));
718 button_pane.add(ok_button);
719 button_pane.add(cancel_button);
720
721 content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
722 content_pane.setLayout(new BorderLayout());
723 content_pane.add(center_pane, BorderLayout.CENTER);
724 content_pane.add(button_pane, BorderLayout.SOUTH);
725 // Display
726 Dimension screen_size = Gatherer.config.screen_size;
727 setLocation((screen_size.width - DIALOG_SIZE.width) / 2, (screen_size.height - DIALOG_SIZE.height) / 2);
728 show();
729 // If not cancelled create mapping.
730 if(!cancelled) {
731 Gatherer.c_man.addDirectoryMapping(name_field.getText(), file);
732 }
733 }
734 public void actionPerformed(ActionEvent event) {
735 if(event.getSource() == cancel_button) {
736 cancelled = true;
737 }
738 dispose();
739 }
740 public void destroy() {
741 cancel_button = null;
742 ok_button = null;
743 name_field = null;
744 }
745 public void keyPressed(KeyEvent event) {
746 }
747 public void keyReleased(KeyEvent event) {
748 ok_button.setEnabled(name_field.getText().length() > 0);
749
750 }
751 public void keyTyped(KeyEvent event) {
752 }
753 }
754 /** This class listens for mouse clicks and responds right mouse button clicks (popup menu). */
755 private class MouseListenerImpl
756 extends MouseAdapter {
757 /** 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. */
758 public void mouseClicked(MouseEvent event) {
759 if(SwingUtilities.isRightMouseButton(event)) {
760 new GPopupMenu((DragTree)event.getSource(), event);
761 }
762 }
763 }
764}
Note: See TracBrowser for help on using the repository browser.