clear selection after trash or delete
[pithos] / src / gr / ebs / gss / client / DnDFolderPopupMenu.java
1 /*
2  * Copyright 2011 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;
20 /*
21  * Copyright 2008, 2009 Electronic Business Systems Ltd.
22  *
23  * This file is part of GSS.
24  *
25  * GSS is free software: you can redistribute it and/or modify
26  * it under the terms of the GNU General Public License as published by
27  * the Free Software Foundation, either version 3 of the License, or
28  * (at your option) any later version.
29  *
30  * GSS is distributed in the hope that it will be useful,
31  * but WITHOUT ANY WARRANTY; without even the implied warranty of
32  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33  * GNU General Public License for more details.
34  *
35  * You should have received a copy of the GNU General Public License
36  * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
37  */
38
39 import gr.ebs.gss.client.CellTreeView;
40 import gr.ebs.gss.client.GSS;
41 import gr.ebs.gss.client.rest.MultiplePostCommand;
42 import gr.ebs.gss.client.rest.PostCommand;
43 import gr.ebs.gss.client.rest.RestException;
44 import gr.ebs.gss.client.rest.resource.FileResource;
45 import gr.ebs.gss.client.rest.resource.FolderResource;
46 import gr.ebs.gss.client.rest.resource.RestResource;
47
48 import java.util.ArrayList;
49 import java.util.List;
50
51 import com.google.gwt.core.client.GWT;
52 import com.google.gwt.http.client.URL;
53 import com.google.gwt.user.client.Command;
54 import com.google.gwt.user.client.DeferredCommand;
55 import com.google.gwt.user.client.ui.AbstractImagePrototype;
56 import com.google.gwt.user.client.ui.MenuBar;
57 import com.google.gwt.user.client.ui.PopupPanel;
58 import com.google.gwt.user.client.ui.TreeItem;
59
60 /**
61  * @author kman
62  */
63 public class DnDFolderPopupMenu extends PopupPanel {
64
65         public DnDFolderPopupMenu(final CellTreeView.Images newImages, final FolderResource target, final Object toCopy) {
66                 // The popup's constructor's argument is a boolean specifying that it
67                 // auto-close itself when the user clicks outside of it.
68                 super(true);
69                 setAnimationEnabled(true);
70                 // A dummy command that we will execute from unimplemented leaves.
71                 final Command cancelCmd = new Command() {
72
73                         @Override
74                         public void execute() {
75                                 hide();
76                         }
77                 };
78
79                 final MenuBar contextMenu = new MenuBar(true);
80                 final CellTreeView folders = GSS.get().getTreeView();
81
82                 contextMenu.addItem("<span>" + AbstractImagePrototype.create(newImages.cut()).getHTML() + "&nbsp;Move</span>", true, new Command() {
83
84                                 @Override
85                                 public void execute() {
86                                         if (toCopy instanceof FolderResource){
87                                                 /*List<TreeItem> treeItems = folders.getItemsOfTreeForPath(((RestResource) toCopy).getUri());
88                                                 List<TreeItem> parents = new ArrayList();
89                                                 for(TreeItem item : treeItems)
90                                                         if(item.getParentItem() != null)
91                                                                 parents.add(item.getParentItem());*/
92                                                 moveFolder(target, (FolderResource) toCopy);
93                                         }
94                                         else if(toCopy instanceof List)
95                                                 moveFiles(target, (List<FileResource>) toCopy);
96                                         hide();
97                                 }
98
99                         }).setVisible(target != null);
100
101                 contextMenu.addItem("<span>" + AbstractImagePrototype.create(newImages.copy()).getHTML() + "&nbsp;Copy</span>", true, new Command() {
102
103                         @Override
104                         public void execute() {
105                                 if (toCopy instanceof FolderResource)
106                                         copyFolder(target, (FolderResource) toCopy);
107                                 else if(toCopy instanceof List)
108                                         copyFiles(target, (List<FileResource>) toCopy);
109                                 hide();
110                         }
111
112                 }).setVisible(target != null);
113
114                 contextMenu.addItem("<span>" + AbstractImagePrototype.create(newImages.trash()).getHTML() + "&nbsp;Delete (Trash)</span>", true, new Command() {
115
116                         @Override
117                         public void execute() {
118                                 if (toCopy instanceof FolderResource){
119                                         /*final List<TreeItem> treeItems = folders.getItemsOfTreeForPath(((RestResource) toCopy).getUri());
120                                         List<TreeItem> parents = new ArrayList();
121                                         for(TreeItem item : treeItems)
122                                                 if(item.getParentItem() != null)
123                                                         parents.add(item.getParentItem());
124                                         */
125                                         trashFolder((FolderResource) toCopy);
126                                 }
127                                 else if(toCopy instanceof List)
128                                         trashFiles((List<FileResource>) toCopy);
129                                 hide();
130                         }
131
132                 }).setVisible(target == null);
133                 contextMenu.addItem("<span>" + AbstractImagePrototype.create(newImages.delete()).getHTML() + "&nbsp;Cancel</span>", true, cancelCmd);
134
135                 add(contextMenu);
136
137         }
138
139         private void copyFolder(final FolderResource target, FolderResource toCopy) {
140                 String atarget = target.getUri();
141                 atarget = atarget.endsWith("/") ? atarget : atarget + '/';
142                 atarget = atarget + toCopy.getName();
143                 PostCommand cf = new PostCommand(toCopy.getUri() + "?copy=" + atarget, "", 200) {
144
145                         @Override
146                         public void onComplete() {
147                                 /*final TreeItem folder;
148                                 TreeItem folderTemp = GSS.get().getFolders().getUserItem(target);
149                                 if (folderTemp == null)
150                                         folder = GSS.get().getFolders().getOtherSharedItem(target);
151                                 else
152                                         folder = folderTemp;
153                                 GSS.get().getFolders().updateFolder((DnDTreeItem) folder);
154                                 GSS.get().getStatusPanel().updateStats();*/
155                         }
156
157                         @Override
158                         public void onError(Throwable t) {
159                                 GWT.log("", t);
160                                 if (t instanceof RestException) {
161                                         int statusCode = ((RestException) t).getHttpStatusCode();
162                                         if (statusCode == 405)
163                                                 GSS.get().displayError("You don't have the necessary permissions");
164
165                                         else if (statusCode == 409)
166                                                 GSS.get().displayError("A folder with the same name already exists");
167                                         else if (statusCode == 413)
168                                                 GSS.get().displayError("Your quota has been exceeded");
169                                         else
170                                                 GSS.get().displayError("Unable to copy folder:" + ((RestException)t).getHttpStatusText());
171                                 } else
172                                         GSS.get().displayError("System error copying folder:" + t.getMessage());
173                         }
174                 };
175                 DeferredCommand.addCommand(cf);
176         }
177
178         private void moveFolder(final FolderResource target, final FolderResource toCopy) {
179                 String atarget = target.getUri();
180                 atarget = atarget.endsWith("/") ? atarget : atarget + '/';
181                 atarget = atarget + toCopy.getName();
182
183                 PostCommand cf = new PostCommand(toCopy.getUri() + "?move=" + atarget, "", 200) {
184
185                         @Override
186                         public void onComplete() {
187                                 /*final TreeItem folder;
188                                 for(TreeItem i : items){
189                                         DnDTreeItem id = (DnDTreeItem)i;
190                                         if(id.getChild(toCopy) != null)
191                                                 id.removeItem(id.getChild(toCopy));
192                                 }
193                                 GSS.get().getFolders().clearSelection();
194                                 TreeItem folderTemp = GSS.get().getFolders().getUserItem(target);
195                                 if (folderTemp == null)
196                                         folder = GSS.get().getFolders().getOtherSharedItem(target);
197                                 else
198                                         folder = folderTemp;
199                                 GSS.get().getFolders().updateFolder((DnDTreeItem) folder);
200                                 GSS.get().showFileList(true);
201                                 GSS.get().getStatusPanel().updateStats();*/
202                         }
203
204                         @Override
205                         public void onError(Throwable t) {
206                                 GWT.log("", t);
207                                 if (t instanceof RestException) {
208                                         int statusCode = ((RestException) t).getHttpStatusCode();
209                                         if (statusCode == 405)
210                                                 GSS.get().displayError("You don't have the necessary permissions");
211
212                                         else if (statusCode == 409)
213                                                 GSS.get().displayError("A folder with the same name already exists");
214                                         else if (statusCode == 413)
215                                                 GSS.get().displayError("Your quota has been exceeded");
216                                         else
217                                                 GSS.get().displayError("Unable to copy folder:" + ((RestException)t).getHttpStatusText());
218                                 } else
219                                         GSS.get().displayError("System error copying folder:" + t.getMessage());
220                         }
221                 };
222                 DeferredCommand.addCommand(cf);
223         }
224
225         private void copyFiles(final FolderResource ftarget, List<FileResource> files) {
226                 List<String> fileIds = new ArrayList<String>();
227                 String target = ftarget.getUri();
228                 target = target.endsWith("/") ? target : target + '/';
229                 for (FileResource fileResource : files) {
230                         String fileTarget = target + URL.encodeComponent(fileResource.getName());
231                         fileIds.add(fileResource.getUri() + "?copy=" + fileTarget);
232                 }
233                 int index = 0;
234                 executeCopyOrMoveFiles(index, fileIds);
235
236         }
237
238         private void moveFiles(final FolderResource ftarget, List<FileResource> files) {
239                 List<String> fileIds = new ArrayList<String>();
240                 String target = ftarget.getUri();
241                 target = target.endsWith("/") ? target : target + '/';
242                 for (FileResource fileResource : files) {
243                         String fileTarget = target + URL.encodeComponent(fileResource.getName());
244                         fileIds.add(fileResource.getUri() + "?move=" + fileTarget);
245                 }
246                 int index = 0;
247                 executeCopyOrMoveFiles(index, fileIds);
248
249         }
250
251         private void trashFolder(final FolderResource folder){
252                 PostCommand tot = new PostCommand(folder.getUri()+"?trash=","",200){
253
254                         @Override
255                         public void onComplete() {
256                                 /*for(TreeItem item : items)
257                                         GSS.get().getFolders().updateFolder((DnDTreeItem) item);
258                                 GSS.get().getFolders().update(GSS.get().getFolders().getTrashItem());
259                                
260                                 GSS.get().showFileList(true);
261                                 */
262                         }
263
264                         @Override
265                         public void onError(Throwable t) {
266                                 GWT.log("", t);
267                                 if(t instanceof RestException){
268                                         int statusCode = ((RestException)t).getHttpStatusCode();
269                                         if(statusCode == 405)
270                                                 GSS.get().displayError("You don't have the necessary permissions");
271                                         else if(statusCode == 404)
272                                                 GSS.get().displayError("Folder does not exist");
273                                         else
274                                                 GSS.get().displayError("Unable to trash folder:"+((RestException)t).getHttpStatusText());
275                                 }
276                                 else
277                                         GSS.get().displayError("System error trashing folder:"+t.getMessage());
278                         }
279                 };
280                 DeferredCommand.addCommand(tot);
281         }
282
283         private void trashFiles(List<FileResource> files){
284                 final List<String> fileIds = new ArrayList<String>();
285                 for(FileResource f : files)
286                         fileIds.add(f.getUri()+"?trash=");
287                 MultiplePostCommand tot = new MultiplePostCommand(fileIds.toArray(new String[0]),200){
288
289                         @Override
290                         public void onComplete() {
291                                 GSS.get().showFileList(true);
292                         }
293
294                         @Override
295                         public void onError(String p, Throwable t) {
296                                 GWT.log("", t);
297                                 if(t instanceof RestException){
298                                         int statusCode = ((RestException)t).getHttpStatusCode();
299                                         if(statusCode == 405)
300                                                 GSS.get().displayError("You don't have the necessary permissions");
301                                         else if(statusCode == 404)
302                                                 GSS.get().displayError("File does not exist");
303                                         else
304                                                 GSS.get().displayError("Unable to trash file:"+((RestException)t).getHttpStatusText());
305                                 }
306                                 else
307                                         GSS.get().displayError("System error trashing file:"+t.getMessage());
308                         }
309                 };
310                 DeferredCommand.addCommand(tot);
311         }
312
313
314         private void executeCopyOrMoveFiles(final int index, final List<String> paths) {
315                 if (index >= paths.size()) {
316                         GSS.get().showFileList(true);
317                         GSS.get().getStatusPanel().updateStats();
318                         return;
319                 }
320                 PostCommand cf = new PostCommand(paths.get(index), "", 200) {
321
322                         @Override
323                         public void onComplete() {
324                                 executeCopyOrMoveFiles(index + 1, paths);
325                         }
326
327                         @Override
328                         public void onError(Throwable t) {
329                                 GWT.log("", t);
330                                 if (t instanceof RestException) {
331                                         int statusCode = ((RestException) t).getHttpStatusCode();
332                                         if (statusCode == 405)
333                                                 GSS.get().displayError("You don't have the necessary permissions");
334                                         else if (statusCode == 404)
335                                                 GSS.get().displayError("File not found");
336                                         else if (statusCode == 409)
337                                                 GSS.get().displayError("A file with the same name already exists");
338                                         else if (statusCode == 413)
339                                                 GSS.get().displayError("Your quota has been exceeded");
340                                         else
341                                                 GSS.get().displayError("Unable to copy file:" + ((RestException)t).getHttpStatusText());
342                                 } else
343                                         GSS.get().displayError("System error copying file:" + t.getMessage());
344
345                         }
346                 };
347                 DeferredCommand.addCommand(cf);
348         }
349
350 }