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.MenuBar;
38 import com.google.gwt.user.client.ui.PopupPanel;
39 import com.google.gwt.user.client.ui.TreeItem;
44 public class DnDFolderPopupMenu extends PopupPanel {
47 * The widget's images.
49 private final Folders.Images images;
51 public DnDFolderPopupMenu(final Folders.Images newImages, final FolderResource target, final Object toCopy, boolean othersShared) {
52 // The popup's constructor's argument is a boolean specifying that it
53 // auto-close itself when the user clicks outside of it.
55 setAnimationEnabled(true);
58 // A dummy command that we will execute from unimplemented leaves.
59 final Command cancelCmd = new Command() {
61 public void execute() {
66 final MenuBar contextMenu = new MenuBar(true);
67 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) {
129 public void onComplete() {
130 final TreeItem folder;
131 TreeItem folderTemp = GSS.get().getFolders().getUserItem(target);
132 if (folderTemp == null)
133 folder = GSS.get().getFolders().getOtherSharedItem(target);
136 GSS.get().getFolders().updateFolder((DnDTreeItem) folder);
137 GSS.get().getStatusPanel().updateStats();
141 public void onError(Throwable t) {
143 if (t instanceof RestException) {
144 int statusCode = ((RestException) t).getHttpStatusCode();
145 if (statusCode == 405)
146 GSS.get().displayError("You don't have the necessary permissions");
148 else if (statusCode == 409)
149 GSS.get().displayError("A folder with the same name already exists");
150 else if (statusCode == 413)
151 GSS.get().displayError("Your quota has been exceeded");
153 GSS.get().displayError("Unable to copy folder:" + ((RestException)t).getHttpStatusText());
155 GSS.get().displayError("System error copying folder:" + t.getMessage());
158 DeferredCommand.addCommand(cf);
161 private void moveFolder(final FolderResource target, final FolderResource toCopy, final List<TreeItem> items) {
162 String atarget = target.getUri();
163 atarget = atarget.endsWith("/") ? atarget : atarget + '/';
164 atarget = atarget + toCopy.getName();
166 PostCommand cf = new PostCommand(toCopy.getUri() + "?move=" + atarget, "", 200) {
169 public void onComplete() {
170 final TreeItem folder;
171 for(TreeItem i : items){
172 DnDTreeItem id = (DnDTreeItem)i;
173 if(id.getChild(toCopy) != null)
174 id.removeItem(id.getChild(toCopy));
176 GSS.get().getFolders().clearSelection();
177 TreeItem folderTemp = GSS.get().getFolders().getUserItem(target);
178 if (folderTemp == null)
179 folder = GSS.get().getFolders().getOtherSharedItem(target);
182 GSS.get().getFolders().updateFolder((DnDTreeItem) folder);
183 GSS.get().showFileList(true);
184 GSS.get().getStatusPanel().updateStats();
188 public void onError(Throwable t) {
190 if (t instanceof RestException) {
191 int statusCode = ((RestException) t).getHttpStatusCode();
192 if (statusCode == 405)
193 GSS.get().displayError("You don't have the necessary permissions");
195 else if (statusCode == 409)
196 GSS.get().displayError("A folder with the same name already exists");
197 else if (statusCode == 413)
198 GSS.get().displayError("Your quota has been exceeded");
200 GSS.get().displayError("Unable to copy folder:" + ((RestException)t).getHttpStatusText());
202 GSS.get().displayError("System error copying folder:" + t.getMessage());
205 DeferredCommand.addCommand(cf);
208 private void copyFiles(final FolderResource ftarget, List<FileResource> files) {
209 List<String> fileIds = new ArrayList<String>();
210 String target = ftarget.getUri();
211 target = target.endsWith("/") ? target : target + '/';
212 for (FileResource fileResource : files) {
213 String fileTarget = target + URL.encodeComponent(fileResource.getName());
214 fileIds.add(fileResource.getUri() + "?copy=" + fileTarget);
217 executeCopyOrMoveFiles(index, fileIds);
221 private void moveFiles(final FolderResource ftarget, List<FileResource> files) {
222 List<String> fileIds = new ArrayList<String>();
223 String target = ftarget.getUri();
224 target = target.endsWith("/") ? target : target + '/';
225 for (FileResource fileResource : files) {
226 String fileTarget = target + URL.encodeComponent(fileResource.getName());
227 fileIds.add(fileResource.getUri() + "?move=" + fileTarget);
230 executeCopyOrMoveFiles(index, fileIds);
234 private void trashFolder(final FolderResource folder, final List<TreeItem> items){
235 PostCommand tot = new PostCommand(folder.getUri()+"?trash=","",200){
238 public void onComplete() {
239 for(TreeItem item : items)
240 GSS.get().getFolders().updateFolder((DnDTreeItem) item);
241 GSS.get().getFolders().update(GSS.get().getFolders().getTrashItem());
242 GSS.get().showFileList(true);
246 public void onError(Throwable t) {
248 if(t instanceof RestException){
249 int statusCode = ((RestException)t).getHttpStatusCode();
250 if(statusCode == 405)
251 GSS.get().displayError("You don't have the necessary permissions");
252 else if(statusCode == 404)
253 GSS.get().displayError("Folder does not exist");
255 GSS.get().displayError("Unable to trash folder:"+((RestException)t).getHttpStatusText());
258 GSS.get().displayError("System error trashing folder:"+t.getMessage());
261 DeferredCommand.addCommand(tot);
264 private void trashFiles(List<FileResource> files){
265 final List<String> fileIds = new ArrayList<String>();
266 for(FileResource f : files)
267 fileIds.add(f.getUri()+"?trash=");
268 MultiplePostCommand tot = new MultiplePostCommand(fileIds.toArray(new String[0]),200){
271 public void onComplete() {
272 GSS.get().showFileList(true);
276 public void onError(String p, Throwable t) {
278 if(t instanceof RestException){
279 int statusCode = ((RestException)t).getHttpStatusCode();
280 if(statusCode == 405)
281 GSS.get().displayError("You don't have the necessary permissions");
282 else if(statusCode == 404)
283 GSS.get().displayError("File does not exist");
285 GSS.get().displayError("Unable to trash file:"+((RestException)t).getHttpStatusText());
288 GSS.get().displayError("System error trashing file:"+t.getMessage());
291 DeferredCommand.addCommand(tot);
295 private void executeCopyOrMoveFiles(final int index, final List<String> paths) {
296 if (index >= paths.size()) {
297 GSS.get().showFileList(true);
298 GSS.get().getStatusPanel().updateStats();
301 PostCommand cf = new PostCommand(paths.get(index), "", 200) {
304 public void onComplete() {
305 executeCopyOrMoveFiles(index + 1, paths);
309 public void onError(Throwable t) {
311 if (t instanceof RestException) {
312 int statusCode = ((RestException) t).getHttpStatusCode();
313 if (statusCode == 405)
314 GSS.get().displayError("You don't have the necessary permissions");
315 else if (statusCode == 404)
316 GSS.get().displayError("File not found");
317 else if (statusCode == 409)
318 GSS.get().displayError("A file with the same name already exists");
319 else if (statusCode == 413)
320 GSS.get().displayError("Your quota has been exceeded");
322 GSS.get().displayError("Unable to copy file:" + ((RestException)t).getHttpStatusText());
324 GSS.get().displayError("System error copying file:" + t.getMessage());
328 DeferredCommand.addCommand(cf);