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

Last change on this file since 10007 was 9114, checked in by kjdon, 19 years ago

added new dummy doc creation functionality

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