Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / DnDFolderPopupMenu.java @ fad48f0d

History | View | Annotate | Download (13.6 kB)

1 a57faaf0 Christos Stathis
/*
2 63366925 Christos Stathis
 * Copyright 2011 GRNET S.A. All rights reserved.
3 63366925 Christos Stathis
 *
4 63366925 Christos Stathis
 * Redistribution and use in source and binary forms, with or
5 63366925 Christos Stathis
 * without modification, are permitted provided that the following
6 63366925 Christos Stathis
 * conditions are met:
7 63366925 Christos Stathis
 *
8 63366925 Christos Stathis
 *   1. Redistributions of source code must retain the above
9 63366925 Christos Stathis
 *      copyright notice, this list of conditions and the following
10 63366925 Christos Stathis
 *      disclaimer.
11 63366925 Christos Stathis
 *
12 63366925 Christos Stathis
 *   2. Redistributions in binary form must reproduce the above
13 63366925 Christos Stathis
 *      copyright notice, this list of conditions and the following
14 63366925 Christos Stathis
 *      disclaimer in the documentation and/or other materials
15 63366925 Christos Stathis
 *      provided with the distribution.
16 63366925 Christos Stathis
 *
17 63366925 Christos Stathis
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 63366925 Christos Stathis
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 63366925 Christos Stathis
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 63366925 Christos Stathis
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 63366925 Christos Stathis
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 63366925 Christos Stathis
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 63366925 Christos Stathis
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 63366925 Christos Stathis
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 63366925 Christos Stathis
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 63366925 Christos Stathis
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 63366925 Christos Stathis
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 63366925 Christos Stathis
 * POSSIBILITY OF SUCH DAMAGE.
29 63366925 Christos Stathis
 *
30 63366925 Christos Stathis
 * The views and conclusions contained in the software and
31 63366925 Christos Stathis
 * documentation are those of the authors and should not be
32 63366925 Christos Stathis
 * interpreted as representing official policies, either expressed
33 63366925 Christos Stathis
 * or implied, of GRNET S.A.
34 a57faaf0 Christos Stathis
 */
35 a57faaf0 Christos Stathis
package gr.grnet.pithos.web.client;
36 a57faaf0 Christos Stathis
37 875a0179 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.File;
38 875a0179 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.Folder;
39 a57faaf0 Christos Stathis
import gr.grnet.pithos.web.client.rest.MultiplePostCommand;
40 a57faaf0 Christos Stathis
import gr.grnet.pithos.web.client.rest.PostCommand;
41 a57faaf0 Christos Stathis
import gr.grnet.pithos.web.client.rest.RestException;
42 a57faaf0 Christos Stathis
import gr.grnet.pithos.web.client.rest.resource.FileResource;
43 a57faaf0 Christos Stathis
import gr.grnet.pithos.web.client.rest.resource.FolderResource;
44 a57faaf0 Christos Stathis
import gr.grnet.pithos.web.client.rest.resource.RestResourceWrapper;
45 a57faaf0 Christos Stathis
46 a57faaf0 Christos Stathis
import java.util.ArrayList;
47 a57faaf0 Christos Stathis
import java.util.List;
48 a57faaf0 Christos Stathis
49 a57faaf0 Christos Stathis
import com.google.gwt.core.client.GWT;
50 a57faaf0 Christos Stathis
import com.google.gwt.http.client.URL;
51 a57faaf0 Christos Stathis
import com.google.gwt.user.client.Command;
52 a57faaf0 Christos Stathis
import com.google.gwt.user.client.DeferredCommand;
53 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.AbstractImagePrototype;
54 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.MenuBar;
55 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.PopupPanel;
56 a57faaf0 Christos Stathis
57 a57faaf0 Christos Stathis
public class DnDFolderPopupMenu extends PopupPanel {
58 a57faaf0 Christos Stathis
59 875a0179 Christos Stathis
    public DnDFolderPopupMenu(final CellTreeView.Images newImages, final Folder target, final Object toCopy) {
60 875a0179 Christos Stathis
        // The popup's constructor's argument is a boolean specifying that it
61 875a0179 Christos Stathis
        // auto-close itself when the user clicks outside of it.
62 875a0179 Christos Stathis
        super(true);
63 875a0179 Christos Stathis
        setAnimationEnabled(true);
64 875a0179 Christos Stathis
        // A dummy command that we will execute from unimplemented leaves.
65 875a0179 Christos Stathis
        final Command cancelCmd = new Command() {
66 875a0179 Christos Stathis
67 875a0179 Christos Stathis
            @Override
68 875a0179 Christos Stathis
            public void execute() {
69 875a0179 Christos Stathis
                hide();
70 875a0179 Christos Stathis
            }
71 875a0179 Christos Stathis
        };
72 875a0179 Christos Stathis
73 875a0179 Christos Stathis
        final MenuBar contextMenu = new MenuBar(true);
74 875a0179 Christos Stathis
        final CellTreeView folders = GSS.get().getTreeView();
75 875a0179 Christos Stathis
76 875a0179 Christos Stathis
        contextMenu.addItem("<span>" + AbstractImagePrototype.create(newImages.cut()).getHTML() + "&nbsp;Move</span>", true, new Command() {
77 875a0179 Christos Stathis
78 875a0179 Christos Stathis
            @Override
79 875a0179 Christos Stathis
            public void execute() {
80 875a0179 Christos Stathis
                if (toCopy instanceof Folder) {
81 875a0179 Christos Stathis
                    moveFolder(target, (Folder) toCopy);
82 a57faaf0 Christos Stathis
                }
83 875a0179 Christos Stathis
                else if (toCopy instanceof List) {
84 875a0179 Christos Stathis
                    List<File> files = GSS.get().getFileList().getSelectedFiles();
85 875a0179 Christos Stathis
                    moveFiles(target, files);
86 a57faaf0 Christos Stathis
                }
87 875a0179 Christos Stathis
                hide();
88 875a0179 Christos Stathis
            }
89 875a0179 Christos Stathis
        }).setVisible(target != null);
90 875a0179 Christos Stathis
91 875a0179 Christos Stathis
        contextMenu.addItem("<span>" + AbstractImagePrototype.create(newImages.copy()).getHTML() + "&nbsp;Copy</span>", true, new Command() {
92 875a0179 Christos Stathis
93 875a0179 Christos Stathis
            @Override
94 875a0179 Christos Stathis
            public void execute() {
95 875a0179 Christos Stathis
                if (toCopy instanceof Folder)
96 875a0179 Christos Stathis
                    copyFolder(target, (Folder) toCopy);
97 875a0179 Christos Stathis
                else if (toCopy instanceof List) {
98 875a0179 Christos Stathis
                    List<File> files = GSS.get().getFileList().getSelectedFiles();
99 875a0179 Christos Stathis
                    copyFiles(target, files);
100 a57faaf0 Christos Stathis
                }
101 875a0179 Christos Stathis
                hide();
102 875a0179 Christos Stathis
            }
103 875a0179 Christos Stathis
        }).setVisible(target != null);
104 a57faaf0 Christos Stathis
105 875a0179 Christos Stathis
        contextMenu.addItem("<span>" + AbstractImagePrototype.create(newImages.trash()).getHTML() + "&nbsp;Delete (Trash)</span>", true, new Command() {
106 a57faaf0 Christos Stathis
107 875a0179 Christos Stathis
            @Override
108 875a0179 Christos Stathis
            public void execute() {
109 875a0179 Christos Stathis
                GWT.log("EXECUTE TRASH:" + toCopy.getClass().getName());
110 875a0179 Christos Stathis
                if (toCopy instanceof RestResourceWrapper) {
111 875a0179 Christos Stathis
                    trashFolder(((RestResourceWrapper) toCopy).getResource());
112 875a0179 Christos Stathis
                }
113 875a0179 Christos Stathis
                else if (toCopy instanceof List) {
114 875a0179 Christos Stathis
                    List<File> files = GSS.get().getFileList().getSelectedFiles();
115 875a0179 Christos Stathis
                    trashFiles(files);
116 875a0179 Christos Stathis
                }
117 875a0179 Christos Stathis
                hide();
118 875a0179 Christos Stathis
            }
119 875a0179 Christos Stathis
        }).setVisible(target == null);
120 875a0179 Christos Stathis
        contextMenu.addItem("<span>" + AbstractImagePrototype.create(newImages.delete()).getHTML() + "&nbsp;Cancel</span>", true, cancelCmd);
121 875a0179 Christos Stathis
122 875a0179 Christos Stathis
        add(contextMenu);
123 875a0179 Christos Stathis
    }
124 875a0179 Christos Stathis
125 875a0179 Christos Stathis
    private void copyFolder(final Folder target, Folder toCopy) {
126 875a0179 Christos Stathis
//        String atarget = target.getUri();
127 875a0179 Christos Stathis
//        atarget = atarget.endsWith("/") ? atarget : atarget + '/';
128 875a0179 Christos Stathis
//        atarget = atarget + toCopy.getName();
129 875a0179 Christos Stathis
//        PostCommand cf = new PostCommand(toCopy.getUri() + "?copy=" + atarget, "", 200) {
130 875a0179 Christos Stathis
//
131 875a0179 Christos Stathis
//            @Override
132 875a0179 Christos Stathis
//            public void onComplete() {
133 875a0179 Christos Stathis
//                GSS.get().getTreeView().updateNodeChildren(new RestResourceWrapper(target));
134 875a0179 Christos Stathis
//                GSS.get().getStatusPanel().updateStats();
135 875a0179 Christos Stathis
//            }
136 875a0179 Christos Stathis
//
137 875a0179 Christos Stathis
//            @Override
138 875a0179 Christos Stathis
//            public void onError(Throwable t) {
139 875a0179 Christos Stathis
//                GWT.log("", t);
140 875a0179 Christos Stathis
//                if (t instanceof RestException) {
141 875a0179 Christos Stathis
//                    int statusCode = ((RestException) t).getHttpStatusCode();
142 875a0179 Christos Stathis
//                    if (statusCode == 405)
143 875a0179 Christos Stathis
//                        GSS.get().displayError("You don't have the necessary permissions");
144 875a0179 Christos Stathis
//
145 875a0179 Christos Stathis
//                    else if (statusCode == 409)
146 875a0179 Christos Stathis
//                        GSS.get().displayError("A folder with the same name already exists");
147 875a0179 Christos Stathis
//                    else if (statusCode == 413)
148 875a0179 Christos Stathis
//                        GSS.get().displayError("Your quota has been exceeded");
149 875a0179 Christos Stathis
//                    else
150 875a0179 Christos Stathis
//                        GSS.get().displayError("Unable to copy folder:" + ((RestException) t).getHttpStatusText());
151 875a0179 Christos Stathis
//                }
152 875a0179 Christos Stathis
//                else
153 875a0179 Christos Stathis
//                    GSS.get().displayError("System error copying folder:" + t.getMessage());
154 875a0179 Christos Stathis
//            }
155 875a0179 Christos Stathis
//        };
156 875a0179 Christos Stathis
//        DeferredCommand.addCommand(cf);
157 875a0179 Christos Stathis
    }
158 875a0179 Christos Stathis
159 875a0179 Christos Stathis
    private void moveFolder(final Folder target, final Folder toCopy) {
160 875a0179 Christos Stathis
//        String atarget = target.getUri();
161 875a0179 Christos Stathis
//        atarget = atarget.endsWith("/") ? atarget : atarget + '/';
162 875a0179 Christos Stathis
//        atarget = atarget + toCopy.getName();
163 875a0179 Christos Stathis
//
164 875a0179 Christos Stathis
//        PostCommand cf = new PostCommand(toCopy.getUri() + "?move=" + atarget, "", 200) {
165 875a0179 Christos Stathis
//
166 875a0179 Christos Stathis
//            @Override
167 875a0179 Christos Stathis
//            public void onComplete() {
168 875a0179 Christos Stathis
//                GWT.log("[MOVE]" + target.getUri() + "   " + toCopy.getParentURI());
169 875a0179 Christos Stathis
//                GSS.get().getTreeView().updateNodeChildren(new RestResourceWrapper(target));
170 875a0179 Christos Stathis
//                GSS.get().getTreeView().updateNodeChildrenForRemove(toCopy.getParentURI());
171 875a0179 Christos Stathis
//                GSS.get().getStatusPanel().updateStats();
172 875a0179 Christos Stathis
//            }
173 875a0179 Christos Stathis
//
174 875a0179 Christos Stathis
//            @Override
175 875a0179 Christos Stathis
//            public void onError(Throwable t) {
176 875a0179 Christos Stathis
//                GWT.log("", t);
177 875a0179 Christos Stathis
//                if (t instanceof RestException) {
178 875a0179 Christos Stathis
//                    int statusCode = ((RestException) t).getHttpStatusCode();
179 875a0179 Christos Stathis
//                    if (statusCode == 405)
180 875a0179 Christos Stathis
//                        GSS.get().displayError("You don't have the necessary permissions");
181 875a0179 Christos Stathis
//
182 875a0179 Christos Stathis
//                    else if (statusCode == 409)
183 875a0179 Christos Stathis
//                        GSS.get().displayError("A folder with the same name already exists");
184 875a0179 Christos Stathis
//                    else if (statusCode == 413)
185 875a0179 Christos Stathis
//                        GSS.get().displayError("Your quota has been exceeded");
186 875a0179 Christos Stathis
//                    else
187 875a0179 Christos Stathis
//                        GSS.get().displayError("Unable to copy folder:" + ((RestException) t).getHttpStatusText());
188 875a0179 Christos Stathis
//                }
189 875a0179 Christos Stathis
//                else
190 875a0179 Christos Stathis
//                    GSS.get().displayError("System error copying folder:" + t.getMessage());
191 875a0179 Christos Stathis
//            }
192 875a0179 Christos Stathis
//        };
193 875a0179 Christos Stathis
//        DeferredCommand.addCommand(cf);
194 875a0179 Christos Stathis
    }
195 875a0179 Christos Stathis
196 875a0179 Christos Stathis
    private void copyFiles(final Folder ftarget, List<File> files) {
197 875a0179 Christos Stathis
//        List<String> fileIds = new ArrayList<String>();
198 875a0179 Christos Stathis
//        String target = ftarget.getUri();
199 875a0179 Christos Stathis
//        target = target.endsWith("/") ? target : target + '/';
200 875a0179 Christos Stathis
//        for (File file : files) {
201 875a0179 Christos Stathis
//            String fileTarget = target + URL.encodeComponent(file.getName());
202 875a0179 Christos Stathis
//            fileIds.add(file.getUri() + "?copy=" + fileTarget);
203 875a0179 Christos Stathis
//        }
204 875a0179 Christos Stathis
//        int index = 0;
205 875a0179 Christos Stathis
//        executeCopyOrMoveFiles(index, fileIds);
206 875a0179 Christos Stathis
    }
207 875a0179 Christos Stathis
208 875a0179 Christos Stathis
    private void moveFiles(final Folder ftarget, List<File> files) {
209 875a0179 Christos Stathis
//        List<String> fileIds = new ArrayList<String>();
210 875a0179 Christos Stathis
//        String target = ftarget.getUri();
211 875a0179 Christos Stathis
//        target = target.endsWith("/") ? target : target + '/';
212 875a0179 Christos Stathis
//        for (File file : files) {
213 875a0179 Christos Stathis
//            String fileTarget = target + URL.encodeComponent(file.getName());
214 875a0179 Christos Stathis
//            fileIds.add(file.getUri() + "?move=" + fileTarget);
215 875a0179 Christos Stathis
//        }
216 875a0179 Christos Stathis
//        int index = 0;
217 875a0179 Christos Stathis
//        executeCopyOrMoveFiles(index, fileIds);
218 875a0179 Christos Stathis
    }
219 875a0179 Christos Stathis
220 875a0179 Christos Stathis
    private void trashFolder(final FolderResource folder) {
221 875a0179 Christos Stathis
        PostCommand tot = new PostCommand(folder.getUri() + "?trash=", "", 200) {
222 875a0179 Christos Stathis
223 875a0179 Christos Stathis
            @Override
224 875a0179 Christos Stathis
            public void onComplete() {
225 875a0179 Christos Stathis
                GSS.get().getTreeView().updateNodeChildrenForRemove(folder.getParentURI());
226 875a0179 Christos Stathis
                GSS.get().getTreeView().updateTrashNode();
227 875a0179 Christos Stathis
                /*for(TreeItem item : items)
228 875a0179 Christos Stathis
                        GSS.get().getFolders().updateFolder((DnDTreeItem) item);
229 875a0179 Christos Stathis
                GSS.get().getFolders().update(GSS.get().getFolders().getTrashItem());
230 875a0179 Christos Stathis

231 875a0179 Christos Stathis
                GSS.get().showFileList(true);
232 875a0179 Christos Stathis
                */
233 875a0179 Christos Stathis
            }
234 875a0179 Christos Stathis
235 875a0179 Christos Stathis
            @Override
236 875a0179 Christos Stathis
            public void onError(Throwable t) {
237 875a0179 Christos Stathis
                GWT.log("", t);
238 875a0179 Christos Stathis
                if (t instanceof RestException) {
239 875a0179 Christos Stathis
                    int statusCode = ((RestException) t).getHttpStatusCode();
240 875a0179 Christos Stathis
                    if (statusCode == 405)
241 875a0179 Christos Stathis
                        GSS.get().displayError("You don't have the necessary permissions");
242 875a0179 Christos Stathis
                    else if (statusCode == 404)
243 875a0179 Christos Stathis
                        GSS.get().displayError("Folder does not exist");
244 875a0179 Christos Stathis
                    else
245 875a0179 Christos Stathis
                        GSS.get().displayError("Unable to trash folder:" + ((RestException) t).getHttpStatusText());
246 875a0179 Christos Stathis
                }
247 875a0179 Christos Stathis
                else
248 875a0179 Christos Stathis
                    GSS.get().displayError("System error trashing folder:" + t.getMessage());
249 875a0179 Christos Stathis
            }
250 875a0179 Christos Stathis
        };
251 875a0179 Christos Stathis
        DeferredCommand.addCommand(tot);
252 875a0179 Christos Stathis
    }
253 875a0179 Christos Stathis
254 875a0179 Christos Stathis
    private void trashFiles(List<File> files) {
255 875a0179 Christos Stathis
        final List<String> fileIds = new ArrayList<String>();
256 875a0179 Christos Stathis
        for (File f : files)
257 875a0179 Christos Stathis
            fileIds.add(f.getUri() + "?trash=");
258 875a0179 Christos Stathis
        MultiplePostCommand tot = new MultiplePostCommand(fileIds.toArray(new String[0]), 200) {
259 875a0179 Christos Stathis
260 875a0179 Christos Stathis
            @Override
261 875a0179 Christos Stathis
            public void onComplete() {
262 875a0179 Christos Stathis
                GSS.get().showFileList(true);
263 875a0179 Christos Stathis
            }
264 875a0179 Christos Stathis
265 875a0179 Christos Stathis
            @Override
266 875a0179 Christos Stathis
            public void onError(String p, Throwable t) {
267 875a0179 Christos Stathis
                GWT.log("", t);
268 875a0179 Christos Stathis
                if (t instanceof RestException) {
269 875a0179 Christos Stathis
                    int statusCode = ((RestException) t).getHttpStatusCode();
270 875a0179 Christos Stathis
                    if (statusCode == 405)
271 875a0179 Christos Stathis
                        GSS.get().displayError("You don't have the necessary permissions");
272 875a0179 Christos Stathis
                    else if (statusCode == 404)
273 875a0179 Christos Stathis
                        GSS.get().displayError("File does not exist");
274 875a0179 Christos Stathis
                    else
275 875a0179 Christos Stathis
                        GSS.get().displayError("Unable to trash file:" + ((RestException) t).getHttpStatusText());
276 875a0179 Christos Stathis
                }
277 875a0179 Christos Stathis
                else
278 875a0179 Christos Stathis
                    GSS.get().displayError("System error trashing file:" + t.getMessage());
279 875a0179 Christos Stathis
            }
280 875a0179 Christos Stathis
        };
281 875a0179 Christos Stathis
        DeferredCommand.addCommand(tot);
282 875a0179 Christos Stathis
    }
283 875a0179 Christos Stathis
284 875a0179 Christos Stathis
    private void executeCopyOrMoveFiles(final int index, final List<String> paths) {
285 875a0179 Christos Stathis
        if (index >= paths.size()) {
286 875a0179 Christos Stathis
            GSS.get().showFileList(true);
287 875a0179 Christos Stathis
            GSS.get().getStatusPanel().updateStats();
288 875a0179 Christos Stathis
            return;
289 a57faaf0 Christos Stathis
        }
290 875a0179 Christos Stathis
        PostCommand cf = new PostCommand(paths.get(index), "", 200) {
291 875a0179 Christos Stathis
292 875a0179 Christos Stathis
            @Override
293 875a0179 Christos Stathis
            public void onComplete() {
294 875a0179 Christos Stathis
                executeCopyOrMoveFiles(index + 1, paths);
295 875a0179 Christos Stathis
            }
296 875a0179 Christos Stathis
297 875a0179 Christos Stathis
            @Override
298 875a0179 Christos Stathis
            public void onError(Throwable t) {
299 875a0179 Christos Stathis
                GWT.log("", t);
300 875a0179 Christos Stathis
                if (t instanceof RestException) {
301 875a0179 Christos Stathis
                    int statusCode = ((RestException) t).getHttpStatusCode();
302 875a0179 Christos Stathis
                    if (statusCode == 405)
303 875a0179 Christos Stathis
                        GSS.get().displayError("You don't have the necessary permissions");
304 875a0179 Christos Stathis
                    else if (statusCode == 404)
305 875a0179 Christos Stathis
                        GSS.get().displayError("File not found");
306 875a0179 Christos Stathis
                    else if (statusCode == 409)
307 875a0179 Christos Stathis
                        GSS.get().displayError("A file with the same name already exists");
308 875a0179 Christos Stathis
                    else if (statusCode == 413)
309 875a0179 Christos Stathis
                        GSS.get().displayError("Your quota has been exceeded");
310 875a0179 Christos Stathis
                    else
311 875a0179 Christos Stathis
                        GSS.get().displayError("Unable to copy file:" + ((RestException) t).getHttpStatusText());
312 875a0179 Christos Stathis
                }
313 875a0179 Christos Stathis
                else
314 875a0179 Christos Stathis
                    GSS.get().displayError("System error copying file:" + t.getMessage());
315 875a0179 Christos Stathis
            }
316 875a0179 Christos Stathis
        };
317 875a0179 Christos Stathis
        DeferredCommand.addCommand(cf);
318 875a0179 Christos Stathis
    }
319 a57faaf0 Christos Stathis
}