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

Last change on this file since 13605 was 13605, checked in by mdewsnip, 17 years ago

Started work on the "Replace" option when using a remote Greenstone.

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