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 | */
|
---|
37 | package org.greenstone.gatherer.file;
|
---|
38 |
|
---|
39 | import java.io.File;
|
---|
40 | import javax.swing.*;
|
---|
41 | import org.greenstone.gatherer.Dictionary;
|
---|
42 | import org.greenstone.gatherer.Gatherer;
|
---|
43 | import org.greenstone.gatherer.file.FileNode;
|
---|
44 | import org.greenstone.gatherer.file.FileQueue;
|
---|
45 | import org.greenstone.gatherer.gui.NewFolderPrompt;
|
---|
46 | import org.greenstone.gatherer.gui.tree.DragTree;
|
---|
47 | import org.greenstone.gatherer.undo.UndoManager;
|
---|
48 | import org.greenstone.gatherer.util.DragComponent;
|
---|
49 | import 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 | */
|
---|
55 | public class FileManager {
|
---|
56 | /** Not only the queue of files to be moved, but also the object that moves them. */
|
---|
57 | private FileQueue queue = null;
|
---|
58 | /** Constructor.
|
---|
59 | * @see org.greenstone.gatherer.file.FileQueue
|
---|
60 | */
|
---|
61 | public FileManager() {
|
---|
62 | queue = new FileQueue(false);
|
---|
63 | queue.start();
|
---|
64 | }
|
---|
65 |
|
---|
66 | /** Given the arguments, determine what action should be carried out by the file queue, and add all of the necessary file jobs. */
|
---|
67 | public void action(DragComponent source, FileNode[] source_nodes, DragComponent target, FileNode target_node) {
|
---|
68 | byte type = 0;
|
---|
69 | // If source and target are the same we are moving
|
---|
70 | if(source == target) {
|
---|
71 | type = FileJob.MOVE;
|
---|
72 | }
|
---|
73 | // If source and target are different
|
---|
74 | else {
|
---|
75 | // If target is the UndoManager, we're deleting
|
---|
76 | if(target instanceof UndoManager) {
|
---|
77 | // If the source is the workspace then display an error message. Workspace is read only.
|
---|
78 | if(source.toString().equals("Workspace")) {
|
---|
79 | JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.Read_Only"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
|
---|
80 | return;
|
---|
81 | }
|
---|
82 | // Normal delete. Go ahead.
|
---|
83 | else {
|
---|
84 | type = FileJob.DELETE;
|
---|
85 | }
|
---|
86 | }
|
---|
87 | // Otherwise we are copying
|
---|
88 | else {
|
---|
89 | type = FileJob.COPY;
|
---|
90 | }
|
---|
91 | }
|
---|
92 | Task task = new Task(System.currentTimeMillis(), source, source_nodes, target, target_node, type);
|
---|
93 | task.start();
|
---|
94 | }
|
---|
95 |
|
---|
96 | /** Retrieves the file queue object. */
|
---|
97 | public FileQueue getQueue() {
|
---|
98 | return queue;
|
---|
99 | }
|
---|
100 |
|
---|
101 | public void newFolder(DragTree tree, FileNode parent_node) {
|
---|
102 | // Ask the user for the directories name.
|
---|
103 | NewFolderPrompt new_folder_prompt = new NewFolderPrompt(parent_node);
|
---|
104 | String name = new_folder_prompt.display();
|
---|
105 | new_folder_prompt.dispose();
|
---|
106 | new_folder_prompt = null;
|
---|
107 | // And if the name is non-null...
|
---|
108 | if(name != null) {
|
---|
109 | FileSystemModel model = (FileSystemModel) tree.getModel();
|
---|
110 | File folder_file = new File(parent_node.getFile(), name);
|
---|
111 | //... check if it already exists.
|
---|
112 | if(folder_file.exists()) {
|
---|
113 | JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.Folder_Already_Exists", name), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
|
---|
114 | }
|
---|
115 | // Otherwise create it.
|
---|
116 | else {
|
---|
117 | folder_file.mkdirs();
|
---|
118 | FileNode folder_node = new FileNode(folder_file);
|
---|
119 | SynchronizedTreeModelTools.insertNodeInto(model, parent_node, folder_node);
|
---|
120 | folder_node = null;
|
---|
121 | }
|
---|
122 | folder_file = null;
|
---|
123 | model = null;
|
---|
124 | }
|
---|
125 | name = null;
|
---|
126 | }
|
---|
127 |
|
---|
128 | private class Task
|
---|
129 | extends Thread {
|
---|
130 | private byte type;
|
---|
131 | private DragComponent source;
|
---|
132 | private DragComponent target;
|
---|
133 | private FileNode target_node;
|
---|
134 | private FileNode[] source_nodes;
|
---|
135 | private long id;
|
---|
136 | public Task(long id, DragComponent source, FileNode[] source_nodes, DragComponent target, FileNode target_node, byte type) {
|
---|
137 | this.id = id;
|
---|
138 | this.source = source;
|
---|
139 | this.source_nodes = source_nodes;
|
---|
140 | this.target = target;
|
---|
141 | this.target_node = target_node;
|
---|
142 | this.type = type;
|
---|
143 | }
|
---|
144 | public void run() {
|
---|
145 | // Reset, and calculate progress bar size.
|
---|
146 | boolean cancelled = queue.calculateSize(source_nodes);
|
---|
147 | // 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.
|
---|
148 | for(int i = 0; !cancelled && source_nodes != null && i < source_nodes.length; i++) {
|
---|
149 | queue.addJob(id, source, source_nodes[i], target, target_node, type, true, true, true);
|
---|
150 | }
|
---|
151 | }
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 |
|
---|
156 |
|
---|
157 |
|
---|
158 |
|
---|