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, NZDL Project, University of Waikato
|
---|
11 | *
|
---|
12 | * <BR><BR>
|
---|
13 | *
|
---|
14 | * Copyright (C) 2005 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 | */
|
---|
37 | package org.greenstone.gatherer.file;
|
---|
38 |
|
---|
39 | import java.io.File;
|
---|
40 | import javax.swing.*;
|
---|
41 | import org.greenstone.gatherer.Dictionary;
|
---|
42 | import org.greenstone.gatherer.Gatherer;
|
---|
43 | import org.greenstone.gatherer.collection.CollectionManager;
|
---|
44 | import org.greenstone.gatherer.collection.CollectionTree;
|
---|
45 | import org.greenstone.gatherer.collection.CollectionTreeNode;
|
---|
46 | import org.greenstone.gatherer.gui.ExplodeMetadataDatabasePrompt;
|
---|
47 | import org.greenstone.gatherer.gui.ReplaceSrcDocWithHtmlPrompt;
|
---|
48 | import org.greenstone.gatherer.gui.GProgressBar;
|
---|
49 | import org.greenstone.gatherer.gui.NewFolderOrFilePrompt;
|
---|
50 | import org.greenstone.gatherer.gui.RenamePrompt;
|
---|
51 | import org.greenstone.gatherer.gui.tree.DragTree;
|
---|
52 | import org.greenstone.gatherer.remote.RemoteGreenstoneServer;
|
---|
53 | import org.greenstone.gatherer.util.DragComponent;
|
---|
54 | import org.greenstone.gatherer.util.Utility;
|
---|
55 |
|
---|
56 | /** Manages the moving of files within a separate thread.
|
---|
57 | * @author John Thompson, NZDL Project, University of Waikato
|
---|
58 | */
|
---|
59 | public class FileManager
|
---|
60 | {
|
---|
61 | /** Not only the queue of files to be moved, but also the object that moves them. */
|
---|
62 | static private FileQueue file_queue = null;
|
---|
63 |
|
---|
64 | public static int FILE_TYPE = 0;
|
---|
65 | public static int FOLDER_TYPE = 1;
|
---|
66 | protected static File startup_directory = null;
|
---|
67 |
|
---|
68 |
|
---|
69 | /** Constructor.
|
---|
70 | * @see org.greenstone.gatherer.file.FileQueue
|
---|
71 | */
|
---|
72 | public FileManager()
|
---|
73 | {
|
---|
74 | file_queue = new FileQueue();
|
---|
75 | file_queue.start();
|
---|
76 | }
|
---|
77 |
|
---|
78 |
|
---|
79 | /** Determine what action should be carried out by the file queue, and add all of the necessary file jobs. */
|
---|
80 | public void action(DragComponent source, FileNode[] source_nodes, DragComponent target, FileNode target_node)
|
---|
81 | {
|
---|
82 | // Check there is something to do
|
---|
83 | if (source_nodes == null || source_nodes.length == 0) {
|
---|
84 | return;
|
---|
85 | }
|
---|
86 |
|
---|
87 | // We need a unique ID for each file task
|
---|
88 | long id = System.currentTimeMillis();
|
---|
89 |
|
---|
90 | // If source and target are the same we're moving
|
---|
91 | if (source == target) {
|
---|
92 | // Start a new move FileTask and we're done
|
---|
93 | (new FileTask(id, source, source_nodes, target, target_node, FileJob.MOVE)).start();
|
---|
94 | return;
|
---|
95 | }
|
---|
96 |
|
---|
97 | // If target isn't the RecycleBin, we're copying
|
---|
98 | if (!(target instanceof RecycleBin)) {
|
---|
99 | // Start a new copy FileTask and we're done
|
---|
100 | (new FileTask(id, source, source_nodes, target, target_node, FileJob.COPY)).start();
|
---|
101 | return;
|
---|
102 | }
|
---|
103 |
|
---|
104 | // We're deleting... but first make sure source isn't read-only
|
---|
105 | boolean read_only_source = false;
|
---|
106 |
|
---|
107 | // The workspace tree is read-only...
|
---|
108 | if (source.toString().equals("Workspace")) {
|
---|
109 | read_only_source = true;
|
---|
110 |
|
---|
111 | // ...except for files from the "Downloaded Files" folder
|
---|
112 | String downloaded_files_folder_path = Gatherer.getGLIUserCacheDirectoryPath();
|
---|
113 | for (int i = 0; i < source_nodes.length; i++) {
|
---|
114 | // Is this the "Downloaded Files" folder?
|
---|
115 | if (source_nodes[i].getFile().getAbsolutePath().startsWith(downloaded_files_folder_path)) {
|
---|
116 | read_only_source = false;
|
---|
117 | }
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | // The source is read-only, so tell the user and abort
|
---|
122 | if (read_only_source) {
|
---|
123 | JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.Read_Only"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
|
---|
124 | return;
|
---|
125 | }
|
---|
126 |
|
---|
127 | // Start a new delete FileTask and we're done
|
---|
128 | (new FileTask(id, source, source_nodes, target, target_node, FileJob.DELETE)).start();
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 | /** Retrieves the file queue object. */
|
---|
133 | public FileQueue getQueue()
|
---|
134 | {
|
---|
135 | return file_queue;
|
---|
136 | }
|
---|
137 |
|
---|
138 |
|
---|
139 | private class FileTask
|
---|
140 | extends Thread
|
---|
141 | {
|
---|
142 | private long id;
|
---|
143 | private DragComponent source;
|
---|
144 | private FileNode[] source_nodes;
|
---|
145 | private DragComponent target;
|
---|
146 | private FileNode target_node;
|
---|
147 | private byte type;
|
---|
148 |
|
---|
149 |
|
---|
150 | public FileTask(long id, DragComponent source, FileNode[] source_nodes, DragComponent target, FileNode target_node, byte type)
|
---|
151 | {
|
---|
152 | this.id = id;
|
---|
153 | this.source = source;
|
---|
154 | this.source_nodes = source_nodes;
|
---|
155 | this.target = target;
|
---|
156 | this.target_node = target_node;
|
---|
157 | this.type = type;
|
---|
158 | }
|
---|
159 |
|
---|
160 |
|
---|
161 | public void run()
|
---|
162 | {
|
---|
163 | // Reset the progress bar and set it to indeterminate while calculating its size
|
---|
164 | GProgressBar progress_bar = file_queue.getProgressBar();
|
---|
165 | progress_bar.reset();
|
---|
166 | progress_bar.setIndeterminate(true);
|
---|
167 |
|
---|
168 | // Calculate the progress bar size
|
---|
169 | boolean cancelled = file_queue.calculateSize(source_nodes);
|
---|
170 | if (!cancelled) {
|
---|
171 | file_queue.addJob(id, source, source_nodes, target, target_node, type);
|
---|
172 | if (Gatherer.isGsdlRemote) {
|
---|
173 | String collection_name = CollectionManager.getLoadedCollectionName();
|
---|
174 |
|
---|
175 | // Perform the appropriate action based on the job type (RemoteGreenstoneServer will queue)
|
---|
176 | if (type == FileJob.COPY) {
|
---|
177 | // Copies: upload all the files at once in one zip file
|
---|
178 | File[] source_files = new File[source_nodes.length];
|
---|
179 | for (int i = 0; i < source_nodes.length; i++) {
|
---|
180 | source_files[i] = source_nodes[i].getFile();
|
---|
181 | }
|
---|
182 | RemoteGreenstoneServer.uploadFilesIntoCollection(collection_name, source_files, target_node.getFile());
|
---|
183 | }
|
---|
184 | else if (type == FileJob.DELETE) {
|
---|
185 | // Deletes: delete each top-level file/directory one at a time
|
---|
186 | for (int i = 0; i < source_nodes.length; i++) {
|
---|
187 | RemoteGreenstoneServer.deleteCollectionFile(collection_name, source_nodes[i].getFile());
|
---|
188 | }
|
---|
189 | }
|
---|
190 | else if (type == FileJob.MOVE) {
|
---|
191 | // Moves: move each top-level file/directory one at a time
|
---|
192 | for (int i = 0; i < source_nodes.length; i++) {
|
---|
193 | RemoteGreenstoneServer.moveCollectionFile(collection_name, source_nodes[i].getFile(), target_node.getFile());
|
---|
194 | }
|
---|
195 | }
|
---|
196 | }
|
---|
197 | }
|
---|
198 |
|
---|
199 | progress_bar.setIndeterminate(false);
|
---|
200 | progress_bar.clear();
|
---|
201 | }
|
---|
202 | }
|
---|
203 |
|
---|
204 | public void explodeMetadataDatabase(File file)
|
---|
205 | {
|
---|
206 | // This must go in a separate thread because we need the progress bar to work (remote Greenstone server)
|
---|
207 | new ExplodeMetadataDatabasePromptTask(file).start();
|
---|
208 | }
|
---|
209 |
|
---|
210 | // Works with replace_srcdoc_with_html.pl
|
---|
211 | public void replaceSrcDocWithHtml(File[] files)
|
---|
212 | {
|
---|
213 | // This must go in a separate thread because we need the progress bar to work (remote Greenstone server)
|
---|
214 | new ReplaceSrcDocWithHtmlPromptTask(files).start();
|
---|
215 | }
|
---|
216 |
|
---|
217 | private class ExplodeMetadataDatabasePromptTask
|
---|
218 | extends Thread
|
---|
219 | {
|
---|
220 | private File metadata_database_file = null;
|
---|
221 |
|
---|
222 | public ExplodeMetadataDatabasePromptTask(File metadata_database_file)
|
---|
223 | {
|
---|
224 | this.metadata_database_file = metadata_database_file;
|
---|
225 | }
|
---|
226 |
|
---|
227 | public void run()
|
---|
228 | {
|
---|
229 | ExplodeMetadataDatabasePrompt emp = new ExplodeMetadataDatabasePrompt(metadata_database_file);
|
---|
230 | }
|
---|
231 | }
|
---|
232 |
|
---|
233 | // Works with replace_srcdoc_with_html.pl
|
---|
234 | private class ReplaceSrcDocWithHtmlPromptTask
|
---|
235 | extends Thread
|
---|
236 | {
|
---|
237 | private File[] replace_these_srcdoc_files = null;
|
---|
238 |
|
---|
239 | public ReplaceSrcDocWithHtmlPromptTask(File[] replace_these_srcdoc_files)
|
---|
240 | {
|
---|
241 | this.replace_these_srcdoc_files = replace_these_srcdoc_files;
|
---|
242 | }
|
---|
243 |
|
---|
244 | public void run()
|
---|
245 | {
|
---|
246 | ReplaceSrcDocWithHtmlPrompt prompt = new ReplaceSrcDocWithHtmlPrompt(replace_these_srcdoc_files);
|
---|
247 | }
|
---|
248 | }
|
---|
249 |
|
---|
250 |
|
---|
251 | public void openFileInExternalApplication(File file)
|
---|
252 | {
|
---|
253 | // This must go in a separate thread because we need the progress bar to work (remote Greenstone server)
|
---|
254 | new OpenFileInExternalApplicationTask(file).start();
|
---|
255 | }
|
---|
256 |
|
---|
257 |
|
---|
258 | private class OpenFileInExternalApplicationTask
|
---|
259 | extends Thread
|
---|
260 | {
|
---|
261 | private File file = null;
|
---|
262 |
|
---|
263 | public OpenFileInExternalApplicationTask(File file)
|
---|
264 | {
|
---|
265 | this.file = file;
|
---|
266 | }
|
---|
267 |
|
---|
268 | public void run()
|
---|
269 | {
|
---|
270 | // If we're using a remote Greenstone server, we need to download the file before viewing it...
|
---|
271 | if (Gatherer.isGsdlRemote) {
|
---|
272 | // ... but only if it is inside the collection and we haven't already downloaded it
|
---|
273 | if (file.getAbsolutePath().startsWith(Gatherer.getCollectDirectoryPath()) && file.length() == 0) {
|
---|
274 | if (RemoteGreenstoneServer.downloadCollectionFile(CollectionManager.getLoadedCollectionName(), file).equals("")) {
|
---|
275 | // Something has gone wrong downloading the file
|
---|
276 | return;
|
---|
277 | }
|
---|
278 | }
|
---|
279 | }
|
---|
280 |
|
---|
281 | // View the file in an external application
|
---|
282 | Gatherer.spawnApplication(file);
|
---|
283 | }
|
---|
284 | }
|
---|
285 |
|
---|
286 |
|
---|
287 | public void newDummyDoc(DragTree tree, CollectionTreeNode parent_node){
|
---|
288 | newFolderOrDummyDoc(tree, parent_node, FILE_TYPE);
|
---|
289 | }
|
---|
290 |
|
---|
291 |
|
---|
292 | public void newFolder(DragTree tree, CollectionTreeNode parent_node) {
|
---|
293 | newFolderOrDummyDoc(tree, parent_node, FOLDER_TYPE);
|
---|
294 | }
|
---|
295 |
|
---|
296 |
|
---|
297 | protected void newFolderOrDummyDoc(DragTree tree, CollectionTreeNode parent_node, int type) {
|
---|
298 | (new NewFolderOrDummyDocumentTask(tree, parent_node, type)).start();
|
---|
299 | }
|
---|
300 |
|
---|
301 |
|
---|
302 | private class NewFolderOrDummyDocumentTask
|
---|
303 | extends Thread
|
---|
304 | {
|
---|
305 | private DragTree tree = null;
|
---|
306 | private CollectionTreeNode parent_node = null;
|
---|
307 | private int type;
|
---|
308 |
|
---|
309 | public NewFolderOrDummyDocumentTask(DragTree tree, CollectionTreeNode parent_node, int type)
|
---|
310 | {
|
---|
311 | this.tree = tree;
|
---|
312 | this.parent_node = parent_node;
|
---|
313 | this.type = type;
|
---|
314 | }
|
---|
315 |
|
---|
316 | public void run()
|
---|
317 | {
|
---|
318 | // Ask the user for the directories name.
|
---|
319 | String extension = "";
|
---|
320 | if (type == FILE_TYPE) {
|
---|
321 | extension = ".nul";
|
---|
322 | }
|
---|
323 |
|
---|
324 | NewFolderOrFilePrompt new_folder_prompt = new NewFolderOrFilePrompt(parent_node, type, extension);
|
---|
325 | String name = new_folder_prompt.display();
|
---|
326 | new_folder_prompt.dispose();
|
---|
327 | new_folder_prompt = null;
|
---|
328 |
|
---|
329 | // And if the name is non-null...
|
---|
330 | if (name != null) {
|
---|
331 | FileSystemModel model = (FileSystemModel) tree.getModel();
|
---|
332 | File folder_file = new File(parent_node.getFile(), name);
|
---|
333 |
|
---|
334 | //... check if it already exists.
|
---|
335 | if (folder_file.exists()) {
|
---|
336 | if (type == FILE_TYPE) {
|
---|
337 | JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.File_Already_Exists_No_Create", name), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
|
---|
338 | }
|
---|
339 | else {
|
---|
340 | JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.Folder_Already_Exists", name), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
|
---|
341 | }
|
---|
342 | }
|
---|
343 | // Otherwise create it.
|
---|
344 | else {
|
---|
345 | try {
|
---|
346 | if (type == FILE_TYPE) {
|
---|
347 | folder_file.createNewFile();
|
---|
348 | if (Gatherer.isGsdlRemote) {
|
---|
349 | RemoteGreenstoneServer.uploadCollectionFile(CollectionManager.getLoadedCollectionName(), folder_file);
|
---|
350 | }
|
---|
351 | }
|
---|
352 | else {
|
---|
353 | folder_file.mkdirs();
|
---|
354 | if (Gatherer.isGsdlRemote) {
|
---|
355 | RemoteGreenstoneServer.newCollectionDirectory(CollectionManager.getLoadedCollectionName(), folder_file);
|
---|
356 | }
|
---|
357 | }
|
---|
358 |
|
---|
359 | // Update the parent node to show the new folder
|
---|
360 | parent_node.refresh();
|
---|
361 |
|
---|
362 | // Refresh workspace tree (collection tree is done automatically)
|
---|
363 | Gatherer.g_man.refreshWorkspaceTree(DragTree.COLLECTION_CONTENTS_CHANGED);
|
---|
364 | }
|
---|
365 | catch (Exception exception) {
|
---|
366 | if (type == FILE_TYPE) {
|
---|
367 | JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.File_Create_Error", name), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
|
---|
368 | }
|
---|
369 | else {
|
---|
370 | JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.Folder_Create_Error", name), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
|
---|
371 | }
|
---|
372 | }
|
---|
373 | }
|
---|
374 |
|
---|
375 | folder_file = null;
|
---|
376 | model = null;
|
---|
377 | }
|
---|
378 | name = null;
|
---|
379 | }
|
---|
380 | }
|
---|
381 |
|
---|
382 |
|
---|
383 | public void renameCollectionFile(CollectionTree collection_tree, CollectionTreeNode collection_tree_node)
|
---|
384 | {
|
---|
385 | // This must go in a separate thread because we need the progress bar to work (remote Greenstone server)
|
---|
386 | new RenameTask(collection_tree, collection_tree_node).start();
|
---|
387 | }
|
---|
388 |
|
---|
389 |
|
---|
390 | private class RenameTask
|
---|
391 | extends Thread
|
---|
392 | {
|
---|
393 | private CollectionTree collection_tree = null;
|
---|
394 | private CollectionTreeNode collection_tree_node = null;
|
---|
395 |
|
---|
396 | public RenameTask(CollectionTree collection_tree, CollectionTreeNode collection_tree_node)
|
---|
397 | {
|
---|
398 | this.collection_tree = collection_tree;
|
---|
399 | this.collection_tree_node = collection_tree_node;
|
---|
400 | }
|
---|
401 |
|
---|
402 | public void run()
|
---|
403 | {
|
---|
404 | RenamePrompt rename_prompt = new RenamePrompt(collection_tree_node);
|
---|
405 | String new_collection_file_name = rename_prompt.display();
|
---|
406 | rename_prompt.dispose();
|
---|
407 | rename_prompt = null;
|
---|
408 |
|
---|
409 | if (new_collection_file_name != null) {
|
---|
410 | File collection_file = collection_tree_node.getFile();
|
---|
411 | File new_collection_file = new File(collection_file.getParentFile(), new_collection_file_name);
|
---|
412 | CollectionTreeNode new_collection_tree_node = new CollectionTreeNode(new_collection_file);
|
---|
413 | file_queue.addJob(System.currentTimeMillis(), collection_tree, new FileNode[] { collection_tree_node }, collection_tree, new_collection_tree_node, FileJob.RENAME);
|
---|
414 | if (Gatherer.isGsdlRemote) {
|
---|
415 | RemoteGreenstoneServer.moveCollectionFile(CollectionManager.getLoadedCollectionName(), collection_file, new_collection_file);
|
---|
416 | }
|
---|
417 | }
|
---|
418 | }
|
---|
419 | }
|
---|
420 |
|
---|
421 | public void replaceCollectionFile(CollectionTree collection_tree, CollectionTreeNode collection_tree_node)
|
---|
422 | {
|
---|
423 | // This must go in a separate thread because we need the progress bar to work (remote Greenstone server)
|
---|
424 | new ReplaceTask(collection_tree, collection_tree_node).start();
|
---|
425 | }
|
---|
426 |
|
---|
427 |
|
---|
428 | private class ReplaceTask
|
---|
429 | extends Thread
|
---|
430 | {
|
---|
431 | private CollectionTree collection_tree = null;
|
---|
432 | private CollectionTreeNode collection_tree_node = null;
|
---|
433 |
|
---|
434 | public ReplaceTask(CollectionTree collection_tree, CollectionTreeNode collection_tree_node)
|
---|
435 | {
|
---|
436 | this.collection_tree = collection_tree;
|
---|
437 | this.collection_tree_node = collection_tree_node;
|
---|
438 | }
|
---|
439 |
|
---|
440 | public void run()
|
---|
441 | {
|
---|
442 | JFileChooser file_chooser = new JFileChooser(startup_directory);
|
---|
443 | file_chooser.setDialogTitle(Dictionary.get("ReplacePrompt.Title"));
|
---|
444 | File new_file = null;
|
---|
445 | int return_val = file_chooser.showOpenDialog(null);
|
---|
446 | if(return_val == JFileChooser.APPROVE_OPTION) {
|
---|
447 | new_file = file_chooser.getSelectedFile();
|
---|
448 | }
|
---|
449 |
|
---|
450 | if (new_file == null) {
|
---|
451 | return;
|
---|
452 | }
|
---|
453 |
|
---|
454 | // save the search path for next time
|
---|
455 | startup_directory = new_file.getParentFile();
|
---|
456 | // make up a node for the file to bring in
|
---|
457 | WorkspaceTreeNode source_node = new WorkspaceTreeNode(new_file);
|
---|
458 |
|
---|
459 | File target_directory = collection_tree_node.getFile().getParentFile();
|
---|
460 | CollectionTreeNode new_collection_tree_node = new CollectionTreeNode(new File(target_directory, new_file.getName()));
|
---|
461 | // copy the new file in - but don't bring metadata
|
---|
462 | file_queue.addJob(System.currentTimeMillis(), Gatherer.g_man.gather_pane.workspace_tree, new FileNode[] { source_node }, collection_tree, (FileNode)collection_tree_node.getParent(), FileJob.COPY_FILE_ONLY);
|
---|
463 | if (Gatherer.isGsdlRemote) {
|
---|
464 | RemoteGreenstoneServer.uploadFilesIntoCollection(CollectionManager.getLoadedCollectionName(), new File[] { new_file }, target_directory);
|
---|
465 | }
|
---|
466 | // do a replace of old file with new file
|
---|
467 | file_queue.addJob(System.currentTimeMillis(), collection_tree, new FileNode[] { collection_tree_node }, collection_tree, new_collection_tree_node, FileJob.REPLACE);
|
---|
468 | }
|
---|
469 | }
|
---|
470 | }
|
---|