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();
69 contextMenu.addItem("<span>" + newImages.cut().getHTML() + " Move</span>", true, new Command() {
71 public void execute() {
72 if (toCopy instanceof FolderResource){
73 List<TreeItem> treeItems = folders.getItemsOfTreeForPath(((RestResource) toCopy).getUri());
74 List<TreeItem> parents = new ArrayList();
75 for(TreeItem item : treeItems)
76 if(item.getParentItem() != null)
77 parents.add(item.getParentItem());
78 moveFolder(target, (FolderResource) toCopy, parents);
80 else if(toCopy instanceof List)
81 moveFiles(target, (List<FileResource>) toCopy);
85 }).setVisible(target != null);
87 contextMenu.addItem("<span>" + newImages.copy().getHTML() + " Copy</span>", true, new Command() {
89 public void execute() {
90 if (toCopy instanceof FolderResource)
91 copyFolder(target, (FolderResource) toCopy);
92 else if(toCopy instanceof List)
93 copyFiles(target, (List<FileResource>) toCopy);
97 }).setVisible(target != null);
99 contextMenu.addItem("<span>" + newImages.trash().getHTML() + " Delete (Trash)</span>", true, new Command() {
101 public void execute() {
102 if (toCopy instanceof FolderResource){
103 final List<TreeItem> treeItems = folders.getItemsOfTreeForPath(((RestResource) toCopy).getUri());
104 List<TreeItem> parents = new ArrayList();
105 for(TreeItem item : treeItems)
106 if(item.getParentItem() != null)
107 parents.add(item.getParentItem());
108 trashFolder((FolderResource) toCopy, parents);
110 else if(toCopy instanceof List)
111 trashFiles((List<FileResource>) toCopy);
115 }).setVisible(target == null);
116 contextMenu.addItem("<span>" + newImages.delete().getHTML() + " Cancel</span>", true, cancelCmd);
122 private void copyFolder(final FolderResource target, FolderResource toCopy) {
123 String atarget = target.getUri();
124 atarget = atarget.endsWith("/") ? atarget : atarget + '/';
125 atarget = atarget + toCopy.getName();
126 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);
138 public void onError(Throwable t) {
140 if (t instanceof RestException) {
141 int statusCode = ((RestException) t).getHttpStatusCode();
142 if (statusCode == 405)
143 GSS.get().displayError("You don't have the necessary permissions");
145 else if (statusCode == 409)
146 GSS.get().displayError("A folder with the same name already exists");
147 else if (statusCode == 413)
148 GSS.get().displayError("Your quota has been exceeded");
150 GSS.get().displayError("Unable to copy folder:" + ((RestException)t).getHttpStatusText());
152 GSS.get().displayError("System error copying folder:" + t.getMessage());
155 DeferredCommand.addCommand(cf);
158 private void moveFolder(final FolderResource target, final FolderResource toCopy, final List<TreeItem> items) {
159 String atarget = target.getUri();
160 atarget = atarget.endsWith("/") ? atarget : atarget + '/';
161 atarget = atarget + toCopy.getName();
163 PostCommand cf = new PostCommand(toCopy.getUri() + "?move=" + atarget, "", 200) {
165 public void onComplete() {
166 final TreeItem folder;
167 for(TreeItem i : items){
168 DnDTreeItem id = (DnDTreeItem)i;
169 if(id.getChild(toCopy) != null)
170 id.removeItem(id.getChild(toCopy));
172 GSS.get().getFolders().clearSelection();
173 TreeItem folderTemp = GSS.get().getFolders().getUserItem(target);
174 if (folderTemp == null)
175 folder = GSS.get().getFolders().getOtherSharedItem(target);
178 GSS.get().getFolders().updateFolder((DnDTreeItem) folder);
179 GSS.get().showFileList(true);
182 public void onError(Throwable t) {
184 if (t instanceof RestException) {
185 int statusCode = ((RestException) t).getHttpStatusCode();
186 if (statusCode == 405)
187 GSS.get().displayError("You don't have the necessary permissions");
189 else if (statusCode == 409)
190 GSS.get().displayError("A folder with the same name already exists");
191 else if (statusCode == 413)
192 GSS.get().displayError("Your quota has been exceeded");
194 GSS.get().displayError("Unable to copy folder:" + ((RestException)t).getHttpStatusText());
196 GSS.get().displayError("System error copying folder:" + t.getMessage());
199 DeferredCommand.addCommand(cf);
202 private void copyFiles(final FolderResource ftarget, List<FileResource> files) {
203 List<String> fileIds = new ArrayList<String>();
204 String target = ftarget.getUri();
205 target = target.endsWith("/") ? target : target + '/';
206 for (FileResource fileResource : files) {
207 String fileTarget = target + fileResource.getName();
208 fileIds.add(fileResource.getUri() + "?copy=" + fileTarget);
211 executeCopyOrMoveFiles(index, fileIds);
215 private void moveFiles(final FolderResource ftarget, List<FileResource> files) {
216 List<String> fileIds = new ArrayList<String>();
217 String target = ftarget.getUri();
218 target = target.endsWith("/") ? target : target + '/';
219 for (FileResource fileResource : files) {
220 String fileTarget = target + fileResource.getName();
221 fileIds.add(fileResource.getUri() + "?move=" + fileTarget);
224 executeCopyOrMoveFiles(index, fileIds);
228 private void trashFolder(final FolderResource folder, final List<TreeItem> items){
229 PostCommand tot = new PostCommand(folder.getUri()+"?trash=","",200){
231 public void onComplete() {
232 for(TreeItem item : items)
233 GSS.get().getFolders().updateFolder((DnDTreeItem) item);
234 GSS.get().getFolders().update(GSS.get().getFolders().getTrashItem());
235 GSS.get().showFileList(true);
238 public void onError(Throwable t) {
240 if(t instanceof RestException){
241 int statusCode = ((RestException)t).getHttpStatusCode();
242 if(statusCode == 405)
243 GSS.get().displayError("You don't have the necessary permissions");
244 else if(statusCode == 404)
245 GSS.get().displayError("Folder does not exist");
247 GSS.get().displayError("Unable to trash folder:"+((RestException)t).getHttpStatusText());
250 GSS.get().displayError("System error trashing folder:"+t.getMessage());
253 DeferredCommand.addCommand(tot);
256 private void trashFiles(List<FileResource> files){
257 final List<String> fileIds = new ArrayList<String>();
258 for(FileResource f : files)
259 fileIds.add(f.getUri()+"?trash=");
260 MultiplePostCommand tot = new MultiplePostCommand(fileIds.toArray(new String[0]),200){
262 public void onComplete() {
263 GSS.get().showFileList(true);
267 public void onError(String p, Throwable t) {
269 if(t instanceof RestException){
270 int statusCode = ((RestException)t).getHttpStatusCode();
271 if(statusCode == 405)
272 GSS.get().displayError("You don't have the necessary permissions");
273 else if(statusCode == 404)
274 GSS.get().displayError("File does not exist");
276 GSS.get().displayError("Unable to trash file:"+((RestException)t).getHttpStatusText());
279 GSS.get().displayError("System error trashing file:"+t.getMessage());
282 DeferredCommand.addCommand(tot);
286 private void executeCopyOrMoveFiles(final int index, final List<String> paths) {
287 if (index >= paths.size()) {
288 GSS.get().showFileList(true);
291 PostCommand cf = new PostCommand(paths.get(index), "", 200) {
294 public void onComplete() {
295 executeCopyOrMoveFiles(index + 1, paths);
299 public void onError(Throwable t) {
301 if (t instanceof RestException) {
302 int statusCode = ((RestException) t).getHttpStatusCode();
303 if (statusCode == 405)
304 GSS.get().displayError("You don't have the necessary permissions");
305 else if (statusCode == 404)
306 GSS.get().displayError("File not found");
307 else if (statusCode == 409)
308 GSS.get().displayError("A file with the same name already exists");
309 else if (statusCode == 413)
310 GSS.get().displayError("Your quota has been exceeded");
312 GSS.get().displayError("Unable to copy file:" + ((RestException)t).getHttpStatusText());
314 GSS.get().displayError("System error copying file:" + t.getMessage());
318 DeferredCommand.addCommand(cf);