Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (13.6 kB)

1
/*
2
 * Copyright 2011 GRNET S.A. All rights reserved.
3
 *
4
 * Redistribution and use in source and binary forms, with or
5
 * without modification, are permitted provided that the following
6
 * conditions are met:
7
 *
8
 *   1. Redistributions of source code must retain the above
9
 *      copyright notice, this list of conditions and the following
10
 *      disclaimer.
11
 *
12
 *   2. Redistributions in binary form must reproduce the above
13
 *      copyright notice, this list of conditions and the following
14
 *      disclaimer in the documentation and/or other materials
15
 *      provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
 * POSSIBILITY OF SUCH DAMAGE.
29
 *
30
 * The views and conclusions contained in the software and
31
 * documentation are those of the authors and should not be
32
 * interpreted as representing official policies, either expressed
33
 * or implied, of GRNET S.A.
34
 */
35
package gr.grnet.pithos.web.client;
36

    
37
import gr.grnet.pithos.web.client.foldertree.File;
38
import gr.grnet.pithos.web.client.foldertree.Folder;
39
import gr.grnet.pithos.web.client.rest.MultiplePostCommand;
40
import gr.grnet.pithos.web.client.rest.PostCommand;
41
import gr.grnet.pithos.web.client.rest.RestException;
42
import gr.grnet.pithos.web.client.rest.resource.FileResource;
43
import gr.grnet.pithos.web.client.rest.resource.FolderResource;
44
import gr.grnet.pithos.web.client.rest.resource.RestResourceWrapper;
45

    
46
import java.util.ArrayList;
47
import java.util.List;
48

    
49
import com.google.gwt.core.client.GWT;
50
import com.google.gwt.http.client.URL;
51
import com.google.gwt.user.client.Command;
52
import com.google.gwt.user.client.DeferredCommand;
53
import com.google.gwt.user.client.ui.AbstractImagePrototype;
54
import com.google.gwt.user.client.ui.MenuBar;
55
import com.google.gwt.user.client.ui.PopupPanel;
56

    
57
public class DnDFolderPopupMenu extends PopupPanel {
58

    
59
    public DnDFolderPopupMenu(final CellTreeView.Images newImages, final Folder target, final Object toCopy) {
60
        // The popup's constructor's argument is a boolean specifying that it
61
        // auto-close itself when the user clicks outside of it.
62
        super(true);
63
        setAnimationEnabled(true);
64
        // A dummy command that we will execute from unimplemented leaves.
65
        final Command cancelCmd = new Command() {
66

    
67
            @Override
68
            public void execute() {
69
                hide();
70
            }
71
        };
72

    
73
        final MenuBar contextMenu = new MenuBar(true);
74
        final CellTreeView folders = GSS.get().getTreeView();
75

    
76
        contextMenu.addItem("<span>" + AbstractImagePrototype.create(newImages.cut()).getHTML() + "&nbsp;Move</span>", true, new Command() {
77

    
78
            @Override
79
            public void execute() {
80
                if (toCopy instanceof Folder) {
81
                    moveFolder(target, (Folder) toCopy);
82
                }
83
                else if (toCopy instanceof List) {
84
                    List<File> files = GSS.get().getFileList().getSelectedFiles();
85
                    moveFiles(target, files);
86
                }
87
                hide();
88
            }
89
        }).setVisible(target != null);
90

    
91
        contextMenu.addItem("<span>" + AbstractImagePrototype.create(newImages.copy()).getHTML() + "&nbsp;Copy</span>", true, new Command() {
92

    
93
            @Override
94
            public void execute() {
95
                if (toCopy instanceof Folder)
96
                    copyFolder(target, (Folder) toCopy);
97
                else if (toCopy instanceof List) {
98
                    List<File> files = GSS.get().getFileList().getSelectedFiles();
99
                    copyFiles(target, files);
100
                }
101
                hide();
102
            }
103
        }).setVisible(target != null);
104

    
105
        contextMenu.addItem("<span>" + AbstractImagePrototype.create(newImages.trash()).getHTML() + "&nbsp;Delete (Trash)</span>", true, new Command() {
106

    
107
            @Override
108
            public void execute() {
109
                GWT.log("EXECUTE TRASH:" + toCopy.getClass().getName());
110
                if (toCopy instanceof RestResourceWrapper) {
111
                    trashFolder(((RestResourceWrapper) toCopy).getResource());
112
                }
113
                else if (toCopy instanceof List) {
114
                    List<File> files = GSS.get().getFileList().getSelectedFiles();
115
                    trashFiles(files);
116
                }
117
                hide();
118
            }
119
        }).setVisible(target == null);
120
        contextMenu.addItem("<span>" + AbstractImagePrototype.create(newImages.delete()).getHTML() + "&nbsp;Cancel</span>", true, cancelCmd);
121

    
122
        add(contextMenu);
123
    }
124

    
125
    private void copyFolder(final Folder target, Folder toCopy) {
126
//        String atarget = target.getUri();
127
//        atarget = atarget.endsWith("/") ? atarget : atarget + '/';
128
//        atarget = atarget + toCopy.getName();
129
//        PostCommand cf = new PostCommand(toCopy.getUri() + "?copy=" + atarget, "", 200) {
130
//
131
//            @Override
132
//            public void onComplete() {
133
//                GSS.get().getTreeView().updateNodeChildren(new RestResourceWrapper(target));
134
//                GSS.get().getStatusPanel().updateStats();
135
//            }
136
//
137
//            @Override
138
//            public void onError(Throwable t) {
139
//                GWT.log("", t);
140
//                if (t instanceof RestException) {
141
//                    int statusCode = ((RestException) t).getHttpStatusCode();
142
//                    if (statusCode == 405)
143
//                        GSS.get().displayError("You don't have the necessary permissions");
144
//
145
//                    else if (statusCode == 409)
146
//                        GSS.get().displayError("A folder with the same name already exists");
147
//                    else if (statusCode == 413)
148
//                        GSS.get().displayError("Your quota has been exceeded");
149
//                    else
150
//                        GSS.get().displayError("Unable to copy folder:" + ((RestException) t).getHttpStatusText());
151
//                }
152
//                else
153
//                    GSS.get().displayError("System error copying folder:" + t.getMessage());
154
//            }
155
//        };
156
//        DeferredCommand.addCommand(cf);
157
    }
158

    
159
    private void moveFolder(final Folder target, final Folder toCopy) {
160
//        String atarget = target.getUri();
161
//        atarget = atarget.endsWith("/") ? atarget : atarget + '/';
162
//        atarget = atarget + toCopy.getName();
163
//
164
//        PostCommand cf = new PostCommand(toCopy.getUri() + "?move=" + atarget, "", 200) {
165
//
166
//            @Override
167
//            public void onComplete() {
168
//                GWT.log("[MOVE]" + target.getUri() + "   " + toCopy.getParentURI());
169
//                GSS.get().getTreeView().updateNodeChildren(new RestResourceWrapper(target));
170
//                GSS.get().getTreeView().updateNodeChildrenForRemove(toCopy.getParentURI());
171
//                GSS.get().getStatusPanel().updateStats();
172
//            }
173
//
174
//            @Override
175
//            public void onError(Throwable t) {
176
//                GWT.log("", t);
177
//                if (t instanceof RestException) {
178
//                    int statusCode = ((RestException) t).getHttpStatusCode();
179
//                    if (statusCode == 405)
180
//                        GSS.get().displayError("You don't have the necessary permissions");
181
//
182
//                    else if (statusCode == 409)
183
//                        GSS.get().displayError("A folder with the same name already exists");
184
//                    else if (statusCode == 413)
185
//                        GSS.get().displayError("Your quota has been exceeded");
186
//                    else
187
//                        GSS.get().displayError("Unable to copy folder:" + ((RestException) t).getHttpStatusText());
188
//                }
189
//                else
190
//                    GSS.get().displayError("System error copying folder:" + t.getMessage());
191
//            }
192
//        };
193
//        DeferredCommand.addCommand(cf);
194
    }
195

    
196
    private void copyFiles(final Folder ftarget, List<File> files) {
197
//        List<String> fileIds = new ArrayList<String>();
198
//        String target = ftarget.getUri();
199
//        target = target.endsWith("/") ? target : target + '/';
200
//        for (File file : files) {
201
//            String fileTarget = target + URL.encodeComponent(file.getName());
202
//            fileIds.add(file.getUri() + "?copy=" + fileTarget);
203
//        }
204
//        int index = 0;
205
//        executeCopyOrMoveFiles(index, fileIds);
206
    }
207

    
208
    private void moveFiles(final Folder ftarget, List<File> files) {
209
//        List<String> fileIds = new ArrayList<String>();
210
//        String target = ftarget.getUri();
211
//        target = target.endsWith("/") ? target : target + '/';
212
//        for (File file : files) {
213
//            String fileTarget = target + URL.encodeComponent(file.getName());
214
//            fileIds.add(file.getUri() + "?move=" + fileTarget);
215
//        }
216
//        int index = 0;
217
//        executeCopyOrMoveFiles(index, fileIds);
218
    }
219

    
220
    private void trashFolder(final FolderResource folder) {
221
        PostCommand tot = new PostCommand(folder.getUri() + "?trash=", "", 200) {
222

    
223
            @Override
224
            public void onComplete() {
225
                GSS.get().getTreeView().updateNodeChildrenForRemove(folder.getParentURI());
226
                GSS.get().getTreeView().updateTrashNode();
227
                /*for(TreeItem item : items)
228
                        GSS.get().getFolders().updateFolder((DnDTreeItem) item);
229
                GSS.get().getFolders().update(GSS.get().getFolders().getTrashItem());
230

231
                GSS.get().showFileList(true);
232
                */
233
            }
234

    
235
            @Override
236
            public void onError(Throwable t) {
237
                GWT.log("", t);
238
                if (t instanceof RestException) {
239
                    int statusCode = ((RestException) t).getHttpStatusCode();
240
                    if (statusCode == 405)
241
                        GSS.get().displayError("You don't have the necessary permissions");
242
                    else if (statusCode == 404)
243
                        GSS.get().displayError("Folder does not exist");
244
                    else
245
                        GSS.get().displayError("Unable to trash folder:" + ((RestException) t).getHttpStatusText());
246
                }
247
                else
248
                    GSS.get().displayError("System error trashing folder:" + t.getMessage());
249
            }
250
        };
251
        DeferredCommand.addCommand(tot);
252
    }
253

    
254
    private void trashFiles(List<File> files) {
255
        final List<String> fileIds = new ArrayList<String>();
256
        for (File f : files)
257
            fileIds.add(f.getUri() + "?trash=");
258
        MultiplePostCommand tot = new MultiplePostCommand(fileIds.toArray(new String[0]), 200) {
259

    
260
            @Override
261
            public void onComplete() {
262
                GSS.get().showFileList(true);
263
            }
264

    
265
            @Override
266
            public void onError(String p, Throwable t) {
267
                GWT.log("", t);
268
                if (t instanceof RestException) {
269
                    int statusCode = ((RestException) t).getHttpStatusCode();
270
                    if (statusCode == 405)
271
                        GSS.get().displayError("You don't have the necessary permissions");
272
                    else if (statusCode == 404)
273
                        GSS.get().displayError("File does not exist");
274
                    else
275
                        GSS.get().displayError("Unable to trash file:" + ((RestException) t).getHttpStatusText());
276
                }
277
                else
278
                    GSS.get().displayError("System error trashing file:" + t.getMessage());
279
            }
280
        };
281
        DeferredCommand.addCommand(tot);
282
    }
283

    
284
    private void executeCopyOrMoveFiles(final int index, final List<String> paths) {
285
        if (index >= paths.size()) {
286
            GSS.get().showFileList(true);
287
            GSS.get().getStatusPanel().updateStats();
288
            return;
289
        }
290
        PostCommand cf = new PostCommand(paths.get(index), "", 200) {
291

    
292
            @Override
293
            public void onComplete() {
294
                executeCopyOrMoveFiles(index + 1, paths);
295
            }
296

    
297
            @Override
298
            public void onError(Throwable t) {
299
                GWT.log("", t);
300
                if (t instanceof RestException) {
301
                    int statusCode = ((RestException) t).getHttpStatusCode();
302
                    if (statusCode == 405)
303
                        GSS.get().displayError("You don't have the necessary permissions");
304
                    else if (statusCode == 404)
305
                        GSS.get().displayError("File not found");
306
                    else if (statusCode == 409)
307
                        GSS.get().displayError("A file with the same name already exists");
308
                    else if (statusCode == 413)
309
                        GSS.get().displayError("Your quota has been exceeded");
310
                    else
311
                        GSS.get().displayError("Unable to copy file:" + ((RestException) t).getHttpStatusText());
312
                }
313
                else
314
                    GSS.get().displayError("System error copying file:" + t.getMessage());
315
            }
316
        };
317
        DeferredCommand.addCommand(cf);
318
    }
319
}