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) 2004 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.file;
|
---|
28 |
|
---|
29 | import java.io.*;
|
---|
30 | import java.util.*;
|
---|
31 | import javax.swing.tree.TreePath;
|
---|
32 | import org.greenstone.gatherer.Configuration;
|
---|
33 | import org.greenstone.gatherer.DebugStream;
|
---|
34 | import org.greenstone.gatherer.Dictionary;
|
---|
35 | import org.greenstone.gatherer.Gatherer;
|
---|
36 | import org.greenstone.gatherer.util.SynchronizedTreeModelTools;
|
---|
37 |
|
---|
38 |
|
---|
39 | public class WorkspaceTreeModel
|
---|
40 | extends FileSystemModel
|
---|
41 | {
|
---|
42 | static private WorkspaceTreeModel workspace_tree_model = null;
|
---|
43 | static private WorkspaceTreeNode workspace_tree_root = null;
|
---|
44 |
|
---|
45 | static private WorkspaceTreeNode greenstone_collections_node = null;
|
---|
46 | static private WorkspaceTreeNode local_filespace_node = null;
|
---|
47 | static private WorkspaceTreeNode downloaded_files_node = null;
|
---|
48 | static private WorkspaceTreeNode[] folder_shortcuts = null;
|
---|
49 |
|
---|
50 |
|
---|
51 | public WorkspaceTreeModel(WorkspaceTreeNode root_node)
|
---|
52 | {
|
---|
53 | super(root_node);
|
---|
54 | }
|
---|
55 |
|
---|
56 |
|
---|
57 | static public WorkspaceTreeNode[] getFolderShortcuts()
|
---|
58 | {
|
---|
59 | // Return any predefined special directories
|
---|
60 | HashMap mappings = Configuration.getDirectoryMappings();
|
---|
61 | WorkspaceTreeNode[] mapping_nodes = new WorkspaceTreeNode[mappings.size()];
|
---|
62 | Iterator mappings_iterator = mappings.keySet().iterator();
|
---|
63 | for (int i = 0; mappings_iterator.hasNext(); i++) {
|
---|
64 | String mapping_name = (String) mappings_iterator.next();
|
---|
65 | File mapping_file = (File) mappings.get(mapping_name);
|
---|
66 | mapping_nodes[i] = new WorkspaceTreeNode(mapping_file, mapping_name);
|
---|
67 | }
|
---|
68 | return mapping_nodes;
|
---|
69 | }
|
---|
70 |
|
---|
71 |
|
---|
72 | static public WorkspaceTreeModel getWorkspaceTreeModel()
|
---|
73 | {
|
---|
74 | // Create a root node to contain the various nodes in the workspace tree
|
---|
75 | workspace_tree_root = new WorkspaceTreeNode(null, "ABS_ROOT");
|
---|
76 | workspace_tree_model = new WorkspaceTreeModel(workspace_tree_root);
|
---|
77 |
|
---|
78 | // Add the "Documents in Greenstone Collections" node
|
---|
79 | greenstone_collections_node = new WorkspaceTreeNode(null, Dictionary.get("Tree.World"));
|
---|
80 | workspace_tree_root.add(greenstone_collections_node);
|
---|
81 |
|
---|
82 | // Add the "Local Filespace" node
|
---|
83 | local_filespace_node = FileSystem.getLocalFilespaceNode(workspace_tree_model);
|
---|
84 | workspace_tree_root.add(local_filespace_node);
|
---|
85 |
|
---|
86 | // Add the "Home Folder" node
|
---|
87 | workspace_tree_root.add(FileSystem.getHomeFolderNode());
|
---|
88 |
|
---|
89 | // Add the "Downloaded Files" node, if the Download pane is enabled
|
---|
90 | if (Gatherer.g_man.download_pane != null) {
|
---|
91 | downloaded_files_node = FileSystem.getDownloadedFilesNode();
|
---|
92 | workspace_tree_root.add(downloaded_files_node);
|
---|
93 | }
|
---|
94 |
|
---|
95 | // Add any folder shortcuts the user has created
|
---|
96 | refreshFolderShortcuts();
|
---|
97 |
|
---|
98 | return workspace_tree_model;
|
---|
99 | }
|
---|
100 |
|
---|
101 |
|
---|
102 | static public void refreshGreenstoneCollectionsNode()
|
---|
103 | {
|
---|
104 | greenstone_collections_node.refresh();
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | static public void refreshDownloadedFilesNode()
|
---|
109 | {
|
---|
110 | downloaded_files_node.refresh();
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | static public void refreshFolderShortcuts()
|
---|
115 | {
|
---|
116 | // Check for new/deleted folder shortcuts
|
---|
117 | WorkspaceTreeNode[] old_folder_shortcuts = folder_shortcuts;
|
---|
118 | folder_shortcuts = getFolderShortcuts();
|
---|
119 |
|
---|
120 | // Remove any deleted shortcuts from the tree
|
---|
121 | if (old_folder_shortcuts != null) {
|
---|
122 | for (int i = 0; i < old_folder_shortcuts.length; i++) {
|
---|
123 | if (!doesArrayContain(folder_shortcuts, old_folder_shortcuts[i])) {
|
---|
124 | DebugStream.println("Deleted shortcut: " + old_folder_shortcuts[i]);
|
---|
125 | SynchronizedTreeModelTools.removeNodeFromParent(workspace_tree_model, old_folder_shortcuts[i]);
|
---|
126 | }
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | // Add any new shortcuts to the tree
|
---|
131 | if (folder_shortcuts != null) {
|
---|
132 | for (int i = 0; i < folder_shortcuts.length; i++) {
|
---|
133 | if (!doesArrayContain(old_folder_shortcuts, folder_shortcuts[i])) {
|
---|
134 | DebugStream.println("Added shortcut: " + folder_shortcuts[i]);
|
---|
135 | SynchronizedTreeModelTools.insertNodeInto(workspace_tree_model, workspace_tree_root, folder_shortcuts[i], false);
|
---|
136 | }
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 |
|
---|
142 | // -- This code is necessary to support the workspace tree file filter --
|
---|
143 | public void refresh(TreePath path)
|
---|
144 | {
|
---|
145 | // If we're not refreshing the whole tree just refresh a certain path
|
---|
146 | if (path != null) {
|
---|
147 | super.refresh(path);
|
---|
148 | return;
|
---|
149 | }
|
---|
150 |
|
---|
151 | // Refresh each of the nodes in the workspace tree
|
---|
152 | for (int i = 0; i < workspace_tree_root.getChildCount(); i++) {
|
---|
153 | WorkspaceTreeNode child_node = (WorkspaceTreeNode) workspace_tree_root.getChildAt(i);
|
---|
154 | super.refresh(new TreePath(child_node.getPath()));
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 |
|
---|
159 | static private boolean doesArrayContain(Object[] array, Object item)
|
---|
160 | {
|
---|
161 | if (array == null) {
|
---|
162 | return false;
|
---|
163 | }
|
---|
164 |
|
---|
165 | for (int i = 0; i < array.length; i++) {
|
---|
166 | if (item.toString().equals(array[i].toString())) {
|
---|
167 | return true;
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | return false;
|
---|
172 | }
|
---|
173 | }
|
---|