2 * Copyright (c) 2011 Greek Research and Technology Network
4 package gr.grnet.pithos.web.client;
6 import gr.grnet.pithos.web.client.rest.MultiplePostCommand;
7 import gr.grnet.pithos.web.client.rest.PostCommand;
8 import gr.grnet.pithos.web.client.rest.RestException;
9 import gr.grnet.pithos.web.client.rest.resource.FileResource;
10 import gr.grnet.pithos.web.client.rest.resource.FolderResource;
11 import gr.grnet.pithos.web.client.rest.resource.RestResourceWrapper;
13 import java.util.ArrayList;
14 import java.util.List;
16 import com.google.gwt.core.client.GWT;
17 import com.google.gwt.http.client.URL;
18 import com.google.gwt.user.client.Command;
19 import com.google.gwt.user.client.DeferredCommand;
20 import com.google.gwt.user.client.ui.AbstractImagePrototype;
21 import com.google.gwt.user.client.ui.MenuBar;
22 import com.google.gwt.user.client.ui.PopupPanel;
24 public class DnDFolderPopupMenu extends PopupPanel {
26 public DnDFolderPopupMenu(final CellTreeView.Images newImages, final FolderResource target, final Object toCopy) {
27 // The popup's constructor's argument is a boolean specifying that it
28 // auto-close itself when the user clicks outside of it.
30 setAnimationEnabled(true);
31 // A dummy command that we will execute from unimplemented leaves.
32 final Command cancelCmd = new Command() {
35 public void execute() {
40 final MenuBar contextMenu = new MenuBar(true);
41 final CellTreeView folders = GSS.get().getTreeView();
43 contextMenu.addItem("<span>" + AbstractImagePrototype.create(newImages.cut()).getHTML() + " Move</span>", true, new Command() {
46 public void execute() {
47 if (toCopy instanceof RestResourceWrapper){
48 moveFolder(target, ((RestResourceWrapper) toCopy).getResource());
50 else if(toCopy instanceof List){
51 List<FileResource> files = GSS.get().getFileList().getSelectedFiles();
52 moveFiles(target, files);
57 }).setVisible(target != null);
59 contextMenu.addItem("<span>" + AbstractImagePrototype.create(newImages.copy()).getHTML() + " Copy</span>", true, new Command() {
62 public void execute() {
63 if (toCopy instanceof RestResourceWrapper)
64 copyFolder(target, ((RestResourceWrapper) toCopy).getResource());
65 else if(toCopy instanceof List){
66 List<FileResource> files = GSS.get().getFileList().getSelectedFiles();
67 copyFiles(target, files);
72 }).setVisible(target != null);
74 contextMenu.addItem("<span>" + AbstractImagePrototype.create(newImages.trash()).getHTML() + " Delete (Trash)</span>", true, new Command() {
77 public void execute() {
78 GWT.log("EXECUTE TRASH:"+toCopy.getClass().getName());
79 if (toCopy instanceof RestResourceWrapper){
80 trashFolder(((RestResourceWrapper) toCopy).getResource());
82 else if(toCopy instanceof List){
83 List<FileResource> files = GSS.get().getFileList().getSelectedFiles();
89 }).setVisible(target == null);
90 contextMenu.addItem("<span>" + AbstractImagePrototype.create(newImages.delete()).getHTML() + " Cancel</span>", true, cancelCmd);
96 private void copyFolder(final FolderResource target, FolderResource toCopy) {
97 String atarget = target.getUri();
98 atarget = atarget.endsWith("/") ? atarget : atarget + '/';
99 atarget = atarget + toCopy.getName();
100 PostCommand cf = new PostCommand(toCopy.getUri() + "?copy=" + atarget, "", 200) {
103 public void onComplete() {
104 GSS.get().getTreeView().updateNodeChildren(new RestResourceWrapper(target));
105 GSS.get().getStatusPanel().updateStats();
109 public void onError(Throwable t) {
111 if (t instanceof RestException) {
112 int statusCode = ((RestException) t).getHttpStatusCode();
113 if (statusCode == 405)
114 GSS.get().displayError("You don't have the necessary permissions");
116 else if (statusCode == 409)
117 GSS.get().displayError("A folder with the same name already exists");
118 else if (statusCode == 413)
119 GSS.get().displayError("Your quota has been exceeded");
121 GSS.get().displayError("Unable to copy folder:" + ((RestException)t).getHttpStatusText());
123 GSS.get().displayError("System error copying folder:" + t.getMessage());
126 DeferredCommand.addCommand(cf);
129 private void moveFolder(final FolderResource target, final FolderResource toCopy) {
130 String atarget = target.getUri();
131 atarget = atarget.endsWith("/") ? atarget : atarget + '/';
132 atarget = atarget + toCopy.getName();
134 PostCommand cf = new PostCommand(toCopy.getUri() + "?move=" + atarget, "", 200) {
137 public void onComplete() {
138 GWT.log("[MOVE]"+target.getUri()+" "+ toCopy.getParentURI());
139 GSS.get().getTreeView().updateNodeChildren(new RestResourceWrapper(target));
140 GSS.get().getTreeView().updateNodeChildrenForRemove(toCopy.getParentURI());
141 GSS.get().getStatusPanel().updateStats();
145 public void onError(Throwable t) {
147 if (t instanceof RestException) {
148 int statusCode = ((RestException) t).getHttpStatusCode();
149 if (statusCode == 405)
150 GSS.get().displayError("You don't have the necessary permissions");
152 else if (statusCode == 409)
153 GSS.get().displayError("A folder with the same name already exists");
154 else if (statusCode == 413)
155 GSS.get().displayError("Your quota has been exceeded");
157 GSS.get().displayError("Unable to copy folder:" + ((RestException)t).getHttpStatusText());
159 GSS.get().displayError("System error copying folder:" + t.getMessage());
162 DeferredCommand.addCommand(cf);
165 private void copyFiles(final FolderResource ftarget, List<FileResource> files) {
166 List<String> fileIds = new ArrayList<String>();
167 String target = ftarget.getUri();
168 target = target.endsWith("/") ? target : target + '/';
169 for (FileResource fileResource : files) {
170 String fileTarget = target + URL.encodeComponent(fileResource.getName());
171 fileIds.add(fileResource.getUri() + "?copy=" + fileTarget);
174 executeCopyOrMoveFiles(index, fileIds);
178 private void moveFiles(final FolderResource ftarget, List<FileResource> files) {
179 List<String> fileIds = new ArrayList<String>();
180 String target = ftarget.getUri();
181 target = target.endsWith("/") ? target : target + '/';
182 for (FileResource fileResource : files) {
183 String fileTarget = target + URL.encodeComponent(fileResource.getName());
184 fileIds.add(fileResource.getUri() + "?move=" + fileTarget);
187 executeCopyOrMoveFiles(index, fileIds);
191 private void trashFolder(final FolderResource folder){
192 PostCommand tot = new PostCommand(folder.getUri()+"?trash=","",200){
195 public void onComplete() {
196 GSS.get().getTreeView().updateNodeChildrenForRemove(folder.getParentURI());
197 GSS.get().getTreeView().updateTrashNode();
198 /*for(TreeItem item : items)
199 GSS.get().getFolders().updateFolder((DnDTreeItem) item);
200 GSS.get().getFolders().update(GSS.get().getFolders().getTrashItem());
202 GSS.get().showFileList(true);
207 public void onError(Throwable t) {
209 if(t instanceof RestException){
210 int statusCode = ((RestException)t).getHttpStatusCode();
211 if(statusCode == 405)
212 GSS.get().displayError("You don't have the necessary permissions");
213 else if(statusCode == 404)
214 GSS.get().displayError("Folder does not exist");
216 GSS.get().displayError("Unable to trash folder:"+((RestException)t).getHttpStatusText());
219 GSS.get().displayError("System error trashing folder:"+t.getMessage());
222 DeferredCommand.addCommand(tot);
225 private void trashFiles(List<FileResource> files){
226 final List<String> fileIds = new ArrayList<String>();
227 for(FileResource f : files)
228 fileIds.add(f.getUri()+"?trash=");
229 MultiplePostCommand tot = new MultiplePostCommand(fileIds.toArray(new String[0]),200){
232 public void onComplete() {
233 GSS.get().showFileList(true);
237 public void onError(String p, Throwable t) {
239 if(t instanceof RestException){
240 int statusCode = ((RestException)t).getHttpStatusCode();
241 if(statusCode == 405)
242 GSS.get().displayError("You don't have the necessary permissions");
243 else if(statusCode == 404)
244 GSS.get().displayError("File does not exist");
246 GSS.get().displayError("Unable to trash file:"+((RestException)t).getHttpStatusText());
249 GSS.get().displayError("System error trashing file:"+t.getMessage());
252 DeferredCommand.addCommand(tot);
256 private void executeCopyOrMoveFiles(final int index, final List<String> paths) {
257 if (index >= paths.size()) {
258 GSS.get().showFileList(true);
259 GSS.get().getStatusPanel().updateStats();
262 PostCommand cf = new PostCommand(paths.get(index), "", 200) {
265 public void onComplete() {
266 executeCopyOrMoveFiles(index + 1, paths);
270 public void onError(Throwable t) {
272 if (t instanceof RestException) {
273 int statusCode = ((RestException) t).getHttpStatusCode();
274 if (statusCode == 405)
275 GSS.get().displayError("You don't have the necessary permissions");
276 else if (statusCode == 404)
277 GSS.get().displayError("File not found");
278 else if (statusCode == 409)
279 GSS.get().displayError("A file with the same name already exists");
280 else if (statusCode == 413)
281 GSS.get().displayError("Your quota has been exceeded");
283 GSS.get().displayError("Unable to copy file:" + ((RestException)t).getHttpStatusText());
285 GSS.get().displayError("System error copying file:" + t.getMessage());
289 DeferredCommand.addCommand(cf);