Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / FolderPropertiesDialog.java @ 749068ba

History | View | Annotate | Download (14.7 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 gr.grnet.pithos.web.client.foldertree.File;
41
import gr.grnet.pithos.web.client.foldertree.Folder;
42
import gr.grnet.pithos.web.client.foldertree.Resource;
43
import gr.grnet.pithos.web.client.rest.PutRequest;
44
import gr.grnet.pithos.web.client.rest.RestException;
45

    
46
import com.google.gwt.core.client.GWT;
47
import com.google.gwt.dom.client.NativeEvent;
48
import com.google.gwt.event.dom.client.ClickEvent;
49
import com.google.gwt.event.dom.client.ClickHandler;
50
import com.google.gwt.event.dom.client.KeyCodes;
51
import com.google.gwt.i18n.client.DateTimeFormat;
52
import com.google.gwt.user.client.Event.NativePreviewEvent;
53
import com.google.gwt.user.client.ui.Button;
54
import com.google.gwt.user.client.ui.CheckBox;
55
import com.google.gwt.user.client.ui.DecoratedTabPanel;
56
import com.google.gwt.user.client.ui.DialogBox;
57
import com.google.gwt.user.client.ui.FlexTable;
58
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
59
import com.google.gwt.user.client.ui.HorizontalPanel;
60
import com.google.gwt.user.client.ui.TabPanel;
61
import com.google.gwt.user.client.ui.TextBox;
62
import com.google.gwt.user.client.ui.VerticalPanel;
63
import java.util.Iterator;
64

    
65
/**
66
 * The 'Folder properties' dialog box implementation.
67
 */
68
public class FolderPropertiesDialog extends DialogBox {
69

    
70
    private Pithos app;
71

    
72
        private CheckBox readForAll;
73

    
74
        /**
75
         * The widget that holds the folderName of the folder.
76
         */
77
        private TextBox folderName = new TextBox();
78

    
79
        /**
80
         * A flag that denotes whether the dialog will be used to create or modify a
81
         * folder.
82
         */
83
        private final boolean create;
84

    
85
        final Folder folder;
86

    
87
        final TabPanel inner;
88

    
89
        /**
90
         * The widget's constructor.
91
         */
92
        public FolderPropertiesDialog(Pithos app, boolean _create,  Folder selected) {
93
        this.app = app;
94
                setAnimationEnabled(true);
95

    
96
                // Enable IE selection for the dialog (must disable it upon closing it)
97
                app.enableIESelection();
98

    
99
                create = _create;
100
                
101
                folder = selected;
102

    
103
                // Use this opportunity to set the dialog's caption.
104
                if (create)
105
                        setText("Create folder");
106
                else
107
                        setText("Folder properties");
108

    
109
                // Outer contains inner and buttons
110
                VerticalPanel outer = new VerticalPanel();
111
                // Inner contains generalPanel and permPanel
112
                inner = new DecoratedTabPanel();
113
                inner.setAnimationEnabled(true);
114
                VerticalPanel generalPanel = new VerticalPanel();
115
                VerticalPanel permPanel = new VerticalPanel();
116
                final HorizontalPanel permForAll = new HorizontalPanel();
117
                final HorizontalPanel pathPanel = new HorizontalPanel();
118
                HorizontalPanel buttons = new HorizontalPanel();
119
                HorizontalPanel permButtons = new HorizontalPanel();
120

    
121
                inner.add(generalPanel, "General");
122
                if (!create)
123
                        inner.add(permPanel, "Sharing");
124
                inner.selectTab(0);
125

    
126
                FlexTable generalTable = new FlexTable();
127
                generalTable.setText(0, 0, "Name");
128
                generalTable.setText(1, 0, "Parent");
129
                generalTable.setText(2, 0, "Creator");
130
                generalTable.setText(3, 0, "Last modified");
131
                folderName.setText(create ? "" : folder.getName());
132
        folderName.setReadOnly(folder.isContainer() && !create);
133
                generalTable.setWidget(0, 1, folderName);
134

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

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

    
183
                generalPanel.add(generalTable);
184

    
185

    
186
                outer.add(inner);
187
                outer.add(buttons);
188
                outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
189
                outer.addStyleName("pithos-TabPanelBottom");
190

    
191
                setWidget(outer);
192

    
193
                /*if (create)
194
                        folderName.setFocus(true);
195
                else
196
                        ok.setFocus(true);*/
197
        }
198

    
199
        @Override
200
        public void center() {
201
                super.center();
202
                folderName.setFocus(true);
203
        }
204

    
205
        @Override
206
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
207
                super.onPreviewNativeEvent(preview);
208

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

    
224

    
225
        /**
226
         * Enables IE selection prevention and hides the dialog
227
         * (we disable the prevention on creation of the dialog)
228
         */
229
        public void closeDialog() {
230
                app.preventIESelection();
231
                hide();
232
        }
233

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

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

    
264
        /**
265
         * Upon closing the dialog by clicking OK or pressing ENTER this method does
266
         * the actual work of modifying folder properties or creating a new Folder
267
         * depending on the value of the create field
268
         */
269
        private void createOrUpdateFolder() {
270
                if (create)
271
                        createFolder();
272
                else
273
                        updateFolder();
274

    
275
        }
276

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

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

    
395
        public void selectTab(int _tab) {
396
                inner.selectTab(_tab);
397
        }
398
}