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

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

A further fix to the progress bar so it shows "0%" as soon as it is ready to start copying/moving/deleting.

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