source: trunk/gli/src/org/greenstone/gatherer/collection/CollectionTree.java@ 13604

Last change on this file since 13604 was 13604, checked in by mdewsnip, 17 years ago

Added remote Greenstone support for the "Rename" option when right-clicking on a collection file.

  • Property svn:keywords set to Author Date Id Revision
File size: 11.7 KB
Line 
1/**
2 *############################################################################
3 * A component of the Greenstone Librarian Interface, part of the Greenstone
4 * digital library suite from the New Zealand Digital Library Project at the
5 * University of Waikato, New Zealand.
6 *
7 * Author: Michael Dewsnip, NZDL Project, University of Waikato, NZ
8 *
9 * Copyright (C) 2006 New Zealand Digital Library Project
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *############################################################################
25 */
26
27package org.greenstone.gatherer.collection;
28
29import java.awt.*;
30import java.awt.event.*;
31import javax.swing.*;
32import javax.swing.tree.*;
33import org.greenstone.gatherer.Configuration;
34import org.greenstone.gatherer.Dictionary;
35import org.greenstone.gatherer.Gatherer;
36import org.greenstone.gatherer.gui.tree.DragTree;
37import org.greenstone.gatherer.gui.tree.DragTreeCellRenderer;
38
39
40public class CollectionTree
41 extends DragTree
42 implements MouseListener
43{
44 public CollectionTree(CollectionTreeModel collection_tree_model, boolean mixed_selection)
45 {
46 super(collection_tree_model, mixed_selection);
47 addMouseListener(this);
48
49 setCellRenderer(new CollectionTreeCellRenderer());
50 setBackgroundNonSelectionColor(Configuration.getColor("coloring.collection_tree_background", false));
51 setBackgroundSelectionColor(Configuration.getColor("coloring.collection_selection_background", false));
52 setTextNonSelectionColor(Configuration.getColor("coloring.collection_tree_foreground", false));
53 setTextSelectionColor(Configuration.getColor("coloring.collection_selection_foreground", false));
54
55 filter.setBackground(Configuration.getColor("coloring.collection_heading_background", false));
56 filter.setEditable(Configuration.getMode() > Configuration.LIBRARIAN_MODE);
57 }
58
59
60 public boolean isDraggable()
61 {
62 return true;
63 }
64
65
66 public boolean isDroppable()
67 {
68 return true;
69 }
70
71
72 public void mouseClicked(MouseEvent event)
73 {
74 if (SwingUtilities.isRightMouseButton(event)) {
75 new CollectionTreeRightClickMenu(this, event);
76 }
77 }
78
79 public void mouseEntered(MouseEvent event) { }
80
81 public void mouseExited(MouseEvent event) { }
82
83 public void mousePressed(MouseEvent event) { }
84
85 public void mouseReleased(MouseEvent event) { }
86
87
88 public String toString()
89 {
90 return "Collection";
91 }
92
93
94 private class CollectionTreeCellRenderer
95 extends DragTreeCellRenderer
96 {
97 public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus)
98 {
99 JLabel tree_cell = (JLabel) super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
100
101 // Mark explodable files with a different icon
102 if (value instanceof CollectionTreeNode && ((CollectionTreeNode) value).isExplodable()) {
103 tree_cell.setIcon(CollectionTreeNode.GREEN_FILE_ICON);
104 }
105
106 return tree_cell;
107 }
108 }
109
110
111 /** 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.
112 */
113 private class CollectionTreeRightClickMenu
114 extends JPopupMenu
115 implements ActionListener
116 {
117 /** The tree over which the right click action occurred. */
118 private CollectionTree collection_tree = null;
119 /** The tree nodes selected when the right click action occurred. */
120 private TreePath[] selection_paths = null;
121 /** The file record over which the right click action occurred. */
122 private CollectionTreeNode node = null;
123
124 private JMenuItem collapse_folder = null;
125 private JMenuItem expand_folder = null;
126 private JMenuItem explode_metadata_database = null;
127 private JMenuItem delete = null;
128 private JMenuItem metaaudit = null;
129 private JMenuItem new_folder = null;
130 private JMenuItem new_dummy_doc = null;
131 private JMenuItem open_externally = null;
132 private JMenuItem rename = null;
133 private JMenuItem replace = null;
134
135
136 private CollectionTreeRightClickMenu(CollectionTree collection_tree, MouseEvent event)
137 {
138 super();
139 this.collection_tree = collection_tree;
140
141 // Note we have to use setImmediate() with the set selction paths
142 // otherwise the selection doesn't get updated until after the
143 // popup comes up.
144
145 // the right click position
146 TreePath right_click_path = collection_tree.getPathForLocation(event.getX(), event.getY());
147 if (right_click_path == null) {
148 // user has clicked outside of the tree, clear the selection
149 selection_paths = null;
150 collection_tree.setImmediate(true);
151 collection_tree.clearSelection();
152 collection_tree.setImmediate(false);
153 }
154 else {
155 // Get the paths currently selected in the tree
156 selection_paths = collection_tree.getSelectionPaths();
157 if (selection_paths == null) {
158 // nothing currently selected - we shift the selection to
159 // the node that was right clicked on
160 selection_paths = new TreePath[1];
161 selection_paths[0] = right_click_path;
162 collection_tree.setImmediate(true);
163 collection_tree.setSelectionPath(right_click_path);
164 collection_tree.setImmediate(false);
165 }
166 else if (selection_paths.length == 1 && ! selection_paths[0].equals( right_click_path)) {
167 collection_tree.setImmediate(true);
168 collection_tree.clearSelection();
169 collection_tree.setSelectionPath(right_click_path);
170 collection_tree.setImmediate(false);
171 selection_paths[0] = right_click_path;
172 }
173 else {
174 // we had multiply selected paths in the tree.
175 // if we clicked on one of those paths, then use all the
176 // current selection, otherwise clear the selection and
177 // select the one we right clicked on
178 boolean clicked_in_selection = false;
179 for (int i = 0; i < selection_paths.length; i++) {
180 if (selection_paths[i].equals(right_click_path)) {
181 clicked_in_selection = true;
182 break;
183 }
184 }
185 if (!clicked_in_selection) {
186 // want the tree to update right away
187 collection_tree.setImmediate(true);
188 collection_tree.clearSelection();
189 collection_tree.setSelectionPath(right_click_path);
190 collection_tree.setImmediate(false);
191 selection_paths = new TreePath[1];
192 selection_paths[0] = right_click_path;
193 }
194 }
195 }
196
197 // Create an appropriate context menu, based on what is selected
198 buildContextMenu(selection_paths);
199
200 // Show the popup menu on screen
201 show(collection_tree, event.getX(), event.getY());
202 }
203
204
205 private void buildContextMenu(TreePath[] selection_paths)
206 {
207 // If nothing is selected, only the new folder/dummy doc options are available...
208 if (selection_paths == null) {
209 new_folder = new JMenuItem(Dictionary.get("CollectionPopupMenu.New_Folder"), KeyEvent.VK_N);
210 new_folder.addActionListener(this);
211 add(new_folder);
212
213 new_dummy_doc = new JMenuItem(Dictionary.get("CollectionPopupMenu.New_Dummy_Doc"));
214 new_dummy_doc.addActionListener(this);
215 add(new_dummy_doc);
216
217 node = (CollectionTreeNode) collection_tree.getModel().getRoot();
218 return;
219 }
220
221 // Meta-audit and delete options
222 metaaudit = new JMenuItem(Dictionary.get("Menu.Metadata_View", collection_tree.getSelectionDetails()), KeyEvent.VK_A);
223 metaaudit.addActionListener(this);
224 add(metaaudit);
225
226 delete = new JMenuItem(Dictionary.get("CollectionPopupMenu.Delete"), KeyEvent.VK_D);
227 delete.addActionListener(this);
228 add(delete);
229
230 // Only meta-audit and delete are available if multiple items are selected...
231 if (selection_paths.length > 1) {
232 return;
233 }
234
235 // Rename option
236 rename = new JMenuItem(Dictionary.get("CollectionPopupMenu.Rename"), KeyEvent.VK_R);
237 rename.addActionListener(this);
238 add(rename);
239
240 TreePath path = selection_paths[0];
241 node = (CollectionTreeNode) path.getLastPathComponent();
242
243 // ---- Options for file nodes ----
244 if (node.isLeaf()) {
245 // Explode metadata databases, for explodable files only
246 if (node.isExplodable()) {
247 explode_metadata_database = new JMenuItem(Dictionary.get("Menu.Explode_Metadata_Database"), KeyEvent.VK_E);
248 explode_metadata_database.addActionListener(this);
249 add(explode_metadata_database);
250 }
251
252 // Replace file
253 // !! TO DO: Remote building
254 if (!Gatherer.isGsdlRemote) {
255 replace = new JMenuItem(Dictionary.get("CollectionPopupMenu.Replace"), KeyEvent.VK_P);
256 replace.addActionListener(this);
257 add(replace);
258 }
259
260 // Open the file in an external program
261 open_externally = new JMenuItem(Dictionary.get("Menu.Open_Externally"), KeyEvent.VK_O);
262 open_externally.addActionListener(this);
263 add(open_externally);
264
265 return;
266 }
267
268 // ---- Options for folder nodes ----
269 // Collapse or expand, depending on current status
270 if (collection_tree.isExpanded(path)) {
271 collapse_folder = new JMenuItem(Dictionary.get("Menu.Collapse"), KeyEvent.VK_C);
272 collapse_folder.addActionListener(this);
273 add(collapse_folder);
274 }
275 else {
276 expand_folder = new JMenuItem(Dictionary.get("Menu.Expand"), KeyEvent.VK_O);
277 expand_folder.addActionListener(this);
278 add(expand_folder);
279 }
280
281 // New folder/dummy doc options
282 if (!node.isReadOnly()) {
283 new_folder = new JMenuItem(Dictionary.get("CollectionPopupMenu.New_Folder"), KeyEvent.VK_N);
284 new_folder.addActionListener(this);
285 add(new_folder);
286
287 new_dummy_doc = new JMenuItem(Dictionary.get("CollectionPopupMenu.New_Dummy_Doc"));
288 new_dummy_doc.addActionListener(this);
289 add(new_dummy_doc);
290 }
291 }
292
293
294 /** Called whenever one of the menu items is clicked, this method then causes the appropriate effect. */
295 public void actionPerformed(ActionEvent event)
296 {
297 Object source = event.getSource();
298
299 // Collapse folder
300 if (source == collapse_folder) {
301 collection_tree.collapsePath(selection_paths[0]);
302 }
303
304 // Expand folder
305 else if (source == expand_folder) {
306 collection_tree.expandPath(selection_paths[0]);
307 }
308
309 // Explode metadata database
310 else if (source == explode_metadata_database) {
311 Gatherer.f_man.explodeMetadataDatabase(node.getFile());
312 }
313
314 // Delete
315 else if (source == delete) {
316 CollectionTreeNode[] source_nodes = new CollectionTreeNode[selection_paths.length];
317 for (int i = 0; i < selection_paths.length; i++) {
318 source_nodes[i] = (CollectionTreeNode) selection_paths[i].getLastPathComponent();
319 }
320
321 // Fire a delete action
322 Gatherer.f_man.action(collection_tree, source_nodes, Gatherer.recycle_bin, null);
323 }
324
325 // Meta-audit
326 else if (source == metaaudit) {
327 Gatherer.g_man.showMetaAuditBox();
328 }
329
330 // New folder
331 else if (source == new_folder) {
332 Gatherer.f_man.newFolder(collection_tree, node);
333 }
334
335 // New dummy doc
336 else if (source == new_dummy_doc) {
337 Gatherer.f_man.newDummyDoc(collection_tree, node);
338 }
339
340 // Open in external program
341 else if (source == open_externally) {
342 Gatherer.f_man.openFileInExternalApplication(node.getFile());
343 }
344
345 // Rename
346 else if (source == rename) {
347 Gatherer.f_man.renameCollectionFile(collection_tree, node);
348 }
349
350 // Replace
351 else if (source == replace) {
352 Gatherer.f_man.replaceCollectionFile(collection_tree, node);
353 }
354 }
355 }
356}
Note: See TracBrowser for help on using the repository browser.