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

Last change on this file since 7647 was 7647, checked in by mdewsnip, 20 years ago

Special case: you can now delete files from the "Downloaded Files" folder.

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