[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;
|
---|
[8783] | 43 | import org.greenstone.gatherer.collection.CollectionTreeNode;
|
---|
[7360] | 44 | import org.greenstone.gatherer.gui.LongProgressBar;
|
---|
[9114] | 45 | import org.greenstone.gatherer.gui.NewFolderOrFilePrompt;
|
---|
[4293] | 46 | import org.greenstone.gatherer.gui.tree.DragTree;
|
---|
| 47 | import org.greenstone.gatherer.util.DragComponent;
|
---|
[7647] | 48 | import org.greenstone.gatherer.util.Utility;
|
---|
[5564] | 49 |
|
---|
[4675] | 50 | /** Manages the moving of files within a separate thread.
|
---|
[4293] | 51 | * @author John Thompson, Greenstone Digital Library, University of Waikato
|
---|
| 52 | * @version 2.3
|
---|
| 53 | */
|
---|
| 54 | public class FileManager {
|
---|
[6319] | 55 |
|
---|
[9114] | 56 | public static int FILE_TYPE = 0;
|
---|
| 57 | public static int FOLDER_TYPE = 1;
|
---|
| 58 |
|
---|
[6319] | 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 |
|
---|
[4366] | 68 | /** Not only the queue of files to be moved, but also the object that moves them. */
|
---|
| 69 | private FileQueue queue = null;
|
---|
| 70 | /** Constructor.
|
---|
| 71 | * @see org.greenstone.gatherer.file.FileQueue
|
---|
| 72 | */
|
---|
| 73 | public FileManager() {
|
---|
[8595] | 74 | queue = new FileQueue();
|
---|
[4366] | 75 | queue.start();
|
---|
| 76 | }
|
---|
[4293] | 77 |
|
---|
[4366] | 78 | /** Given the arguments, 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 | byte type = 0;
|
---|
| 81 | // If source and target are the same we are moving
|
---|
| 82 | if(source == target) {
|
---|
| 83 | type = FileJob.MOVE;
|
---|
| 84 | }
|
---|
| 85 | // If source and target are different
|
---|
| 86 | else {
|
---|
[8041] | 87 | // If target is the RecycleBin, we're deleting
|
---|
| 88 | if (target instanceof RecycleBin) {
|
---|
| 89 | // The workspace tree is read only.
|
---|
| 90 | // ... except for files from the "Downloaded Files" folder
|
---|
[7647] | 91 | boolean from_downloaded_files_folder = false;
|
---|
| 92 | if (source_nodes != null) {
|
---|
| 93 | for (int i = 0; i < source_nodes.length; i++) {
|
---|
| 94 | if (source_nodes[i].getFile() != null) {
|
---|
| 95 | if (source_nodes[i].getFile().getAbsolutePath().startsWith(Utility.getCacheDir().getAbsolutePath())) {
|
---|
| 96 | from_downloaded_files_folder = true;
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[8391] | 102 | // Can only delete from the "Downloaded Files" folder if a collection is open
|
---|
| 103 | if (from_downloaded_files_folder && Gatherer.c_man.ready() == false) {
|
---|
| 104 | return;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[7647] | 107 | if (source.toString().equals("Workspace") && !from_downloaded_files_folder) {
|
---|
[5593] | 108 | JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.Read_Only"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
|
---|
[4366] | 109 | return;
|
---|
| 110 | }
|
---|
| 111 | // Normal delete. Go ahead.
|
---|
| 112 | else {
|
---|
| 113 | type = FileJob.DELETE;
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
[5581] | 116 | // Otherwise we are copying
|
---|
[4366] | 117 | else {
|
---|
| 118 | type = FileJob.COPY;
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 | Task task = new Task(System.currentTimeMillis(), source, source_nodes, target, target_node, type);
|
---|
[4383] | 122 | task.start();
|
---|
[4366] | 123 | }
|
---|
[4293] | 124 |
|
---|
[4366] | 125 | /** Retrieves the file queue object. */
|
---|
| 126 | public FileQueue getQueue() {
|
---|
| 127 | return queue;
|
---|
| 128 | }
|
---|
[4293] | 129 |
|
---|
[9114] | 130 | public void newDummyDoc(DragTree tree, CollectionTreeNode parent_node){
|
---|
| 131 | newFolderOrDummyDoc(tree, parent_node, FILE_TYPE);
|
---|
| 132 | }
|
---|
| 133 |
|
---|
[8783] | 134 | public void newFolder(DragTree tree, CollectionTreeNode parent_node) {
|
---|
[9114] | 135 | newFolderOrDummyDoc(tree, parent_node, FOLDER_TYPE);
|
---|
| 136 | }
|
---|
| 137 | protected void newFolderOrDummyDoc(DragTree tree, CollectionTreeNode parent_node, int type) {
|
---|
[4366] | 138 | // Ask the user for the directories name.
|
---|
[9114] | 139 | String extension = "";
|
---|
| 140 | if (type == FILE_TYPE) {
|
---|
| 141 | extension = ".nul";
|
---|
| 142 | }
|
---|
| 143 | NewFolderOrFilePrompt new_folder_prompt = new NewFolderOrFilePrompt(parent_node, type, extension);
|
---|
[4366] | 144 | String name = new_folder_prompt.display();
|
---|
| 145 | new_folder_prompt.dispose();
|
---|
| 146 | new_folder_prompt = null;
|
---|
| 147 | // And if the name is non-null...
|
---|
| 148 | if(name != null) {
|
---|
| 149 | FileSystemModel model = (FileSystemModel) tree.getModel();
|
---|
| 150 | File folder_file = new File(parent_node.getFile(), name);
|
---|
[5564] | 151 | //... check if it already exists.
|
---|
[9114] | 152 | if(folder_file.exists()) {
|
---|
| 153 | if (type == FILE_TYPE) {
|
---|
| 154 | JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.File_Already_Exists_No_Create", name), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
|
---|
| 155 | } else {
|
---|
| 156 | JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.Folder_Already_Exists", name), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
|
---|
| 157 | }
|
---|
[4366] | 158 | }
|
---|
[5564] | 159 | // Otherwise create it.
|
---|
[4366] | 160 | else {
|
---|
[9114] | 161 | try {
|
---|
| 162 | if (type == FILE_TYPE) {
|
---|
| 163 | folder_file.createNewFile();
|
---|
| 164 | } else {
|
---|
| 165 | folder_file.mkdirs();
|
---|
| 166 | }
|
---|
| 167 | // Update the parent node to show the new folder
|
---|
| 168 | parent_node.refresh();
|
---|
| 169 |
|
---|
| 170 | // Refresh workspace tree (collection tree is done automatically)
|
---|
| 171 | Gatherer.g_man.refreshWorkspaceTree(DragTree.COLLECTION_CONTENTS_CHANGED);
|
---|
| 172 | } catch (Exception e) {
|
---|
| 173 | if (type == FILE_TYPE) {
|
---|
| 174 | JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.File_Create_Error", name), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
|
---|
| 175 | } else {
|
---|
| 176 | JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.Folder_Create_Error", name), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
[4366] | 179 | }
|
---|
[9114] | 180 |
|
---|
[4366] | 181 | folder_file = null;
|
---|
| 182 | model = null;
|
---|
| 183 | }
|
---|
| 184 | name = null;
|
---|
| 185 | }
|
---|
[4293] | 186 |
|
---|
[4366] | 187 | private class Task
|
---|
[4383] | 188 | extends Thread {
|
---|
[4366] | 189 | private byte type;
|
---|
| 190 | private DragComponent source;
|
---|
| 191 | private DragComponent target;
|
---|
| 192 | private FileNode target_node;
|
---|
| 193 | private FileNode[] source_nodes;
|
---|
| 194 | private long id;
|
---|
[7360] | 195 |
|
---|
[4366] | 196 | public Task(long id, DragComponent source, FileNode[] source_nodes, DragComponent target, FileNode target_node, byte type) {
|
---|
| 197 | this.id = id;
|
---|
| 198 | this.source = source;
|
---|
| 199 | this.source_nodes = source_nodes;
|
---|
| 200 | this.target = target;
|
---|
| 201 | this.target_node = target_node;
|
---|
| 202 | this.type = type;
|
---|
| 203 | }
|
---|
[7360] | 204 |
|
---|
| 205 | public void run()
|
---|
| 206 | {
|
---|
| 207 | // Check there is something to do
|
---|
| 208 | if (source_nodes == null) {
|
---|
| 209 | return;
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | // Reset the progress bar and set it to indeterminate while calculating its size
|
---|
| 213 | LongProgressBar progress_bar = queue.getProgressBar();
|
---|
| 214 | progress_bar.reset();
|
---|
| 215 | progress_bar.setIndeterminate(true);
|
---|
| 216 |
|
---|
| 217 | // Calculate the progress bar size
|
---|
[4675] | 218 | boolean cancelled = queue.calculateSize(source_nodes);
|
---|
[7360] | 219 | if (!cancelled) {
|
---|
| 220 | // Queue the job(s) (this may fail if we are asked to delete a read only file)
|
---|
| 221 | for (int i = 0; i < source_nodes.length; i++) {
|
---|
[8599] | 222 | queue.addJob(id, source, source_nodes[i], target, target_node, type, true);
|
---|
[7360] | 223 | }
|
---|
[4366] | 224 | }
|
---|
[7360] | 225 |
|
---|
| 226 | progress_bar.setIndeterminate(false);
|
---|
[7361] | 227 | progress_bar.clear();
|
---|
[4366] | 228 | }
|
---|
| 229 | }
|
---|
[4293] | 230 | }
|
---|