Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / FolderPropertiesDialog.java @ 4fe76337

History | View | Annotate | Download (15 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 com.google.gwt.core.client.Scheduler;
38
import com.google.gwt.event.dom.client.KeyDownEvent;
39
import com.google.gwt.user.client.Command;
40
import com.google.gwt.user.client.Event;
41
import gr.grnet.pithos.web.client.foldertree.File;
42
import gr.grnet.pithos.web.client.foldertree.Folder;
43
import gr.grnet.pithos.web.client.foldertree.Resource;
44
import gr.grnet.pithos.web.client.rest.PostCommand;
45
import gr.grnet.pithos.web.client.rest.PutRequest;
46
import gr.grnet.pithos.web.client.rest.RestException;
47
import gr.grnet.pithos.web.client.rest.resource.RestResourceWrapper;
48

    
49
import com.google.gwt.core.client.GWT;
50
import com.google.gwt.dom.client.NativeEvent;
51
import com.google.gwt.event.dom.client.ClickEvent;
52
import com.google.gwt.event.dom.client.ClickHandler;
53
import com.google.gwt.event.dom.client.KeyCodes;
54
import com.google.gwt.http.client.URL;
55
import com.google.gwt.i18n.client.DateTimeFormat;
56
import com.google.gwt.user.client.DeferredCommand;
57
import com.google.gwt.user.client.Event.NativePreviewEvent;
58
import com.google.gwt.user.client.ui.Button;
59
import com.google.gwt.user.client.ui.CheckBox;
60
import com.google.gwt.user.client.ui.DecoratedTabPanel;
61
import com.google.gwt.user.client.ui.DialogBox;
62
import com.google.gwt.user.client.ui.FlexTable;
63
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
64
import com.google.gwt.user.client.ui.HorizontalPanel;
65
import com.google.gwt.user.client.ui.Label;
66
import com.google.gwt.user.client.ui.TabPanel;
67
import com.google.gwt.user.client.ui.TextBox;
68
import com.google.gwt.user.client.ui.VerticalPanel;
69
import java.util.Iterator;
70

    
71
/**
72
 * The 'Folder properties' dialog box implementation.
73
 */
74
public class FolderPropertiesDialog extends DialogBox {
75

    
76
    private GSS app;
77

    
78
        private CheckBox readForAll;
79

    
80
        /**
81
         * The widget that holds the folderName of the folder.
82
         */
83
        private TextBox folderName = new TextBox();
84

    
85
        /**
86
         * A flag that denotes whether the dialog will be used to create or modify a
87
         * folder.
88
         */
89
        private final boolean create;
90

    
91
        final Folder folder;
92

    
93
        final TabPanel inner;
94

    
95
        /**
96
         * The widget's constructor.
97
         */
98
        public FolderPropertiesDialog(GSS app, boolean _create,  Folder selected) {
99
        this.app = app;
100
                setAnimationEnabled(true);
101

    
102
                // Enable IE selection for the dialog (must disable it upon closing it)
103
                app.enableIESelection();
104

    
105
                create = _create;
106
                
107
                folder = selected;
108

    
109
                // Use this opportunity to set the dialog's caption.
110
                if (create)
111
                        setText("Create folder");
112
                else
113
                        setText("Folder properties");
114

    
115
                // Outer contains inner and buttons
116
                VerticalPanel outer = new VerticalPanel();
117
                // Inner contains generalPanel and permPanel
118
                inner = new DecoratedTabPanel();
119
                inner.setAnimationEnabled(true);
120
                VerticalPanel generalPanel = new VerticalPanel();
121
                VerticalPanel permPanel = new VerticalPanel();
122
                final HorizontalPanel permForAll = new HorizontalPanel();
123
                final HorizontalPanel pathPanel = new HorizontalPanel();
124
                HorizontalPanel buttons = new HorizontalPanel();
125
                HorizontalPanel permButtons = new HorizontalPanel();
126

    
127
                inner.add(generalPanel, "General");
128
                if (!create)
129
                        inner.add(permPanel, "Sharing");
130
                inner.selectTab(0);
131

    
132
                FlexTable generalTable = new FlexTable();
133
                generalTable.setText(0, 0, "Name");
134
                generalTable.setText(1, 0, "Parent");
135
                generalTable.setText(2, 0, "Creator");
136
                generalTable.setText(3, 0, "Last modified");
137
                folderName.setText(create ? "" : folder.getName());
138
        folderName.setReadOnly(folder.isContainer() && !create);
139
                generalTable.setWidget(0, 1, folderName);
140

    
141
                if (create)
142
                        generalTable.setText(1, 1, folder.getName());
143
                else
144
                        generalTable.setText(1, 1, folder.getPrefix());
145
                generalTable.setText(2, 1, "");
146
                DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
147
                if(folder.getLastModified() != null)
148
                        generalTable.setText(3, 1, formatter.format(folder.getLastModified()));
149
                generalTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
150
                generalTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
151
                generalTable.getFlexCellFormatter().setStyleName(2, 0, "props-labels");
152
                generalTable.getFlexCellFormatter().setStyleName(3, 0, "props-labels");
153
                generalTable.getFlexCellFormatter().setStyleName(0, 1, "props-values");
154
                generalTable.getFlexCellFormatter().setStyleName(1, 1, "props-values");
155
                generalTable.getFlexCellFormatter().setStyleName(2, 1, "props-values");
156
                generalTable.getFlexCellFormatter().setStyleName(3, 1, "props-values");
157
                generalTable.setCellSpacing(4);
158

    
159
                // Create the 'Create/Update' button, along with a listener that hides the dialog
160
                // when the button is clicked and quits the application.
161
                String okLabel;
162
                if (create)
163
                        okLabel = "Create";
164
                else
165
                        okLabel = "Update";
166
                Button ok = new Button(okLabel, new ClickHandler() {
167
                        @Override
168
                        public void onClick(ClickEvent event) {
169
                                createOrUpdateFolder();
170
                                closeDialog();
171
                        }
172
                });
173
                buttons.add(ok);
174
                buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
175
                // Create the 'Cancel' button, along with a listener that hides the
176
                // dialog
177
                // when the button is clicked.
178
                Button cancel = new Button("Cancel", new ClickHandler() {
179
                        @Override
180
                        public void onClick(ClickEvent event) {
181
                                closeDialog();
182
                        }
183
                });
184
                buttons.add(cancel);
185
                buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
186
                buttons.setSpacing(8);
187
                buttons.addStyleName("pithos-TabPanelBottom");
188

    
189
                generalPanel.add(generalTable);
190

    
191

    
192
                outer.add(inner);
193
                outer.add(buttons);
194
                outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
195
                outer.addStyleName("pithos-TabPanelBottom");
196

    
197
                setWidget(outer);
198

    
199
                /*if (create)
200
                        folderName.setFocus(true);
201
                else
202
                        ok.setFocus(true);*/
203
        }
204

    
205
        @Override
206
        public void center() {
207
                super.center();
208
                folderName.setFocus(true);
209
        }
210

    
211
        @Override
212
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
213
                super.onPreviewNativeEvent(preview);
214

    
215
                NativeEvent evt = preview.getNativeEvent();
216
                if (evt.getType().equals(KeyDownEvent.getType().getName()))
217
                        // Use the popup's key preview hooks to close the dialog when either
218
                        // enter or escape is pressed.
219
                        switch (evt.getKeyCode()) {
220
                                case KeyCodes.KEY_ENTER:
221
                                        closeDialog();
222
                                        createOrUpdateFolder();
223
                                        break;
224
                                case KeyCodes.KEY_ESCAPE:
225
                                        closeDialog();
226
                                        break;
227
                        }
228
        }
229

    
230

    
231
        /**
232
         * Enables IE selection prevention and hides the dialog
233
         * (we disable the prevention on creation of the dialog)
234
         */
235
        public void closeDialog() {
236
                app.preventIESelection();
237
                hide();
238
        }
239

    
240
        /**
241
         * Generate an RPC request to create a new folder.
242
         */
243
        private void createFolder() {
244
                String name = folderName.getText();
245
        String prefix = folder.getPrefix();
246
        String path = app.getApiPath() + app.getUsername() + "/" + folder.getContainer() + "/" + (prefix.length() == 0 ? "" : prefix +  "/") + name;
247
        PutRequest createFolder = new PutRequest(path) {
248
            @Override
249
            public void onSuccess(Resource result) {
250
                app.updateFolder(folder);
251
            }
252

    
253
            @Override
254
            public void onError(Throwable t) {
255
                                GWT.log("", t);
256
                                if (t instanceof RestException) {
257
                                        app.displayError("Unable to create folder:" + ((RestException) t).getHttpStatusText());
258
                                }
259
                                else
260
                                        app.displayError("System error creating folder:" + t.getMessage());
261
            }
262
        };
263
        createFolder.setHeader("X-Auth-Token", app.getToken());
264
        createFolder.setHeader("Accept", "*/*");
265
        createFolder.setHeader("Content-Length", "0");
266
        createFolder.setHeader("Content-Type", "application/folder");
267
        Scheduler.get().scheduleDeferred(createFolder);
268
        }
269

    
270
        /**
271
         * Upon closing the dialog by clicking OK or pressing ENTER this method does
272
         * the actual work of modifying folder properties or creating a new Folder
273
         * depending on the value of the create field
274
         */
275
        private void createOrUpdateFolder() {
276
                if (create)
277
                        createFolder();
278
                else
279
                        updateFolder();
280

    
281
        }
282

    
283
        private void updateFolder() {
284
//                permList.updatePermissionsAccordingToInput();
285
//                Set<PermissionHolder> perms = permList.getPermissions();
286
//                JSONObject json = new JSONObject();
287
//                if(!folder.getName().equals(folderName.getText()))
288
//                        json.put("name", new JSONString(folderName.getText()));
289
//                //only update the read for all perm if the user is the owner
290
//                if (readForAll.getValue() != folder.isReadForAll())
291
//                        if (folder.getOwner().equals(app.getCurrentUserResource().getUsername()))
292
//                                json.put("readForAll", JSONBoolean.getInstance(readForAll.getValue()));
293
//                if (permList.hasChanges()) {
294
//                        JSONArray perma = new JSONArray();
295
//                        int i=0;
296
//                        for(PermissionHolder p : perms){
297
//                                JSONObject po = new JSONObject();
298
//                                if(p.getUser() != null)
299
//                                        po.put("user", new JSONString(p.getUser()));
300
//                                if(p.getGroup() != null)
301
//                                        po.put("group", new JSONString(p.getGroup()));
302
//                                po.put("read", JSONBoolean.getInstance(p.isRead()));
303
//                                po.put("write", JSONBoolean.getInstance(p.isWrite()));
304
//                                po.put("modifyACL", JSONBoolean.getInstance(p.isModifyACL()));
305
//                                perma.set(i,po);
306
//                                i++;
307
//                        }
308
//                        json.put("permissions", perma);
309
//                        GWT.log(json.toString(), null);
310
//                }
311
//                PostCommand ep = new PostCommand(folder.getUri()+"?update=", json.toString(), 200){
312
//
313
//                        @Override
314
//                        public void onComplete() {
315
//                                //TODO:CELLTREE
316
//
317
//                                if(getPostBody() != null && !"".equals(getPostBody().trim())){
318
//
319
//
320
//                                        FolderResource fres = ((RestResourceWrapper) app.getTreeView().getSelection()).getResource();
321
//                                        String initialPath = fres.getUri();
322
//                                        String newPath =  getPostBody().trim();
323
//                                        fres.setUri(newPath);
324
//                                        ((RestResourceWrapper) app.getTreeView().getSelection()).getResource().setUri(newPath);
325
//                                        ((RestResourceWrapper) app.getTreeView().getSelection()).setUri(newPath);
326
//                                        app.getTreeView().updateNodeChildren(fres.getParentURI());
327
//                                        if (permList.hasChanges()) {
328
//                                                app.getTreeView().updateMySharedNode();
329
//                                        }
330
//                                        /*
331
//                                        if(folderItem.getParentItem() != null && ((DnDTreeItem)folderItem.getParentItem()).getFolderResource() != null){
332
//                                                ((DnDTreeItem)folderItem.getParentItem()).getFolderResource().removeSubfolderPath(initialPath);
333
//                                                ((DnDTreeItem)folderItem.getParentItem()).getFolderResource().getSubfolderPaths().add(newPath);
334
//                                        }*/
335
//                                }
336
//                                //app.getFolders().updateFolder( (DnDTreeItem) app.getFolders().getCurrent());
337
//
338
//                                app.get().showFileList(true);
339
//                        }
340
//
341
//                        @Override
342
//                        public void onError(Throwable t) {
343
//                                GWT.log("", t);
344
//                                if(t instanceof RestException){
345
//                                        int statusCode = ((RestException)t).getHttpStatusCode();
346
//                                        if(statusCode == 405)
347
//                                                app.displayError("You don't have the necessary permissions or" +
348
//                                                                " a folder with same name already exists");
349
//                                        else if(statusCode == 404)
350
//                                                app.displayError("Resource not found, or user specified in sharing does not exist");
351
//                                        else
352
//                                                app.displayError("Unable to update folder: "+((RestException)t).getHttpStatusText());
353
//                                }
354
//                                else
355
//                                        app.displayError("System error moifying file: "+t.getMessage());
356
//                                //TODO:CELLTREE
357
//                                //app.getFolders().updateFolder( (DnDTreeItem) app.getFolders().getCurrent());
358
//                        }
359
//                };
360
//                DeferredCommand.addCommand(ep);
361
        final String newName = folderName.getText();
362
        if (!folder.isContainer() && !folder.getName().equals(newName)) {
363
            final String path = app.getApiPath() + app.getUsername() + folder.getParent().getUri() + "/" + newName;
364
            PutRequest newFolder = new PutRequest(path) {
365
                @Override
366
                public void onSuccess(Resource result) {
367
                    Iterator<File> iter = folder.getFiles().iterator();
368
                    app.copyFiles(iter, folder.getParent().getUri() + "/" + newName, new Command() {
369
                        @Override
370
                        public void execute() {
371
                            Iterator<Folder> iterf = folder.getSubfolders().iterator();
372
                            app.copySubfolders(iterf, folder.getParent().getUri() + "/" + newName, new Command() {
373
                                @Override
374
                                public void execute() {
375
                                    app.deleteFolder(folder);
376
                                    app.updateFolder(folder.getParent());
377
                                }
378
                            });
379
                        }
380
                    });
381
                }
382

    
383
                @Override
384
                public void onError(Throwable t) {
385
                    GWT.log("", t);
386
                    if(t instanceof RestException){
387
                        app.displayError("Unable to update folder: " + ((RestException) t).getHttpStatusText());
388
                    }
389
                    else
390
                        app.displayError("System error modifying folder: " + t.getMessage());
391
                }
392
            };
393
            newFolder.setHeader("X-Auth-Token", app.getToken());
394
            newFolder.setHeader("Content-Type", "application/folder");
395
            newFolder.setHeader("Accept", "*/*");
396
            newFolder.setHeader("Content-Length", "0");
397
            Scheduler.get().scheduleDeferred(newFolder);
398
        }
399
        }
400

    
401
        public void selectTab(int _tab) {
402
                inner.selectTab(_tab);
403
        }
404
}