source: trunk/gli/src/org/greenstone/gatherer/gui/GatherPane.java@ 11315

Last change on this file since 11315 was 11315, checked in by kjdon, 18 years ago

added in replace option to collection tree menu

  • Property svn:keywords set to Author Date Id Revision
File size: 38.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.event.*;
41import java.io.*;
42import java.util.*;
43import javax.swing.*;
44import javax.swing.event.*;
45import javax.swing.tree.*;
46import org.greenstone.gatherer.Configuration;
47import org.greenstone.gatherer.DebugStream;
48import org.greenstone.gatherer.Dictionary;
49import org.greenstone.gatherer.Gatherer;
50import org.greenstone.gatherer.collection.CollectionTree;
51import org.greenstone.gatherer.collection.CollectionTreeNode;
52import org.greenstone.gatherer.file.FileNode;
53import org.greenstone.gatherer.file.FileOpenActionListener;
54import org.greenstone.gatherer.file.FileQueue;
55import org.greenstone.gatherer.file.FileSystemModel;
56import org.greenstone.gatherer.file.RecycleBin;
57import org.greenstone.gatherer.file.WorkspaceTree;
58import org.greenstone.gatherer.file.WorkspaceTreeNode;
59import org.greenstone.gatherer.gui.tree.DragTree;
60import org.greenstone.gatherer.gui.ExplodeMetadataPrompt;
61import org.greenstone.gatherer.util.DragComponent;
62import org.greenstone.gatherer.util.DragGroup;
63import org.greenstone.gatherer.util.JarTools;
64import org.greenstone.gatherer.util.TreeSynchronizer;
65import org.greenstone.gatherer.util.Utility;
66
67/** 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.
68 * @author John Thompson, Greenstone Digital Library, University of Waikato
69 * @version 2.3
70 */
71public class GatherPane
72 extends JPanel
73 implements ActionListener, FocusListener {
74 /** 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. */
75 private DragGroup group = null;
76 /** The tree showing the files within the collection. */
77 private CollectionTree collection_tree = null;
78 /** The threaded queue that handles the actually movement of files, so that the gui remains responsive. */
79 private FileQueue file_queue = null;
80 /** The filter currently applied to the collection tree. */
81 private Filter collection_filter = null;
82 /** The filter currently applied to the workspace tree. */
83 private Filter workspace_filter = null;
84 /** The button used to cancel all pending file queue jobs. */
85 private JButton stop_action = null;
86 /** The button used to create a new folder in the collection tree. */
87 private JButton new_folder = null;
88 /** The label shown at the top of the collection tree. */
89 private JLabel collection_label = null;
90 /** The label shown in the status area explaining the file apon which action is taking place. */
91 private JLabel filename_label = null;
92 /** The label shown explaining the current state of the file queue thread. */
93 private JLabel status_label = null;
94 /** The label at the top of the workspace tree. */
95 private JLabel workspace_label = null;
96 /** The panel that contains the collection tree. */
97 private JPanel collection_pane = null;
98 /** The panel that contains the various controls including the status area. */
99 private JPanel control_pane = null;
100 /** The panel that contains the workspace tree. */
101 private JPanel workspace_pane = null;
102 /** The scrollable area into which the collection tree is placed. */
103 private JScrollPane collection_scroll = null;
104 /** The scrollable area into which the workspace tree is placed. */
105 private JScrollPane workspace_scroll = null;
106 /** A split pane seperating the two trees, allowing for the screen real-estate for each to be changed. */
107 private JSplitPane tree_pane = null;
108 /** Text fragment arguments used to fill in phrases returned from the dictionary. */
109 private String args[] = null;
110 /** Ensures that expansion and selection events between collection trees based on the same model are synchronized. */
111 private TreeSynchronizer collection_tree_sync = null;
112 /** The button used to delete files, which also doubles as a drop target for files from the Trees. */
113 private RecycleBin bin_button = null;
114 /** The default size of a special mapping dialog. */
115 static final Dimension DIALOG_SIZE = new Dimension(400, 120);
116 /** The minimum size a gui component can become. */
117 static private Dimension MIN_SIZE = new Dimension( 90, 90);
118 /** The default size of the status area. */
119 static private Dimension STATUS_SIZE = new Dimension(450, 120);
120 /** The initial size of the trees. */
121 static private Dimension TREE_SIZE = new Dimension(400, 430);
122
123 /** The tree showing the available source workspace. */
124 public WorkspaceTree workspace_tree = null;
125
126
127 /* Constructor.
128 * @param tree_sync Ensures that expansion events between like trees are synchronized.
129 * @see org.greenstone.gatherer.file.FileManager
130 * @see org.greenstone.gatherer.file.FileQueue
131 */
132 public GatherPane(TreeSynchronizer collection_tree_sync) {
133 this.group = new DragGroup();
134 this.file_queue = Gatherer.f_man.getQueue();
135 this.collection_tree_sync = collection_tree_sync;
136
137 // Create components.
138 stop_action = new GLIButton();
139 stop_action.addActionListener(this);
140 stop_action.setEnabled(false);
141 stop_action.setMnemonic(KeyEvent.VK_S);
142 file_queue.registerStopButton(stop_action);
143 Dictionary.registerBoth(stop_action, "Collection.Stop", "Collection.Stop_Tooltip");
144
145 new_folder = new GLIButton(JarTools.getImage("folder.gif"));
146 new_folder.addActionListener(this);
147 new_folder.setEnabled(false);
148 new_folder.setMinimumSize(MIN_SIZE);
149 new_folder.setMnemonic(KeyEvent.VK_N);
150 new_folder.setPreferredSize(MIN_SIZE);
151 Dictionary.registerTooltip(new_folder, "Collection.New_Folder_Tooltip");
152 }
153
154 /** 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. */
155 public void actionPerformed(ActionEvent event) {
156 // If a user has clicked on the bin button directly remove whatever
157 // files are selected in the active tree.
158 if(event.getSource() == bin_button) {
159 if(!bin_button.ignore()) {
160 // Find the active tree (you've made selections in).
161 DragTree tree = (DragTree) group.getActive();
162 // Fudge things a bit
163 group.setSource(tree);
164 // Determine the selection.
165 TreePath paths[] = tree.getSelectionPaths();
166 if(paths != null) {
167 FileNode[] source_nodes = new FileNode[paths.length];
168 for(int i = 0; i < paths.length; i++) {
169 source_nodes[i] = (FileNode)(paths[i].getLastPathComponent());
170 }
171 Gatherer.f_man.action(tree, source_nodes, bin_button, null);
172 }
173 }
174 }
175 // If a user has clicked on new_folder create a new folder under
176 // whatever node is selected.
177 else if(event.getSource() == new_folder && collection_tree != null) {
178 int count = collection_tree.getSelectionCount();
179 boolean error = false;
180 if(count == 1) {
181 TreePath path = collection_tree.getSelectionPath();
182 CollectionTreeNode node = (CollectionTreeNode) path.getLastPathComponent();
183 if (node.getAllowsChildren()) {
184 Gatherer.f_man.newFolder(collection_tree, node);
185 }
186 else {
187 // try the parent
188 CollectionTreeNode parent = (CollectionTreeNode) node.getParent();
189 if (parent!=null && parent.getAllowsChildren()) {
190 Gatherer.f_man.newFolder(collection_tree, parent);
191 } else {
192 error = true;
193 }
194 }
195 }
196 else {
197 error = true;
198 }
199 if(error) {
200 // instead of an error, we now create a new folder at the root
201 CollectionTreeNode node = (CollectionTreeNode) collection_tree.getModel().getRoot();
202 Gatherer.f_man.newFolder(collection_tree, node);
203 }
204 }
205 else if(event.getSource() == stop_action) {
206 file_queue.cancelAction();
207 }
208 }
209
210
211 /** Generates the pane on controls used to 'collect' files into the collection. Resposible for creating, connecting and laying out these controls. */
212 public void display() {
213 // Create Components.
214 KeyListenerImpl key_listener = new KeyListenerImpl();
215 MouseListenerImpl mouse_listener = new MouseListenerImpl();
216 this.addKeyListener(key_listener);
217
218 // Workspace Tree
219 workspace_pane = new JPanel();
220 workspace_pane.setMinimumSize(MIN_SIZE);
221 workspace_pane.setPreferredSize(TREE_SIZE);
222 workspace_pane.setSize(TREE_SIZE);
223
224 workspace_label = new JLabel();
225 workspace_label.setOpaque(true);
226 workspace_label.setBackground(Configuration.getColor("coloring.workspace_heading_background", false));
227 workspace_label.setForeground(Configuration.getColor("coloring.workspace_heading_foreground", false));
228 Dictionary.registerText(workspace_label, "Collection.Workspace");
229
230 workspace_tree = new WorkspaceTree(Utility.WORKSPACE_TREE);
231 group.add(workspace_tree);
232 workspace_tree.addFocusListener(this);
233 workspace_tree.addKeyListener(key_listener);
234 workspace_tree.addMouseListener(mouse_listener);
235 workspace_tree.addMouseListener(Gatherer.g_man.foa_listener);
236 workspace_tree.addTreeExpansionListener(Gatherer.g_man.foa_listener);
237 workspace_tree.putClientProperty("JTree.lineStyle", "Angled");
238 workspace_tree.setBackgroundNonSelectionColor(Configuration.getColor("coloring.workspace_tree_background", false));
239 workspace_tree.setTextNonSelectionColor(Configuration.getColor("coloring.workspace_tree_foreground", false));
240 workspace_tree.setBackgroundSelectionColor(Configuration.getColor("coloring.workspace_selection_background", false));
241 workspace_tree.setTextSelectionColor(Configuration.getColor("coloring.workspace_selection_foreground", false));
242 workspace_tree.setRootVisible(false);
243
244 workspace_scroll = new JScrollPane(workspace_tree);
245
246 workspace_filter = Gatherer.g_man.getFilter(workspace_tree);
247 workspace_filter.setBackground(Configuration.getColor("coloring.workspace_heading_background", false));
248 workspace_filter.setEditable(Configuration.getMode() > Configuration.LIBRARIAN_MODE);
249 Dictionary.registerTooltip(workspace_filter.getComboBox(), "Collection.Filter_Tooltip");
250
251 // Collection Tree
252 collection_pane = new JPanel();
253 collection_pane.setMinimumSize(MIN_SIZE);
254 collection_pane.setPreferredSize(TREE_SIZE);
255 collection_pane.setSize(TREE_SIZE);
256
257 collection_label = new JLabel();
258 collection_label.setOpaque(true);
259 Dictionary.registerText(collection_label, "Collection.No_Collection");
260
261 collection_tree = new CollectionTree(Utility.COLLECTION_TREE, Gatherer.c_man.getCollectionTreeModel(), true);
262 collection_tree.setEnabled(Gatherer.c_man.getCollectionTreeModel() != null);
263 group.add(collection_tree);
264 collection_tree.addFocusListener(this);
265 collection_tree.addKeyListener(key_listener);
266 collection_tree.addMouseListener(mouse_listener);
267 collection_tree.addMouseListener(Gatherer.g_man.foa_listener);
268 collection_tree.addTreeExpansionListener(Gatherer.g_man.foa_listener);
269 collection_tree.putClientProperty("JTree.lineStyle", "Angled");
270 collection_tree.setBackgroundNonSelectionColor(Configuration.getColor("coloring.collection_tree_background", false));
271 collection_tree.setTextNonSelectionColor(Configuration.getColor("coloring.collection_tree_foreground", false));
272 collection_tree.setBackgroundSelectionColor(Configuration.getColor("coloring.collection_selection_background", false));
273 collection_tree.setTextSelectionColor(Configuration.getColor("coloring.collection_selection_foreground", false));
274 collection_tree.setRootVisible(false);
275
276 collection_scroll = new JScrollPane(collection_tree);
277
278 collection_filter = Gatherer.g_man.getFilter(collection_tree);
279 collection_filter.setBackground(Configuration.getColor("coloring.collection_heading_background", false));
280 collection_filter.setEditable(Configuration.getMode() > Configuration.LIBRARIAN_MODE);
281 Dictionary.registerTooltip(collection_filter.getComboBox(), "Collection.Filter_Tooltip");
282
283 tree_pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
284
285 // Status pane
286 control_pane = new JPanel();
287
288 JPanel inner_pane = new JPanel();
289 inner_pane.setSize(STATUS_SIZE);
290
291 JPanel file_pane = new JPanel();
292 JPanel progress_pane = new JPanel();
293 JLabel file_status = file_queue.getFileStatus();
294
295 JProgressBar progress_bar = file_queue.getProgressBar();
296
297 JPanel button_pane = new JPanel();
298
299 bin_button = new RecycleBin();
300 bin_button.addActionListener(this);
301 bin_button.setEnabled(false);
302 bin_button.setMinimumSize(MIN_SIZE);
303 bin_button.setPreferredSize(MIN_SIZE);
304 Dictionary.registerTooltip(bin_button, "Collection.Delete_Tooltip");
305 group.add(bin_button);
306
307 // Layout Components.
308 workspace_pane.setLayout(new BorderLayout());
309 workspace_pane.add(workspace_label, BorderLayout.NORTH);
310 workspace_pane.add(workspace_scroll, BorderLayout.CENTER);
311 workspace_pane.add(workspace_filter, BorderLayout.SOUTH);
312
313 collection_pane.setLayout(new BorderLayout());
314 collection_pane.add(collection_label, BorderLayout.NORTH);
315 collection_pane.add(collection_scroll, BorderLayout.CENTER);
316 collection_pane.add(collection_filter, BorderLayout.SOUTH);
317
318 tree_pane.add(workspace_pane, JSplitPane.LEFT);
319 tree_pane.add(collection_pane, JSplitPane.RIGHT);
320 tree_pane.setDividerLocation(TREE_SIZE.width - 10);
321
322 file_pane.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
323 file_pane.setLayout(new BorderLayout());
324 file_pane.add(file_status, BorderLayout.CENTER);
325 file_pane.add(stop_action, BorderLayout.EAST);
326
327 progress_pane.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
328 progress_pane.setLayout(new BorderLayout());
329 progress_pane.add(progress_bar, BorderLayout.CENTER);
330
331 inner_pane.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(10,10,10,10), BorderFactory.createLoweredBevelBorder()));
332 inner_pane.setLayout(new GridLayout(2,1));
333 inner_pane.add(file_pane);
334 inner_pane.add(progress_pane);
335
336 button_pane.add(new_folder);
337 button_pane.add(bin_button);
338
339 control_pane.setLayout(new BorderLayout());
340 control_pane.add(inner_pane, BorderLayout.CENTER);
341 control_pane.add(button_pane, BorderLayout.EAST);
342
343 this.setLayout(new BorderLayout());
344 this.add(tree_pane, BorderLayout.CENTER);
345 this.add(control_pane, BorderLayout.SOUTH);
346 }
347 /** 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. */
348 public Rectangle expandPath(TreePath path) {
349 collection_tree.setImmediate(true);
350 collection_tree.scrollPathToVisible(path);
351 collection_tree.setSelectionPath(path);
352 collection_tree.setImmediate(false);
353 return collection_tree.getRowBounds(collection_tree.getRowForPath(path));
354 }
355 /** 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).
356 * @param event A <strong>FocusEvent</strong> containing details about the focus action performed.
357 */
358 public void focusGained(FocusEvent event) {
359 DefaultTreeCellRenderer def = new DefaultTreeCellRenderer();
360 DefaultTreeCellRenderer w = (DefaultTreeCellRenderer)workspace_tree.getCellRenderer();
361 DefaultTreeCellRenderer c = (DefaultTreeCellRenderer)collection_tree.getCellRenderer();
362 if(event.getSource() == workspace_tree) {
363 w.setBackgroundSelectionColor(def.getBackgroundSelectionColor());
364 c.setBackgroundSelectionColor(Color.lightGray);
365 }
366 else if(event.getSource() == collection_tree) {
367 c.setBackgroundSelectionColor(def.getBackgroundSelectionColor());
368 w.setBackgroundSelectionColor(Color.lightGray);
369 }
370 repaint();
371 }
372 /** Implementation side-effect, not used in any way.
373 * @param event A <strong>FocusEvent</strong> containing details about the focus action performed.
374 */
375 public void focusLost(FocusEvent event) {
376 }
377
378 /** Called to inform this control panel that it has just gained focus as an effect of the user clicking on its tab.
379 */
380 public void gainFocus() {
381 // Update the meta-audit view to show the current selection, if any.
382 Gatherer.g_man.meta_audit.setRecords(getCollectionTreeSelection());
383 }
384
385 /** Retrieve a list of the currently selected file records in the collection tree. */
386 private CollectionTreeNode[] getCollectionTreeSelection()
387 {
388 TreePath paths[] = collection_tree.getSelectionPaths();
389 CollectionTreeNode records[] = null;
390 if (paths != null) {
391 records = new CollectionTreeNode[paths.length];
392 for (int i = 0; i < records.length; i++) {
393 records[i] = (CollectionTreeNode) paths[i].getLastPathComponent();
394 }
395 }
396 return records;
397 }
398
399
400 /** Called whenever the detail mode changes to ensure the filters are at an appropriate level (ie only editable by those that understand regular expression matching)
401 * @param mode the mode level as an int
402 */
403 public void modeChanged(int mode) {
404 collection_filter.setEditable(mode > Configuration.LIBRARIAN_MODE);
405 workspace_filter.setEditable(mode > Configuration.LIBRARIAN_MODE);
406 }
407
408
409 /** Refresh this pane, depending on what has just happened (refresh_reason). */
410 public void refresh(int refresh_reason, boolean collection_loaded)
411 {
412 if (collection_loaded) {
413 // Update collection label
414 Dictionary.registerText(collection_label, "Collection.Collection");
415 collection_label.setBackground(Configuration.getColor("coloring.collection_heading_background", false));
416 collection_label.setForeground(Configuration.getColor("coloring.collection_heading_foreground", false));
417
418 // Update collection tree
419 if (refresh_reason == Gatherer.COLLECTION_OPENED) {
420 collection_tree.setModel(Gatherer.c_man.getCollectionTreeModel());
421 }
422
423 // Update collection filter
424 collection_filter.setBackground(Configuration.getColor("coloring.collection_heading_background", false));
425 }
426 else {
427 // Update collection label
428 String[] args = new String[1];
429 args[0] = Dictionary.get("Collection.No_Collection");
430 Dictionary.registerText(collection_label, "Collection.Collection", args);
431 collection_label.setBackground(Color.lightGray);
432 collection_label.setForeground(Color.black);
433
434 // Update collection tree
435 collection_tree.setModel(new DefaultTreeModel(new DefaultMutableTreeNode("Error")));
436
437 // Update collection filter
438 collection_filter.setBackground(Color.lightGray);
439 }
440
441 // Enable or disable the controls
442 workspace_tree.setEnabled(true);
443 collection_tree.setEnabled(collection_loaded);
444 collection_filter.setEnabled(collection_loaded);
445 bin_button.setEnabled(collection_loaded);
446 new_folder.setEnabled(collection_loaded);
447
448 // Ensure that this collection tree view is synchronized with all others
449 collection_tree_sync.add(collection_tree);
450 }
451
452
453 public void refreshCollectionTree(int refresh_reason) {
454 collection_tree.refresh(null);
455 }
456
457
458 public void refreshWorkspaceTree(int refresh_reason) {
459 workspace_tree.refresh(refresh_reason);
460 }
461
462
463 /** When a user right-clicks within the workspace and collection trees they are presented with a small popup menu of context based options. This class provides such functionality.
464 */
465 private class RightClickMenu
466 extends JPopupMenu
467 implements ActionListener {
468
469 /** The tree over which the right click action occurred. */
470 private DragTree tree = null;
471 /** The tree nodes selected when the right click action occurred. */
472 private TreePath[] selection_paths = null;
473 /** The file record over which the right click action occurred. */
474 private FileNode node = null;
475
476 private JMenuItem create_shortcut = null;
477 private JMenuItem delete_shortcut = null;
478 private JMenuItem collapse_folder = null;
479 private JMenuItem expand_folder = null;
480 private JMenuItem explode_metadata_database = null;
481 private JMenuItem delete = null;
482 private JMenuItem metaaudit = null;
483 private JMenuItem new_folder = null;
484 private JMenuItem new_dummy_doc = null;
485 private JMenuItem open_externally = null;
486 private JMenuItem rename = null;
487 private JMenuItem replace = null;
488
489
490 private RightClickMenu(DragTree tree, MouseEvent event)
491 {
492 super();
493 this.tree = tree;
494
495 // Note we have to use setImmediate() with the set selction paths
496 // otherwise the selection doesn't get updated until after the
497 // popup comes up.
498
499 // the right click position
500 TreePath right_click_path = tree.getPathForLocation(event.getX(), event.getY());
501 if (right_click_path == null) {
502 // user has clicked outside of the tree, clear the selection
503 selection_paths = null;
504 tree.setImmediate(true);
505 tree.clearSelection();
506 tree.setImmediate(false);
507 } else {
508 // Get the paths currently selected in the tree
509 selection_paths = tree.getSelectionPaths();
510 if (selection_paths == null) {
511 // nothing currently selected - we shift the selection to
512 // the node that was right clicked on
513 selection_paths = new TreePath[1];
514 selection_paths[0] = right_click_path;
515 tree.setImmediate(true);
516 tree.setSelectionPath(right_click_path);
517 tree.setImmediate(false);
518 } else if (selection_paths.length == 1 && ! selection_paths[0].equals( right_click_path)) {
519 tree.setImmediate(true);
520 tree.clearSelection();
521 tree.setSelectionPath(right_click_path);
522 tree.setImmediate(false);
523 selection_paths[0] = right_click_path;
524 } else {
525 // we had multiply selected paths in the tree.
526 // if we clicked on one of those paths, then use all the
527 // current selection, otherwise clear the selection and
528 // select the one we right clicked on
529 boolean clicked_in_selection = false;
530 for (int i=0; i<selection_paths.length; i++) {
531 if (selection_paths[i].equals(right_click_path)) {
532 clicked_in_selection = true;
533 break;
534 }
535 }
536 if (!clicked_in_selection) {
537 // want the tree to update right away
538 tree.setImmediate(true);
539 tree.clearSelection();
540 tree.setSelectionPath(right_click_path);
541 tree.setImmediate(false);
542 selection_paths = new TreePath[1];
543 selection_paths[0] = right_click_path;
544 }
545 }
546 }
547 // finally we have the correct selection paths!
548
549 // Create an appropriate context menu, based on what is selected
550 buildContextMenu(selection_paths);
551
552 // Show the popup menu on screen
553 show(tree, event.getX(), event.getY());
554 }
555
556
557 private void buildContextMenu(TreePath[] selection_paths)
558 {
559 // If nothing is selected, only the new folder/dummy doc options are available...
560 if (selection_paths == null) {
561 // ... but only in the collection tree!
562 if (tree == collection_tree) {
563 new_folder = new JMenuItem(Dictionary.get("CollectionPopupMenu.New_Folder"), KeyEvent.VK_N);
564 new_folder.addActionListener(this);
565 add(new_folder);
566
567 new_dummy_doc = new JMenuItem(Dictionary.get("CollectionPopupMenu.New_Dummy_Doc"));
568 new_dummy_doc.addActionListener(this);
569 add(new_dummy_doc);
570
571 node = (CollectionTreeNode) tree.getModel().getRoot();
572 }
573
574 return;
575 }
576
577 DebugStream.println("Number of files/folders selected: " + selection_paths.length);
578
579 // Collection tree: display meta-audit option and delete options
580 if (tree == collection_tree) {
581 String[] args = new String[1];
582 args[0] = collection_tree.getSelectionDetails();
583 metaaudit = new JMenuItem(Dictionary.get("Menu.Metadata_View", args), KeyEvent.VK_A);
584 metaaudit.addActionListener(this);
585 add(metaaudit);
586
587 delete = new JMenuItem(Dictionary.get("CollectionPopupMenu.Delete"), KeyEvent.VK_D);
588 delete.addActionListener(this);
589 add(delete);
590
591 }
592
593 // Only meta-audit and delete are available if multiple items are selected...
594 if (selection_paths.length > 1) {
595 return;
596 }
597
598 // collection tree gets rename option
599 if (tree == collection_tree) {
600 rename = new JMenuItem(Dictionary.get("CollectionPopupMenu.Rename"), KeyEvent.VK_R);
601 rename.addActionListener(this);
602 add(rename);
603 }
604
605 TreePath path = selection_paths[0];
606 node = (FileNode) path.getLastPathComponent();
607
608 // ---- Options for file nodes ----
609 if (node.isLeaf()) {
610 if (tree == collection_tree) {
611 // Explode metadata databases, for explodable files only
612 if (((CollectionTreeNode) node).isExplodable()) {
613 explode_metadata_database = new JMenuItem(Dictionary.get("Menu.Explode_Metadata_Database"), KeyEvent.VK_E);
614 explode_metadata_database.addActionListener(this);
615 explode_metadata_database.setEnabled(!Gatherer.isGsdlRemote);
616 add(explode_metadata_database);
617 }
618 // replace file
619 replace = new JMenuItem(Dictionary.get("CollectionPopupMenu.Replace"), KeyEvent.VK_P);
620 replace.addActionListener(this);
621 add(replace);
622 }
623
624 // Open the file in an external program
625 open_externally = new JMenuItem(Dictionary.get("Menu.Open_Externally"), KeyEvent.VK_O);
626 open_externally.addActionListener(this);
627 add(open_externally);
628
629 return;
630 }
631
632 // ---- Options for folder nodes ----
633 // Collapse or expand, depending on current status
634 if (tree.isExpanded(path)) {
635 collapse_folder = new JMenuItem(Dictionary.get("Menu.Collapse"), KeyEvent.VK_C);
636 collapse_folder.addActionListener(this);
637 add(collapse_folder);
638 }
639 else {
640 expand_folder = new JMenuItem(Dictionary.get("Menu.Expand"), KeyEvent.VK_O);
641 expand_folder.addActionListener(this);
642 add(expand_folder);
643 }
644
645 // New folder/dummy doc options, for collection tree only
646 if (tree == collection_tree && !node.isReadOnly()) {
647 new_folder = new JMenuItem(Dictionary.get("CollectionPopupMenu.New_Folder"), KeyEvent.VK_N);
648 new_folder.addActionListener(this);
649 add(new_folder);
650 new_dummy_doc = new JMenuItem(Dictionary.get("CollectionPopupMenu.New_Dummy_Doc"));
651 new_dummy_doc.addActionListener(this);
652 add(new_dummy_doc);
653 }
654
655 // Create/remove shortcut option, for workspace tree only
656 if (tree == workspace_tree) {
657 // The "built-in" folders can't be modified
658 String node_name = node.toString();
659 if (node_name.equals(Dictionary.get("Tree.World")) || node_name.equals(Dictionary.get("Tree.Root")) || node_name.equals(Dictionary.get("Tree.DownloadedFiles"))) {
660 return;
661 }
662
663 // You can unmap 1st level nodes
664 WorkspaceTreeNode root = (WorkspaceTreeNode) tree.getModel().getRoot();
665 if (root.getIndex(node) != -1) {
666 delete_shortcut = new JMenuItem(Dictionary.get("MappingPrompt.Unmap"), KeyEvent.VK_R);
667 delete_shortcut.addActionListener(this);
668 add(delete_shortcut);
669 }
670 // Or map any other level directories
671 else {
672 create_shortcut = new JMenuItem(Dictionary.get("MappingPrompt.Map"), KeyEvent.VK_S);
673 create_shortcut.addActionListener(this);
674 add(create_shortcut);
675 }
676 }
677 }
678
679
680 /** Called whenever one of the menu items is clicked, this method then causes the appropriate effect. */
681 public void actionPerformed(ActionEvent event)
682 {
683 Object source = event.getSource();
684
685 // Create shortcut
686 if (source == create_shortcut) {
687 MappingPrompt mp = new MappingPrompt(node.getFile());
688 mp.destroy();
689 }
690
691 // Delete shortcut
692 else if (source == delete_shortcut) {
693 workspace_tree.removeDirectoryMapping((WorkspaceTreeNode) node);
694 }
695
696 // Collapse folder
697 else if (source == collapse_folder) {
698 tree.collapsePath(selection_paths[0]);
699 }
700
701 // Expand folder
702 else if (source == expand_folder) {
703 tree.expandPath(selection_paths[0]);
704 }
705
706 // Explode metadata database
707 else if (source == explode_metadata_database) {
708 ExplodeMetadataPrompt emp = new ExplodeMetadataPrompt(node.getFile());
709 //emp.destroy();
710 }
711
712 // Delete
713 else if (source == delete) {
714 FileNode[] source_nodes = new FileNode[selection_paths.length];
715 for (int i = 0; i < selection_paths.length; i++) {
716 source_nodes[i] = (FileNode) selection_paths[i].getLastPathComponent();
717 }
718
719 // Fire a delete action
720 Gatherer.f_man.action(tree, source_nodes, bin_button, null);
721 }
722
723 // Meta-audit
724 else if (source == metaaudit) {
725 Gatherer.g_man.showMetaAuditBox();
726 }
727
728 // New folder
729 else if (source == new_folder) {
730 Gatherer.f_man.newFolder(tree, (CollectionTreeNode) node);
731 }
732
733 // New dummy doc
734 else if (source == new_dummy_doc) {
735 Gatherer.f_man.newDummyDoc(tree, (CollectionTreeNode) node);
736 }
737
738 // Open in external program
739 else if (source == open_externally) {
740 Gatherer.f_man.openFileInExternalApplication(node.getFile());
741 }
742
743 // Rename
744 else if (source == rename) {
745 Gatherer.f_man.renameCollectionFile(collection_tree, (CollectionTreeNode) node);
746 }
747
748 // Replace
749 else if (source == replace) {
750 Gatherer.f_man.replaceCollectionFile(collection_tree, (CollectionTreeNode) node);
751 }
752 }
753 }
754
755
756 /** This class listens for certain key presses, such as [Enter] or [Delete], and responds appropriately. */
757 private class KeyListenerImpl
758 extends KeyAdapter {
759 private boolean vk_left_pressed = false;
760 /** 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). */
761 public void keyReleased(KeyEvent event) {
762 ///ystem.err.println("Key Release detected. " + event.getKeyCode());
763 if(event.getKeyCode() == KeyEvent.VK_DELETE) {
764 // Get the selected files from the tree and removal them using the default dnd removal method.
765 // Find the active tree (you've made selections in).
766 DragTree tree = (DragTree) group.getActive();
767 // Fudge things a bit
768 group.setSource(tree);
769 // Determine the selection.
770 TreePath paths[] = tree.getSelectionPaths();
771 if(paths != null) {
772 FileNode[] source_nodes = new FileNode[paths.length];
773 for(int i = 0; i < source_nodes.length; i++) {
774 source_nodes[i] = (FileNode) paths[i].getLastPathComponent();
775 }
776 Gatherer.f_man.action(tree, source_nodes, bin_button, null);
777 source_nodes = null;
778 }
779 }
780 else if(event.getKeyCode() == KeyEvent.VK_ENTER) {
781 // Get the first selected file.
782 DragTree tree = (DragTree)event.getSource();
783 TreePath path = tree.getSelectionPath();
784 if(path != null) {
785 File file = ((FileNode)path.getLastPathComponent()).getFile();
786 if (file != null && file.isFile()) {
787 Gatherer.f_man.openFileInExternalApplication(file);
788 }
789 else {
790 if(!tree.isExpanded(path)) {
791 tree.expandPath(path);
792 }
793 else {
794 tree.collapsePath(path);
795 }
796 }
797 }
798 } else if (event.getKeyCode() == KeyEvent.VK_UP || event.getKeyCode() == KeyEvent.VK_DOWN) {
799 DragTree tree = (DragTree)event.getSource();
800 // we need to manually do the up and down selections
801 boolean up = (event.getKeyCode() == KeyEvent.VK_UP);
802 int current_row = tree.getLeadSelectionRow();
803 tree.setImmediate(true);
804 if (up) {
805 if (current_row > 0) {
806 tree.clearSelection();
807 tree.setSelectionRow(current_row-1);
808 }
809 } else {
810 if (current_row < tree.getRowCount()-1){
811 tree.clearSelection();
812 tree.setSelectionRow(current_row+1);
813 }
814 }
815 tree.setImmediate(false);
816 } else if (event.getKeyCode() == KeyEvent.VK_LEFT) {
817 // left key on a file shifts the selection to the parent folder
818 DragTree tree = (DragTree)event.getSource();
819 TreePath path = tree.getLeadSelectionPath();
820 if(path != null) {
821 File file = ((FileNode)path.getLastPathComponent()).getFile();
822 if(file != null) {
823 if (file.isFile() || vk_left_pressed) {
824 vk_left_pressed = false;
825 TreePath parent_path = path.getParentPath();
826 if (parent_path != null && parent_path.getParentPath() != null) {
827 // if this file is in the top level folder, don't move the focus
828 tree.setImmediate(true);
829 tree.clearSelection();
830 tree.setSelectionPath(parent_path);
831 tree.setImmediate(false);
832 }
833 }
834 }
835 }
836 }
837 }
838
839 // we need to watch for left clicks on an unopened folder - should shift the focus to teh parent folder. But because there is some other mysterious key listener that does opening and closing folders on right and left clicks, we must detect the situation before the other handler has done its job, and process it after.
840 public void keyPressed(KeyEvent event) {
841 if (event.getKeyCode() == KeyEvent.VK_LEFT) {
842 // left key on closed directory shifts the selection to the parent folder
843 DragTree tree = (DragTree)event.getSource();
844 TreePath path = tree.getLeadSelectionPath();
845 if(path == null) return;
846 File file = ((FileNode)path.getLastPathComponent()).getFile();
847 if(file == null) return;
848
849 if (file.isDirectory() && tree.isCollapsed(path)) {
850 vk_left_pressed = true;
851 }
852 }
853 }
854 }
855
856 /** This provides a small prompt for gathering addition details about a special directory mapping such as its symbolic name.
857 * Used when creating a new shortcut to a folder */
858 private class MappingPrompt
859 extends JDialog
860 implements ActionListener, KeyListener {
861 private boolean cancelled = false;
862 private JButton cancel_button = null;
863 private JButton ok_button = null;
864 private JTextField name_field = null;
865 public MappingPrompt(File file) {
866 super(Gatherer.g_man);
867 setModal(true);
868 setSize(DIALOG_SIZE);
869 Dictionary.setText(this, "MappingPrompt.Title");
870
871 // Creation
872 JPanel content_pane = (JPanel) getContentPane();
873 JPanel center_pane = new JPanel();
874 JPanel labels_pane = new JPanel();
875 JPanel fields_pane = new JPanel();
876
877 JLabel file_label = new JLabel();
878 Dictionary.setText(file_label, "MappingPrompt.File");
879 JLabel file_field = new JLabel(file.getAbsolutePath());
880
881 JLabel name_label = new JLabel();
882 Dictionary.setText(name_label, "MappingPrompt.Name");
883 name_field = new JTextField(file.getName());
884
885 JPanel button_pane = new JPanel();
886 ok_button = new GLIButton();
887 ok_button.setEnabled(name_field.getText().length() > 0);
888 ok_button.setMnemonic(KeyEvent.VK_O);
889 Dictionary.setBoth(ok_button, "General.OK", "General.OK_Tooltip");
890 cancel_button = new GLIButton();
891 cancel_button.setMnemonic(KeyEvent.VK_C);
892 Dictionary.setBoth(cancel_button, "General.Cancel", "General.Cancel_Tooltip");
893
894 // Connection
895 cancel_button.addActionListener(this);
896 ok_button.addActionListener(this);
897 name_field.addKeyListener(this);
898
899 // Layout
900 labels_pane.setLayout(new GridLayout(2,1,5,0));
901 labels_pane.add(file_label);
902 labels_pane.add(name_label);
903
904 fields_pane.setLayout(new GridLayout(2,1,5,0));
905 fields_pane.add(file_field);
906 fields_pane.add(name_field);
907
908 center_pane.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
909 center_pane.setLayout(new BorderLayout(5,0));
910 center_pane.add(labels_pane, BorderLayout.WEST);
911 center_pane.add(fields_pane, BorderLayout.CENTER);
912
913 button_pane.setLayout(new GridLayout(1,2,5,5));
914 button_pane.add(ok_button);
915 button_pane.add(cancel_button);
916
917 content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
918 content_pane.setLayout(new BorderLayout());
919 content_pane.add(center_pane, BorderLayout.CENTER);
920 content_pane.add(button_pane, BorderLayout.SOUTH);
921 // Display
922 Dimension screen_size = Configuration.screen_size;
923 setLocation((screen_size.width - DIALOG_SIZE.width) / 2, (screen_size.height - DIALOG_SIZE.height) / 2);
924 setVisible(true);
925 // If not cancelled create mapping.
926 if (!cancelled) {
927 workspace_tree.addDirectoryMapping(name_field.getText(), file);
928 }
929 }
930 public void actionPerformed(ActionEvent event) {
931 if(event.getSource() == cancel_button) {
932 cancelled = true;
933 }
934 dispose();
935 }
936 public void destroy() {
937 cancel_button = null;
938 ok_button = null;
939 name_field = null;
940 }
941 public void keyPressed(KeyEvent event) {
942 }
943 public void keyReleased(KeyEvent event) {
944 ok_button.setEnabled(name_field.getText().length() > 0);
945
946 }
947 public void keyTyped(KeyEvent event) {
948 }
949 }
950
951 /** This class listens for mouse clicks and responds right mouse button clicks (popup menu). */
952 private class MouseListenerImpl
953 extends MouseAdapter {
954 /** 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. */
955 public void mouseClicked(MouseEvent event) {
956 if (SwingUtilities.isRightMouseButton(event)) {
957 new RightClickMenu((DragTree) event.getSource(), event);
958 }
959 }
960 }
961}
Note: See TracBrowser for help on using the repository browser.