Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / client / FolderPropertiesDialog.java @ 5b09ea6c

History | View | Annotate | Download (16.1 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 gr.ebs.gss.client;
20

    
21
import gr.ebs.gss.client.FilePropertiesDialog.Images;
22
import gr.ebs.gss.client.rest.PostCommand;
23
import gr.ebs.gss.client.rest.RestException;
24
import gr.ebs.gss.client.rest.resource.FolderResource;
25
import gr.ebs.gss.client.rest.resource.GroupResource;
26
import gr.ebs.gss.client.rest.resource.PermissionHolder;
27

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

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

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

    
65
        private List<GroupResource> groups = null;
66

    
67
        final PermissionsList permList;
68

    
69
        private CheckBox readForAll;
70

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

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

    
82
        final FolderResource folder;
83

    
84
        final TabPanel inner;
85

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

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

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

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

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

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

    
129
                final Label folderNameNote = new Label("Please note that slashes ('/') are not allowed in folder names.", true);
130
                folderNameNote.setVisible(false);
131
                folderNameNote.setStylePrimaryName("gss-readForAllNote");
132

    
133
                FlexTable generalTable = new FlexTable();
134
                generalTable.setText(0, 0, "Name");
135
                generalTable.setText(1, 0, "Parent");
136
                generalTable.setText(2, 0, "Creator");
137
                generalTable.setText(3, 0, "Last modified");
138
                folderName.setText(create ? "" : folder.getName());
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
                buttons.add(ok);
195
                buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
196
                // Create the 'Cancel' button, along with a listener that hides the
197
                // dialog
198
                // when the button is clicked.
199
                Button cancel = new Button("Cancel", new ClickHandler() {
200
                        @Override
201
                        public void onClick(ClickEvent event) {
202
                                closeDialog();
203
                        }
204
                });
205
                buttons.add(cancel);
206
                buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
207
                buttons.setSpacing(8);
208
                buttons.addStyleName("gss-TabPanelBottom");
209

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

    
220
                Button addUser = new Button("Add User", new ClickHandler() {
221
                        @Override
222
                        public void onClick(ClickEvent event) {
223
                                PermissionsAddDialog dlg = new PermissionsAddDialog(groups, permList, true);
224
                                dlg.center();
225
                        }
226
                });
227
                permButtons.add(addUser);
228
                permButtons.setCellHorizontalAlignment(addUser, HasHorizontalAlignment.ALIGN_CENTER);
229

    
230
                permButtons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
231
                permButtons.setSpacing(8);
232
                permButtons.addStyleName("gss-TabPanelBottom");
233

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

    
240
                readForAll = new CheckBox();
241
                readForAll.setValue(folder.isReadForAll());
242
                readForAll.addClickHandler(new ClickHandler() {
243
                        @Override
244
                        public void onClick(ClickEvent event) {
245
                                readForAllNote.setVisible(readForAll.getValue());
246
                        }
247

    
248
                });
249

    
250
                generalPanel.add(generalTable);
251
                permPanel.add(permList);
252
                permPanel.add(permButtons);
253

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

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

    
286
                outer.add(inner);
287
                outer.add(buttons);
288
                outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
289
                outer.addStyleName("gss-TabPanelBottom");
290

    
291
                setWidget(outer);
292

    
293
                /*if (create)
294
                        folderName.setFocus(true);
295
                else
296
                        ok.setFocus(true);*/
297
        }
298

    
299
        @Override
300
        public void center() {
301
                super.center();
302
                folderName.setFocus(true);
303
        }
304

    
305
        @Override
306
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
307
                super.onPreviewNativeEvent(preview);
308

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

    
324

    
325
        /**
326
         * Enables IE selection prevention and hides the dialog
327
         * (we disable the prevention on creation of the dialog)
328
         */
329
        public void closeDialog() {
330
                GSS.preventIESelection();
331
                hide();
332
        }
333

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

    
350
                        @Override
351
                        public void onComplete() {
352
                                //TODO:CELLTREE
353
                                //GSS.get().getFolders().updateFolder((DnDTreeItem) GSS.get().getFolders().getCurrent());
354
                        }
355

    
356
                        @Override
357
                        public void onError(Throwable t) {
358
                                GWT.log("", t);
359
                                if(t instanceof RestException){
360
                                        int statusCode = ((RestException)t).getHttpStatusCode();
361
                                        if(statusCode == 405)
362
                                                GSS.get().displayError("You don't have the necessary" +
363
                                                                " permissions or a folder with same name " +
364
                                                                "already exists");
365
                                        else if(statusCode == 404)
366
                                                GSS.get().displayError("Resource not found");
367
                                        else
368
                                                GSS.get().displayError("Unable to create folder:" +
369
                                                                ((RestException)t).getHttpStatusText());
370
                                }
371
                                else
372
                                        GSS.get().displayError("System error creating folder:" +
373
                                                        t.getMessage());
374
                        }
375
                };
376
                DeferredCommand.addCommand(ep);
377

    
378
        }
379

    
380
        /**
381
         * Upon closing the dialog by clicking OK or pressing ENTER this method does
382
         * the actual work of modifying folder properties or creating a new Folder
383
         * depending on the value of the create field
384
         *
385
         * @param userId
386
         */
387
        private void createOrUpdateFolder() {
388
                if (create)
389
                        createFolder();
390
                else
391
                        updateFolder();
392

    
393
        }
394

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

    
425
                        @Override
426
                        public void onComplete() {
427
                                //TODO:CELLTREE
428
                                /*
429
                                if(getPostBody() != null && !"".equals(getPostBody().trim())){
430
                                        
431
                                        DnDTreeItem folderItem = (DnDTreeItem) GSS.get().getFolders().getCurrent();
432
                                        FolderResource fres = folderItem.getFolderResource();
433
                                        String initialPath = fres.getUri();
434
                                        String newPath =  getPostBody().trim();
435
                                        fres.setUri(newPath);
436

437
                                        if(folderItem.getParentItem() != null && ((DnDTreeItem)folderItem.getParentItem()).getFolderResource() != null){
438
                                                ((DnDTreeItem)folderItem.getParentItem()).getFolderResource().removeSubfolderPath(initialPath);
439
                                                ((DnDTreeItem)folderItem.getParentItem()).getFolderResource().getSubfolderPaths().add(newPath);
440
                                        }
441
                                }
442
                                GSS.get().getFolders().updateFolder( (DnDTreeItem) GSS.get().getFolders().getCurrent());
443
                                */
444
                                GSS.get().showFileList(true);
445
                        }
446

    
447
                        @Override
448
                        public void onError(Throwable t) {
449
                                GWT.log("", t);
450
                                if(t instanceof RestException){
451
                                        int statusCode = ((RestException)t).getHttpStatusCode();
452
                                        if(statusCode == 405)
453
                                                GSS.get().displayError("You don't have the necessary permissions or" +
454
                                                                " a folder with same name already exists");
455
                                        else if(statusCode == 404)
456
                                                GSS.get().displayError("Resource not found, or user specified in sharing does not exist");
457
                                        else
458
                                                GSS.get().displayError("Unable to update folder: "+((RestException)t).getHttpStatusText());
459
                                }
460
                                else
461
                                        GSS.get().displayError("System error moifying file: "+t.getMessage());
462
                                //TODO:CELLTREE
463
                                //GSS.get().getFolders().updateFolder( (DnDTreeItem) GSS.get().getFolders().getCurrent());
464
                        }
465
                };
466
                DeferredCommand.addCommand(ep);
467
        }
468

    
469
        public void selectTab(int _tab) {
470
                inner.selectTab(_tab);
471
        }
472

    
473
}