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

Last change on this file since 37341 was 37341, checked in by davidb, 14 months ago

Next phase of webswing gli development: better colour for text on disabled tab text; busy cursor shown when first loading in; expanded to previewURL and downloadURL for better future-proofing

  • Property svn:keywords set to Author Date Id Revision
File size: 25.3 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.*;
41
42import org.webswing.toolkit.api.WebswingUtil;
43
44import org.greenstone.gatherer.Configuration;
45import org.greenstone.gatherer.Dictionary;
46import org.greenstone.gatherer.Gatherer;
47import org.greenstone.gatherer.collection.CollectionManager;
48import org.greenstone.gatherer.collection.CollectionTree;
49import org.greenstone.gatherer.collection.CollectionTreeNode;
50import org.greenstone.gatherer.gui.ExplodeMetadataDatabasePrompt;
51import org.greenstone.gatherer.gui.ReplaceSrcDocWithHtmlPrompt;
52import org.greenstone.gatherer.gui.GProgressBar;
53import org.greenstone.gatherer.gui.NewFolderOrFilePrompt;
54import org.greenstone.gatherer.gui.RenamePrompt;
55import org.greenstone.gatherer.gui.tree.DragTree;
56import org.greenstone.gatherer.remote.RemoteGreenstoneServer;
57import org.greenstone.gatherer.util.DragComponent;
58import org.greenstone.gatherer.util.Utility;
59import org.greenstone.gatherer.DebugStream;
60
61/** Manages the moving of files within a separate thread.
62 * @author John Thompson, NZDL Project, University of Waikato
63 */
64public class FileManager
65{
66 /** Not only the queue of files to be moved, but also the object that moves them. */
67 static private FileQueue file_queue = null;
68
69 public static final int COPY = 0;
70 public static final int MOVE = 1;
71
72 public static final int FILE_TYPE = 0;
73 public static final int FOLDER_TYPE = 1;
74
75 protected static File startup_directory = null;
76
77
78 /** Constructor.
79 * @see org.greenstone.gatherer.file.FileQueue
80 */
81 public FileManager()
82 {
83 file_queue = new FileQueue();
84 file_queue.start();
85 }
86
87
88 /** Determine what action should be carried out by the file queue, and add all of the necessary file jobs. */
89 public void action(DragComponent source, FileNode[] source_nodes, DragComponent target, FileNode target_node)
90 {
91 // Check there is something to do
92 if (source_nodes == null || source_nodes.length == 0) {
93 return;
94 }
95
96 // We need a unique ID for each file task
97 long id = System.currentTimeMillis();
98
99 // If source and target are the same we're moving
100 if (source == target) {
101 // Start a new move FileTask and we're done
102 (new FileTask(id, source, source_nodes, target, target_node, FileJob.MOVE)).start();
103 return;
104 }
105
106 // If target isn't the RecycleBin, we're copying
107 if (!(target instanceof RecycleBin)) {
108 // Start a new copy FileTask and we're done
109 (new FileTask(id, source, source_nodes, target, target_node, FileJob.COPY)).start();
110 return;
111 }
112
113 // We're deleting... but first make sure source isn't read-only
114 boolean read_only_source = false;
115
116 // The workspace tree is read-only...
117 if (source.toString().equals("Workspace")) {
118 read_only_source = true;
119
120 // ...except for files from the "Downloaded Files" folder
121 String downloaded_files_folder_path = Gatherer.getGLIUserCacheDirectoryPath();
122 for (int i = 0; i < source_nodes.length; i++) {
123 // Is this the "Downloaded Files" folder?
124 if (source_nodes[i].getFile().getAbsolutePath().startsWith(downloaded_files_folder_path)) {
125 read_only_source = false;
126 }
127 }
128 }
129
130 // The source is read-only, so tell the user and abort
131 if (read_only_source) {
132 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.Read_Only"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
133 return;
134 }
135
136 // Start a new delete FileTask and we're done
137 (new FileTask(id, source, source_nodes, target, target_node, FileJob.DELETE)).start();
138 }
139
140 /** For moving and copying of folders. */
141 public void action(File sourceFolder, File targetFolder, int operation) {
142 (new SimpleFileTask(sourceFolder, targetFolder, operation)).start();
143 }
144
145
146 /** Retrieves the file queue object. */
147 public FileQueue getQueue()
148 {
149 return file_queue;
150 }
151
152 /** Performs the simple file task of moving or copying folders. */
153 private class SimpleFileTask
154 extends Thread
155 {
156 private File sourceFolder;
157 private File targetFolder;
158 int operation; // MOVE or COPY
159
160 public SimpleFileTask(File sourceFolder, File targetFolder, int operation)
161 {
162 this.sourceFolder = sourceFolder;
163 this.targetFolder = targetFolder;
164 this.operation = operation;
165 }
166
167
168 public void run()
169 {
170 // check if we're moving or overwriting the current collection
171 String currentColPath = Gatherer.getCollectDirectoryPath()+CollectionManager.getLoadedCollectionName();
172 if(currentColPath.equals(sourceFolder.getAbsolutePath())
173 || currentColPath.equals(targetFolder.getAbsolutePath())) {
174 Gatherer.g_man.saveThenCloseCurrentCollection();
175 }
176
177 // if moving, try a simple move operation (if it works, it
178 // shouldn't take long at all and doesn't need a progress bar)
179 if(operation == MOVE && sourceFolder.renameTo(targetFolder)) {
180 //System.err.println("**** A simple renameTo() worked.");
181 WorkspaceTreeModel.refreshGreenstoneCollectionsNode();
182 return;
183 }
184
185 // Reset the progress bar and set it to indeterminate while calculating its size
186 GProgressBar progress_bar = file_queue.getProgressBar();
187 progress_bar.reset();
188 progress_bar.setIndeterminate(true);
189
190 String status = "FileActions.Moving";
191 if(operation == COPY) {
192 status = "FileActions.Copying";
193 }
194 progress_bar.setString(Dictionary.get(status));
195 file_queue.getFileStatus().setText(Dictionary.get(status,
196 file_queue.formatPath(status,
197 sourceFolder.getAbsolutePath(),
198 file_queue.getFileStatus().getSize().width)));
199
200 // do the move or copy operation
201 try {
202 //System.err.println("**** Copying " + sourceFolder + " to: " + targetFolder);
203 file_queue.copyDirectoryContents(sourceFolder, targetFolder);
204 } catch(Exception e) {
205 JOptionPane.showMessageDialog(Gatherer.g_man, e.getMessage(),
206 "Can't perform file operation", JOptionPane.ERROR_MESSAGE);
207
208 progress_bar.setIndeterminate(false);
209 progress_bar.clear();
210 return;
211 }
212
213 // if moving, delete the original source folder and
214 // update the docs in GS collections node in the workspace tree
215
216 if(operation == MOVE) {
217 Utility.delete(sourceFolder);
218 WorkspaceTreeModel.refreshGreenstoneCollectionsNode();
219 }
220
221
222 progress_bar.setIndeterminate(false);
223 progress_bar.clear();
224 file_queue.getFileStatus().setText(Dictionary.get("FileActions.No_Activity"));
225 progress_bar.setString(Dictionary.get("FileActions.No_Activity"));
226 }
227 }
228
229 private class FileTask
230 extends Thread
231 {
232 private long id;
233 private DragComponent source;
234 private FileNode[] source_nodes;
235 private DragComponent target;
236 private FileNode target_node;
237 private byte type;
238
239
240 public FileTask(long id, DragComponent source, FileNode[] source_nodes, DragComponent target, FileNode target_node, byte type)
241 {
242 this.id = id;
243 this.source = source;
244 this.source_nodes = source_nodes;
245 this.target = target;
246 this.target_node = target_node;
247 this.type = type;
248 }
249
250
251 public void run()
252 {
253 // Reset the progress bar and set it to indeterminate while calculating its size
254 GProgressBar progress_bar = file_queue.getProgressBar();
255 progress_bar.reset();
256 progress_bar.setIndeterminate(true);
257
258 // Calculate the progress bar size
259 boolean cancelled = file_queue.calculateSize(source_nodes);
260 if (!cancelled) {
261 file_queue.addJob(id, source, source_nodes, target, target_node, type);
262 if (Gatherer.isGsdlRemote) {
263 String collection_name = CollectionManager.getLoadedCollectionName();
264
265 // Perform the appropriate action based on the job type (RemoteGreenstoneServer will queue)
266 if (type == FileJob.COPY) {
267 // Copies: upload all the files at once in one zip file
268 File[] source_files = new File[source_nodes.length];
269 for (int i = 0; i < source_nodes.length; i++) {
270 source_files[i] = source_nodes[i].getFile();
271 }
272 Gatherer.remoteGreenstoneServer.uploadFilesIntoCollection(collection_name, source_files, target_node.getFile());
273 }
274 else if (type == FileJob.DELETE) {
275 // Deletes: delete each top-level file/directory one at a time
276 for (int i = 0; i < source_nodes.length; i++) {
277 Gatherer.remoteGreenstoneServer.deleteCollectionFile(collection_name, source_nodes[i].getFile());
278 }
279 }
280 else if (type == FileJob.MOVE) {
281 // Moves: move each top-level file/directory one at a time
282 for (int i = 0; i < source_nodes.length; i++) {
283 Gatherer.remoteGreenstoneServer.moveCollectionFile(
284 collection_name, source_nodes[i].getFile(), target_node.getFile());
285 }
286 }
287 }
288 }
289
290 progress_bar.setIndeterminate(false);
291 progress_bar.clear();
292 }
293 }
294
295 public void explodeMetadataDatabase(File file)
296 {
297 // This must go in a separate thread because we need the progress bar to work (remote Greenstone server)
298 new ExplodeMetadataDatabasePromptTask(file).start();
299 }
300
301 // Works with replace_srcdoc_with_html.pl
302 public void replaceSrcDocWithHtml(File[] files)
303 {
304 // This must go in a separate thread because we need the progress bar to work (remote Greenstone server)
305 new ReplaceSrcDocWithHtmlPromptTask(files).start();
306 }
307
308 private class ExplodeMetadataDatabasePromptTask
309 extends Thread
310 {
311 private File metadata_database_file = null;
312
313 public ExplodeMetadataDatabasePromptTask(File metadata_database_file)
314 {
315 this.metadata_database_file = metadata_database_file;
316 }
317
318 public void run()
319 {
320 ExplodeMetadataDatabasePrompt emp = new ExplodeMetadataDatabasePrompt(metadata_database_file);
321 }
322 }
323
324 // Works with replace_srcdoc_with_html.pl
325 private class ReplaceSrcDocWithHtmlPromptTask
326 extends Thread
327 {
328 private File[] replace_these_srcdoc_files = null;
329
330 public ReplaceSrcDocWithHtmlPromptTask(File[] replace_these_srcdoc_files)
331 {
332 this.replace_these_srcdoc_files = replace_these_srcdoc_files;
333 }
334
335 public void run()
336 {
337 ReplaceSrcDocWithHtmlPrompt prompt = new ReplaceSrcDocWithHtmlPrompt(replace_these_srcdoc_files);
338 }
339 }
340
341
342 public void openFileInExternalApplication(File file)
343 {
344 // This must go in a separate thread because we need the progress bar to work (remote Greenstone server)
345 new OpenFileInExternalApplicationTask(file).start();
346 }
347
348
349 private class OpenFileInExternalApplicationTask
350 extends Thread
351 {
352 private File file = null;
353
354 public OpenFileInExternalApplicationTask(File file)
355 {
356 this.file = file;
357 }
358
359 public void run()
360 {
361 // If we're using a remote Greenstone server, we need to download the file before viewing it...
362 if (Gatherer.isGsdlRemote) {
363 // ... but only if it is inside the collection and we haven't already downloaded it
364 if (file.getAbsolutePath().startsWith(Gatherer.getCollectDirectoryPath()) && file.length() == 0) {
365 if (Gatherer.remoteGreenstoneServer.downloadCollectionFile(
366 CollectionManager.getLoadedCollectionName(), file).equals("")) {
367 // Something has gone wrong downloading the file
368 return;
369 }
370 }
371 }
372 else if (Gatherer.isWebswing) {
373 if (Gatherer.GS3) {
374 String full_filename = file.getAbsolutePath();
375 String collect_dirname = Gatherer.getCollectDirectoryPath();
376
377 if (full_filename.startsWith(collect_dirname)) {
378 // Strip off everything up to .../site/<site>/collect
379 // => so local_filename start with the name of the actual collection
380
381 String local_filename = full_filename.substring(collect_dirname.length());
382 //String coll_name = CollectionManager.getLoadedCollectionTailName();
383 String opt_group_name = CollectionManager.getLoadedCollectionGroupName();
384
385 //String this_collect_dirname = collect_dirname + coll_group_with_name;
386 //if (full_filename.startsWith(this_collect_dirname)) {
387 //String local_filename = full_filename.substring(this_collect_dirname.length());
388
389 String library_url = Configuration.library_url.toString();
390 String site = Configuration.site_name;
391
392
393 //String library_collect_url = library_url + "/sites/" + site + "/collect/" + coll_group_with_name;
394 String library_site_url = library_url + "/sites/" + site;
395
396 String library_collect_url = library_site_url + "/collect";
397 if (opt_group_name != null && !opt_group_name.equals("")) {
398 library_collect_url = library_collect_url + "/" + opt_group_name;
399 }
400
401 String library_collect_file_url = library_collect_url + "/" + local_filename;
402
403 WebswingUtil.getWebswingApi().sendActionEvent("downloadURL", library_collect_file_url, null);
404 }
405 else {
406 System.err.println("Warning - OpenFileInExternalApplicationTask.run() unable to construct a URL to access the following file" );
407 System.err.println(" " + full_filename);
408 System.err.println("Did not fall within " + collect_dirname);
409
410 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.Webswing_Unable_To_Open"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
411 }
412 }
413 else {
414 System.err.println("Error - Greenstone2 version of GLI using WebSwing does not currently support opening files externally" );
415 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.Webswing_Unable_To_Open_GS2"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
416 }
417
418 // finish here
419 return;
420 }
421
422 // View the file in an external application
423 Gatherer.spawnApplication(file);
424 }
425 }
426
427
428 public void newDummyDoc(DragTree tree, CollectionTreeNode parent_node){
429 newFolderOrDummyDoc(tree, parent_node, FILE_TYPE);
430 }
431
432
433 public void newFolder(DragTree tree, CollectionTreeNode parent_node) {
434 newFolderOrDummyDoc(tree, parent_node, FOLDER_TYPE);
435 }
436
437
438 protected void newFolderOrDummyDoc(DragTree tree, CollectionTreeNode parent_node, int type) {
439 (new NewFolderOrDummyDocumentTask(tree, parent_node, type)).start();
440 }
441
442
443 private class NewFolderOrDummyDocumentTask
444 extends Thread
445 {
446 private DragTree tree = null;
447 private CollectionTreeNode parent_node = null;
448 private int type;
449
450 public NewFolderOrDummyDocumentTask(DragTree tree, CollectionTreeNode parent_node, int type)
451 {
452 this.tree = tree;
453 this.parent_node = parent_node;
454 this.type = type;
455 }
456
457 public void run()
458 {
459 // Ask the user for the directories name.
460 String extension = "";
461 if (type == FILE_TYPE) {
462 extension = ".nul";
463 }
464
465 NewFolderOrFilePrompt new_folder_prompt = new NewFolderOrFilePrompt(parent_node, type, extension);
466 String name = new_folder_prompt.display();
467 new_folder_prompt.dispose();
468 new_folder_prompt = null;
469
470 // And if the name is non-null...
471 if (name != null) {
472 FileSystemModel model = (FileSystemModel) tree.getModel();
473 File folder_file = new File(parent_node.getFile(), name);
474
475 //... check if it already exists.
476 if (folder_file.exists()) {
477 if (type == FILE_TYPE) {
478 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.File_Already_Exists_No_Create", name), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
479 }
480 else {
481 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.Folder_Already_Exists", name), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
482 }
483 }
484 // Otherwise create it.
485 else {
486 try {
487 if (type == FILE_TYPE) {
488 folder_file.createNewFile();
489 if (Gatherer.isGsdlRemote) {
490 Gatherer.remoteGreenstoneServer.uploadCollectionFile(
491 CollectionManager.getLoadedCollectionName(), folder_file);
492 }
493 }
494 else {
495 folder_file.mkdirs();
496 if (Gatherer.isGsdlRemote) {
497 Gatherer.remoteGreenstoneServer.newCollectionDirectory(
498 CollectionManager.getLoadedCollectionName(), folder_file);
499 }
500 }
501
502 // Update the parent node to show the new folder
503 parent_node.refresh();
504
505 // Refresh workspace tree (collection tree is done automatically)
506 Gatherer.g_man.refreshWorkspaceTree(DragTree.COLLECTION_CONTENTS_CHANGED);
507 }
508 catch (Exception exception) {
509 if (type == FILE_TYPE) {
510 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.File_Create_Error", name), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
511 }
512 else {
513 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.Folder_Create_Error", name), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
514 }
515 }
516 }
517
518 folder_file = null;
519 model = null;
520 }
521 name = null;
522 }
523 }
524
525
526 public void renameCollectionFile(CollectionTree collection_tree, CollectionTreeNode collection_tree_node)
527 {
528 // This must go in a separate thread because we need the progress bar to work (remote Greenstone server)
529 new RenameTask(collection_tree, collection_tree_node).start();
530 }
531
532
533 private class RenameTask
534 extends Thread
535 {
536 private CollectionTree collection_tree = null;
537 private CollectionTreeNode collection_tree_node = null;
538
539 public RenameTask(CollectionTree collection_tree, CollectionTreeNode collection_tree_node)
540 {
541 this.collection_tree = collection_tree;
542 this.collection_tree_node = collection_tree_node;
543 }
544
545 public void run()
546 {
547 RenamePrompt rename_prompt = new RenamePrompt(collection_tree_node);
548 String new_collection_file_name = rename_prompt.display();
549 rename_prompt.dispose();
550 rename_prompt = null;
551
552 if (new_collection_file_name != null) {
553 File collection_file = collection_tree_node.getFile();
554 File new_collection_file = new File(collection_file.getParentFile(), new_collection_file_name);
555 CollectionTreeNode new_collection_tree_node = new CollectionTreeNode(new_collection_file);
556 file_queue.addJob(System.currentTimeMillis(), collection_tree, new FileNode[] { collection_tree_node }, collection_tree, new_collection_tree_node, FileJob.RENAME);
557 if (Gatherer.isGsdlRemote) {
558 Gatherer.remoteGreenstoneServer.moveCollectionFile(
559 CollectionManager.getLoadedCollectionName(), collection_file, new_collection_file);
560 }
561 }
562 }
563 }
564
565 public void newCollectionFile(CollectionTree collection_tree, CollectionTreeNode parent_node)
566 {
567 new ReplaceOrNewTask(collection_tree, parent_node, false ).start();
568 }
569
570 public void replaceCollectionFile(CollectionTree collection_tree, CollectionTreeNode collection_tree_node)
571 {
572 // This must go in a separate thread because we need the progress bar to work (remote Greenstone server)
573 new ReplaceOrNewTask(collection_tree, collection_tree_node, true).start();
574 }
575
576
577 private class ReplaceOrNewTask
578 extends Thread implements FileCopiedSuccessListener
579 {
580 private CollectionTree collection_tree = null;
581 private CollectionTreeNode collection_tree_node = null;
582 private boolean replacing = false;
583
584 public ReplaceOrNewTask(CollectionTree collection_tree, CollectionTreeNode collection_tree_node, boolean replacing)
585 {
586 this.collection_tree = collection_tree;
587 this.collection_tree_node = collection_tree_node; // either the file to replace, or the folder to go into
588 this.replacing = replacing;
589 }
590
591 public void run()
592 {
593 JFileChooser file_chooser = new JFileChooser(startup_directory);
594 if (replacing) {
595 file_chooser.setDialogTitle(Dictionary.get("ReplacePrompt.Title"));
596 } else {
597 file_chooser.setDialogTitle(Dictionary.get("NewPrompt.Title"));
598 }
599 File new_file = null;
600 int return_val = file_chooser.showOpenDialog(null);
601 if(return_val == JFileChooser.APPROVE_OPTION) {
602 new_file = file_chooser.getSelectedFile();
603 }
604
605 if (new_file == null) {
606 return;
607 }
608
609 // save the search path for next time
610 startup_directory = new_file.getParentFile();
611 // make up a node for the file to bring in
612 WorkspaceTreeNode source_node = new WorkspaceTreeNode(new_file);
613
614 //DebugStream.setDebugging(true, "FileManager.ReplaceTask");
615
616 // Some different handling if the old and new tail file names are the same and target file goes into the same
617 // location in collection tree as source.
618 // This avoids past errors upon replacing with same filename (diff file contents) where the attached meta gets lost,
619 // or remote file gets updated but file gone missing in client-GLI view until collection reopened.
620 boolean isSameLeafName = false;
621 if(replacing && collection_tree_node.getFile().getName().equals(new_file.getName())) {
622 DebugStream.println(" @@@ File Replace: New file has the same name as existing.");
623 isSameLeafName = true;
624 }
625
626 File target_directory;
627 FileNode parent; // store the original source's parent, need it several times after changing source
628 if (replacing) {
629 target_directory = collection_tree_node.getFile().getParentFile();
630 parent = (FileNode)collection_tree_node.getParent();
631 } else {
632 target_directory = collection_tree_node.getFile();
633 parent = collection_tree_node;
634 }
635 CollectionTreeNode new_collection_tree_node = new CollectionTreeNode(new File(target_directory, new_file.getName()));
636
637 //FileNode parent = (FileNode)collection_tree_node.getParent(); // store the original source's parent, need it several times after changing source
638
639 if(isSameLeafName) {
640 // If the file name of the replacing file IS the same as the one being replaced
641 // perform a COPY operation, which will copy across metadata too, after confirming whether the user really wants to replace the source with identically named target
642
643 // (a) First, this instance of ReplaceTask and no other starts listening to whether the user
644 // DIDN'T CANCEL out of an identical filename copy operation and if this local file copy
645 // was a success. If so, on successful file copy event fired (only then), the source file
646 // from the workspace tree will also be uploaded to the remote GS3
647 file_queue.addFileCopiedSuccessListener(this);
648
649 // (b) Now can finally add the COPY job to the queue
650 file_queue.addJob(System.currentTimeMillis(), Gatherer.g_man.gather_pane.workspace_tree, new FileNode[] { source_node }, collection_tree, parent, FileJob.COPY);
651
652 } else {
653 // If the file name of the replacing file is NOT the same as the one being replaced:
654 // (a) Again, this ReplaceTask instance needs to listen for the file copy event fired,
655 // so that the source file will also get uploaded to the remote GS3 on FileCopiedSuccess
656 file_queue.addFileCopiedSuccessListener(this);
657
658 // (b) copy the new file in - but don't bring metadata
659 file_queue.addJob(System.currentTimeMillis(), Gatherer.g_man.gather_pane.workspace_tree, new FileNode[] { source_node }, collection_tree, parent, FileJob.COPY_FILE_ONLY);
660
661 if (replacing) {
662 // (c) final step to finish off: do a replace of old file with new file
663 file_queue.addJob(System.currentTimeMillis(), collection_tree, new FileNode[] { collection_tree_node }, collection_tree, new_collection_tree_node, FileJob.REPLACE);
664 }
665 }
666
667
668
669 //DebugStream.setDebugging(false, "FileManager.ReplaceTask");
670 }
671
672
673 /** In order to detect that the user cancelled out of replacing an identically named target file,
674 * we now listen to events fired that the file was successfully copied across. Only then do we
675 * bother transferring the source file (from the workspace) into the target location in the
676 * collection on the remote file system. We don't do this if the user cancelled.
677 */
678 public void fileCopiedSuccessfully(File new_file) {
679
680 //DebugStream.setDebugging(true, "FileManager.ReplaceTask.fileCopiedSuccessfully");
681
682 if (Gatherer.isGsdlRemote) {
683 File target_directory;
684 if (this.replacing) {
685 target_directory = this.collection_tree_node.getFile().getParentFile();
686 } else {
687 target_directory = this.collection_tree_node.getFile();
688 }
689 File collection_tree_node_file = this.collection_tree_node.getFile();
690
691 String collection_name = CollectionManager.getLoadedCollectionName();
692 if (this.replacing) {
693 Gatherer.remoteGreenstoneServer.deleteCollectionFile(collection_name, collection_tree_node_file);
694 }
695 Gatherer.remoteGreenstoneServer.uploadFilesIntoCollection(collection_name, new File[] { new_file }, target_directory);
696 }
697
698 // stop listening to further events fired now that we've handled this event successfully
699 file_queue.removeFileCopiedSuccessListener(this);
700 //DebugStream.setDebugging(false, "FileManager.ReplaceTask.fileCopiedSuccessfully");
701 }
702 }
703}
Note: See TracBrowser for help on using the repository browser.