Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / client / DnDFolderPopupMenu.java @ 81d44c27

History | View | Annotate | Download (16.1 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.ebs.gss.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.ebs.gss.client.CellTreeView;
40
import gr.ebs.gss.client.GSS;
41
import gr.ebs.gss.client.rest.MultiplePostCommand;
42
import gr.ebs.gss.client.rest.PostCommand;
43
import gr.ebs.gss.client.rest.RestException;
44
import gr.ebs.gss.client.rest.resource.FileResource;
45
import gr.ebs.gss.client.rest.resource.FolderResource;
46
import gr.ebs.gss.client.rest.resource.RestResource;
47
import gr.ebs.gss.client.rest.resource.RestResourceWrapper;
48

    
49
import java.util.ArrayList;
50
import java.util.List;
51

    
52
import com.google.gwt.core.client.GWT;
53
import com.google.gwt.http.client.URL;
54
import com.google.gwt.user.client.Command;
55
import com.google.gwt.user.client.DeferredCommand;
56
import com.google.gwt.user.client.ui.AbstractImagePrototype;
57
import com.google.gwt.user.client.ui.MenuBar;
58
import com.google.gwt.user.client.ui.PopupPanel;
59
import com.google.gwt.user.client.ui.TreeItem;
60

    
61
/**
62
 * @author kman
63
 */
64
public class DnDFolderPopupMenu extends PopupPanel {
65

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

    
74
                        @Override
75
                        public void execute() {
76
                                hide();
77
                        }
78
                };
79

    
80
                final MenuBar contextMenu = new MenuBar(true);
81
                final CellTreeView folders = GSS.get().getTreeView();
82

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

    
85
                                @Override
86
                                public void execute() {
87
                                        if (toCopy instanceof RestResourceWrapper){
88
                                                moveFolder(target, ((RestResourceWrapper) toCopy).getResource());
89
                                        }
90
                                        else if(toCopy instanceof List)
91
                                                moveFiles(target, (List<FileResource>) toCopy);
92
                                        hide();
93
                                }
94

    
95
                        }).setVisible(target != null);
96

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

    
99
                        @Override
100
                        public void execute() {
101
                                if (toCopy instanceof RestResourceWrapper)
102
                                        copyFolder(target, ((RestResourceWrapper) toCopy).getResource());
103
                                else if(toCopy instanceof List)
104
                                        copyFiles(target, (List<FileResource>) toCopy);
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
                                        trashFiles((List<FileResource>) toCopy);
120
                                hide();
121
                        }
122

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

    
126
                add(contextMenu);
127

    
128
        }
129

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

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

    
142
                        @Override
143
                        public void onError(Throwable t) {
144
                                GWT.log("", t);
145
                                if (t instanceof RestException) {
146
                                        int statusCode = ((RestException) t).getHttpStatusCode();
147
                                        if (statusCode == 405)
148
                                                GSS.get().displayError("You don't have the necessary permissions");
149

    
150
                                        else if (statusCode == 409)
151
                                                GSS.get().displayError("A folder with the same name already exists");
152
                                        else if (statusCode == 413)
153
                                                GSS.get().displayError("Your quota has been exceeded");
154
                                        else
155
                                                GSS.get().displayError("Unable to copy folder:" + ((RestException)t).getHttpStatusText());
156
                                } else
157
                                        GSS.get().displayError("System error copying folder:" + t.getMessage());
158
                        }
159
                };
160
                DeferredCommand.addCommand(cf);
161
        }
162

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

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

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

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

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

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

    
210
        }
211

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

    
223
        }
224

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

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

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

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

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

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

    
289

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

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

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

    
321
                        }
322
                };
323
                DeferredCommand.addCommand(cf);
324
        }
325

    
326
}