Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (16.3 kB)

1
/*
2
 * Copyright 2011 Electronic Business Systems Ltd.
3
 *
4
 * This file is part of GSS.
5
 *
6
 * GSS is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * GSS is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
package gr.grnet.pithos.web.client;
20
/*
21
 * Copyright 2008, 2009 Electronic Business Systems Ltd.
22
 *
23
 * This file is part of GSS.
24
 *
25
 * GSS is free software: you can redistribute it and/or modify
26
 * it under the terms of the GNU General Public License as published by
27
 * the Free Software Foundation, either version 3 of the License, or
28
 * (at your option) any later version.
29
 *
30
 * GSS is distributed in the hope that it will be useful,
31
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33
 * GNU General Public License for more details.
34
 *
35
 * You should have received a copy of the GNU General Public License
36
 * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
37
 */
38

    
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
/**
58
 * @author kman
59
 */
60
public class DnDFolderPopupMenu extends PopupPanel {
61

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

    
70
                        @Override
71
                        public void execute() {
72
                                hide();
73
                        }
74
                };
75

    
76
                final MenuBar contextMenu = new MenuBar(true);
77
                final CellTreeView folders = GSS.get().getTreeView();
78

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

    
81
                                @Override
82
                                public void execute() {
83
                                        if (toCopy instanceof RestResourceWrapper){
84
                                                moveFolder(target, ((RestResourceWrapper) toCopy).getResource());
85
                                        }
86
                                        else if(toCopy instanceof List){
87
                                                List<FileResource> files = GSS.get().getFileList().getSelectedFiles();
88
                                                moveFiles(target, files);
89
                                        }
90
                                        hide();
91
                                }
92

    
93
                        }).setVisible(target != null);
94

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

    
97
                        @Override
98
                        public void execute() {
99
                                if (toCopy instanceof RestResourceWrapper)
100
                                        copyFolder(target, ((RestResourceWrapper) toCopy).getResource());
101
                                else if(toCopy instanceof List){
102
                                        List<FileResource> files = GSS.get().getFileList().getSelectedFiles();
103
                                    copyFiles(target, files);
104
                                }
105
                                hide();
106
                        }
107

    
108
                }).setVisible(target != null);
109

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

    
112
                        @Override
113
                        public void execute() {
114
                                GWT.log("EXECUTE TRASH:"+toCopy.getClass().getName());
115
                                if (toCopy instanceof RestResourceWrapper){
116
                                        trashFolder(((RestResourceWrapper) toCopy).getResource());
117
                                }
118
                                else if(toCopy instanceof List){
119
                                        List<FileResource> files = GSS.get().getFileList().getSelectedFiles();
120
                                    trashFiles(files);
121
                                }
122
                                hide();
123
                        }
124

    
125
                }).setVisible(target == null);
126
                contextMenu.addItem("<span>" + AbstractImagePrototype.create(newImages.delete()).getHTML() + "&nbsp;Cancel</span>", true, cancelCmd);
127

    
128
                add(contextMenu);
129

    
130
        }
131

    
132
        private void copyFolder(final FolderResource target, FolderResource toCopy) {
133
                String atarget = target.getUri();
134
                atarget = atarget.endsWith("/") ? atarget : atarget + '/';
135
                atarget = atarget + toCopy.getName();
136
                PostCommand cf = new PostCommand(toCopy.getUri() + "?copy=" + atarget, "", 200) {
137

    
138
                        @Override
139
                        public void onComplete() {
140
                                        GSS.get().getTreeView().updateNodeChildren(new RestResourceWrapper(target));
141
                                                                GSS.get().getStatusPanel().updateStats();
142
                        }
143

    
144
                        @Override
145
                        public void onError(Throwable t) {
146
                                GWT.log("", 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");
151

    
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");
156
                                        else
157
                                                GSS.get().displayError("Unable to copy folder:" + ((RestException)t).getHttpStatusText());
158
                                } else
159
                                        GSS.get().displayError("System error copying folder:" + t.getMessage());
160
                        }
161
                };
162
                DeferredCommand.addCommand(cf);
163
        }
164

    
165
        private void moveFolder(final FolderResource target, final FolderResource toCopy) {
166
                String atarget = target.getUri();
167
                atarget = atarget.endsWith("/") ? atarget : atarget + '/';
168
                atarget = atarget + toCopy.getName();
169

    
170
                PostCommand cf = new PostCommand(toCopy.getUri() + "?move=" + atarget, "", 200) {
171

    
172
                        @Override
173
                        public void onComplete() {
174
                                GWT.log("[MOVE]"+target.getUri()+"   "+ toCopy.getParentURI());
175
                                GSS.get().getTreeView().updateNodeChildren(new RestResourceWrapper(target));
176
                                                        GSS.get().getTreeView().updateNodeChildrenForRemove(toCopy.getParentURI());
177
                                                        GSS.get().getStatusPanel().updateStats();
178
                        }
179

    
180
                        @Override
181
                        public void onError(Throwable t) {
182
                                GWT.log("", t);
183
                                if (t instanceof RestException) {
184
                                        int statusCode = ((RestException) t).getHttpStatusCode();
185
                                        if (statusCode == 405)
186
                                                GSS.get().displayError("You don't have the necessary permissions");
187

    
188
                                        else if (statusCode == 409)
189
                                                GSS.get().displayError("A folder with the same name already exists");
190
                                        else if (statusCode == 413)
191
                                                GSS.get().displayError("Your quota has been exceeded");
192
                                        else
193
                                                GSS.get().displayError("Unable to copy folder:" + ((RestException)t).getHttpStatusText());
194
                                } else
195
                                        GSS.get().displayError("System error copying folder:" + t.getMessage());
196
                        }
197
                };
198
                DeferredCommand.addCommand(cf);
199
        }
200

    
201
        private void copyFiles(final FolderResource ftarget, List<FileResource> files) {
202
                List<String> fileIds = new ArrayList<String>();
203
                String target = ftarget.getUri();
204
                target = target.endsWith("/") ? target : target + '/';
205
                for (FileResource fileResource : files) {
206
                        String fileTarget = target + URL.encodeComponent(fileResource.getName());
207
                        fileIds.add(fileResource.getUri() + "?copy=" + fileTarget);
208
                }
209
                int index = 0;
210
                executeCopyOrMoveFiles(index, fileIds);
211

    
212
        }
213

    
214
        private void moveFiles(final FolderResource ftarget, List<FileResource> files) {
215
                List<String> fileIds = new ArrayList<String>();
216
                String target = ftarget.getUri();
217
                target = target.endsWith("/") ? target : target + '/';
218
                for (FileResource fileResource : files) {
219
                        String fileTarget = target + URL.encodeComponent(fileResource.getName());
220
                        fileIds.add(fileResource.getUri() + "?move=" + fileTarget);
221
                }
222
                int index = 0;
223
                executeCopyOrMoveFiles(index, fileIds);
224

    
225
        }
226

    
227
        private void trashFolder(final FolderResource folder){
228
                PostCommand tot = new PostCommand(folder.getUri()+"?trash=","",200){
229

    
230
                        @Override
231
                        public void onComplete() {
232
                                GSS.get().getTreeView().updateNodeChildrenForRemove(folder.getParentURI());
233
                                GSS.get().getTreeView().updateTrashNode();
234
                                /*for(TreeItem item : items)
235
                                        GSS.get().getFolders().updateFolder((DnDTreeItem) item);
236
                                GSS.get().getFolders().update(GSS.get().getFolders().getTrashItem());
237
                               
238
                                GSS.get().showFileList(true);
239
                                */
240
                        }
241

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

    
261
        private void trashFiles(List<FileResource> files){
262
                final List<String> fileIds = new ArrayList<String>();
263
                for(FileResource f : files)
264
                        fileIds.add(f.getUri()+"?trash=");
265
                MultiplePostCommand tot = new MultiplePostCommand(fileIds.toArray(new String[0]),200){
266

    
267
                        @Override
268
                        public void onComplete() {
269
                                GSS.get().showFileList(true);
270
                        }
271

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

    
291

    
292
        private void executeCopyOrMoveFiles(final int index, final List<String> paths) {
293
                if (index >= paths.size()) {
294
                        GSS.get().showFileList(true);
295
                        GSS.get().getStatusPanel().updateStats();
296
                        return;
297
                }
298
                PostCommand cf = new PostCommand(paths.get(index), "", 200) {
299

    
300
                        @Override
301
                        public void onComplete() {
302
                                executeCopyOrMoveFiles(index + 1, paths);
303
                        }
304

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

    
323
                        }
324
                };
325
                DeferredCommand.addCommand(cf);
326
        }
327

    
328
}