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

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

A new RemoteFileQueue class that is conceptually similar to FileQueue but makes the necessary changes to the collection on the server.

  • Property svn:keywords set to Author Date Id Revision
File size: 7.8 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.util.DragComponent;
48import org.greenstone.gatherer.util.Utility;
49
50
51/** Manages the moving of files within a separate thread.
52 * @author John Thompson, NZDL Project, University of Waikato
53 */
54public class FileManager
55{
56 /** Not only the queue of files to be moved, but also the object that moves them. */
57 static private FileQueue file_queue = null;
58 static private RemoteFileQueue remote_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 if (Gatherer.isGsdlRemote) {
73 remote_file_queue = new RemoteFileQueue();
74 remote_file_queue.start();
75 }
76 }
77
78
79 /** Determine what action should be carried out by the file queue, and add all of the necessary file jobs. */
80 public void action(DragComponent source, FileNode[] source_nodes, DragComponent target, FileNode target_node)
81 {
82 // Check there is something to do
83 if (source_nodes == null) {
84 return;
85 }
86
87 // We need a unique ID for each file task
88 long id = System.currentTimeMillis();
89
90 // If source and target are the same we're moving
91 if (source == target) {
92 // Start a new move FileTask and we're done
93 (new FileTask(id, source, source_nodes, target, target_node, FileJob.MOVE)).start();
94 return;
95 }
96
97 // If target isn't the RecycleBin, we're copying
98 if (!(target instanceof RecycleBin)) {
99 // Start a new copy FileTask and we're done
100 (new FileTask(id, source, source_nodes, target, target_node, FileJob.COPY)).start();
101 return;
102 }
103
104 // We're deleting... but first make sure source isn't read-only
105 boolean read_only_source = false;
106
107 // The workspace tree is read-only...
108 if (source.toString().equals("Workspace")) {
109 read_only_source = true;
110
111 // ...except for files from the "Downloaded Files" folder
112 String downloaded_files_folder_path = Utility.getCacheDir().getAbsolutePath();
113 for (int i = 0; i < source_nodes.length; i++) {
114 // Is this the "Downloaded Files" folder?
115 if (source_nodes[i].getFile().getAbsolutePath().startsWith(downloaded_files_folder_path)) {
116 // Can only delete from the "Downloaded Files" folder if a collection is open
117 if (Gatherer.c_man.ready() == false) {
118 return;
119 }
120 read_only_source = false;
121 }
122 }
123 }
124
125 // The source is read-only, so tell the user and abort
126 if (read_only_source) {
127 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.Read_Only"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
128 return;
129 }
130
131 // Start a new delete FileTask and we're done
132 (new FileTask(id, source, source_nodes, target, target_node, FileJob.DELETE)).start();
133 }
134
135
136 /** Retrieves the file queue object. */
137 public FileQueue getQueue()
138 {
139 return file_queue;
140 }
141
142
143 private class FileTask
144 extends Thread
145 {
146 private long id;
147 private DragComponent source;
148 private FileNode[] source_nodes;
149 private DragComponent target;
150 private FileNode target_node;
151 private byte type;
152
153
154 public FileTask(long id, DragComponent source, FileNode[] source_nodes, DragComponent target, FileNode target_node, byte type)
155 {
156 this.id = id;
157 this.source = source;
158 this.source_nodes = source_nodes;
159 this.target = target;
160 this.target_node = target_node;
161 this.type = type;
162 }
163
164
165 public void run()
166 {
167 // Reset the progress bar and set it to indeterminate while calculating its size
168 LongProgressBar progress_bar = file_queue.getProgressBar();
169 progress_bar.reset();
170 progress_bar.setIndeterminate(true);
171
172 // Calculate the progress bar size
173 boolean cancelled = file_queue.calculateSize(source_nodes);
174 if (!cancelled) {
175 file_queue.addJob(id, source, source_nodes, target, target_node, type, true);
176 if (Gatherer.isGsdlRemote) {
177 remote_file_queue.addJob(id, source, source_nodes, target, target_node, type);
178 }
179 }
180
181 progress_bar.setIndeterminate(false);
182 progress_bar.clear();
183 }
184 }
185
186
187 public void newDummyDoc(DragTree tree, CollectionTreeNode parent_node){
188 newFolderOrDummyDoc(tree, parent_node, FILE_TYPE);
189 }
190
191
192 public void newFolder(DragTree tree, CollectionTreeNode parent_node) {
193 newFolderOrDummyDoc(tree, parent_node, FOLDER_TYPE);
194 }
195
196
197 protected void newFolderOrDummyDoc(DragTree tree, CollectionTreeNode parent_node, int type) {
198 // Ask the user for the directories name.
199 String extension = "";
200 if (type == FILE_TYPE) {
201 extension = ".nul";
202 }
203 NewFolderOrFilePrompt new_folder_prompt = new NewFolderOrFilePrompt(parent_node, type, extension);
204 String name = new_folder_prompt.display();
205 new_folder_prompt.dispose();
206 new_folder_prompt = null;
207 // And if the name is non-null...
208 if(name != null) {
209 FileSystemModel model = (FileSystemModel) tree.getModel();
210 File folder_file = new File(parent_node.getFile(), name);
211 //... check if it already exists.
212 if(folder_file.exists()) {
213 if (type == FILE_TYPE) {
214 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.File_Already_Exists_No_Create", name), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
215 } else {
216 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.Folder_Already_Exists", name), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
217 }
218 }
219 // Otherwise create it.
220 else {
221 try {
222 if (type == FILE_TYPE) {
223 folder_file.createNewFile();
224 } else {
225 folder_file.mkdirs();
226 }
227 // Update the parent node to show the new folder
228 parent_node.refresh();
229
230 // Refresh workspace tree (collection tree is done automatically)
231 Gatherer.g_man.refreshWorkspaceTree(DragTree.COLLECTION_CONTENTS_CHANGED);
232 } catch (Exception e) {
233 if (type == FILE_TYPE) {
234 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.File_Create_Error", name), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
235 } else {
236 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.Folder_Create_Error", name), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
237 }
238 }
239 }
240
241 folder_file = null;
242 model = null;
243 }
244 name = null;
245 }
246}
Note: See TracBrowser for help on using the repository browser.