Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / client / FolderPropertiesDialog.java @ 783db80b

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.dnd.DnDTreeItem;
23
import gr.ebs.gss.client.rest.PostCommand;
24
import gr.ebs.gss.client.rest.RestException;
25
import gr.ebs.gss.client.rest.resource.FolderResource;
26
import gr.ebs.gss.client.rest.resource.GroupResource;
27
import gr.ebs.gss.client.rest.resource.PermissionHolder;
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.event.dom.client.KeyDownEvent;
40
import com.google.gwt.event.dom.client.KeyDownHandler;
41
import com.google.gwt.http.client.URL;
42
import com.google.gwt.i18n.client.DateTimeFormat;
43
import com.google.gwt.json.client.JSONArray;
44
import com.google.gwt.json.client.JSONBoolean;
45
import com.google.gwt.json.client.JSONObject;
46
import com.google.gwt.json.client.JSONString;
47
import com.google.gwt.user.client.DeferredCommand;
48
import com.google.gwt.user.client.Event.NativePreviewEvent;
49
import com.google.gwt.user.client.ui.Button;
50
import com.google.gwt.user.client.ui.CheckBox;
51
import com.google.gwt.user.client.ui.DecoratedTabPanel;
52
import com.google.gwt.user.client.ui.DialogBox;
53
import com.google.gwt.user.client.ui.FlexTable;
54
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
55
import com.google.gwt.user.client.ui.HorizontalPanel;
56
import com.google.gwt.user.client.ui.Label;
57
import com.google.gwt.user.client.ui.TabPanel;
58
import com.google.gwt.user.client.ui.TextBox;
59
import com.google.gwt.user.client.ui.VerticalPanel;
60

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

    
66
        private List<GroupResource> groups = null;
67

    
68
        final PermissionsList permList;
69

    
70
        private CheckBox readForAll;
71

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

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

    
83
        final FolderResource folder;
84

    
85
        final TabPanel inner;
86

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

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

    
101
                create = _create;
102
                DnDTreeItem folderItem = (DnDTreeItem)GSS.get().getFolders().getCurrent();
103
                folder = folderItem.getFolderResource();
104
                permList = new PermissionsList(images, folder.getPermissions(), folder.getOwner());
105
                groups = _groups;
106

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

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

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

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

    
134
                FlexTable generalTable = new FlexTable();
135
                generalTable.setText(0, 0, "Name");
136
                generalTable.setText(1, 0, "Parent");
137
                generalTable.setText(2, 0, "Creator");
138
                generalTable.setText(3, 0, "Last modified");
139
                folderName.setText(create ? "" : folder.getName());
140
                generalTable.setWidget(0, 1, folderName);
141
                folderName.addChangeHandler(new ChangeHandler() {
142
                        
143
                        @Override
144
                        public void onChange(ChangeEvent event) {
145
                                if(folderName.getText().contains("/"))
146
                                        folderNameNote.setVisible(true);
147
                                else
148
                                        folderNameNote.setVisible(false);                                
149
                                
150
                        }
151
                });
152

    
153

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

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

    
193
                        }
194
                });                
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
                buttons.add(cancel);
207
                buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
208
                buttons.setSpacing(8);
209
                buttons.addStyleName("gss-TabPanelBottom");
210

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

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

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

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

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

    
249
                });
250

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

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

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

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

    
292
                setWidget(outer);
293

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

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

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

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

    
325

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

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

    
351
                        @Override
352
                        public void onComplete() {
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
                                if(getPostBody() != null && !"".equals(getPostBody().trim())){
428
                                        DnDTreeItem folderItem = (DnDTreeItem) GSS.get().getFolders().getCurrent();
429
                                        FolderResource fres = folderItem.getFolderResource();
430
                                        String initialPath = fres.getUri();
431
                                        String newPath =  getPostBody().trim();
432
                                        fres.setUri(newPath);
433

    
434
                                        if(folderItem.getParentItem() != null && ((DnDTreeItem)folderItem.getParentItem()).getFolderResource() != null){
435
                                                ((DnDTreeItem)folderItem.getParentItem()).getFolderResource().removeSubfolderPath(initialPath);
436
                                                ((DnDTreeItem)folderItem.getParentItem()).getFolderResource().getSubfolderPaths().add(newPath);
437
                                        }
438
                                }
439
                                GSS.get().getFolders().updateFolder( (DnDTreeItem) GSS.get().getFolders().getCurrent());
440
                                GSS.get().showFileList(true);
441
                        }
442

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

    
464
        public void selectTab(int _tab) {
465
                inner.selectTab(_tab);
466
        }
467

    
468
}