Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (15 kB)

1
/*
2
 *  Copyright (c) 2011 Greek Research and Technology Network
3
 */
4
package gr.grnet.pithos.web.client;
5

    
6
import gr.grnet.pithos.web.client.rest.MultiplePostCommand;
7
import gr.grnet.pithos.web.client.rest.PostCommand;
8
import gr.grnet.pithos.web.client.rest.RestException;
9
import gr.grnet.pithos.web.client.rest.resource.FileResource;
10
import gr.grnet.pithos.web.client.rest.resource.FolderResource;
11
import gr.grnet.pithos.web.client.rest.resource.RestResourceWrapper;
12

    
13
import java.util.ArrayList;
14
import java.util.List;
15

    
16
import com.google.gwt.core.client.GWT;
17
import com.google.gwt.http.client.URL;
18
import com.google.gwt.user.client.Command;
19
import com.google.gwt.user.client.DeferredCommand;
20
import com.google.gwt.user.client.ui.AbstractImagePrototype;
21
import com.google.gwt.user.client.ui.MenuBar;
22
import com.google.gwt.user.client.ui.PopupPanel;
23

    
24
public class DnDFolderPopupMenu extends PopupPanel {
25

    
26
        public DnDFolderPopupMenu(final CellTreeView.Images newImages, final FolderResource target, final Object toCopy) {
27
                // The popup's constructor's argument is a boolean specifying that it
28
                // auto-close itself when the user clicks outside of it.
29
                super(true);
30
                setAnimationEnabled(true);
31
                // A dummy command that we will execute from unimplemented leaves.
32
                final Command cancelCmd = new Command() {
33

    
34
                        @Override
35
                        public void execute() {
36
                                hide();
37
                        }
38
                };
39

    
40
                final MenuBar contextMenu = new MenuBar(true);
41
                final CellTreeView folders = GSS.get().getTreeView();
42

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

    
45
                                @Override
46
                                public void execute() {
47
                                        if (toCopy instanceof RestResourceWrapper){
48
                                                moveFolder(target, ((RestResourceWrapper) toCopy).getResource());
49
                                        }
50
                                        else if(toCopy instanceof List){
51
                                                List<FileResource> files = GSS.get().getFileList().getSelectedFiles();
52
                                                moveFiles(target, files);
53
                                        }
54
                                        hide();
55
                                }
56

    
57
                        }).setVisible(target != null);
58

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

    
61
                        @Override
62
                        public void execute() {
63
                                if (toCopy instanceof RestResourceWrapper)
64
                                        copyFolder(target, ((RestResourceWrapper) toCopy).getResource());
65
                                else if(toCopy instanceof List){
66
                                        List<FileResource> files = GSS.get().getFileList().getSelectedFiles();
67
                                    copyFiles(target, files);
68
                                }
69
                                hide();
70
                        }
71

    
72
                }).setVisible(target != null);
73

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

    
76
                        @Override
77
                        public void execute() {
78
                                GWT.log("EXECUTE TRASH:"+toCopy.getClass().getName());
79
                                if (toCopy instanceof RestResourceWrapper){
80
                                        trashFolder(((RestResourceWrapper) toCopy).getResource());
81
                                }
82
                                else if(toCopy instanceof List){
83
                                        List<FileResource> files = GSS.get().getFileList().getSelectedFiles();
84
                                    trashFiles(files);
85
                                }
86
                                hide();
87
                        }
88

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

    
92
                add(contextMenu);
93

    
94
        }
95

    
96
        private void copyFolder(final FolderResource target, FolderResource toCopy) {
97
                String atarget = target.getUri();
98
                atarget = atarget.endsWith("/") ? atarget : atarget + '/';
99
                atarget = atarget + toCopy.getName();
100
                PostCommand cf = new PostCommand(toCopy.getUri() + "?copy=" + atarget, "", 200) {
101

    
102
                        @Override
103
                        public void onComplete() {
104
                                        GSS.get().getTreeView().updateNodeChildren(new RestResourceWrapper(target));
105
                                                                GSS.get().getStatusPanel().updateStats();
106
                        }
107

    
108
                        @Override
109
                        public void onError(Throwable t) {
110
                                GWT.log("", t);
111
                                if (t instanceof RestException) {
112
                                        int statusCode = ((RestException) t).getHttpStatusCode();
113
                                        if (statusCode == 405)
114
                                                GSS.get().displayError("You don't have the necessary permissions");
115

    
116
                                        else if (statusCode == 409)
117
                                                GSS.get().displayError("A folder with the same name already exists");
118
                                        else if (statusCode == 413)
119
                                                GSS.get().displayError("Your quota has been exceeded");
120
                                        else
121
                                                GSS.get().displayError("Unable to copy folder:" + ((RestException)t).getHttpStatusText());
122
                                } else
123
                                        GSS.get().displayError("System error copying folder:" + t.getMessage());
124
                        }
125
                };
126
                DeferredCommand.addCommand(cf);
127
        }
128

    
129
        private void moveFolder(final FolderResource target, final FolderResource toCopy) {
130
                String atarget = target.getUri();
131
                atarget = atarget.endsWith("/") ? atarget : atarget + '/';
132
                atarget = atarget + toCopy.getName();
133

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

    
136
                        @Override
137
                        public void onComplete() {
138
                                GWT.log("[MOVE]"+target.getUri()+"   "+ toCopy.getParentURI());
139
                                GSS.get().getTreeView().updateNodeChildren(new RestResourceWrapper(target));
140
                                                        GSS.get().getTreeView().updateNodeChildrenForRemove(toCopy.getParentURI());
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 copyFiles(final FolderResource ftarget, List<FileResource> files) {
166
                List<String> fileIds = new ArrayList<String>();
167
                String target = ftarget.getUri();
168
                target = target.endsWith("/") ? target : target + '/';
169
                for (FileResource fileResource : files) {
170
                        String fileTarget = target + URL.encodeComponent(fileResource.getName());
171
                        fileIds.add(fileResource.getUri() + "?copy=" + fileTarget);
172
                }
173
                int index = 0;
174
                executeCopyOrMoveFiles(index, fileIds);
175

    
176
        }
177

    
178
        private void moveFiles(final FolderResource ftarget, List<FileResource> files) {
179
                List<String> fileIds = new ArrayList<String>();
180
                String target = ftarget.getUri();
181
                target = target.endsWith("/") ? target : target + '/';
182
                for (FileResource fileResource : files) {
183
                        String fileTarget = target + URL.encodeComponent(fileResource.getName());
184
                        fileIds.add(fileResource.getUri() + "?move=" + fileTarget);
185
                }
186
                int index = 0;
187
                executeCopyOrMoveFiles(index, fileIds);
188

    
189
        }
190

    
191
        private void trashFolder(final FolderResource folder){
192
                PostCommand tot = new PostCommand(folder.getUri()+"?trash=","",200){
193

    
194
                        @Override
195
                        public void onComplete() {
196
                                GSS.get().getTreeView().updateNodeChildrenForRemove(folder.getParentURI());
197
                                GSS.get().getTreeView().updateTrashNode();
198
                                /*for(TreeItem item : items)
199
                                        GSS.get().getFolders().updateFolder((DnDTreeItem) item);
200
                                GSS.get().getFolders().update(GSS.get().getFolders().getTrashItem());
201
                               
202
                                GSS.get().showFileList(true);
203
                                */
204
                        }
205

    
206
                        @Override
207
                        public void onError(Throwable t) {
208
                                GWT.log("", t);
209
                                if(t instanceof RestException){
210
                                        int statusCode = ((RestException)t).getHttpStatusCode();
211
                                        if(statusCode == 405)
212
                                                GSS.get().displayError("You don't have the necessary permissions");
213
                                        else if(statusCode == 404)
214
                                                GSS.get().displayError("Folder does not exist");
215
                                        else
216
                                                GSS.get().displayError("Unable to trash folder:"+((RestException)t).getHttpStatusText());
217
                                }
218
                                else
219
                                        GSS.get().displayError("System error trashing folder:"+t.getMessage());
220
                        }
221
                };
222
                DeferredCommand.addCommand(tot);
223
        }
224

    
225
        private void trashFiles(List<FileResource> files){
226
                final List<String> fileIds = new ArrayList<String>();
227
                for(FileResource f : files)
228
                        fileIds.add(f.getUri()+"?trash=");
229
                MultiplePostCommand tot = new MultiplePostCommand(fileIds.toArray(new String[0]),200){
230

    
231
                        @Override
232
                        public void onComplete() {
233
                                GSS.get().showFileList(true);
234
                        }
235

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

    
255

    
256
        private void executeCopyOrMoveFiles(final int index, final List<String> paths) {
257
                if (index >= paths.size()) {
258
                        GSS.get().showFileList(true);
259
                        GSS.get().getStatusPanel().updateStats();
260
                        return;
261
                }
262
                PostCommand cf = new PostCommand(paths.get(index), "", 200) {
263

    
264
                        @Override
265
                        public void onComplete() {
266
                                executeCopyOrMoveFiles(index + 1, paths);
267
                        }
268

    
269
                        @Override
270
                        public void onError(Throwable t) {
271
                                GWT.log("", t);
272
                                if (t instanceof RestException) {
273
                                        int statusCode = ((RestException) t).getHttpStatusCode();
274
                                        if (statusCode == 405)
275
                                                GSS.get().displayError("You don't have the necessary permissions");
276
                                        else if (statusCode == 404)
277
                                                GSS.get().displayError("File not found");
278
                                        else if (statusCode == 409)
279
                                                GSS.get().displayError("A file with the same name already exists");
280
                                        else if (statusCode == 413)
281
                                                GSS.get().displayError("Your quota has been exceeded");
282
                                        else
283
                                                GSS.get().displayError("Unable to copy file:" + ((RestException)t).getHttpStatusText());
284
                                } else
285
                                        GSS.get().displayError("System error copying file:" + t.getMessage());
286

    
287
                        }
288
                };
289
                DeferredCommand.addCommand(cf);
290
        }
291

    
292
}