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

Last change on this file since 6901 was 6539, checked in by jmt12, 20 years ago

Heres a bunch of other changed files. If it wasn't a Friday afternoon I might be bothered finding out what I actually changed in them. Such changes include: a new option or three on preferences, a bug fix for the GDM classes, several changes to CDM to allow for G2.39 configuration files, a fix to Codec to allow for quotes in format strings and more work on CommandTokenizer to allow for stupid, stupid, stupid collectionextra's starting with speech marks then a new line. Plus other stuff. And things. Peace Out.

  • Property svn:keywords set to Author Date Id Revision
File size: 5.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.file.FileNode;
44import org.greenstone.gatherer.file.FileQueue;
45import org.greenstone.gatherer.gui.NewFolderPrompt;
46import org.greenstone.gatherer.gui.tree.DragTree;
47import org.greenstone.gatherer.undo.UndoManager;
48import org.greenstone.gatherer.util.DragComponent;
49import org.greenstone.gatherer.util.SynchronizedTreeModelTools;
50
51/** Manages the moving of files within a separate thread.
52 * @author John Thompson, Greenstone Digital Library, University of Waikato
53 * @version 2.3
54 */
55public class FileManager {
56
57 static public int countFolderDepth(File file) {
58 int count = 0;
59 while(file != null) {
60 count++;
61 file = file.getParentFile();
62 }
63 return count;
64 }
65
66 public boolean complain_if_no_sets = true;
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(false);
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 UndoManager, we're deleting
88 if(target instanceof UndoManager) {
89 // If the source is the workspace then display an error message. Workspace is read only.
90 if(source.toString().equals("Workspace")) {
91 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.Read_Only"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
92 return;
93 }
94 // Normal delete. Go ahead.
95 else {
96 type = FileJob.DELETE;
97 }
98 }
99 // Otherwise we are copying
100 else {
101 type = FileJob.COPY;
102 }
103 }
104 Task task = new Task(System.currentTimeMillis(), source, source_nodes, target, target_node, type);
105 task.start();
106 }
107
108 /** Retrieves the file queue object. */
109 public FileQueue getQueue() {
110 return queue;
111 }
112
113 public void newFolder(DragTree tree, FileNode parent_node) {
114 // Ask the user for the directories name.
115 NewFolderPrompt new_folder_prompt = new NewFolderPrompt(parent_node);
116 String name = new_folder_prompt.display();
117 new_folder_prompt.dispose();
118 new_folder_prompt = null;
119 // And if the name is non-null...
120 if(name != null) {
121 FileSystemModel model = (FileSystemModel) tree.getModel();
122 File folder_file = new File(parent_node.getFile(), name);
123 //... check if it already exists.
124 if(folder_file.exists()) {
125 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.Folder_Already_Exists", name), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
126 }
127 // Otherwise create it.
128 else {
129 folder_file.mkdirs();
130 FileNode folder_node = new FileNode(folder_file);
131 SynchronizedTreeModelTools.insertNodeInto(model, parent_node, folder_node);
132 folder_node = null;
133 }
134 folder_file = null;
135 model = null;
136 }
137 name = null;
138 }
139
140 private class Task
141 extends Thread {
142 private byte type;
143 private DragComponent source;
144 private DragComponent target;
145 private FileNode target_node;
146 private FileNode[] source_nodes;
147 private long id;
148 public Task(long id, DragComponent source, FileNode[] source_nodes, DragComponent target, FileNode target_node, byte type) {
149 this.id = id;
150 this.source = source;
151 this.source_nodes = source_nodes;
152 this.target = target;
153 this.target_node = target_node;
154 this.type = type;
155 }
156 public void run() {
157 // Reset, and calculate progress bar size.
158 boolean cancelled = queue.calculateSize(source_nodes);
159 // Now we queue the job(s). Note that this may fail if a read only file is encountered and we have been asked to delete.
160 for(int i = 0; !cancelled && source_nodes != null && i < source_nodes.length; i++) {
161 queue.addJob(id, source, source_nodes[i], target, target_node, type, true, true, true);
162 }
163 }
164 }
165}
166
167
168
169
170
Note: See TracBrowser for help on using the repository browser.