Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / client / FolderPropertiesDialog.java @ a60ea262

History | View | Annotate | Download (16 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.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
                DnDTreeItem folderItem = (DnDTreeItem)GSS.get().getFolders().getCurrent();
101
                folder = folderItem.getFolderResource();
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
                generalTable.setWidget(0, 1, folderName);
139
                folderName.addChangeHandler(new ChangeHandler() {
140
                        
141
                        @Override
142
                        public void onChange(ChangeEvent event) {
143
                                if(folderName.getText().contains("/"))
144
                                        folderNameNote.setVisible(true);
145
                                else
146
                                        folderNameNote.setVisible(false);                                
147
                                
148
                        }
149
                });
150

    
151

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

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

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

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

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

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

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

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

    
247
                });
248

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

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

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

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

    
290
                setWidget(outer);
291

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

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

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

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

    
323

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

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

    
349
                        @Override
350
                        public void onComplete() {
351
                                GSS.get().getFolders().updateFolder((DnDTreeItem) GSS.get().getFolders().getCurrent());
352
                        }
353

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

    
376
        }
377

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

    
391
        }
392

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

    
423
                        @Override
424
                        public void onComplete() {
425
                                if(getPostBody() != null && !"".equals(getPostBody().trim())){
426
                                        DnDTreeItem folderItem = (DnDTreeItem) GSS.get().getFolders().getCurrent();
427
                                        FolderResource fres = folderItem.getFolderResource();
428
                                        String initialPath = fres.getUri();
429
                                        String newPath =  getPostBody().trim();
430
                                        fres.setUri(newPath);
431

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

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

    
462
        public void selectTab(int _tab) {
463
                inner.selectTab(_tab);
464
        }
465

    
466
}