Formating commit. Comments fixed after code review. 1. Added a 2010 entry in copyrigh...
[pithos] / src / gr / ebs / gss / client / dnd / DnDFolderPopupMenu.java
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         /**
48          * The widget's images.
49          */
50         private final Folders.Images images;
51
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.
55                 super(true);
56                 setAnimationEnabled(true);
57                 images = newImages;
58
59                 // A dummy command that we will execute from unimplemented leaves.
60                 final Command cancelCmd = new Command() {
61
62                         public void execute() {
63                                 hide();
64                         }
65                 };
66
67                 final MenuBar contextMenu = new MenuBar(true);
68                 final Folders folders = GSS.get().getFolders();
69
70                 contextMenu.addItem("<span>" + AbstractImagePrototype.create(newImages.cut()).getHTML() + "&nbsp;Move</span>", true, new Command() {
71
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);
80                                         }
81                                         else if(toCopy instanceof List)
82                                                 moveFiles(target, (List<FileResource>) toCopy);
83                                         hide();
84                                 }
85
86                         }).setVisible(target != null);
87
88                 contextMenu.addItem("<span>" + AbstractImagePrototype.create(newImages.copy()).getHTML() + "&nbsp;Copy</span>", true, new Command() {
89
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);
95                                 hide();
96                         }
97
98                 }).setVisible(target != null);
99
100                 contextMenu.addItem("<span>" + AbstractImagePrototype.create(newImages.trash()).getHTML() + "&nbsp;Delete (Trash)</span>", true, new Command() {
101
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);
110                                 }
111                                 else if(toCopy instanceof List)
112                                         trashFiles((List<FileResource>) toCopy);
113                                 hide();
114                         }
115
116                 }).setVisible(target == null);
117                 contextMenu.addItem("<span>" + AbstractImagePrototype.create(newImages.delete()).getHTML() + "&nbsp;Cancel</span>", true, cancelCmd);
118
119                 add(contextMenu);
120
121         }
122
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) {
128
129                         @Override
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);
135                                 else
136                                         folder = folderTemp;
137                                 GSS.get().getFolders().updateFolder((DnDTreeItem) folder);
138                                 GSS.get().getStatusPanel().updateStats();
139                         }
140
141                         @Override
142                         public void onError(Throwable t) {
143                                 GWT.log("", 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");
148
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");
153                                         else
154                                                 GSS.get().displayError("Unable to copy folder:" + ((RestException)t).getHttpStatusText());
155                                 } else
156                                         GSS.get().displayError("System error copying folder:" + t.getMessage());
157                         }
158                 };
159                 DeferredCommand.addCommand(cf);
160         }
161
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();
166
167                 PostCommand cf = new PostCommand(toCopy.getUri() + "?move=" + atarget, "", 200) {
168
169                         @Override
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));
176                                 }
177                                 GSS.get().getFolders().clearSelection();
178                                 TreeItem folderTemp = GSS.get().getFolders().getUserItem(target);
179                                 if (folderTemp == null)
180                                         folder = GSS.get().getFolders().getOtherSharedItem(target);
181                                 else
182                                         folder = folderTemp;
183                                 GSS.get().getFolders().updateFolder((DnDTreeItem) folder);
184                                 GSS.get().showFileList(true);
185                                 GSS.get().getStatusPanel().updateStats();
186                         }
187
188                         @Override
189                         public void onError(Throwable t) {
190                                 GWT.log("", 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");
195
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");
200                                         else
201                                                 GSS.get().displayError("Unable to copy folder:" + ((RestException)t).getHttpStatusText());
202                                 } else
203                                         GSS.get().displayError("System error copying folder:" + t.getMessage());
204                         }
205                 };
206                 DeferredCommand.addCommand(cf);
207         }
208
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);
216                 }
217                 int index = 0;
218                 executeCopyOrMoveFiles(index, fileIds);
219
220         }
221
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);
229                 }
230                 int index = 0;
231                 executeCopyOrMoveFiles(index, fileIds);
232
233         }
234
235         private void trashFolder(final FolderResource folder, final List<TreeItem> items){
236                 PostCommand tot = new PostCommand(folder.getUri()+"?trash=","",200){
237
238                         @Override
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);
244                         }
245
246                         @Override
247                         public void onError(Throwable t) {
248                                 GWT.log("", 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");
255                                         else
256                                                 GSS.get().displayError("Unable to trash folder:"+((RestException)t).getHttpStatusText());
257                                 }
258                                 else
259                                         GSS.get().displayError("System error trashing folder:"+t.getMessage());
260                         }
261                 };
262                 DeferredCommand.addCommand(tot);
263         }
264
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){
270
271                         @Override
272                         public void onComplete() {
273                                 GSS.get().showFileList(true);
274                         }
275
276                         @Override
277                         public void onError(String p, Throwable t) {
278                                 GWT.log("", 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");
285                                         else
286                                                 GSS.get().displayError("Unable to trash file:"+((RestException)t).getHttpStatusText());
287                                 }
288                                 else
289                                         GSS.get().displayError("System error trashing file:"+t.getMessage());
290                         }
291                 };
292                 DeferredCommand.addCommand(tot);
293         }
294
295
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();
300                         return;
301                 }
302                 PostCommand cf = new PostCommand(paths.get(index), "", 200) {
303
304                         @Override
305                         public void onComplete() {
306                                 executeCopyOrMoveFiles(index + 1, paths);
307                         }
308
309                         @Override
310                         public void onError(Throwable t) {
311                                 GWT.log("", 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");
322                                         else
323                                                 GSS.get().displayError("Unable to copy file:" + ((RestException)t).getHttpStatusText());
324                                 } else
325                                         GSS.get().displayError("System error copying file:" + t.getMessage());
326
327                         }
328                 };
329                 DeferredCommand.addCommand(cf);
330         }
331
332 }