Statistics
| Branch: | Tag: | Revision:

root / web_client / src / gr / grnet / pithos / web / client / DnDFolderPopupMenu.java @ 9e6e0572

History | View | Annotate | Download (15 kB)

1 ab1eb3f8 Christos Stathis
/*
2 6dd67d1c Christos Stathis
 *  Copyright (c) 2011 Greek Research and Technology Network
3 ab1eb3f8 Christos Stathis
 */
4 ab1eb3f8 Christos Stathis
package gr.grnet.pithos.web.client;
5 ab1eb3f8 Christos Stathis
6 ab1eb3f8 Christos Stathis
import gr.grnet.pithos.web.client.rest.MultiplePostCommand;
7 ab1eb3f8 Christos Stathis
import gr.grnet.pithos.web.client.rest.PostCommand;
8 ab1eb3f8 Christos Stathis
import gr.grnet.pithos.web.client.rest.RestException;
9 ab1eb3f8 Christos Stathis
import gr.grnet.pithos.web.client.rest.resource.FileResource;
10 ab1eb3f8 Christos Stathis
import gr.grnet.pithos.web.client.rest.resource.FolderResource;
11 ab1eb3f8 Christos Stathis
import gr.grnet.pithos.web.client.rest.resource.RestResourceWrapper;
12 ab1eb3f8 Christos Stathis
13 ab1eb3f8 Christos Stathis
import java.util.ArrayList;
14 ab1eb3f8 Christos Stathis
import java.util.List;
15 ab1eb3f8 Christos Stathis
16 ab1eb3f8 Christos Stathis
import com.google.gwt.core.client.GWT;
17 ab1eb3f8 Christos Stathis
import com.google.gwt.http.client.URL;
18 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.Command;
19 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.DeferredCommand;
20 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.AbstractImagePrototype;
21 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.MenuBar;
22 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.PopupPanel;
23 ab1eb3f8 Christos Stathis
24 ab1eb3f8 Christos Stathis
public class DnDFolderPopupMenu extends PopupPanel {
25 ab1eb3f8 Christos Stathis
26 ab1eb3f8 Christos Stathis
        public DnDFolderPopupMenu(final CellTreeView.Images newImages, final FolderResource target, final Object toCopy) {
27 ab1eb3f8 Christos Stathis
                // The popup's constructor's argument is a boolean specifying that it
28 ab1eb3f8 Christos Stathis
                // auto-close itself when the user clicks outside of it.
29 ab1eb3f8 Christos Stathis
                super(true);
30 ab1eb3f8 Christos Stathis
                setAnimationEnabled(true);
31 ab1eb3f8 Christos Stathis
                // A dummy command that we will execute from unimplemented leaves.
32 ab1eb3f8 Christos Stathis
                final Command cancelCmd = new Command() {
33 ab1eb3f8 Christos Stathis
34 ab1eb3f8 Christos Stathis
                        @Override
35 ab1eb3f8 Christos Stathis
                        public void execute() {
36 ab1eb3f8 Christos Stathis
                                hide();
37 ab1eb3f8 Christos Stathis
                        }
38 ab1eb3f8 Christos Stathis
                };
39 ab1eb3f8 Christos Stathis
40 ab1eb3f8 Christos Stathis
                final MenuBar contextMenu = new MenuBar(true);
41 ab1eb3f8 Christos Stathis
                final CellTreeView folders = GSS.get().getTreeView();
42 ab1eb3f8 Christos Stathis
43 ab1eb3f8 Christos Stathis
                contextMenu.addItem("<span>" + AbstractImagePrototype.create(newImages.cut()).getHTML() + "&nbsp;Move</span>", true, new Command() {
44 ab1eb3f8 Christos Stathis
45 ab1eb3f8 Christos Stathis
                                @Override
46 ab1eb3f8 Christos Stathis
                                public void execute() {
47 ab1eb3f8 Christos Stathis
                                        if (toCopy instanceof RestResourceWrapper){
48 ab1eb3f8 Christos Stathis
                                                moveFolder(target, ((RestResourceWrapper) toCopy).getResource());
49 ab1eb3f8 Christos Stathis
                                        }
50 ab1eb3f8 Christos Stathis
                                        else if(toCopy instanceof List){
51 ab1eb3f8 Christos Stathis
                                                List<FileResource> files = GSS.get().getFileList().getSelectedFiles();
52 ab1eb3f8 Christos Stathis
                                                moveFiles(target, files);
53 ab1eb3f8 Christos Stathis
                                        }
54 ab1eb3f8 Christos Stathis
                                        hide();
55 ab1eb3f8 Christos Stathis
                                }
56 ab1eb3f8 Christos Stathis
57 ab1eb3f8 Christos Stathis
                        }).setVisible(target != null);
58 ab1eb3f8 Christos Stathis
59 ab1eb3f8 Christos Stathis
                contextMenu.addItem("<span>" + AbstractImagePrototype.create(newImages.copy()).getHTML() + "&nbsp;Copy</span>", true, new Command() {
60 ab1eb3f8 Christos Stathis
61 ab1eb3f8 Christos Stathis
                        @Override
62 ab1eb3f8 Christos Stathis
                        public void execute() {
63 ab1eb3f8 Christos Stathis
                                if (toCopy instanceof RestResourceWrapper)
64 ab1eb3f8 Christos Stathis
                                        copyFolder(target, ((RestResourceWrapper) toCopy).getResource());
65 ab1eb3f8 Christos Stathis
                                else if(toCopy instanceof List){
66 ab1eb3f8 Christos Stathis
                                        List<FileResource> files = GSS.get().getFileList().getSelectedFiles();
67 ab1eb3f8 Christos Stathis
                                    copyFiles(target, files);
68 ab1eb3f8 Christos Stathis
                                }
69 ab1eb3f8 Christos Stathis
                                hide();
70 ab1eb3f8 Christos Stathis
                        }
71 ab1eb3f8 Christos Stathis
72 ab1eb3f8 Christos Stathis
                }).setVisible(target != null);
73 ab1eb3f8 Christos Stathis
74 ab1eb3f8 Christos Stathis
                contextMenu.addItem("<span>" + AbstractImagePrototype.create(newImages.trash()).getHTML() + "&nbsp;Delete (Trash)</span>", true, new Command() {
75 ab1eb3f8 Christos Stathis
76 ab1eb3f8 Christos Stathis
                        @Override
77 ab1eb3f8 Christos Stathis
                        public void execute() {
78 ab1eb3f8 Christos Stathis
                                GWT.log("EXECUTE TRASH:"+toCopy.getClass().getName());
79 ab1eb3f8 Christos Stathis
                                if (toCopy instanceof RestResourceWrapper){
80 ab1eb3f8 Christos Stathis
                                        trashFolder(((RestResourceWrapper) toCopy).getResource());
81 ab1eb3f8 Christos Stathis
                                }
82 ab1eb3f8 Christos Stathis
                                else if(toCopy instanceof List){
83 ab1eb3f8 Christos Stathis
                                        List<FileResource> files = GSS.get().getFileList().getSelectedFiles();
84 ab1eb3f8 Christos Stathis
                                    trashFiles(files);
85 ab1eb3f8 Christos Stathis
                                }
86 ab1eb3f8 Christos Stathis
                                hide();
87 ab1eb3f8 Christos Stathis
                        }
88 ab1eb3f8 Christos Stathis
89 ab1eb3f8 Christos Stathis
                }).setVisible(target == null);
90 ab1eb3f8 Christos Stathis
                contextMenu.addItem("<span>" + AbstractImagePrototype.create(newImages.delete()).getHTML() + "&nbsp;Cancel</span>", true, cancelCmd);
91 ab1eb3f8 Christos Stathis
92 ab1eb3f8 Christos Stathis
                add(contextMenu);
93 ab1eb3f8 Christos Stathis
94 ab1eb3f8 Christos Stathis
        }
95 ab1eb3f8 Christos Stathis
96 ab1eb3f8 Christos Stathis
        private void copyFolder(final FolderResource target, FolderResource toCopy) {
97 ab1eb3f8 Christos Stathis
                String atarget = target.getUri();
98 ab1eb3f8 Christos Stathis
                atarget = atarget.endsWith("/") ? atarget : atarget + '/';
99 ab1eb3f8 Christos Stathis
                atarget = atarget + toCopy.getName();
100 ab1eb3f8 Christos Stathis
                PostCommand cf = new PostCommand(toCopy.getUri() + "?copy=" + atarget, "", 200) {
101 ab1eb3f8 Christos Stathis
102 ab1eb3f8 Christos Stathis
                        @Override
103 ab1eb3f8 Christos Stathis
                        public void onComplete() {
104 ab1eb3f8 Christos Stathis
                                        GSS.get().getTreeView().updateNodeChildren(new RestResourceWrapper(target));
105 ab1eb3f8 Christos Stathis
                                                                GSS.get().getStatusPanel().updateStats();
106 ab1eb3f8 Christos Stathis
                        }
107 ab1eb3f8 Christos Stathis
108 ab1eb3f8 Christos Stathis
                        @Override
109 ab1eb3f8 Christos Stathis
                        public void onError(Throwable t) {
110 ab1eb3f8 Christos Stathis
                                GWT.log("", t);
111 ab1eb3f8 Christos Stathis
                                if (t instanceof RestException) {
112 ab1eb3f8 Christos Stathis
                                        int statusCode = ((RestException) t).getHttpStatusCode();
113 ab1eb3f8 Christos Stathis
                                        if (statusCode == 405)
114 ab1eb3f8 Christos Stathis
                                                GSS.get().displayError("You don't have the necessary permissions");
115 ab1eb3f8 Christos Stathis
116 ab1eb3f8 Christos Stathis
                                        else if (statusCode == 409)
117 ab1eb3f8 Christos Stathis
                                                GSS.get().displayError("A folder with the same name already exists");
118 ab1eb3f8 Christos Stathis
                                        else if (statusCode == 413)
119 ab1eb3f8 Christos Stathis
                                                GSS.get().displayError("Your quota has been exceeded");
120 ab1eb3f8 Christos Stathis
                                        else
121 ab1eb3f8 Christos Stathis
                                                GSS.get().displayError("Unable to copy folder:" + ((RestException)t).getHttpStatusText());
122 ab1eb3f8 Christos Stathis
                                } else
123 ab1eb3f8 Christos Stathis
                                        GSS.get().displayError("System error copying folder:" + t.getMessage());
124 ab1eb3f8 Christos Stathis
                        }
125 ab1eb3f8 Christos Stathis
                };
126 ab1eb3f8 Christos Stathis
                DeferredCommand.addCommand(cf);
127 ab1eb3f8 Christos Stathis
        }
128 ab1eb3f8 Christos Stathis
129 ab1eb3f8 Christos Stathis
        private void moveFolder(final FolderResource target, final FolderResource toCopy) {
130 ab1eb3f8 Christos Stathis
                String atarget = target.getUri();
131 ab1eb3f8 Christos Stathis
                atarget = atarget.endsWith("/") ? atarget : atarget + '/';
132 ab1eb3f8 Christos Stathis
                atarget = atarget + toCopy.getName();
133 ab1eb3f8 Christos Stathis
134 ab1eb3f8 Christos Stathis
                PostCommand cf = new PostCommand(toCopy.getUri() + "?move=" + atarget, "", 200) {
135 ab1eb3f8 Christos Stathis
136 ab1eb3f8 Christos Stathis
                        @Override
137 ab1eb3f8 Christos Stathis
                        public void onComplete() {
138 ab1eb3f8 Christos Stathis
                                GWT.log("[MOVE]"+target.getUri()+"   "+ toCopy.getParentURI());
139 ab1eb3f8 Christos Stathis
                                GSS.get().getTreeView().updateNodeChildren(new RestResourceWrapper(target));
140 ab1eb3f8 Christos Stathis
                                                        GSS.get().getTreeView().updateNodeChildrenForRemove(toCopy.getParentURI());
141 ab1eb3f8 Christos Stathis
                                                        GSS.get().getStatusPanel().updateStats();
142 ab1eb3f8 Christos Stathis
                        }
143 ab1eb3f8 Christos Stathis
144 ab1eb3f8 Christos Stathis
                        @Override
145 ab1eb3f8 Christos Stathis
                        public void onError(Throwable t) {
146 ab1eb3f8 Christos Stathis
                                GWT.log("", t);
147 ab1eb3f8 Christos Stathis
                                if (t instanceof RestException) {
148 ab1eb3f8 Christos Stathis
                                        int statusCode = ((RestException) t).getHttpStatusCode();
149 ab1eb3f8 Christos Stathis
                                        if (statusCode == 405)
150 ab1eb3f8 Christos Stathis
                                                GSS.get().displayError("You don't have the necessary permissions");
151 ab1eb3f8 Christos Stathis
152 ab1eb3f8 Christos Stathis
                                        else if (statusCode == 409)
153 ab1eb3f8 Christos Stathis
                                                GSS.get().displayError("A folder with the same name already exists");
154 ab1eb3f8 Christos Stathis
                                        else if (statusCode == 413)
155 ab1eb3f8 Christos Stathis
                                                GSS.get().displayError("Your quota has been exceeded");
156 ab1eb3f8 Christos Stathis
                                        else
157 ab1eb3f8 Christos Stathis
                                                GSS.get().displayError("Unable to copy folder:" + ((RestException)t).getHttpStatusText());
158 ab1eb3f8 Christos Stathis
                                } else
159 ab1eb3f8 Christos Stathis
                                        GSS.get().displayError("System error copying folder:" + t.getMessage());
160 ab1eb3f8 Christos Stathis
                        }
161 ab1eb3f8 Christos Stathis
                };
162 ab1eb3f8 Christos Stathis
                DeferredCommand.addCommand(cf);
163 ab1eb3f8 Christos Stathis
        }
164 ab1eb3f8 Christos Stathis
165 ab1eb3f8 Christos Stathis
        private void copyFiles(final FolderResource ftarget, List<FileResource> files) {
166 ab1eb3f8 Christos Stathis
                List<String> fileIds = new ArrayList<String>();
167 ab1eb3f8 Christos Stathis
                String target = ftarget.getUri();
168 ab1eb3f8 Christos Stathis
                target = target.endsWith("/") ? target : target + '/';
169 ab1eb3f8 Christos Stathis
                for (FileResource fileResource : files) {
170 ab1eb3f8 Christos Stathis
                        String fileTarget = target + URL.encodeComponent(fileResource.getName());
171 ab1eb3f8 Christos Stathis
                        fileIds.add(fileResource.getUri() + "?copy=" + fileTarget);
172 ab1eb3f8 Christos Stathis
                }
173 ab1eb3f8 Christos Stathis
                int index = 0;
174 ab1eb3f8 Christos Stathis
                executeCopyOrMoveFiles(index, fileIds);
175 ab1eb3f8 Christos Stathis
176 ab1eb3f8 Christos Stathis
        }
177 ab1eb3f8 Christos Stathis
178 ab1eb3f8 Christos Stathis
        private void moveFiles(final FolderResource ftarget, List<FileResource> files) {
179 ab1eb3f8 Christos Stathis
                List<String> fileIds = new ArrayList<String>();
180 ab1eb3f8 Christos Stathis
                String target = ftarget.getUri();
181 ab1eb3f8 Christos Stathis
                target = target.endsWith("/") ? target : target + '/';
182 ab1eb3f8 Christos Stathis
                for (FileResource fileResource : files) {
183 ab1eb3f8 Christos Stathis
                        String fileTarget = target + URL.encodeComponent(fileResource.getName());
184 ab1eb3f8 Christos Stathis
                        fileIds.add(fileResource.getUri() + "?move=" + fileTarget);
185 ab1eb3f8 Christos Stathis
                }
186 ab1eb3f8 Christos Stathis
                int index = 0;
187 ab1eb3f8 Christos Stathis
                executeCopyOrMoveFiles(index, fileIds);
188 ab1eb3f8 Christos Stathis
189 ab1eb3f8 Christos Stathis
        }
190 ab1eb3f8 Christos Stathis
191 ab1eb3f8 Christos Stathis
        private void trashFolder(final FolderResource folder){
192 ab1eb3f8 Christos Stathis
                PostCommand tot = new PostCommand(folder.getUri()+"?trash=","",200){
193 ab1eb3f8 Christos Stathis
194 ab1eb3f8 Christos Stathis
                        @Override
195 ab1eb3f8 Christos Stathis
                        public void onComplete() {
196 ab1eb3f8 Christos Stathis
                                GSS.get().getTreeView().updateNodeChildrenForRemove(folder.getParentURI());
197 ab1eb3f8 Christos Stathis
                                GSS.get().getTreeView().updateTrashNode();
198 ab1eb3f8 Christos Stathis
                                /*for(TreeItem item : items)
199 ab1eb3f8 Christos Stathis
                                        GSS.get().getFolders().updateFolder((DnDTreeItem) item);
200 ab1eb3f8 Christos Stathis
                                GSS.get().getFolders().update(GSS.get().getFolders().getTrashItem());
201 ab1eb3f8 Christos Stathis
                               
202 ab1eb3f8 Christos Stathis
                                GSS.get().showFileList(true);
203 ab1eb3f8 Christos Stathis
                                */
204 ab1eb3f8 Christos Stathis
                        }
205 ab1eb3f8 Christos Stathis
206 ab1eb3f8 Christos Stathis
                        @Override
207 ab1eb3f8 Christos Stathis
                        public void onError(Throwable t) {
208 ab1eb3f8 Christos Stathis
                                GWT.log("", t);
209 ab1eb3f8 Christos Stathis
                                if(t instanceof RestException){
210 ab1eb3f8 Christos Stathis
                                        int statusCode = ((RestException)t).getHttpStatusCode();
211 ab1eb3f8 Christos Stathis
                                        if(statusCode == 405)
212 ab1eb3f8 Christos Stathis
                                                GSS.get().displayError("You don't have the necessary permissions");
213 ab1eb3f8 Christos Stathis
                                        else if(statusCode == 404)
214 ab1eb3f8 Christos Stathis
                                                GSS.get().displayError("Folder does not exist");
215 ab1eb3f8 Christos Stathis
                                        else
216 ab1eb3f8 Christos Stathis
                                                GSS.get().displayError("Unable to trash folder:"+((RestException)t).getHttpStatusText());
217 ab1eb3f8 Christos Stathis
                                }
218 ab1eb3f8 Christos Stathis
                                else
219 ab1eb3f8 Christos Stathis
                                        GSS.get().displayError("System error trashing folder:"+t.getMessage());
220 ab1eb3f8 Christos Stathis
                        }
221 ab1eb3f8 Christos Stathis
                };
222 ab1eb3f8 Christos Stathis
                DeferredCommand.addCommand(tot);
223 ab1eb3f8 Christos Stathis
        }
224 ab1eb3f8 Christos Stathis
225 ab1eb3f8 Christos Stathis
        private void trashFiles(List<FileResource> files){
226 ab1eb3f8 Christos Stathis
                final List<String> fileIds = new ArrayList<String>();
227 ab1eb3f8 Christos Stathis
                for(FileResource f : files)
228 ab1eb3f8 Christos Stathis
                        fileIds.add(f.getUri()+"?trash=");
229 ab1eb3f8 Christos Stathis
                MultiplePostCommand tot = new MultiplePostCommand(fileIds.toArray(new String[0]),200){
230 ab1eb3f8 Christos Stathis
231 ab1eb3f8 Christos Stathis
                        @Override
232 ab1eb3f8 Christos Stathis
                        public void onComplete() {
233 ab1eb3f8 Christos Stathis
                                GSS.get().showFileList(true);
234 ab1eb3f8 Christos Stathis
                        }
235 ab1eb3f8 Christos Stathis
236 ab1eb3f8 Christos Stathis
                        @Override
237 ab1eb3f8 Christos Stathis
                        public void onError(String p, Throwable t) {
238 ab1eb3f8 Christos Stathis
                                GWT.log("", t);
239 ab1eb3f8 Christos Stathis
                                if(t instanceof RestException){
240 ab1eb3f8 Christos Stathis
                                        int statusCode = ((RestException)t).getHttpStatusCode();
241 ab1eb3f8 Christos Stathis
                                        if(statusCode == 405)
242 ab1eb3f8 Christos Stathis
                                                GSS.get().displayError("You don't have the necessary permissions");
243 ab1eb3f8 Christos Stathis
                                        else if(statusCode == 404)
244 ab1eb3f8 Christos Stathis
                                                GSS.get().displayError("File does not exist");
245 ab1eb3f8 Christos Stathis
                                        else
246 ab1eb3f8 Christos Stathis
                                                GSS.get().displayError("Unable to trash file:"+((RestException)t).getHttpStatusText());
247 ab1eb3f8 Christos Stathis
                                }
248 ab1eb3f8 Christos Stathis
                                else
249 ab1eb3f8 Christos Stathis
                                        GSS.get().displayError("System error trashing file:"+t.getMessage());
250 ab1eb3f8 Christos Stathis
                        }
251 ab1eb3f8 Christos Stathis
                };
252 ab1eb3f8 Christos Stathis
                DeferredCommand.addCommand(tot);
253 ab1eb3f8 Christos Stathis
        }
254 ab1eb3f8 Christos Stathis
255 ab1eb3f8 Christos Stathis
256 ab1eb3f8 Christos Stathis
        private void executeCopyOrMoveFiles(final int index, final List<String> paths) {
257 ab1eb3f8 Christos Stathis
                if (index >= paths.size()) {
258 ab1eb3f8 Christos Stathis
                        GSS.get().showFileList(true);
259 ab1eb3f8 Christos Stathis
                        GSS.get().getStatusPanel().updateStats();
260 ab1eb3f8 Christos Stathis
                        return;
261 ab1eb3f8 Christos Stathis
                }
262 ab1eb3f8 Christos Stathis
                PostCommand cf = new PostCommand(paths.get(index), "", 200) {
263 ab1eb3f8 Christos Stathis
264 ab1eb3f8 Christos Stathis
                        @Override
265 ab1eb3f8 Christos Stathis
                        public void onComplete() {
266 ab1eb3f8 Christos Stathis
                                executeCopyOrMoveFiles(index + 1, paths);
267 ab1eb3f8 Christos Stathis
                        }
268 ab1eb3f8 Christos Stathis
269 ab1eb3f8 Christos Stathis
                        @Override
270 ab1eb3f8 Christos Stathis
                        public void onError(Throwable t) {
271 ab1eb3f8 Christos Stathis
                                GWT.log("", t);
272 ab1eb3f8 Christos Stathis
                                if (t instanceof RestException) {
273 ab1eb3f8 Christos Stathis
                                        int statusCode = ((RestException) t).getHttpStatusCode();
274 ab1eb3f8 Christos Stathis
                                        if (statusCode == 405)
275 ab1eb3f8 Christos Stathis
                                                GSS.get().displayError("You don't have the necessary permissions");
276 ab1eb3f8 Christos Stathis
                                        else if (statusCode == 404)
277 ab1eb3f8 Christos Stathis
                                                GSS.get().displayError("File not found");
278 ab1eb3f8 Christos Stathis
                                        else if (statusCode == 409)
279 ab1eb3f8 Christos Stathis
                                                GSS.get().displayError("A file with the same name already exists");
280 ab1eb3f8 Christos Stathis
                                        else if (statusCode == 413)
281 ab1eb3f8 Christos Stathis
                                                GSS.get().displayError("Your quota has been exceeded");
282 ab1eb3f8 Christos Stathis
                                        else
283 ab1eb3f8 Christos Stathis
                                                GSS.get().displayError("Unable to copy file:" + ((RestException)t).getHttpStatusText());
284 ab1eb3f8 Christos Stathis
                                } else
285 ab1eb3f8 Christos Stathis
                                        GSS.get().displayError("System error copying file:" + t.getMessage());
286 ab1eb3f8 Christos Stathis
287 ab1eb3f8 Christos Stathis
                        }
288 ab1eb3f8 Christos Stathis
                };
289 ab1eb3f8 Christos Stathis
                DeferredCommand.addCommand(cf);
290 ab1eb3f8 Christos Stathis
        }
291 ab1eb3f8 Christos Stathis
292 ab1eb3f8 Christos Stathis
}