Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (14.6 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 path = folder.getUri() + "/" + name;
240
        PutRequest createFolder = new PutRequest(app.getApiPath(), app.getUsername(), path) {
241
            @Override
242
            public void onSuccess(Resource result) {
243
                app.updateFolder(folder);
244
            }
245

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

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

    
274
        }
275

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

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

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