1 | package org.greenstone.gatherer.file;
|
---|
2 | /**
|
---|
3 | *#########################################################################
|
---|
4 | *
|
---|
5 | * A component of the Gatherer application, part of the Greenstone digital
|
---|
6 | * library suite from the New Zealand Digital Library Project at the
|
---|
7 | * University of Waikato, New Zealand.
|
---|
8 | *
|
---|
9 | * <BR><BR>
|
---|
10 | *
|
---|
11 | * Author: John Thompson, Greenstone Digital Library, University of Waikato
|
---|
12 | *
|
---|
13 | * <BR><BR>
|
---|
14 | *
|
---|
15 | * Copyright (C) 1999 New Zealand Digital Library Project
|
---|
16 | *
|
---|
17 | * <BR><BR>
|
---|
18 | *
|
---|
19 | * This program is free software; you can redistribute it and/or modify
|
---|
20 | * it under the terms of the GNU General Public License as published by
|
---|
21 | * the Free Software Foundation; either version 2 of the License, or
|
---|
22 | * (at your option) any later version.
|
---|
23 | *
|
---|
24 | * <BR><BR>
|
---|
25 | *
|
---|
26 | * This program is distributed in the hope that it will be useful,
|
---|
27 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
28 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
29 | * GNU General Public License for more details.
|
---|
30 | *
|
---|
31 | * <BR><BR>
|
---|
32 | *
|
---|
33 | * You should have received a copy of the GNU General Public License
|
---|
34 | * along with this program; if not, write to the Free Software
|
---|
35 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
36 | *########################################################################
|
---|
37 | */
|
---|
38 | import java.io.File;
|
---|
39 | import javax.swing.*;
|
---|
40 | import org.greenstone.gatherer.Gatherer;
|
---|
41 | import org.greenstone.gatherer.file.FileNode;
|
---|
42 | import org.greenstone.gatherer.file.FileQueue;
|
---|
43 | import org.greenstone.gatherer.gui.NewFolderPrompt;
|
---|
44 | import org.greenstone.gatherer.gui.tree.DragTree;
|
---|
45 | import org.greenstone.gatherer.undo.UndoManager;
|
---|
46 | import org.greenstone.gatherer.util.DragComponent;
|
---|
47 | import org.greenstone.gatherer.util.SynchronizedTreeModelTools;
|
---|
48 | /** Manages the moving of files within a separate thread.
|
---|
49 | * @author John Thompson, Greenstone Digital Library, University of Waikato
|
---|
50 | * @version 2.3
|
---|
51 | */
|
---|
52 | public class FileManager {
|
---|
53 | /** Not only the queue of files to be moved, but also the object that moves them. */
|
---|
54 | private FileQueue queue = null;
|
---|
55 | /** Constructor.
|
---|
56 | * @see org.greenstone.gatherer.file.FileQueue
|
---|
57 | */
|
---|
58 | public FileManager() {
|
---|
59 | queue = new FileQueue(false);
|
---|
60 | queue.start();
|
---|
61 | }
|
---|
62 |
|
---|
63 | /** Given the arguments, determine what action should be carried out by the file queue, and add all of the necessary file jobs. */
|
---|
64 | public void action(DragComponent source, FileNode[] source_nodes, DragComponent target, FileNode target_node) {
|
---|
65 | byte type = 0;
|
---|
66 | // If source and target are the same we are moving
|
---|
67 | if(source == target) {
|
---|
68 | type = FileJob.MOVE;
|
---|
69 | }
|
---|
70 | // If source and target are different
|
---|
71 | else {
|
---|
72 | // If target is the UndoManager, we're deleting
|
---|
73 | if(target instanceof UndoManager) {
|
---|
74 | // If the source is the workspace then display an error message. Workspace is read only.
|
---|
75 | if(source.toString().equals("Workspace")) {
|
---|
76 | JOptionPane.showMessageDialog(Gatherer.g_man, Gatherer.dictionary.get("FileActions.Read_Only"), Gatherer.dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
|
---|
77 | return;
|
---|
78 | }
|
---|
79 | // Normal delete. Go ahead.
|
---|
80 | else {
|
---|
81 | type = FileJob.DELETE;
|
---|
82 | }
|
---|
83 | }
|
---|
84 | // Otherwise we are copying
|
---|
85 | else {
|
---|
86 | type = FileJob.COPY;
|
---|
87 | }
|
---|
88 | }
|
---|
89 | Task task = new Task(System.currentTimeMillis(), source, source_nodes, target, target_node, type);
|
---|
90 | SwingUtilities.invokeLater(task);
|
---|
91 | }
|
---|
92 |
|
---|
93 | /** Retrieves the file queue object. */
|
---|
94 | public FileQueue getQueue() {
|
---|
95 | return queue;
|
---|
96 | }
|
---|
97 |
|
---|
98 | public void newFolder(DragTree tree, FileNode parent_node) {
|
---|
99 | // Ask the user for the directories name.
|
---|
100 | NewFolderPrompt new_folder_prompt = new NewFolderPrompt(parent_node);
|
---|
101 | String name = new_folder_prompt.display();
|
---|
102 | new_folder_prompt.dispose();
|
---|
103 | new_folder_prompt = null;
|
---|
104 | // And if the name is non-null...
|
---|
105 | if(name != null) {
|
---|
106 | FileSystemModel model = (FileSystemModel) tree.getModel();
|
---|
107 | File folder_file = new File(parent_node.getFile(), name);
|
---|
108 | //... check if it already exists.
|
---|
109 | if(folder_file.exists()) {
|
---|
110 | JOptionPane.showMessageDialog(Gatherer.g_man, Gatherer.dictionary.get("FileActions.Folder_Already_Exists", name), Gatherer.dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
|
---|
111 | }
|
---|
112 | // Otherwise create it.
|
---|
113 | else {
|
---|
114 | folder_file.mkdirs();
|
---|
115 | FileNode folder_node = new FileNode(folder_file);
|
---|
116 | SynchronizedTreeModelTools.insertNodeInto(model, parent_node, folder_node);
|
---|
117 | folder_node = null;
|
---|
118 | }
|
---|
119 | folder_file = null;
|
---|
120 | model = null;
|
---|
121 | }
|
---|
122 | name = null;
|
---|
123 | }
|
---|
124 |
|
---|
125 | private class Task
|
---|
126 | implements Runnable {
|
---|
127 | private byte type;
|
---|
128 | private DragComponent source;
|
---|
129 | private DragComponent target;
|
---|
130 | private FileNode target_node;
|
---|
131 | private FileNode[] source_nodes;
|
---|
132 | private long id;
|
---|
133 | public Task(long id, DragComponent source, FileNode[] source_nodes, DragComponent target, FileNode target_node, byte type) {
|
---|
134 | this.id = id;
|
---|
135 | this.source = source;
|
---|
136 | this.source_nodes = source_nodes;
|
---|
137 | this.target = target;
|
---|
138 | this.target_node = target_node;
|
---|
139 | this.type = type;
|
---|
140 | }
|
---|
141 | public void run() {
|
---|
142 | // Reset, and calculate progress bar size.
|
---|
143 | queue.calculateSize(source_nodes);
|
---|
144 | // 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.
|
---|
145 | for(int i = 0; source_nodes != null && i < source_nodes.length; i++) {
|
---|
146 | queue.addJob(id, source, source_nodes[i], target, target_node, type, true, true, true);
|
---|
147 | }
|
---|
148 | }
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 |
|
---|
153 |
|
---|
154 |
|
---|
155 |
|
---|