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.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;
45 public class DnDFolderPopupMenu extends PopupPanel {
48 * The widget's images.
50 private final Folders.Images images;
52 public DnDFolderPopupMenu(final Folders.Images newImages, final FolderResource target, final Object toCopy, boolean othersShared) {
53 // The popup's constructor's argument is a boolean specifying that it
54 // auto-close itself when the user clicks outside of it.
56 setAnimationEnabled(true);
59 // A dummy command that we will execute from unimplemented leaves.
60 final Command cancelCmd = new Command() {
62 public void execute() {
67 final MenuBar contextMenu = new MenuBar(true);
68 final Folders folders = GSS.get().getFolders();
70 contextMenu.addItem("<span>" + AbstractImagePrototype.create(newImages.cut()).getHTML() + " Move</span>", true, new Command() {
72 public void execute() {
73 if (toCopy instanceof FolderResource){
74 List<TreeItem> treeItems = folders.getItemsOfTreeForPath(((RestResource) toCopy).getUri());
75 List<TreeItem> parents = new ArrayList();
76 for(TreeItem item : treeItems)
77 if(item.getParentItem() != null)
78 parents.add(item.getParentItem());
79 moveFolder(target, (FolderResource) toCopy, parents);
81 else if(toCopy instanceof List)
82 moveFiles(target, (List<FileResource>) toCopy);
86 }).setVisible(target != null);
88 contextMenu.addItem("<span>" + AbstractImagePrototype.create(newImages.copy()).getHTML() + " Copy</span>", true, new Command() {
90 public void execute() {
91 if (toCopy instanceof FolderResource)
92 copyFolder(target, (FolderResource) toCopy);
93 else if(toCopy instanceof List)
94 copyFiles(target, (List<FileResource>) toCopy);
98 }).setVisible(target != null);
100 contextMenu.addItem("<span>" + AbstractImagePrototype.create(newImages.trash()).getHTML() + " Delete (Trash)</span>", true, new Command() {
102 public void execute() {
103 if (toCopy instanceof FolderResource){
104 final List<TreeItem> treeItems = folders.getItemsOfTreeForPath(((RestResource) toCopy).getUri());
105 List<TreeItem> parents = new ArrayList();
106 for(TreeItem item : treeItems)
107 if(item.getParentItem() != null)
108 parents.add(item.getParentItem());
109 trashFolder((FolderResource) toCopy, parents);
111 else if(toCopy instanceof List)
112 trashFiles((List<FileResource>) toCopy);
116 }).setVisible(target == null);
117 contextMenu.addItem("<span>" + AbstractImagePrototype.create(newImages.delete()).getHTML() + " Cancel</span>", true, cancelCmd);
123 private void copyFolder(final FolderResource target, FolderResource toCopy) {
124 String atarget = target.getUri();
125 atarget = atarget.endsWith("/") ? atarget : atarget + '/';
126 atarget = atarget + toCopy.getName();
127 PostCommand cf = new PostCommand(toCopy.getUri() + "?copy=" + atarget, "", 200) {
130 public void onComplete() {
131 final TreeItem folder;
132 TreeItem folderTemp = GSS.get().getFolders().getUserItem(target);
133 if (folderTemp == null)
134 folder = GSS.get().getFolders().getOtherSharedItem(target);
137 GSS.get().getFolders().updateFolder((DnDTreeItem) folder);
138 GSS.get().getStatusPanel().updateStats();
142 public void onError(Throwable t) {
144 if (t instanceof RestException) {
145 int statusCode = ((RestException) t).getHttpStatusCode();
146 if (statusCode == 405)
147 GSS.get().displayError("You don't have the necessary permissions");
149 else if (statusCode == 409)
150 GSS.get().displayError("A folder with the same name already exists");
151 else if (statusCode == 413)
152 GSS.get().displayError("Your quota has been exceeded");
154 GSS.get().displayError("Unable to copy folder:" + ((RestException)t).getHttpStatusText());
156 GSS.get().displayError("System error copying folder:" + t.getMessage());
159 DeferredCommand.addCommand(cf);
162 private void moveFolder(final FolderResource target, final FolderResource toCopy, final List<TreeItem> items) {
163 String atarget = target.getUri();
164 atarget = atarget.endsWith("/") ? atarget : atarget + '/';
165 atarget = atarget + toCopy.getName();
167 PostCommand cf = new PostCommand(toCopy.getUri() + "?move=" + atarget, "", 200) {
170 public void onComplete() {
171 final TreeItem folder;
172 for(TreeItem i : items){
173 DnDTreeItem id = (DnDTreeItem)i;
174 if(id.getChild(toCopy) != null)
175 id.removeItem(id.getChild(toCopy));
177 GSS.get().getFolders().clearSelection();
178 TreeItem folderTemp = GSS.get().getFolders().getUserItem(target);
179 if (folderTemp == null)
180 folder = GSS.get().getFolders().getOtherSharedItem(target);
183 GSS.get().getFolders().updateFolder((DnDTreeItem) folder);
184 GSS.get().showFileList(true);
185 GSS.get().getStatusPanel().updateStats();
189 public void onError(Throwable t) {
191 if (t instanceof RestException) {
192 int statusCode = ((RestException) t).getHttpStatusCode();
193 if (statusCode == 405)
194 GSS.get().displayError("You don't have the necessary permissions");
196 else if (statusCode == 409)
197 GSS.get().displayError("A folder with the same name already exists");
198 else if (statusCode == 413)
199 GSS.get().displayError("Your quota has been exceeded");
201 GSS.get().displayError("Unable to copy folder:" + ((RestException)t).getHttpStatusText());
203 GSS.get().displayError("System error copying folder:" + t.getMessage());
206 DeferredCommand.addCommand(cf);
209 private void copyFiles(final FolderResource ftarget, List<FileResource> files) {
210 List<String> fileIds = new ArrayList<String>();
211 String target = ftarget.getUri();
212 target = target.endsWith("/") ? target : target + '/';
213 for (FileResource fileResource : files) {
214 String fileTarget = target + URL.encodeComponent(fileResource.getName());
215 fileIds.add(fileResource.getUri() + "?copy=" + fileTarget);
218 executeCopyOrMoveFiles(index, fileIds);
222 private void moveFiles(final FolderResource ftarget, List<FileResource> files) {
223 List<String> fileIds = new ArrayList<String>();
224 String target = ftarget.getUri();
225 target = target.endsWith("/") ? target : target + '/';
226 for (FileResource fileResource : files) {
227 String fileTarget = target + URL.encodeComponent(fileResource.getName());
228 fileIds.add(fileResource.getUri() + "?move=" + fileTarget);
231 executeCopyOrMoveFiles(index, fileIds);
235 private void trashFolder(final FolderResource folder, final List<TreeItem> items){
236 PostCommand tot = new PostCommand(folder.getUri()+"?trash=","",200){
239 public void onComplete() {
240 for(TreeItem item : items)
241 GSS.get().getFolders().updateFolder((DnDTreeItem) item);
242 GSS.get().getFolders().update(GSS.get().getFolders().getTrashItem());
243 GSS.get().showFileList(true);
247 public void onError(Throwable t) {
249 if(t instanceof RestException){
250 int statusCode = ((RestException)t).getHttpStatusCode();
251 if(statusCode == 405)
252 GSS.get().displayError("You don't have the necessary permissions");
253 else if(statusCode == 404)
254 GSS.get().displayError("Folder does not exist");
256 GSS.get().displayError("Unable to trash folder:"+((RestException)t).getHttpStatusText());
259 GSS.get().displayError("System error trashing folder:"+t.getMessage());
262 DeferredCommand.addCommand(tot);
265 private void trashFiles(List<FileResource> files){
266 final List<String> fileIds = new ArrayList<String>();
267 for(FileResource f : files)
268 fileIds.add(f.getUri()+"?trash=");
269 MultiplePostCommand tot = new MultiplePostCommand(fileIds.toArray(new String[0]),200){
272 public void onComplete() {
273 GSS.get().showFileList(true);
277 public void onError(String p, Throwable t) {
279 if(t instanceof RestException){
280 int statusCode = ((RestException)t).getHttpStatusCode();
281 if(statusCode == 405)
282 GSS.get().displayError("You don't have the necessary permissions");
283 else if(statusCode == 404)
284 GSS.get().displayError("File does not exist");
286 GSS.get().displayError("Unable to trash file:"+((RestException)t).getHttpStatusText());
289 GSS.get().displayError("System error trashing file:"+t.getMessage());
292 DeferredCommand.addCommand(tot);
296 private void executeCopyOrMoveFiles(final int index, final List<String> paths) {
297 if (index >= paths.size()) {
298 GSS.get().showFileList(true);
299 GSS.get().getStatusPanel().updateStats();
302 PostCommand cf = new PostCommand(paths.get(index), "", 200) {
305 public void onComplete() {
306 executeCopyOrMoveFiles(index + 1, paths);
310 public void onError(Throwable t) {
312 if (t instanceof RestException) {
313 int statusCode = ((RestException) t).getHttpStatusCode();
314 if (statusCode == 405)
315 GSS.get().displayError("You don't have the necessary permissions");
316 else if (statusCode == 404)
317 GSS.get().displayError("File not found");
318 else if (statusCode == 409)
319 GSS.get().displayError("A file with the same name already exists");
320 else if (statusCode == 413)
321 GSS.get().displayError("Your quota has been exceeded");
323 GSS.get().displayError("Unable to copy file:" + ((RestException)t).getHttpStatusText());
325 GSS.get().displayError("System error copying file:" + t.getMessage());
329 DeferredCommand.addCommand(cf);