[4293] | 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, Greenstone Digital Library, University of Waikato
|
---|
| 11 | *
|
---|
| 12 | * <BR><BR>
|
---|
| 13 | *
|
---|
| 14 | * Copyright (C) 1999 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 | */
|
---|
[5564] | 37 | package org.greenstone.gatherer.file;
|
---|
| 38 |
|
---|
[4293] | 39 | import java.io.File;
|
---|
| 40 | import javax.swing.*;
|
---|
[5564] | 41 | import org.greenstone.gatherer.Dictionary;
|
---|
[4293] | 42 | import org.greenstone.gatherer.Gatherer;
|
---|
| 43 | import org.greenstone.gatherer.file.FileNode;
|
---|
| 44 | import org.greenstone.gatherer.file.FileQueue;
|
---|
[7360] | 45 | import org.greenstone.gatherer.gui.LongProgressBar;
|
---|
[4293] | 46 | import org.greenstone.gatherer.gui.NewFolderPrompt;
|
---|
| 47 | import org.greenstone.gatherer.gui.tree.DragTree;
|
---|
| 48 | import org.greenstone.gatherer.undo.UndoManager;
|
---|
| 49 | import org.greenstone.gatherer.util.DragComponent;
|
---|
| 50 | import org.greenstone.gatherer.util.SynchronizedTreeModelTools;
|
---|
[7647] | 51 | import org.greenstone.gatherer.util.Utility;
|
---|
[5564] | 52 |
|
---|
[4675] | 53 | /** Manages the moving of files within a separate thread.
|
---|
[4293] | 54 | * @author John Thompson, Greenstone Digital Library, University of Waikato
|
---|
| 55 | * @version 2.3
|
---|
| 56 | */
|
---|
| 57 | public class FileManager {
|
---|
[6319] | 58 |
|
---|
| 59 | static public int countFolderDepth(File file) {
|
---|
| 60 | int count = 0;
|
---|
| 61 | while(file != null) {
|
---|
| 62 | count++;
|
---|
| 63 | file = file.getParentFile();
|
---|
| 64 | }
|
---|
| 65 | return count;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
[6539] | 68 | public boolean complain_if_no_sets = true;
|
---|
| 69 |
|
---|
[4366] | 70 | /** Not only the queue of files to be moved, but also the object that moves them. */
|
---|
| 71 | private FileQueue queue = null;
|
---|
| 72 | /** Constructor.
|
---|
| 73 | * @see org.greenstone.gatherer.file.FileQueue
|
---|
| 74 | */
|
---|
| 75 | public FileManager() {
|
---|
| 76 | queue = new FileQueue(false);
|
---|
| 77 | queue.start();
|
---|
| 78 | }
|
---|
[4293] | 79 |
|
---|
[4366] | 80 | /** Given the arguments, determine what action should be carried out by the file queue, and add all of the necessary file jobs. */
|
---|
| 81 | public void action(DragComponent source, FileNode[] source_nodes, DragComponent target, FileNode target_node) {
|
---|
| 82 | byte type = 0;
|
---|
| 83 | // If source and target are the same we are moving
|
---|
| 84 | if(source == target) {
|
---|
| 85 | type = FileJob.MOVE;
|
---|
| 86 | }
|
---|
| 87 | // If source and target are different
|
---|
| 88 | else {
|
---|
[5581] | 89 | // If target is the UndoManager, we're deleting
|
---|
[4366] | 90 | if(target instanceof UndoManager) {
|
---|
| 91 | // If the source is the workspace then display an error message. Workspace is read only.
|
---|
[7647] | 92 |
|
---|
| 93 | // ...except for files from the "Downloaded Files" folder
|
---|
| 94 | boolean from_downloaded_files_folder = false;
|
---|
| 95 | if (source_nodes != null) {
|
---|
| 96 | for (int i = 0; i < source_nodes.length; i++) {
|
---|
| 97 | if (source_nodes[i].getFile() != null) {
|
---|
| 98 | if (source_nodes[i].getFile().getAbsolutePath().startsWith(Utility.getCacheDir().getAbsolutePath())) {
|
---|
| 99 | from_downloaded_files_folder = true;
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | if (source.toString().equals("Workspace") && !from_downloaded_files_folder) {
|
---|
[5593] | 106 | JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.Read_Only"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
|
---|
[4366] | 107 | return;
|
---|
| 108 | }
|
---|
| 109 | // Normal delete. Go ahead.
|
---|
| 110 | else {
|
---|
| 111 | type = FileJob.DELETE;
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
[5581] | 114 | // Otherwise we are copying
|
---|
[4366] | 115 | else {
|
---|
| 116 | type = FileJob.COPY;
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 | Task task = new Task(System.currentTimeMillis(), source, source_nodes, target, target_node, type);
|
---|
[4383] | 120 | task.start();
|
---|
[4366] | 121 | }
|
---|
[4293] | 122 |
|
---|
[4366] | 123 | /** Retrieves the file queue object. */
|
---|
| 124 | public FileQueue getQueue() {
|
---|
| 125 | return queue;
|
---|
| 126 | }
|
---|
[4293] | 127 |
|
---|
[4366] | 128 | public void newFolder(DragTree tree, FileNode parent_node) {
|
---|
| 129 | // Ask the user for the directories name.
|
---|
| 130 | NewFolderPrompt new_folder_prompt = new NewFolderPrompt(parent_node);
|
---|
| 131 | String name = new_folder_prompt.display();
|
---|
| 132 | new_folder_prompt.dispose();
|
---|
| 133 | new_folder_prompt = null;
|
---|
| 134 | // And if the name is non-null...
|
---|
| 135 | if(name != null) {
|
---|
| 136 | FileSystemModel model = (FileSystemModel) tree.getModel();
|
---|
| 137 | File folder_file = new File(parent_node.getFile(), name);
|
---|
[5564] | 138 | //... check if it already exists.
|
---|
[4366] | 139 | if(folder_file.exists()) {
|
---|
[5593] | 140 | JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.Folder_Already_Exists", name), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
|
---|
[4366] | 141 | }
|
---|
[5564] | 142 | // Otherwise create it.
|
---|
[4366] | 143 | else {
|
---|
| 144 | folder_file.mkdirs();
|
---|
[7484] | 145 |
|
---|
[7608] | 146 | // Update the parent node to show the new folder
|
---|
| 147 | parent_node.unmap();
|
---|
| 148 | parent_node.map();
|
---|
| 149 |
|
---|
[7484] | 150 | // Refresh workspace tree (collection tree is done automatically)
|
---|
| 151 | Gatherer.g_man.refreshWorkspaceTree(DragTree.COLLECTION_CONTENTS_CHANGED);
|
---|
[4366] | 152 | }
|
---|
| 153 | folder_file = null;
|
---|
| 154 | model = null;
|
---|
| 155 | }
|
---|
| 156 | name = null;
|
---|
| 157 | }
|
---|
[4293] | 158 |
|
---|
[4366] | 159 | private class Task
|
---|
[4383] | 160 | extends Thread {
|
---|
[4366] | 161 | private byte type;
|
---|
| 162 | private DragComponent source;
|
---|
| 163 | private DragComponent target;
|
---|
| 164 | private FileNode target_node;
|
---|
| 165 | private FileNode[] source_nodes;
|
---|
| 166 | private long id;
|
---|
[7360] | 167 |
|
---|
[4366] | 168 | public Task(long id, DragComponent source, FileNode[] source_nodes, DragComponent target, FileNode target_node, byte type) {
|
---|
| 169 | this.id = id;
|
---|
| 170 | this.source = source;
|
---|
| 171 | this.source_nodes = source_nodes;
|
---|
| 172 | this.target = target;
|
---|
| 173 | this.target_node = target_node;
|
---|
| 174 | this.type = type;
|
---|
| 175 | }
|
---|
[7360] | 176 |
|
---|
| 177 | public void run()
|
---|
| 178 | {
|
---|
| 179 | // Check there is something to do
|
---|
| 180 | if (source_nodes == null) {
|
---|
| 181 | return;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | // Reset the progress bar and set it to indeterminate while calculating its size
|
---|
| 185 | LongProgressBar progress_bar = queue.getProgressBar();
|
---|
| 186 | progress_bar.reset();
|
---|
| 187 | progress_bar.setIndeterminate(true);
|
---|
| 188 |
|
---|
| 189 | // Calculate the progress bar size
|
---|
[4675] | 190 | boolean cancelled = queue.calculateSize(source_nodes);
|
---|
[7360] | 191 | if (!cancelled) {
|
---|
| 192 | // Queue the job(s) (this may fail if we are asked to delete a read only file)
|
---|
| 193 | for (int i = 0; i < source_nodes.length; i++) {
|
---|
| 194 | queue.addJob(id, source, source_nodes[i], target, target_node, type, true, true, true);
|
---|
| 195 | }
|
---|
[4366] | 196 | }
|
---|
[7360] | 197 |
|
---|
| 198 | progress_bar.setIndeterminate(false);
|
---|
[7361] | 199 | progress_bar.clear();
|
---|
[4366] | 200 | }
|
---|
| 201 | }
|
---|
[4293] | 202 | }
|
---|
| 203 |
|
---|
| 204 |
|
---|
| 205 |
|
---|
| 206 |
|
---|
| 207 |
|
---|