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

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

Tidied up FileManager.java in preparation for updating collections on the server immediately when building collections remotely.

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