Statistics
| Branch: | Tag: | Revision:

root / src / org / gss_project / gss / web / client / FolderPropertiesDialog.java @ 1206:292dec4eae08

History | View | Annotate | Download (17.2 kB)

1
/*
2
 * Copyright 2007, 2008, 2009, 2010 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 org.gss_project.gss.web.client;
20

    
21
import org.gss_project.gss.web.client.FilePropertiesDialog.Images;
22
import org.gss_project.gss.web.client.rest.PostCommand;
23
import org.gss_project.gss.web.client.rest.RestException;
24
import org.gss_project.gss.web.client.rest.resource.FolderResource;
25
import org.gss_project.gss.web.client.rest.resource.GroupResource;
26
import org.gss_project.gss.web.client.rest.resource.PermissionHolder;
27
import org.gss_project.gss.web.client.rest.resource.RestResourceWrapper;
28

    
29
import java.util.List;
30
import java.util.Set;
31

    
32
import com.google.gwt.core.client.GWT;
33
import com.google.gwt.dom.client.NativeEvent;
34
import com.google.gwt.event.dom.client.ChangeEvent;
35
import com.google.gwt.event.dom.client.ChangeHandler;
36
import com.google.gwt.event.dom.client.ClickEvent;
37
import com.google.gwt.event.dom.client.ClickHandler;
38
import com.google.gwt.event.dom.client.KeyCodes;
39
import com.google.gwt.http.client.URL;
40
import com.google.gwt.i18n.client.DateTimeFormat;
41
import com.google.gwt.json.client.JSONArray;
42
import com.google.gwt.json.client.JSONBoolean;
43
import com.google.gwt.json.client.JSONObject;
44
import com.google.gwt.json.client.JSONString;
45
import com.google.gwt.user.client.DeferredCommand;
46
import com.google.gwt.user.client.Event.NativePreviewEvent;
47
import com.google.gwt.user.client.ui.Button;
48
import com.google.gwt.user.client.ui.CheckBox;
49
import com.google.gwt.user.client.ui.DecoratedTabPanel;
50
import com.google.gwt.user.client.ui.DialogBox;
51
import com.google.gwt.user.client.ui.FlexTable;
52
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
53
import com.google.gwt.user.client.ui.HorizontalPanel;
54
import com.google.gwt.user.client.ui.Label;
55
import com.google.gwt.user.client.ui.TabPanel;
56
import com.google.gwt.user.client.ui.TextBox;
57
import com.google.gwt.user.client.ui.VerticalPanel;
58

    
59
/**
60
 * The 'Folder properties' dialog box implementation.
61
 */
62
public class FolderPropertiesDialog extends DialogBox {
63

    
64
        private List<GroupResource> groups = null;
65

    
66
        final PermissionsList permList;
67

    
68
        private CheckBox readForAll;
69

    
70
        /**
71
         * The widget that holds the folderName of the folder.
72
         */
73
        private TextBox folderName = new TextBox();
74

    
75
        /**
76
         * A flag that denotes whether the dialog will be used to create or modify a
77
         * folder.
78
         */
79
        private final boolean create;
80

    
81
        final FolderResource folder;
82

    
83
        final TabPanel inner;
84

    
85
        /**
86
         * The widget's constructor.
87
         *
88
         * @param images the image icons from the file properties dialog
89
         * @param _create true if the dialog is displayed for creating a new
90
         *            sub-folder of the selected folder, false if it is displayed
91
         *            for modifying the selected folder
92
         */
93
        public FolderPropertiesDialog(Images images, boolean _create,  final List<GroupResource> _groups) {
94
                setAnimationEnabled(true);
95

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

    
99
                create = _create;
100
                
101
                folder = ((RestResourceWrapper) GSS.get().getTreeView().getSelection()).getResource();
102
                permList = new PermissionsList(images, folder.getPermissions(), folder.getOwner());
103
                groups = _groups;
104

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

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

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

    
128
                final Label folderNameNote = new Label("Please note that slashes ('/') are not allowed in folder names.", true);
129
                folderNameNote.setVisible(false);
130
                folderNameNote.setStylePrimaryName("gss-readForAllNote");
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.getElement().setId("folderPropertiesDialog.textBox.name");
139
                generalTable.setWidget(0, 1, folderName);
140
                folderName.addChangeHandler(new ChangeHandler() {
141
                        
142
                        @Override
143
                        public void onChange(ChangeEvent event) {
144
                                if(folderName.getText().contains("/"))
145
                                        folderNameNote.setVisible(true);
146
                                else
147
                                        folderNameNote.setVisible(false);                                
148
                                
149
                        }
150
                });
151

    
152

    
153
                if (create)
154
                        generalTable.setText(1, 1, folder.getName());
155
                else if(folder.getParentName() == null)
156
                        generalTable.setText(1, 1, "-");
157
                else
158
                        generalTable.setText(1, 1, folder.getParentName());
159
                generalTable.setWidget(0, 2, folderNameNote);
160
                generalTable.setText(2, 1, folder.getOwner());
161
                DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
162
                if(folder.getModificationDate() != null)
163
                        generalTable.setText(3, 1, formatter.format(folder.getModificationDate()));
164
                generalTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
165
                generalTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
166
                generalTable.getFlexCellFormatter().setStyleName(2, 0, "props-labels");
167
                generalTable.getFlexCellFormatter().setStyleName(3, 0, "props-labels");
168
                generalTable.getFlexCellFormatter().setStyleName(0, 1, "props-values");
169
                generalTable.getFlexCellFormatter().setStyleName(1, 1, "props-values");
170
                generalTable.getFlexCellFormatter().setStyleName(2, 1, "props-values");
171
                generalTable.getFlexCellFormatter().setStyleName(3, 1, "props-values");
172
                generalTable.setCellSpacing(4);
173

    
174
                // Create the 'Create/Update' button, along with a listener that hides the dialog
175
                // when the button is clicked and quits the application.
176
                String okLabel;
177
                if (create)
178
                        okLabel = "Create";
179
                else
180
                        okLabel = "Update";
181
                Button ok = new Button(okLabel, new ClickHandler() {
182
                        @Override
183
                        public void onClick(ClickEvent event) {                                
184
                                if(folderName.getText().contains("/"))
185
                                        folderNameNote.setVisible(true);
186
                                else {
187
                                        folderNameNote.setVisible(false);
188
                                        createOrUpdateFolder();
189
                                        closeDialog();
190
                                }
191

    
192
                        }
193
                });
194
                ok.getElement().setId("folderPropertiesDialog.button.ok");
195
                buttons.add(ok);
196
                buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
197
                // Create the 'Cancel' button, along with a listener that hides the
198
                // dialog
199
                // when the button is clicked.
200
                Button cancel = new Button("Cancel", new ClickHandler() {
201
                        @Override
202
                        public void onClick(ClickEvent event) {
203
                                closeDialog();
204
                        }
205
                });
206
                cancel.getElement().setId("folderPropertiesDialog.button.cancel");
207
                buttons.add(cancel);
208
                buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
209
                buttons.setSpacing(8);
210
                buttons.addStyleName("gss-TabPanelBottom");
211

    
212
                Button add = new Button("Add Group", new ClickHandler() {
213
                        @Override
214
                        public void onClick(ClickEvent event) {
215
                                PermissionsAddDialog dlg = new PermissionsAddDialog(groups, permList, false);
216
                                dlg.center();
217
                        }
218
                });
219
                add.getElement().setId("folderPropertiesDialog.button.addGroup");
220
                permButtons.add(add);
221
                permButtons.setCellHorizontalAlignment(add, HasHorizontalAlignment.ALIGN_CENTER);
222

    
223
                Button addUser = new Button("Add User", new ClickHandler() {
224
                        @Override
225
                        public void onClick(ClickEvent event) {
226
                                PermissionsAddDialog dlg = new PermissionsAddDialog(groups, permList, true);
227
                                dlg.center();
228
                        }
229
                });
230
                addUser.getElement().setId("folderPropertiesDialog.button.addUser");
231
                permButtons.add(addUser);
232
                permButtons.setCellHorizontalAlignment(addUser, HasHorizontalAlignment.ALIGN_CENTER);
233

    
234
                permButtons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
235
                permButtons.setSpacing(8);
236
                permButtons.addStyleName("gss-TabPanelBottom");
237

    
238
                final Label readForAllNote = new Label("When this option is enabled, the folder will be readable" +
239
                                        " by everyone. By checking this option, you are certifying that you have the right to " +
240
                                        "distribute this folder's contents and that it does not violate the Terms of Use.", true);
241
                readForAllNote.setVisible(false);
242
                readForAllNote.setStylePrimaryName("gss-readForAllNote");
243

    
244
                readForAll = new CheckBox();
245
                readForAll.getElement().setId("folderPropertiesDialog.checkBox.public");
246
                readForAll.setValue(folder.isReadForAll());
247
                readForAll.addClickHandler(new ClickHandler() {
248
                        @Override
249
                        public void onClick(ClickEvent event) {
250
                                readForAllNote.setVisible(readForAll.getValue());
251
                        }
252

    
253
                });
254

    
255
                generalPanel.add(generalTable);
256
                permPanel.add(permList);
257
                permPanel.add(permButtons);
258

    
259
                // Only show the read for all permission if the user is the owner.
260
                if (folder.getOwner().equals(GSS.get().getCurrentUserResource().getUsername())) {
261
                        permForAll.add(new Label("Public"));
262
                        permForAll.add(readForAll);
263
                        permForAll.setSpacing(8);
264
                        permForAll.addStyleName("gss-TabPanelBottom");
265
                        permForAll.add(readForAllNote);
266
                        permPanel.add(permForAll);
267
                }
268
                TextBox path = new TextBox();
269
                path.getElement().setId("folderPropertiesDialog.textBox.link");
270
                path.setWidth("100%");
271
                path.addClickHandler(new ClickHandler() {
272
                        @Override
273
                        public void onClick(ClickEvent event) {
274
                                GSS.enableIESelection();
275
                                ((TextBox) event.getSource()).selectAll();
276
                                GSS.preventIESelection();
277
                        }
278

    
279
                });
280
                path.setText(folder.getUri());
281
                path.setTitle("Use this link for sharing the folder via e-mail, IM, etc. (crtl-C/cmd-C to copy to system clipboard)");
282
                path.setWidth("100%");
283
                path.setReadOnly(true);
284
                pathPanel.setWidth("100%");
285
                pathPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
286
                pathPanel.add(new Label("Link"));
287
                pathPanel.setSpacing(8);
288
                pathPanel.addStyleName("gss-TabPanelBottom");
289
                pathPanel.add(path);
290
                permPanel.add(pathPanel);
291

    
292
                outer.add(inner);
293
                outer.add(buttons);
294
                outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
295
                outer.addStyleName("gss-TabPanelBottom");
296

    
297
                setWidget(outer);
298

    
299
                /*if (create)
300
                        folderName.setFocus(true);
301
                else
302
                        ok.setFocus(true);*/
303
        }
304

    
305
        @Override
306
        public void center() {
307
                super.center();
308
                folderName.setFocus(true);
309
        }
310

    
311
        @Override
312
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
313
                super.onPreviewNativeEvent(preview);
314

    
315
                NativeEvent evt = preview.getNativeEvent();
316
                if (evt.getType().equals("keydown"))
317
                        // Use the popup's key preview hooks to close the dialog when either
318
                        // enter or escape is pressed.
319
                        switch (evt.getKeyCode()) {
320
                                case KeyCodes.KEY_ENTER:
321
                                        closeDialog();
322
                                        createOrUpdateFolder();
323
                                        break;
324
                                case KeyCodes.KEY_ESCAPE:
325
                                        closeDialog();
326
                                        break;
327
                        }
328
        }
329

    
330

    
331
        /**
332
         * Enables IE selection prevention and hides the dialog
333
         * (we disable the prevention on creation of the dialog)
334
         */
335
        public void closeDialog() {
336
                GSS.preventIESelection();
337
                hide();
338
        }
339

    
340
        /**
341
         * Generate an RPC request to create a new folder.
342
         *
343
         * @param userId the ID of the user whose namespace will be searched for
344
         *            folders
345
         * @param _folderName the name of the folder to create
346
         */
347
        private void createFolder() {
348
                String name = folderName.getText();
349
                if (!GSS.isValidResourceName(name)) {
350
                        GSS.get().displayError("The folder name '" + name + "' is invalid");
351
                        return;
352
                }
353
                PostCommand ep = new PostCommand(folder.getUri() + "?new=" +
354
                                URL.encodeComponent(name), "", 201) {
355

    
356
                        @Override
357
                        public void onComplete() {
358
                                //TODO:CELLTREE
359
                                if(folder.getUri().equals(GSS.get().getTreeView().getMyFolders().getUri())){
360
                                        GSS.get().getTreeView().updateRootNode();
361
                                }
362
                                else
363
                                        GSS.get().getTreeView().updateNodeChildren((RestResourceWrapper) GSS.get().getTreeView().getSelection());
364
                                //GSS.get().getFolders().updateFolder((DnDTreeItem) GSS.get().getFolders().getCurrent());
365
                        }
366

    
367
                        @Override
368
                        public void onError(Throwable t) {
369
                                GWT.log("", t);
370
                                if(t instanceof RestException){
371
                                        int statusCode = ((RestException)t).getHttpStatusCode();
372
                                        if(statusCode == 405)
373
                                                GSS.get().displayError("You don't have the necessary" +
374
                                                                " permissions or a folder with same name " +
375
                                                                "already exists");
376
                                        else if(statusCode == 404)
377
                                                GSS.get().displayError("Resource not found");
378
                                        else
379
                                                GSS.get().displayError("Unable to create folder:" +
380
                                                                ((RestException)t).getHttpStatusText());
381
                                }
382
                                else
383
                                        GSS.get().displayError("System error creating folder:" +
384
                                                        t.getMessage());
385
                        }
386
                };
387
                DeferredCommand.addCommand(ep);
388

    
389
        }
390

    
391
        /**
392
         * Upon closing the dialog by clicking OK or pressing ENTER this method does
393
         * the actual work of modifying folder properties or creating a new Folder
394
         * depending on the value of the create field
395
         *
396
         * @param userId
397
         */
398
        private void createOrUpdateFolder() {
399
                if (create)
400
                        createFolder();
401
                else
402
                        updateFolder();
403

    
404
        }
405

    
406
        private void updateFolder() {
407
                permList.updatePermissionsAccordingToInput();
408
                Set<PermissionHolder> perms = permList.getPermissions();
409
                JSONObject json = new JSONObject();
410
                if(!folder.getName().equals(folderName.getText()))
411
                        json.put("name", new JSONString(folderName.getText()));
412
                //only update the read for all perm if the user is the owner
413
                if (readForAll.getValue() != folder.isReadForAll())
414
                        if (folder.getOwner().equals(GSS.get().getCurrentUserResource().getUsername()))
415
                                json.put("readForAll", JSONBoolean.getInstance(readForAll.getValue()));
416
                if (permList.hasChanges()) {
417
                        JSONArray perma = new JSONArray();
418
                        int i=0;
419
                        for(PermissionHolder p : perms){
420
                                JSONObject po = new JSONObject();
421
                                if(p.getUser() != null)
422
                                        po.put("user", new JSONString(p.getUser()));
423
                                if(p.getGroup() != null)
424
                                        po.put("group", new JSONString(p.getGroup()));
425
                                po.put("read", JSONBoolean.getInstance(p.isRead()));
426
                                po.put("write", JSONBoolean.getInstance(p.isWrite()));
427
                                po.put("modifyACL", JSONBoolean.getInstance(p.isModifyACL()));
428
                                perma.set(i,po);
429
                                i++;
430
                        }
431
                        json.put("permissions", perma);
432
                        GWT.log(json.toString(), null);
433
                }
434
                PostCommand ep = new PostCommand(folder.getUri()+"?update=", json.toString(), 200){
435

    
436
                        @Override
437
                        public void onComplete() {
438
                                //TODO:CELLTREE
439
                                
440
                                if(getPostBody() != null && !"".equals(getPostBody().trim())){
441
                                        
442
                                        
443
                                        FolderResource fres = ((RestResourceWrapper) GSS.get().getTreeView().getSelection()).getResource();
444
                                        String initialPath = fres.getUri();
445
                                        String newPath =  getPostBody().trim();
446
                                        fres.setUri(newPath);
447
                                        ((RestResourceWrapper) GSS.get().getTreeView().getSelection()).getResource().setUri(newPath);
448
                                        ((RestResourceWrapper) GSS.get().getTreeView().getSelection()).setUri(newPath);
449
                                        GSS.get().getTreeView().updateNodeChildren(fres.getParentURI());
450
                                        if (permList.hasChanges()) {
451
                                                GSS.get().getTreeView().updateMySharedNode();
452
                                        }
453
                                        /*
454
                                        if(folderItem.getParentItem() != null && ((DnDTreeItem)folderItem.getParentItem()).getFolderResource() != null){
455
                                                ((DnDTreeItem)folderItem.getParentItem()).getFolderResource().removeSubfolderPath(initialPath);
456
                                                ((DnDTreeItem)folderItem.getParentItem()).getFolderResource().getSubfolderPaths().add(newPath);
457
                                        }*/
458
                                }
459
                                //GSS.get().getFolders().updateFolder( (DnDTreeItem) GSS.get().getFolders().getCurrent());
460
                                
461
                                GSS.get().showFileList(true);
462
                        }
463

    
464
                        @Override
465
                        public void onError(Throwable t) {
466
                                GWT.log("", t);
467
                                if(t instanceof RestException){
468
                                        int statusCode = ((RestException)t).getHttpStatusCode();
469
                                        if(statusCode == 405)
470
                                                GSS.get().displayError("You don't have the necessary permissions or" +
471
                                                                " a folder with same name already exists");
472
                                        else if(statusCode == 404)
473
                                                GSS.get().displayError("Resource not found, or user specified in sharing does not exist");
474
                                        else
475
                                                GSS.get().displayError("Unable to update folder: "+((RestException)t).getHttpStatusText());
476
                                }
477
                                else
478
                                        GSS.get().displayError("System error moifying file: "+t.getMessage());
479
                                //TODO:CELLTREE
480
                                //GSS.get().getFolders().updateFolder( (DnDTreeItem) GSS.get().getFolders().getCurrent());
481
                        }
482
                };
483
                DeferredCommand.addCommand(ep);
484
        }
485

    
486
        public void selectTab(int _tab) {
487
                inner.selectTab(_tab);
488
        }
489

    
490
}