root / src / gr / ebs / gss / client / dnd / DnDFolderPopupMenu.java @ 023f6f1e
History | View | Annotate | Download (11 kB)
1 |
/*
|
---|---|
2 |
* Copyright 2008, 2009 Electronic Business Systems Ltd.
|
3 |
*
|
4 |
* This file is part of GSS.
|
5 |
*
|
6 |
* GSS is free software: you can redistribute it and/or modify
|
7 |
* it under the terms of the GNU General Public License as published by
|
8 |
* the Free Software Foundation, either version 3 of the License, or
|
9 |
* (at your option) any later version.
|
10 |
*
|
11 |
* GSS is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with GSS. If not, see <http://www.gnu.org/licenses/>.
|
18 |
*/
|
19 |
package gr.ebs.gss.client.dnd; |
20 |
|
21 |
import gr.ebs.gss.client.Folders; |
22 |
import gr.ebs.gss.client.GSS; |
23 |
import gr.ebs.gss.client.rest.MultiplePostCommand; |
24 |
import gr.ebs.gss.client.rest.PostCommand; |
25 |
import gr.ebs.gss.client.rest.RestException; |
26 |
import gr.ebs.gss.client.rest.resource.FileResource; |
27 |
import gr.ebs.gss.client.rest.resource.FolderResource; |
28 |
import gr.ebs.gss.client.rest.resource.RestResource; |
29 |
|
30 |
import java.util.ArrayList; |
31 |
import java.util.List; |
32 |
|
33 |
import com.google.gwt.core.client.GWT; |
34 |
import com.google.gwt.http.client.URL; |
35 |
import com.google.gwt.user.client.Command; |
36 |
import com.google.gwt.user.client.DeferredCommand; |
37 |
import com.google.gwt.user.client.ui.AbstractImagePrototype; |
38 |
import com.google.gwt.user.client.ui.MenuBar; |
39 |
import com.google.gwt.user.client.ui.PopupPanel; |
40 |
import com.google.gwt.user.client.ui.TreeItem; |
41 |
|
42 |
/**
|
43 |
* @author kman
|
44 |
*/
|
45 |
public class DnDFolderPopupMenu extends PopupPanel { |
46 |
|
47 |
public DnDFolderPopupMenu(final Folders.Images newImages, final FolderResource target, final Object toCopy) { |
48 |
// The popup's constructor's argument is a boolean specifying that it
|
49 |
// auto-close itself when the user clicks outside of it.
|
50 |
super(true); |
51 |
setAnimationEnabled(true);
|
52 |
// A dummy command that we will execute from unimplemented leaves.
|
53 |
final Command cancelCmd = new Command() { |
54 |
|
55 |
@Override
|
56 |
public void execute() { |
57 |
hide(); |
58 |
} |
59 |
}; |
60 |
|
61 |
final MenuBar contextMenu = new MenuBar(true); |
62 |
final Folders folders = GSS.get().getFolders();
|
63 |
|
64 |
contextMenu.addItem("<span>" + AbstractImagePrototype.create(newImages.cut()).getHTML() + " Move</span>", true, new Command() { |
65 |
|
66 |
@Override
|
67 |
public void execute() { |
68 |
if (toCopy instanceof FolderResource){ |
69 |
List<TreeItem> treeItems = folders.getItemsOfTreeForPath(((RestResource) toCopy).getUri());
|
70 |
List<TreeItem> parents = new ArrayList(); |
71 |
for(TreeItem item : treeItems)
|
72 |
if(item.getParentItem() != null) |
73 |
parents.add(item.getParentItem()); |
74 |
moveFolder(target, (FolderResource) toCopy, parents); |
75 |
} |
76 |
else if(toCopy instanceof List) |
77 |
moveFiles(target, (List<FileResource>) toCopy);
|
78 |
hide(); |
79 |
} |
80 |
|
81 |
}).setVisible(target != null);
|
82 |
|
83 |
contextMenu.addItem("<span>" + AbstractImagePrototype.create(newImages.copy()).getHTML() + " Copy</span>", true, new Command() { |
84 |
|
85 |
@Override
|
86 |
public void execute() { |
87 |
if (toCopy instanceof FolderResource) |
88 |
copyFolder(target, (FolderResource) toCopy); |
89 |
else if(toCopy instanceof List) |
90 |
copyFiles(target, (List<FileResource>) toCopy);
|
91 |
hide(); |
92 |
} |
93 |
|
94 |
}).setVisible(target != null);
|
95 |
|
96 |
contextMenu.addItem("<span>" + AbstractImagePrototype.create(newImages.trash()).getHTML() + " Delete (Trash)</span>", true, new Command() { |
97 |
|
98 |
@Override
|
99 |
public void execute() { |
100 |
if (toCopy instanceof FolderResource){ |
101 |
final List<TreeItem> treeItems = folders.getItemsOfTreeForPath(((RestResource) toCopy).getUri()); |
102 |
List<TreeItem> parents = new ArrayList(); |
103 |
for(TreeItem item : treeItems)
|
104 |
if(item.getParentItem() != null) |
105 |
parents.add(item.getParentItem()); |
106 |
trashFolder((FolderResource) toCopy, parents); |
107 |
} |
108 |
else if(toCopy instanceof List) |
109 |
trashFiles((List<FileResource>) toCopy);
|
110 |
hide(); |
111 |
} |
112 |
|
113 |
}).setVisible(target == null);
|
114 |
contextMenu.addItem("<span>" + AbstractImagePrototype.create(newImages.delete()).getHTML() + " Cancel</span>", true, cancelCmd); |
115 |
|
116 |
add(contextMenu); |
117 |
|
118 |
} |
119 |
|
120 |
private void copyFolder(final FolderResource target, FolderResource toCopy) { |
121 |
String atarget = target.getUri();
|
122 |
atarget = atarget.endsWith("/") ? atarget : atarget + '/'; |
123 |
atarget = atarget + toCopy.getName(); |
124 |
PostCommand cf = new PostCommand(toCopy.getUri() + "?copy=" + atarget, "", 200) { |
125 |
|
126 |
@Override
|
127 |
public void onComplete() { |
128 |
final TreeItem folder;
|
129 |
TreeItem folderTemp = GSS.get().getFolders().getUserItem(target); |
130 |
if (folderTemp == null) |
131 |
folder = GSS.get().getFolders().getOtherSharedItem(target); |
132 |
else
|
133 |
folder = folderTemp; |
134 |
GSS.get().getFolders().updateFolder((DnDTreeItem) folder); |
135 |
GSS.get().getStatusPanel().updateStats(); |
136 |
} |
137 |
|
138 |
@Override
|
139 |
public void onError(Throwable t) { |
140 |
GWT.log("", t);
|
141 |
if (t instanceof RestException) { |
142 |
int statusCode = ((RestException) t).getHttpStatusCode();
|
143 |
if (statusCode == 405) |
144 |
GSS.get().displayError("You don't have the necessary permissions");
|
145 |
|
146 |
else if (statusCode == 409) |
147 |
GSS.get().displayError("A folder with the same name already exists");
|
148 |
else if (statusCode == 413) |
149 |
GSS.get().displayError("Your quota has been exceeded");
|
150 |
else
|
151 |
GSS.get().displayError("Unable to copy folder:" + ((RestException)t).getHttpStatusText());
|
152 |
} else
|
153 |
GSS.get().displayError("System error copying folder:" + t.getMessage());
|
154 |
} |
155 |
}; |
156 |
DeferredCommand.addCommand(cf); |
157 |
} |
158 |
|
159 |
private void moveFolder(final FolderResource target, final FolderResource toCopy, final List<TreeItem> items) { |
160 |
String atarget = target.getUri();
|
161 |
atarget = atarget.endsWith("/") ? atarget : atarget + '/'; |
162 |
atarget = atarget + toCopy.getName(); |
163 |
|
164 |
PostCommand cf = new PostCommand(toCopy.getUri() + "?move=" + atarget, "", 200) { |
165 |
|
166 |
@Override
|
167 |
public void onComplete() { |
168 |
final TreeItem folder;
|
169 |
for(TreeItem i : items){
|
170 |
DnDTreeItem id = (DnDTreeItem)i; |
171 |
if(id.getChild(toCopy) != null) |
172 |
id.removeItem(id.getChild(toCopy)); |
173 |
} |
174 |
GSS.get().getFolders().clearSelection(); |
175 |
TreeItem folderTemp = GSS.get().getFolders().getUserItem(target); |
176 |
if (folderTemp == null) |
177 |
folder = GSS.get().getFolders().getOtherSharedItem(target); |
178 |
else
|
179 |
folder = folderTemp; |
180 |
GSS.get().getFolders().updateFolder((DnDTreeItem) folder); |
181 |
GSS.get().showFileList(true);
|
182 |
GSS.get().getStatusPanel().updateStats(); |
183 |
} |
184 |
|
185 |
@Override
|
186 |
public void onError(Throwable t) { |
187 |
GWT.log("", t);
|
188 |
if (t instanceof RestException) { |
189 |
int statusCode = ((RestException) t).getHttpStatusCode();
|
190 |
if (statusCode == 405) |
191 |
GSS.get().displayError("You don't have the necessary permissions");
|
192 |
|
193 |
else if (statusCode == 409) |
194 |
GSS.get().displayError("A folder with the same name already exists");
|
195 |
else if (statusCode == 413) |
196 |
GSS.get().displayError("Your quota has been exceeded");
|
197 |
else
|
198 |
GSS.get().displayError("Unable to copy folder:" + ((RestException)t).getHttpStatusText());
|
199 |
} else
|
200 |
GSS.get().displayError("System error copying folder:" + t.getMessage());
|
201 |
} |
202 |
}; |
203 |
DeferredCommand.addCommand(cf); |
204 |
} |
205 |
|
206 |
private void copyFiles(final FolderResource ftarget, List<FileResource> files) { |
207 |
List<String> fileIds = new ArrayList<String>(); |
208 |
String target = ftarget.getUri();
|
209 |
target = target.endsWith("/") ? target : target + '/'; |
210 |
for (FileResource fileResource : files) {
|
211 |
String fileTarget = target + URL.encodeComponent(fileResource.getName()); |
212 |
fileIds.add(fileResource.getUri() + "?copy=" + fileTarget);
|
213 |
} |
214 |
int index = 0; |
215 |
executeCopyOrMoveFiles(index, fileIds); |
216 |
|
217 |
} |
218 |
|
219 |
private void moveFiles(final FolderResource ftarget, List<FileResource> files) { |
220 |
List<String> fileIds = new ArrayList<String>(); |
221 |
String target = ftarget.getUri();
|
222 |
target = target.endsWith("/") ? target : target + '/'; |
223 |
for (FileResource fileResource : files) {
|
224 |
String fileTarget = target + URL.encodeComponent(fileResource.getName()); |
225 |
fileIds.add(fileResource.getUri() + "?move=" + fileTarget);
|
226 |
} |
227 |
int index = 0; |
228 |
executeCopyOrMoveFiles(index, fileIds); |
229 |
|
230 |
} |
231 |
|
232 |
private void trashFolder(final FolderResource folder, final List<TreeItem> items){ |
233 |
PostCommand tot = new PostCommand(folder.getUri()+"?trash=","",200){ |
234 |
|
235 |
@Override
|
236 |
public void onComplete() { |
237 |
for(TreeItem item : items)
|
238 |
GSS.get().getFolders().updateFolder((DnDTreeItem) item); |
239 |
GSS.get().getFolders().update(GSS.get().getFolders().getTrashItem()); |
240 |
GSS.get().showFileList(true);
|
241 |
} |
242 |
|
243 |
@Override
|
244 |
public void onError(Throwable t) { |
245 |
GWT.log("", t);
|
246 |
if(t instanceof RestException){ |
247 |
int statusCode = ((RestException)t).getHttpStatusCode();
|
248 |
if(statusCode == 405) |
249 |
GSS.get().displayError("You don't have the necessary permissions");
|
250 |
else if(statusCode == 404) |
251 |
GSS.get().displayError("Folder does not exist");
|
252 |
else
|
253 |
GSS.get().displayError("Unable to trash folder:"+((RestException)t).getHttpStatusText());
|
254 |
} |
255 |
else
|
256 |
GSS.get().displayError("System error trashing folder:"+t.getMessage());
|
257 |
} |
258 |
}; |
259 |
DeferredCommand.addCommand(tot); |
260 |
} |
261 |
|
262 |
private void trashFiles(List<FileResource> files){ |
263 |
final List<String> fileIds = new ArrayList<String>(); |
264 |
for(FileResource f : files)
|
265 |
fileIds.add(f.getUri()+"?trash=");
|
266 |
MultiplePostCommand tot = new MultiplePostCommand(fileIds.toArray(new String[0]),200){ |
267 |
|
268 |
@Override
|
269 |
public void onComplete() { |
270 |
GSS.get().showFileList(true);
|
271 |
} |
272 |
|
273 |
@Override
|
274 |
public void onError(String p, Throwable t) { |
275 |
GWT.log("", t);
|
276 |
if(t instanceof RestException){ |
277 |
int statusCode = ((RestException)t).getHttpStatusCode();
|
278 |
if(statusCode == 405) |
279 |
GSS.get().displayError("You don't have the necessary permissions");
|
280 |
else if(statusCode == 404) |
281 |
GSS.get().displayError("File does not exist");
|
282 |
else
|
283 |
GSS.get().displayError("Unable to trash file:"+((RestException)t).getHttpStatusText());
|
284 |
} |
285 |
else
|
286 |
GSS.get().displayError("System error trashing file:"+t.getMessage());
|
287 |
} |
288 |
}; |
289 |
DeferredCommand.addCommand(tot); |
290 |
} |
291 |
|
292 |
|
293 |
private void executeCopyOrMoveFiles(final int index, final List<String> paths) { |
294 |
if (index >= paths.size()) {
|
295 |
GSS.get().showFileList(true);
|
296 |
GSS.get().getStatusPanel().updateStats(); |
297 |
return;
|
298 |
} |
299 |
PostCommand cf = new PostCommand(paths.get(index), "", 200) { |
300 |
|
301 |
@Override
|
302 |
public void onComplete() { |
303 |
executeCopyOrMoveFiles(index + 1, paths);
|
304 |
} |
305 |
|
306 |
@Override
|
307 |
public void onError(Throwable t) { |
308 |
GWT.log("", t);
|
309 |
if (t instanceof RestException) { |
310 |
int statusCode = ((RestException) t).getHttpStatusCode();
|
311 |
if (statusCode == 405) |
312 |
GSS.get().displayError("You don't have the necessary permissions");
|
313 |
else if (statusCode == 404) |
314 |
GSS.get().displayError("File not found");
|
315 |
else if (statusCode == 409) |
316 |
GSS.get().displayError("A file with the same name already exists");
|
317 |
else if (statusCode == 413) |
318 |
GSS.get().displayError("Your quota has been exceeded");
|
319 |
else
|
320 |
GSS.get().displayError("Unable to copy file:" + ((RestException)t).getHttpStatusText());
|
321 |
} else
|
322 |
GSS.get().displayError("System error copying file:" + t.getMessage());
|
323 |
|
324 |
} |
325 |
}; |
326 |
DeferredCommand.addCommand(cf); |
327 |
} |
328 |
|
329 |
} |