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 |
|
---|
27 | package org.greenstone.gatherer.collection;
|
---|
28 |
|
---|
29 | import java.awt.*;
|
---|
30 | import java.awt.event.*;
|
---|
31 | import javax.swing.*;
|
---|
32 | import javax.swing.tree.*;
|
---|
33 | import org.greenstone.gatherer.Configuration;
|
---|
34 | import org.greenstone.gatherer.Dictionary;
|
---|
35 | import org.greenstone.gatherer.Gatherer;
|
---|
36 | import org.greenstone.gatherer.gui.tree.DragTree;
|
---|
37 | import org.greenstone.gatherer.gui.tree.DragTreeCellRenderer;
|
---|
38 |
|
---|
39 |
|
---|
40 | public 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 | // !! TO DO: Remote building
|
---|
237 | if (!Gatherer.isGsdlRemote) {
|
---|
238 | rename = new JMenuItem(Dictionary.get("CollectionPopupMenu.Rename"), KeyEvent.VK_R);
|
---|
239 | rename.addActionListener(this);
|
---|
240 | add(rename);
|
---|
241 | }
|
---|
242 |
|
---|
243 | TreePath path = selection_paths[0];
|
---|
244 | node = (CollectionTreeNode) path.getLastPathComponent();
|
---|
245 |
|
---|
246 | // ---- Options for file nodes ----
|
---|
247 | if (node.isLeaf()) {
|
---|
248 | // Explode metadata databases, for explodable files only
|
---|
249 | if (node.isExplodable()) {
|
---|
250 | explode_metadata_database = new JMenuItem(Dictionary.get("Menu.Explode_Metadata_Database"), KeyEvent.VK_E);
|
---|
251 | explode_metadata_database.addActionListener(this);
|
---|
252 | add(explode_metadata_database);
|
---|
253 | }
|
---|
254 | // Replace file
|
---|
255 | // !! TO DO: Remote building
|
---|
256 | if (!Gatherer.isGsdlRemote) {
|
---|
257 | replace = new JMenuItem(Dictionary.get("CollectionPopupMenu.Replace"), KeyEvent.VK_P);
|
---|
258 | replace.addActionListener(this);
|
---|
259 | add(replace);
|
---|
260 | }
|
---|
261 |
|
---|
262 | // Open the file in an external program
|
---|
263 | open_externally = new JMenuItem(Dictionary.get("Menu.Open_Externally"), KeyEvent.VK_O);
|
---|
264 | open_externally.addActionListener(this);
|
---|
265 | add(open_externally);
|
---|
266 |
|
---|
267 | return;
|
---|
268 | }
|
---|
269 |
|
---|
270 | // ---- Options for folder nodes ----
|
---|
271 | // Collapse or expand, depending on current status
|
---|
272 | if (collection_tree.isExpanded(path)) {
|
---|
273 | collapse_folder = new JMenuItem(Dictionary.get("Menu.Collapse"), KeyEvent.VK_C);
|
---|
274 | collapse_folder.addActionListener(this);
|
---|
275 | add(collapse_folder);
|
---|
276 | }
|
---|
277 | else {
|
---|
278 | expand_folder = new JMenuItem(Dictionary.get("Menu.Expand"), KeyEvent.VK_O);
|
---|
279 | expand_folder.addActionListener(this);
|
---|
280 | add(expand_folder);
|
---|
281 | }
|
---|
282 |
|
---|
283 | // New folder/dummy doc options
|
---|
284 | if (!node.isReadOnly()) {
|
---|
285 | new_folder = new JMenuItem(Dictionary.get("CollectionPopupMenu.New_Folder"), KeyEvent.VK_N);
|
---|
286 | new_folder.addActionListener(this);
|
---|
287 | add(new_folder);
|
---|
288 |
|
---|
289 | new_dummy_doc = new JMenuItem(Dictionary.get("CollectionPopupMenu.New_Dummy_Doc"));
|
---|
290 | new_dummy_doc.addActionListener(this);
|
---|
291 | add(new_dummy_doc);
|
---|
292 | }
|
---|
293 | }
|
---|
294 |
|
---|
295 |
|
---|
296 | /** Called whenever one of the menu items is clicked, this method then causes the appropriate effect. */
|
---|
297 | public void actionPerformed(ActionEvent event)
|
---|
298 | {
|
---|
299 | Object source = event.getSource();
|
---|
300 |
|
---|
301 | // Collapse folder
|
---|
302 | if (source == collapse_folder) {
|
---|
303 | collection_tree.collapsePath(selection_paths[0]);
|
---|
304 | }
|
---|
305 |
|
---|
306 | // Expand folder
|
---|
307 | else if (source == expand_folder) {
|
---|
308 | collection_tree.expandPath(selection_paths[0]);
|
---|
309 | }
|
---|
310 |
|
---|
311 | // Explode metadata database
|
---|
312 | else if (source == explode_metadata_database) {
|
---|
313 | Gatherer.f_man.explodeMetadataDatabase(node.getFile());
|
---|
314 | }
|
---|
315 |
|
---|
316 | // Delete
|
---|
317 | else if (source == delete) {
|
---|
318 | CollectionTreeNode[] source_nodes = new CollectionTreeNode[selection_paths.length];
|
---|
319 | for (int i = 0; i < selection_paths.length; i++) {
|
---|
320 | source_nodes[i] = (CollectionTreeNode) selection_paths[i].getLastPathComponent();
|
---|
321 | }
|
---|
322 |
|
---|
323 | // Fire a delete action
|
---|
324 | Gatherer.f_man.action(collection_tree, source_nodes, Gatherer.recycle_bin, null);
|
---|
325 | }
|
---|
326 |
|
---|
327 | // Meta-audit
|
---|
328 | else if (source == metaaudit) {
|
---|
329 | Gatherer.g_man.showMetaAuditBox();
|
---|
330 | }
|
---|
331 |
|
---|
332 | // New folder
|
---|
333 | else if (source == new_folder) {
|
---|
334 | Gatherer.f_man.newFolder(collection_tree, node);
|
---|
335 | }
|
---|
336 |
|
---|
337 | // New dummy doc
|
---|
338 | else if (source == new_dummy_doc) {
|
---|
339 | Gatherer.f_man.newDummyDoc(collection_tree, node);
|
---|
340 | }
|
---|
341 |
|
---|
342 | // Open in external program
|
---|
343 | else if (source == open_externally) {
|
---|
344 | Gatherer.f_man.openFileInExternalApplication(node.getFile());
|
---|
345 | }
|
---|
346 |
|
---|
347 | // Rename
|
---|
348 | else if (source == rename) {
|
---|
349 | Gatherer.f_man.renameCollectionFile(collection_tree, node);
|
---|
350 | }
|
---|
351 |
|
---|
352 | // Replace
|
---|
353 | else if (source == replace) {
|
---|
354 | Gatherer.f_man.replaceCollectionFile(collection_tree, node);
|
---|
355 | }
|
---|
356 | }
|
---|
357 | }
|
---|
358 | }
|
---|