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

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

Added code to prevent people from deleting files from the "Downloaded Files" area with the "Del" key when no collection is open. (You get zillions of exceptions otherwise).

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