source: branches/gsdl-2_62-distribution-branch/gli/src/org/greenstone/gatherer/file/FileManager.java@ 10876

Last change on this file since 10876 was 10561, checked in by mdewsnip, 19 years ago

Now calls the appropriate RemoteGreenstoneServer function itself.

  • Property svn:keywords set to Author Date Id Revision
File size: 9.6 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.CollectionTreeNode;
44import org.greenstone.gatherer.gui.LongProgressBar;
45import org.greenstone.gatherer.gui.NewFolderOrFilePrompt;
46import org.greenstone.gatherer.gui.tree.DragTree;
47import org.greenstone.gatherer.remote.RemoteGreenstoneServer;
48import org.greenstone.gatherer.util.DragComponent;
49import org.greenstone.gatherer.util.Utility;
50
51
52/** Manages the moving of files within a separate thread.
53 * @author John Thompson, NZDL Project, University of Waikato
54 */
55public class FileManager
56{
57 /** Not only the queue of files to be moved, but also the object that moves them. */
58 static private FileQueue file_queue = null;
59
60 public static int FILE_TYPE = 0;
61 public static int FOLDER_TYPE = 1;
62
63
64 /** Constructor.
65 * @see org.greenstone.gatherer.file.FileQueue
66 */
67 public FileManager()
68 {
69 file_queue = new FileQueue();
70 file_queue.start();
71 }
72
73
74 /** 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 {
77 // Check there is something to do
78 if (source_nodes == null || source_nodes.length == 0) {
79 return;
80 }
81
82 // We need a unique ID for each file task
83 long id = System.currentTimeMillis();
84
85 // If source and target are the same we're moving
86 if (source == target) {
87 // Start a new move FileTask and we're done
88 (new FileTask(id, source, source_nodes, target, target_node, FileJob.MOVE)).start();
89 return;
90 }
91
92 // If target isn't the RecycleBin, we're copying
93 if (!(target instanceof RecycleBin)) {
94 // Start a new copy FileTask and we're done
95 (new FileTask(id, source, source_nodes, target, target_node, FileJob.COPY)).start();
96 return;
97 }
98
99 // We're deleting... but first make sure source isn't read-only
100 boolean read_only_source = false;
101
102 // The workspace tree is read-only...
103 if (source.toString().equals("Workspace")) {
104 read_only_source = true;
105
106 // ...except for files from the "Downloaded Files" folder
107 String downloaded_files_folder_path = Gatherer.getGLIUserCacheDirectoryPath();
108 for (int i = 0; i < source_nodes.length; i++) {
109 // Is this the "Downloaded Files" folder?
110 if (source_nodes[i].getFile().getAbsolutePath().startsWith(downloaded_files_folder_path)) {
111 // Can only delete from the "Downloaded Files" folder if a collection is open
112 if (Gatherer.c_man.ready() == false) {
113 return;
114 }
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 LongProgressBar 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, true);
171 if (Gatherer.isGsdlRemote) {
172 String collection_name = Gatherer.c_man.getCollection().getName();
173
174 // Perform the appropriate action based on the job type (RemoteGreenstoneServer will queue)
175 if (type == FileJob.COPY) {
176 File[] source_files = new File[source_nodes.length];
177 for (int i = 0; i < source_nodes.length; i++) {
178 source_files[i] = source_nodes[i].getFile();
179 }
180 RemoteGreenstoneServer.uploadFilesIntoCollection(collection_name, source_files, target_node.getFile());
181 }
182 else if (type == FileJob.DELETE) {
183 RemoteGreenstoneServer.deleteCollectionFile(collection_name, source_nodes[0].getFile());
184 }
185 else if (type == FileJob.MOVE) {
186 RemoteGreenstoneServer.moveCollectionFile(collection_name, source_nodes[0].getFile(), target_node.getFile());
187 }
188 }
189 }
190
191 progress_bar.setIndeterminate(false);
192 progress_bar.clear();
193 }
194 }
195
196
197 public void openFileInExternalApplication(File file)
198 {
199 // This must go in a separate thread because we need the progress bar to work (remote Greenstone server)
200 new OpenFileInExternalApplicationTask(file).start();
201 }
202
203
204 private class OpenFileInExternalApplicationTask
205 extends Thread
206 {
207 private File file = null;
208
209 public OpenFileInExternalApplicationTask(File file)
210 {
211 this.file = file;
212 }
213
214 public void run()
215 {
216 // If we're using a remote Greenstone server, we need to download the file before viewing it...
217 if (Gatherer.isGsdlRemote) {
218 // ... but only if it is inside the collection and we haven't already downloaded it
219 if (file.getAbsolutePath().startsWith(Gatherer.getCollectDirectoryPath()) && file.length() == 0) {
220 RemoteGreenstoneServer.downloadCollectionFile(Gatherer.c_man.getCollection().getName(), file);
221 }
222 }
223
224 // View the file in an external application
225 Gatherer.spawnApplication(file);
226 }
227 }
228
229
230 public void newDummyDoc(DragTree tree, CollectionTreeNode parent_node){
231 newFolderOrDummyDoc(tree, parent_node, FILE_TYPE);
232 }
233
234
235 public void newFolder(DragTree tree, CollectionTreeNode parent_node) {
236 newFolderOrDummyDoc(tree, parent_node, FOLDER_TYPE);
237 }
238
239
240 protected void newFolderOrDummyDoc(DragTree tree, CollectionTreeNode parent_node, int type) {
241 // Ask the user for the directories name.
242 String extension = "";
243 if (type == FILE_TYPE) {
244 extension = ".nul";
245 }
246 NewFolderOrFilePrompt new_folder_prompt = new NewFolderOrFilePrompt(parent_node, type, extension);
247 String name = new_folder_prompt.display();
248 new_folder_prompt.dispose();
249 new_folder_prompt = null;
250 // And if the name is non-null...
251 if(name != null) {
252 FileSystemModel model = (FileSystemModel) tree.getModel();
253 File folder_file = new File(parent_node.getFile(), name);
254 //... check if it already exists.
255 if(folder_file.exists()) {
256 if (type == FILE_TYPE) {
257 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.File_Already_Exists_No_Create", name), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
258 } else {
259 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.Folder_Already_Exists", name), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
260 }
261 }
262 // Otherwise create it.
263 else {
264 try {
265 if (type == FILE_TYPE) {
266 folder_file.createNewFile();
267 } else {
268 folder_file.mkdirs();
269 if (Gatherer.isGsdlRemote) {
270 long id = System.currentTimeMillis();
271 RemoteGreenstoneServer.newCollectionDirectory(Gatherer.c_man.getCollection().getName(), folder_file);
272 }
273 }
274 // Update the parent node to show the new folder
275 parent_node.refresh();
276
277 // Refresh workspace tree (collection tree is done automatically)
278 Gatherer.g_man.refreshWorkspaceTree(DragTree.COLLECTION_CONTENTS_CHANGED);
279 } catch (Exception e) {
280 if (type == FILE_TYPE) {
281 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.File_Create_Error", name), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
282 } else {
283 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.Folder_Create_Error", name), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
284 }
285 }
286 }
287
288 folder_file = null;
289 model = null;
290 }
291 name = null;
292 }
293}
Note: See TracBrowser for help on using the repository browser.