Statistics
| Branch: | Tag: | Revision:

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

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.FolderResource;
43
import gr.grnet.pithos.web.client.rest.resource.RestResourceWrapper;
44

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

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

    
55
public class DnDFolderPopupMenu extends PopupPanel {
56

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

    
65
            @Override
66
            public void execute() {
67
                hide();
68
            }
69
        };
70

    
71
        final MenuBar contextMenu = new MenuBar(true);
72
        final CellTreeView folders = Pithos.get().getTreeView();
73

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

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

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

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

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

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

    
120
        add(contextMenu);
121
    }
122

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

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

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

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

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

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

229
                Pithos.get().showFileList(true);
230
                */
231
            }
232

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

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

    
258
            @Override
259
            public void onComplete() {
260
                Pithos.get().showFileList(true);
261
            }
262

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

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

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

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