source: gli/trunk/src/org/greenstone/gatherer/file/FileManager.java@ 19230

Last change on this file since 19230 was 19230, checked in by ak19, 15 years ago

For remote GS server actions, calls getLoadedGroupQualifiedCollectionName() instead of getLoadedCollectionName()

  • Property svn:keywords set to Author Date Id Revision
File size: 16.0 KB
Line 
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 */
37package org.greenstone.gatherer.file;
38
39import java.io.File;
40import javax.swing.*;
41import org.greenstone.gatherer.Dictionary;
42import org.greenstone.gatherer.Gatherer;
43import org.greenstone.gatherer.collection.CollectionManager;
44import org.greenstone.gatherer.collection.CollectionTree;
45import org.greenstone.gatherer.collection.CollectionTreeNode;
46import org.greenstone.gatherer.gui.ExplodeMetadataDatabasePrompt;
47import org.greenstone.gatherer.gui.ReplaceSrcDocWithHtmlPrompt;
48import org.greenstone.gatherer.gui.GProgressBar;
49import org.greenstone.gatherer.gui.NewFolderOrFilePrompt;
50import org.greenstone.gatherer.gui.RenamePrompt;
51import org.greenstone.gatherer.gui.tree.DragTree;
52import org.greenstone.gatherer.remote.RemoteGreenstoneServer;
53import org.greenstone.gatherer.util.DragComponent;
54import 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 */
59public 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.getLoadedGroupQualifiedCollectionName(true);
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 Gatherer.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 Gatherer.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 Gatherer.remoteGreenstoneServer.moveCollectionFile(
194 collection_name, source_nodes[i].getFile(), target_node.getFile());
195 }
196 }
197 }
198 }
199
200 progress_bar.setIndeterminate(false);
201 progress_bar.clear();
202 }
203 }
204
205 public void explodeMetadataDatabase(File file)
206 {
207 // This must go in a separate thread because we need the progress bar to work (remote Greenstone server)
208 new ExplodeMetadataDatabasePromptTask(file).start();
209 }
210
211 // Works with replace_srcdoc_with_html.pl
212 public void replaceSrcDocWithHtml(File[] files)
213 {
214 // This must go in a separate thread because we need the progress bar to work (remote Greenstone server)
215 new ReplaceSrcDocWithHtmlPromptTask(files).start();
216 }
217
218 private class ExplodeMetadataDatabasePromptTask
219 extends Thread
220 {
221 private File metadata_database_file = null;
222
223 public ExplodeMetadataDatabasePromptTask(File metadata_database_file)
224 {
225 this.metadata_database_file = metadata_database_file;
226 }
227
228 public void run()
229 {
230 ExplodeMetadataDatabasePrompt emp = new ExplodeMetadataDatabasePrompt(metadata_database_file);
231 }
232 }
233
234 // Works with replace_srcdoc_with_html.pl
235 private class ReplaceSrcDocWithHtmlPromptTask
236 extends Thread
237 {
238 private File[] replace_these_srcdoc_files = null;
239
240 public ReplaceSrcDocWithHtmlPromptTask(File[] replace_these_srcdoc_files)
241 {
242 this.replace_these_srcdoc_files = replace_these_srcdoc_files;
243 }
244
245 public void run()
246 {
247 ReplaceSrcDocWithHtmlPrompt prompt = new ReplaceSrcDocWithHtmlPrompt(replace_these_srcdoc_files);
248 }
249 }
250
251
252 public void openFileInExternalApplication(File file)
253 {
254 // This must go in a separate thread because we need the progress bar to work (remote Greenstone server)
255 new OpenFileInExternalApplicationTask(file).start();
256 }
257
258
259 private class OpenFileInExternalApplicationTask
260 extends Thread
261 {
262 private File file = null;
263
264 public OpenFileInExternalApplicationTask(File file)
265 {
266 this.file = file;
267 }
268
269 public void run()
270 {
271 // If we're using a remote Greenstone server, we need to download the file before viewing it...
272 if (Gatherer.isGsdlRemote) {
273 // ... but only if it is inside the collection and we haven't already downloaded it
274 if (file.getAbsolutePath().startsWith(Gatherer.getCollectDirectoryPath()) && file.length() == 0) {
275 if (Gatherer.remoteGreenstoneServer.downloadCollectionFile(
276 CollectionManager.getLoadedGroupQualifiedCollectionName(true), file).equals("")) {
277 // Something has gone wrong downloading the file
278 return;
279 }
280 }
281 }
282
283 // View the file in an external application
284 Gatherer.spawnApplication(file);
285 }
286 }
287
288
289 public void newDummyDoc(DragTree tree, CollectionTreeNode parent_node){
290 newFolderOrDummyDoc(tree, parent_node, FILE_TYPE);
291 }
292
293
294 public void newFolder(DragTree tree, CollectionTreeNode parent_node) {
295 newFolderOrDummyDoc(tree, parent_node, FOLDER_TYPE);
296 }
297
298
299 protected void newFolderOrDummyDoc(DragTree tree, CollectionTreeNode parent_node, int type) {
300 (new NewFolderOrDummyDocumentTask(tree, parent_node, type)).start();
301 }
302
303
304 private class NewFolderOrDummyDocumentTask
305 extends Thread
306 {
307 private DragTree tree = null;
308 private CollectionTreeNode parent_node = null;
309 private int type;
310
311 public NewFolderOrDummyDocumentTask(DragTree tree, CollectionTreeNode parent_node, int type)
312 {
313 this.tree = tree;
314 this.parent_node = parent_node;
315 this.type = type;
316 }
317
318 public void run()
319 {
320 // Ask the user for the directories name.
321 String extension = "";
322 if (type == FILE_TYPE) {
323 extension = ".nul";
324 }
325
326 NewFolderOrFilePrompt new_folder_prompt = new NewFolderOrFilePrompt(parent_node, type, extension);
327 String name = new_folder_prompt.display();
328 new_folder_prompt.dispose();
329 new_folder_prompt = null;
330
331 // And if the name is non-null...
332 if (name != null) {
333 FileSystemModel model = (FileSystemModel) tree.getModel();
334 File folder_file = new File(parent_node.getFile(), name);
335
336 //... check if it already exists.
337 if (folder_file.exists()) {
338 if (type == FILE_TYPE) {
339 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.File_Already_Exists_No_Create", name), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
340 }
341 else {
342 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.Folder_Already_Exists", name), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
343 }
344 }
345 // Otherwise create it.
346 else {
347 try {
348 if (type == FILE_TYPE) {
349 folder_file.createNewFile();
350 if (Gatherer.isGsdlRemote) {
351 Gatherer.remoteGreenstoneServer.uploadCollectionFile(
352 CollectionManager.getLoadedGroupQualifiedCollectionName(true), folder_file);
353 }
354 }
355 else {
356 folder_file.mkdirs();
357 if (Gatherer.isGsdlRemote) {
358 Gatherer.remoteGreenstoneServer.newCollectionDirectory(
359 CollectionManager.getLoadedGroupQualifiedCollectionName(true), folder_file);
360 }
361 }
362
363 // Update the parent node to show the new folder
364 parent_node.refresh();
365
366 // Refresh workspace tree (collection tree is done automatically)
367 Gatherer.g_man.refreshWorkspaceTree(DragTree.COLLECTION_CONTENTS_CHANGED);
368 }
369 catch (Exception exception) {
370 if (type == FILE_TYPE) {
371 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.File_Create_Error", name), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
372 }
373 else {
374 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.Folder_Create_Error", name), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
375 }
376 }
377 }
378
379 folder_file = null;
380 model = null;
381 }
382 name = null;
383 }
384 }
385
386
387 public void renameCollectionFile(CollectionTree collection_tree, CollectionTreeNode collection_tree_node)
388 {
389 // This must go in a separate thread because we need the progress bar to work (remote Greenstone server)
390 new RenameTask(collection_tree, collection_tree_node).start();
391 }
392
393
394 private class RenameTask
395 extends Thread
396 {
397 private CollectionTree collection_tree = null;
398 private CollectionTreeNode collection_tree_node = null;
399
400 public RenameTask(CollectionTree collection_tree, CollectionTreeNode collection_tree_node)
401 {
402 this.collection_tree = collection_tree;
403 this.collection_tree_node = collection_tree_node;
404 }
405
406 public void run()
407 {
408 RenamePrompt rename_prompt = new RenamePrompt(collection_tree_node);
409 String new_collection_file_name = rename_prompt.display();
410 rename_prompt.dispose();
411 rename_prompt = null;
412
413 if (new_collection_file_name != null) {
414 File collection_file = collection_tree_node.getFile();
415 File new_collection_file = new File(collection_file.getParentFile(), new_collection_file_name);
416 CollectionTreeNode new_collection_tree_node = new CollectionTreeNode(new_collection_file);
417 file_queue.addJob(System.currentTimeMillis(), collection_tree, new FileNode[] { collection_tree_node }, collection_tree, new_collection_tree_node, FileJob.RENAME);
418 if (Gatherer.isGsdlRemote) {
419 Gatherer.remoteGreenstoneServer.moveCollectionFile(
420 CollectionManager.getLoadedGroupQualifiedCollectionName(true), collection_file, new_collection_file);
421 }
422 }
423 }
424 }
425
426 public void replaceCollectionFile(CollectionTree collection_tree, CollectionTreeNode collection_tree_node)
427 {
428 // This must go in a separate thread because we need the progress bar to work (remote Greenstone server)
429 new ReplaceTask(collection_tree, collection_tree_node).start();
430 }
431
432
433 private class ReplaceTask
434 extends Thread
435 {
436 private CollectionTree collection_tree = null;
437 private CollectionTreeNode collection_tree_node = null;
438
439 public ReplaceTask(CollectionTree collection_tree, CollectionTreeNode collection_tree_node)
440 {
441 this.collection_tree = collection_tree;
442 this.collection_tree_node = collection_tree_node;
443 }
444
445 public void run()
446 {
447 JFileChooser file_chooser = new JFileChooser(startup_directory);
448 file_chooser.setDialogTitle(Dictionary.get("ReplacePrompt.Title"));
449 File new_file = null;
450 int return_val = file_chooser.showOpenDialog(null);
451 if(return_val == JFileChooser.APPROVE_OPTION) {
452 new_file = file_chooser.getSelectedFile();
453 }
454
455 if (new_file == null) {
456 return;
457 }
458
459 // save the search path for next time
460 startup_directory = new_file.getParentFile();
461 // make up a node for the file to bring in
462 WorkspaceTreeNode source_node = new WorkspaceTreeNode(new_file);
463
464 File target_directory = collection_tree_node.getFile().getParentFile();
465 CollectionTreeNode new_collection_tree_node = new CollectionTreeNode(new File(target_directory, new_file.getName()));
466 // copy the new file in - but don't bring metadata
467 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);
468 if (Gatherer.isGsdlRemote) {
469 Gatherer.remoteGreenstoneServer.uploadFilesIntoCollection(
470 CollectionManager.getLoadedGroupQualifiedCollectionName(true), new File[] { new_file }, target_directory);
471 }
472 // do a replace of old file with new file
473 file_queue.addJob(System.currentTimeMillis(), collection_tree, new FileNode[] { collection_tree_node }, collection_tree, new_collection_tree_node, FileJob.REPLACE);
474 }
475 }
476}
Note: See TracBrowser for help on using the repository browser.