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