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

Last change on this file since 12345 was 11622, checked in by mdewsnip, 18 years ago

Lots more object-orientation improvements to the collection and workspace trees.

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