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

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

Moved null test a bit earlier in the process.

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