Statistics
| Branch: | Tag: | Revision:

root / gss / src / gr / ebs / gss / client / dnd / DnDFolderPopupMenu.java @ 555e8e59

History | View | Annotate | Download (10.7 kB)

1
/*
2
 * Copyright 2008, 2009 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.dnd;
20

    
21
import gr.ebs.gss.client.Folders;
22
import gr.ebs.gss.client.GSS;
23
import gr.ebs.gss.client.rest.ExecuteMultiplePost;
24
import gr.ebs.gss.client.rest.ExecutePost;
25
import gr.ebs.gss.client.rest.RestException;
26
import gr.ebs.gss.client.rest.resource.FileResource;
27
import gr.ebs.gss.client.rest.resource.FolderResource;
28
import gr.ebs.gss.client.rest.resource.RestResource;
29

    
30
import java.util.ArrayList;
31
import java.util.List;
32

    
33
import com.google.gwt.core.client.GWT;
34
import com.google.gwt.user.client.Command;
35
import com.google.gwt.user.client.DeferredCommand;
36
import com.google.gwt.user.client.ui.MenuBar;
37
import com.google.gwt.user.client.ui.PopupPanel;
38
import com.google.gwt.user.client.ui.TreeItem;
39

    
40
/**
41
 * @author kman
42
 */
43
public class DnDFolderPopupMenu extends PopupPanel {
44

    
45
        /**
46
         * The widget's images.
47
         */
48
        private final Folders.Images images;
49

    
50
        public DnDFolderPopupMenu(final Folders.Images newImages, final FolderResource target, final Object toCopy, boolean othersShared) {
51
                // The popup's constructor's argument is a boolean specifying that it
52
                // auto-close itself when the user clicks outside of it.
53
                super(true);
54
                setAnimationEnabled(true);
55
                images = newImages;
56

    
57
                // A dummy command that we will execute from unimplemented leaves.
58
                final Command cancelCmd = new Command() {
59

    
60
                        public void execute() {
61
                                hide();
62
                        }
63
                };
64

    
65
                final MenuBar contextMenu = new MenuBar(true);
66
                final Folders folders = GSS.get().getFolders();
67

    
68
                if (!othersShared)
69
                        contextMenu.addItem("<span>" + newImages.cut().getHTML() + "&nbsp;Move</span>", true, new Command() {
70

    
71
                                public void execute() {
72
                                        if (toCopy instanceof FolderResource){
73
                                                List<TreeItem> treeItems = folders.getItemsOfTreeForPath(((RestResource) toCopy).getUri());
74
                                                List<TreeItem> parents = new ArrayList();
75
                                                for(TreeItem item : treeItems)
76
                                                        if(item.getParentItem() != null)
77
                                                                parents.add(item.getParentItem());
78
                                                moveFolder(target, (FolderResource) toCopy, parents);
79
                                        }
80
                                        else if(toCopy instanceof List)
81
                                                moveFiles(target, (List<FileResource>) toCopy);
82
                                        hide();
83
                                }
84

    
85
                        }).setVisible(target != null);
86
                ;
87
                contextMenu.addItem("<span>" + newImages.copy().getHTML() + "&nbsp;Copy</span>", true, new Command() {
88

    
89
                        public void execute() {
90
                                if (toCopy instanceof FolderResource)
91
                                        copyFolder(target, (FolderResource) toCopy);
92
                                else if(toCopy instanceof List)
93
                                        copyFiles(target, (List<FileResource>) toCopy);
94
                                hide();
95
                        }
96

    
97
                }).setVisible(target != null);
98
                ;
99
                contextMenu.addItem("<span>" + newImages.trash().getHTML() + "&nbsp;Delete (Trash)</span>", true, new Command() {
100

    
101
                        public void execute() {
102
                                if (toCopy instanceof FolderResource){
103
                                        final List<TreeItem> treeItems = folders.getItemsOfTreeForPath(((RestResource) toCopy).getUri());
104
                                        List<TreeItem> parents = new ArrayList();
105
                                        for(TreeItem item : treeItems)
106
                                                if(item.getParentItem() != null)
107
                                                        parents.add(item.getParentItem());
108
                                        trashFolder((FolderResource) toCopy, parents);
109
                                }
110
                                else if(toCopy instanceof List)
111
                                        trashFiles((List<FileResource>) toCopy);
112
                                hide();
113
                        }
114

    
115
                }).setVisible(target == null);
116
                contextMenu.addItem("<span>" + newImages.delete().getHTML() + "&nbsp;Cancel</span>", true, cancelCmd);
117

    
118
                add(contextMenu);
119

    
120
        }
121

    
122
        private void copyFolder(final FolderResource target, FolderResource toCopy) {
123
                String atarget = target.getUri();
124
                atarget = atarget.endsWith("/") ? atarget : atarget + '/';
125
                atarget = atarget + toCopy.getName();
126
                ExecutePost cf = new ExecutePost(toCopy.getUri() + "?copy=" + atarget, "", 200) {
127

    
128
                        public void onComplete() {
129
                                final TreeItem folder;
130
                                TreeItem folderTemp = GSS.get().getFolders().getUserItem(target);
131
                                if (folderTemp == null)
132
                                        folder = GSS.get().getFolders().getOtherSharedItem(target);
133
                                else
134
                                        folder = folderTemp;
135
                                GSS.get().getFolders().updateFolder((DnDTreeItem) folder);
136
                        }
137

    
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
                                } else
152
                                        GSS.get().displayError("System error copying folder:" + t.getMessage());
153
                        }
154
                };
155
                DeferredCommand.addCommand(cf);
156
        }
157

    
158
        private void moveFolder(final FolderResource target, final FolderResource toCopy, final List<TreeItem> items) {
159
                String atarget = target.getUri();
160
                atarget = atarget.endsWith("/") ? atarget : atarget + '/';
161
                atarget = atarget + toCopy.getName();
162

    
163
                ExecutePost cf = new ExecutePost(toCopy.getUri() + "?move=" + atarget, "", 200) {
164

    
165
                        public void onComplete() {
166
                                final TreeItem folder;
167
                                for(TreeItem i : items){
168
                                        DnDTreeItem id = (DnDTreeItem)i;
169
                                        if(id.getChild(toCopy) != null)
170
                                                id.removeItem(id.getChild(toCopy));
171
                                }
172
                                TreeItem folderTemp = GSS.get().getFolders().getUserItem(target);
173
                                if (folderTemp == null)
174
                                        folder = GSS.get().getFolders().getOtherSharedItem(target);
175
                                else
176
                                        folder = folderTemp;
177
                                GSS.get().getFolders().updateFolder((DnDTreeItem) folder);
178
                                for(TreeItem item : items)
179
                                        GSS.get().getFolders().updateFolder((DnDTreeItem) item);
180
                                GSS.get().getFolders().clearSelection();
181
                                GSS.get().showFileList(true);
182
                        }
183

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

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

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

    
215
        }
216

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

    
228
        }
229

    
230
        private void trashFolder(final FolderResource folder, final List<TreeItem> items){
231
                ExecutePost tot = new ExecutePost(folder.getUri()+"?trash=","",200){
232

    
233
                        public void onComplete() {
234
                                for(TreeItem item : items)
235
                                        GSS.get().getFolders().updateFolder((DnDTreeItem) item);
236
                                GSS.get().getFolders().update(GSS.get().getFolders().getTrashItem());
237
                                GSS.get().showFileList(true);
238
                        }
239

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

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

    
264
                        public void onComplete() {
265
                                GSS.get().showFileList(true);
266
                        }
267

    
268

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

    
287

    
288
        private void executeCopyOrMoveFiles(final int index, final List<String> paths) {
289
                if (index >= paths.size()) {
290
                        GSS.get().showFileList(true);
291
                        return;
292
                }
293
                ExecutePost cf = new ExecutePost(paths.get(index), "", 200) {
294

    
295
                        @Override
296
                        public void onComplete() {
297
                                executeCopyOrMoveFiles(index + 1, paths);
298
                        }
299

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

    
318
                        }
319
                };
320
                DeferredCommand.addCommand(cf);
321
        }
322

    
323
}