2 * Copyright 2008, 2009 Electronic Business Systems Ltd.
4 * This file is part of GSS.
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.
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.
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/>.
19 package gr.ebs.gss.client.dnd;
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;
30 import java.util.ArrayList;
31 import java.util.List;
33 import com.google.gwt.core.client.GWT;
34 import com.google.gwt.user.client.Command;
35 import com.google.gwt.user.client.DeferredCommand;
36 import com.google.gwt.user.client.ui.MenuBar;
37 import com.google.gwt.user.client.ui.PopupPanel;
38 import com.google.gwt.user.client.ui.TreeItem;
43 public class DnDFolderPopupMenu extends PopupPanel {
46 * The widget's images.
48 private final Folders.Images images;
50 public DnDFolderPopupMenu(final Folders.Images newImages, final FolderResource target, final Object toCopy, boolean othersShared) {
51 // The popup's constructor's argument is a boolean specifying that it
52 // auto-close itself when the user clicks outside of it.
54 setAnimationEnabled(true);
57 // A dummy command that we will execute from unimplemented leaves.
58 final Command cancelCmd = new Command() {
60 public void execute() {
65 final MenuBar contextMenu = new MenuBar(true);
66 final Folders folders = GSS.get().getFolders();
68 contextMenu.addItem("<span>" + newImages.cut().getHTML() + " Move</span>", true, new Command() {
70 public void execute() {
71 if (toCopy instanceof FolderResource){
72 List<TreeItem> treeItems = folders.getItemsOfTreeForPath(((RestResource) toCopy).getUri());
73 List<TreeItem> parents = new ArrayList();
74 for(TreeItem item : treeItems)
75 if(item.getParentItem() != null)
76 parents.add(item.getParentItem());
77 moveFolder(target, (FolderResource) toCopy, parents);
79 else if(toCopy instanceof List)
80 moveFiles(target, (List<FileResource>) toCopy);
84 }).setVisible(target != null);
86 contextMenu.addItem("<span>" + newImages.copy().getHTML() + " Copy</span>", true, new Command() {
88 public void execute() {
89 if (toCopy instanceof FolderResource)
90 copyFolder(target, (FolderResource) toCopy);
91 else if(toCopy instanceof List)
92 copyFiles(target, (List<FileResource>) toCopy);
96 }).setVisible(target != null);
98 contextMenu.addItem("<span>" + newImages.trash().getHTML() + " Delete (Trash)</span>", true, new Command() {
100 public void execute() {
101 if (toCopy instanceof FolderResource){
102 final List<TreeItem> treeItems = folders.getItemsOfTreeForPath(((RestResource) toCopy).getUri());
103 List<TreeItem> parents = new ArrayList();
104 for(TreeItem item : treeItems)
105 if(item.getParentItem() != null)
106 parents.add(item.getParentItem());
107 trashFolder((FolderResource) toCopy, parents);
109 else if(toCopy instanceof List)
110 trashFiles((List<FileResource>) toCopy);
114 }).setVisible(target == null);
115 contextMenu.addItem("<span>" + newImages.delete().getHTML() + " Cancel</span>", true, cancelCmd);
121 private void copyFolder(final FolderResource target, FolderResource toCopy) {
122 String atarget = target.getUri();
123 atarget = atarget.endsWith("/") ? atarget : atarget + '/';
124 atarget = atarget + toCopy.getName();
125 PostCommand cf = new PostCommand(toCopy.getUri() + "?copy=" + atarget, "", 200) {
128 public void onComplete() {
129 final TreeItem folder;
130 TreeItem folderTemp = GSS.get().getFolders().getUserItem(target);
131 if (folderTemp == null)
132 folder = GSS.get().getFolders().getOtherSharedItem(target);
135 GSS.get().getFolders().updateFolder((DnDTreeItem) folder);
136 GSS.get().getStatusPanel().updateStats();
140 public void onError(Throwable t) {
142 if (t instanceof RestException) {
143 int statusCode = ((RestException) t).getHttpStatusCode();
144 if (statusCode == 405)
145 GSS.get().displayError("You don't have the necessary permissions");
147 else if (statusCode == 409)
148 GSS.get().displayError("A folder with the same name already exists");
149 else if (statusCode == 413)
150 GSS.get().displayError("Your quota has been exceeded");
152 GSS.get().displayError("Unable to copy folder:" + ((RestException)t).getHttpStatusText());
154 GSS.get().displayError("System error copying folder:" + t.getMessage());
157 DeferredCommand.addCommand(cf);
160 private void moveFolder(final FolderResource target, final FolderResource toCopy, final List<TreeItem> items) {
161 String atarget = target.getUri();
162 atarget = atarget.endsWith("/") ? atarget : atarget + '/';
163 atarget = atarget + toCopy.getName();
165 PostCommand cf = new PostCommand(toCopy.getUri() + "?move=" + atarget, "", 200) {
168 public void onComplete() {
169 final TreeItem folder;
170 for(TreeItem i : items){
171 DnDTreeItem id = (DnDTreeItem)i;
172 if(id.getChild(toCopy) != null)
173 id.removeItem(id.getChild(toCopy));
175 GSS.get().getFolders().clearSelection();
176 TreeItem folderTemp = GSS.get().getFolders().getUserItem(target);
177 if (folderTemp == null)
178 folder = GSS.get().getFolders().getOtherSharedItem(target);
181 GSS.get().getFolders().updateFolder((DnDTreeItem) folder);
182 GSS.get().showFileList(true);
183 GSS.get().getStatusPanel().updateStats();
187 public void onError(Throwable t) {
189 if (t instanceof RestException) {
190 int statusCode = ((RestException) t).getHttpStatusCode();
191 if (statusCode == 405)
192 GSS.get().displayError("You don't have the necessary permissions");
194 else if (statusCode == 409)
195 GSS.get().displayError("A folder with the same name already exists");
196 else if (statusCode == 413)
197 GSS.get().displayError("Your quota has been exceeded");
199 GSS.get().displayError("Unable to copy folder:" + ((RestException)t).getHttpStatusText());
201 GSS.get().displayError("System error copying folder:" + t.getMessage());
204 DeferredCommand.addCommand(cf);
207 private void copyFiles(final FolderResource ftarget, List<FileResource> files) {
208 List<String> fileIds = new ArrayList<String>();
209 String target = ftarget.getUri();
210 target = target.endsWith("/") ? target : target + '/';
211 for (FileResource fileResource : files) {
212 String fileTarget = target + fileResource.getName();
213 fileIds.add(fileResource.getUri() + "?copy=" + fileTarget);
216 executeCopyOrMoveFiles(index, fileIds);
220 private void moveFiles(final FolderResource ftarget, List<FileResource> files) {
221 List<String> fileIds = new ArrayList<String>();
222 String target = ftarget.getUri();
223 target = target.endsWith("/") ? target : target + '/';
224 for (FileResource fileResource : files) {
225 String fileTarget = target + fileResource.getName();
226 fileIds.add(fileResource.getUri() + "?move=" + fileTarget);
229 executeCopyOrMoveFiles(index, fileIds);
233 private void trashFolder(final FolderResource folder, final List<TreeItem> items){
234 PostCommand tot = new PostCommand(folder.getUri()+"?trash=","",200){
237 public void onComplete() {
238 for(TreeItem item : items)
239 GSS.get().getFolders().updateFolder((DnDTreeItem) item);
240 GSS.get().getFolders().update(GSS.get().getFolders().getTrashItem());
241 GSS.get().showFileList(true);
245 public void onError(Throwable t) {
247 if(t instanceof RestException){
248 int statusCode = ((RestException)t).getHttpStatusCode();
249 if(statusCode == 405)
250 GSS.get().displayError("You don't have the necessary permissions");
251 else if(statusCode == 404)
252 GSS.get().displayError("Folder does not exist");
254 GSS.get().displayError("Unable to trash folder:"+((RestException)t).getHttpStatusText());
257 GSS.get().displayError("System error trashing folder:"+t.getMessage());
260 DeferredCommand.addCommand(tot);
263 private void trashFiles(List<FileResource> files){
264 final List<String> fileIds = new ArrayList<String>();
265 for(FileResource f : files)
266 fileIds.add(f.getUri()+"?trash=");
267 MultiplePostCommand tot = new MultiplePostCommand(fileIds.toArray(new String[0]),200){
270 public void onComplete() {
271 GSS.get().showFileList(true);
275 public void onError(String p, Throwable t) {
277 if(t instanceof RestException){
278 int statusCode = ((RestException)t).getHttpStatusCode();
279 if(statusCode == 405)
280 GSS.get().displayError("You don't have the necessary permissions");
281 else if(statusCode == 404)
282 GSS.get().displayError("File does not exist");
284 GSS.get().displayError("Unable to trash file:"+((RestException)t).getHttpStatusText());
287 GSS.get().displayError("System error trashing file:"+t.getMessage());
290 DeferredCommand.addCommand(tot);
294 private void executeCopyOrMoveFiles(final int index, final List<String> paths) {
295 if (index >= paths.size()) {
296 GSS.get().showFileList(true);
297 GSS.get().getStatusPanel().updateStats();
300 PostCommand cf = new PostCommand(paths.get(index), "", 200) {
303 public void onComplete() {
304 executeCopyOrMoveFiles(index + 1, paths);
308 public void onError(Throwable t) {
310 if (t instanceof RestException) {
311 int statusCode = ((RestException) t).getHttpStatusCode();
312 if (statusCode == 405)
313 GSS.get().displayError("You don't have the necessary permissions");
314 else if (statusCode == 404)
315 GSS.get().displayError("File not found");
316 else if (statusCode == 409)
317 GSS.get().displayError("A file with the same name already exists");
318 else if (statusCode == 413)
319 GSS.get().displayError("Your quota has been exceeded");
321 GSS.get().displayError("Unable to copy file:" + ((RestException)t).getHttpStatusText());
323 GSS.get().displayError("System error copying file:" + t.getMessage());
327 DeferredCommand.addCommand(cf);