Statistics
| Branch: | Tag: | Revision:

root / gss / src / gr / ebs / gss / client / FilePropertiesDialog.java @ 895035a2

History | View | Annotate | Download (19.1 kB)

1
/*
2
 * Copyright 2007, 2008, 2009 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.rest.GetCommand;
22
import gr.ebs.gss.client.rest.PostCommand;
23
import gr.ebs.gss.client.rest.RestException;
24
import gr.ebs.gss.client.rest.resource.FileResource;
25
import gr.ebs.gss.client.rest.resource.GroupResource;
26
import gr.ebs.gss.client.rest.resource.PermissionHolder;
27
import gr.ebs.gss.client.rest.resource.TagsResource;
28

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

    
33
import com.google.gwt.core.client.GWT;
34
import com.google.gwt.i18n.client.DateTimeFormat;
35
import com.google.gwt.json.client.JSONArray;
36
import com.google.gwt.json.client.JSONBoolean;
37
import com.google.gwt.json.client.JSONObject;
38
import com.google.gwt.json.client.JSONString;
39
import com.google.gwt.user.client.Command;
40
import com.google.gwt.user.client.DeferredCommand;
41
import com.google.gwt.user.client.ui.AbstractImagePrototype;
42
import com.google.gwt.user.client.ui.Button;
43
import com.google.gwt.user.client.ui.CheckBox;
44
import com.google.gwt.user.client.ui.ClickListener;
45
import com.google.gwt.user.client.ui.DialogBox;
46
import com.google.gwt.user.client.ui.DisclosurePanel;
47
import com.google.gwt.user.client.ui.FlexTable;
48
import com.google.gwt.user.client.ui.FlowPanel;
49
import com.google.gwt.user.client.ui.FocusPanel;
50
import com.google.gwt.user.client.ui.HTML;
51
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
52
import com.google.gwt.user.client.ui.HorizontalPanel;
53
import com.google.gwt.user.client.ui.KeyboardListener;
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
import com.google.gwt.user.client.ui.Widget;
59

    
60
/**
61
 * The 'File properties' dialog box implementation.
62
 *
63
 * @author past
64
 */
65
public class FilePropertiesDialog extends DialogBox {
66

    
67
        final PermissionsList permList;
68

    
69
        private CheckBox readForAll;
70

    
71
        /**
72
         * An image bundle for this widgets images.
73
         */
74
        public interface Images extends MessagePanel.Images {
75

    
76
                @Resource("gr/ebs/gss/resources/edit_user.png")
77
                AbstractImagePrototype permUser();
78

    
79
                @Resource("gr/ebs/gss/resources/groupevent.png")
80
                AbstractImagePrototype permGroup();
81

    
82
                @Resource("gr/ebs/gss/resources/editdelete.png")
83
                AbstractImagePrototype delete();
84

    
85
                @Resource("gr/ebs/gss/resources/db_update.png")
86
                AbstractImagePrototype restore();
87

    
88
                @Resource("gr/ebs/gss/resources/folder_inbox.png")
89
                AbstractImagePrototype download();
90
        }
91

    
92
        /**
93
         * The widget that holds the name of the file.
94
         */
95
        private TextBox name = new TextBox();
96

    
97
        /**
98
         * Text box with the tags associated with the file
99
         */
100
        private TextBox tags = new TextBox();
101

    
102
        /**
103
         * A FlowPanel with all user tags
104
         */
105
        private FlowPanel allTagsContent;
106

    
107
        private final CheckBox versioned = new CheckBox();
108

    
109
        final FileResource file;
110

    
111
        String initialTagText;
112

    
113
        final TabPanel inner;
114

    
115
        /**
116
         * The widget's constructor.
117
         *
118
         * @param images the dialog's ImageBundle
119
         * @param groups
120
         * @param bodies
121
         */
122
        public FilePropertiesDialog(final Images images, final List<GroupResource> groups, List<FileResource> bodies) {
123

    
124
                // Enable IE selection for the dialog (must disable it upon closing it)
125
                GSS.enableIESelection();
126

    
127
                // Set the dialog's caption.
128
                setText("File properties");
129
                setAnimationEnabled(true);
130
                file = (FileResource) GSS.get().getCurrentSelection();
131
                permList = new PermissionsList(images, file.getPermissions(), file.getOwner());
132

    
133
                // Outer contains inner and buttons.
134
                final VerticalPanel outer = new VerticalPanel();
135
                final FocusPanel focusPanel = new FocusPanel(outer);
136
                // Inner contains generalPanel and permPanel.
137
                inner = new TabPanel();
138
                final VerticalPanel generalPanel = new VerticalPanel();
139
                final VerticalPanel permPanel = new VerticalPanel();
140
                final HorizontalPanel buttons = new HorizontalPanel();
141
                final HorizontalPanel permButtons = new HorizontalPanel();
142
                final HorizontalPanel permForAll = new HorizontalPanel();
143
                final HorizontalPanel pathPanel = new HorizontalPanel();
144
                final VerticalPanel verPanel = new VerticalPanel();
145
                final HorizontalPanel vPanel = new HorizontalPanel();
146
                final HorizontalPanel vPanel2 = new HorizontalPanel();
147

    
148
                versioned.setChecked(file.isVersioned());
149
                inner.add(generalPanel, "General");
150
                inner.add(permPanel, "Sharing");
151
                inner.add(verPanel, "Versions");
152
                inner.selectTab(0);
153

    
154
                final FlexTable generalTable = new FlexTable();
155
                generalTable.setText(0, 0, "Name");
156
                generalTable.setText(1, 0, "Folder");
157
                generalTable.setText(2, 0, "Owner");
158
                generalTable.setText(3, 0, "Date");
159
                generalTable.setText(4, 0, "Tags");
160
                name.setText(file.getName());
161
                generalTable.setWidget(0, 1, name);
162
                if(file.getFolderName() != null)
163
                        generalTable.setText(1, 1, file.getFolderName());
164
                else
165
                        generalTable.setText(1, 1, "-");
166
                generalTable.setText(2, 1, file.getOwner());
167
                final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
168
                generalTable.setText(3, 1, formatter.format(file.getCreationDate()));
169
                // Get the tags.
170
                StringBuffer tagsBuffer = new StringBuffer();
171
                Iterator i = file.getTags().iterator();
172
                while (i.hasNext()) {
173
                        String tag = (String) i.next();
174
                        tagsBuffer.append(tag).append(", ");
175
                }
176
                if (tagsBuffer.length() > 1)
177
                        tagsBuffer.delete(tagsBuffer.length() - 2, tagsBuffer.length() - 1);
178
                initialTagText = tagsBuffer.toString();
179
                tags.setText(initialTagText);
180
                generalTable.setWidget(4, 1, tags);
181
                generalTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
182
                generalTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
183
                generalTable.getFlexCellFormatter().setStyleName(2, 0, "props-labels");
184
                generalTable.getFlexCellFormatter().setStyleName(3, 0, "props-labels");
185
                generalTable.getFlexCellFormatter().setStyleName(4, 0, "props-labels");
186
                generalTable.getFlexCellFormatter().setStyleName(0, 1, "props-values");
187
                generalTable.getFlexCellFormatter().setStyleName(1, 1, "props-values");
188
                generalTable.getFlexCellFormatter().setStyleName(2, 1, "props-values");
189
                generalTable.getFlexCellFormatter().setStyleName(3, 1, "props-values");
190
                generalTable.getFlexCellFormatter().setStyleName(4, 1, "props-values");
191
                generalTable.setCellSpacing(4);
192

    
193
                // Create the 'OK' button, along with a listener that hides the dialog
194
                // when the button is clicked.
195
                final Button ok = new Button("OK", new ClickListener() {
196

    
197
                        public void onClick(Widget sender) {
198
                                accept();
199
                                closeDialog();
200
                        }
201
                });
202
                buttons.add(ok);
203
                buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
204
                // Create the 'Cancel' button, along with a listener that hides the
205
                // dialog when the button is clicked.
206
                final Button cancel = new Button("Cancel", new ClickListener() {
207

    
208
                        public void onClick(Widget sender) {
209
                                closeDialog();
210
                        }
211
                });
212
                buttons.add(cancel);
213
                buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
214
                buttons.setSpacing(8);
215
                buttons.addStyleName("gwt-TabPanelBottom");
216

    
217
                generalPanel.add(generalTable);
218

    
219
                // Asynchronously retrieve the tags defined by this user.
220
                DeferredCommand.addCommand(new Command() {
221

    
222
                        public void execute() {
223
                                updateTags();
224
                        }
225
                });
226

    
227
                DisclosurePanel allTags = new DisclosurePanel("All tags");
228
                allTagsContent = new FlowPanel();
229
                allTags.setContent(allTagsContent);
230
                generalPanel.add(allTags);
231
                generalPanel.setSpacing(4);
232

    
233
                final Button add = new Button("Add Group", new ClickListener() {
234

    
235
                        public void onClick(Widget sender) {
236
                                PermissionsAddDialog dlg = new PermissionsAddDialog(groups, permList, false);
237
                                dlg.center();
238
                        }
239
                });
240
                permButtons.add(add);
241
                permButtons.setCellHorizontalAlignment(add, HasHorizontalAlignment.ALIGN_CENTER);
242

    
243
                final Button addUser = new Button("Add User", new ClickListener() {
244

    
245
                        public void onClick(Widget sender) {
246
                                PermissionsAddDialog dlg = new PermissionsAddDialog(groups, permList, true);
247
                                dlg.center();
248
                        }
249
                });
250
                permButtons.add(addUser);
251
                permButtons.setCellHorizontalAlignment(addUser, HasHorizontalAlignment.ALIGN_CENTER);
252

    
253
                permButtons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
254
                permButtons.setSpacing(8);
255
                permButtons.addStyleName("gwt-TabPanelBottom");
256

    
257
                final Label readForAllNote = new Label("When this option is enabled, the file will be readable" +
258
                                        " by everyone. By checking this option, you are certifying that you have the right to " +
259
                                        "distribute this file and that it does not violate the Terms of Use.", true);
260
                readForAllNote.setVisible(false);
261
                readForAllNote.setStylePrimaryName("gss-readForAllNote");
262

    
263
                readForAll = new CheckBox();
264
                readForAll.setChecked(file.isReadForAll());
265
                readForAll.addClickListener(new ClickListener() {
266

    
267
                        public void onClick(Widget sender) {
268
                                if (readForAll.isChecked()) {
269
                                        readForAllNote.setVisible(true);
270
                                        pathPanel.setVisible(true);
271
                                }
272
                                else {
273
                                        readForAllNote.setVisible(false);
274
                                        pathPanel.setVisible(false);
275
                                }
276
                        }
277

    
278
                });
279

    
280
                permPanel.add(permList);
281
                permPanel.add(permButtons);
282
                // Only show the read for all permission if the user is the owner.
283
                if (file.getOwner().equals(GSS.get().getCurrentUserResource().getUsername())) {
284
                        permForAll.add(new Label("Make Public"));
285
                        permForAll.add(readForAll);
286
                        permForAll.setSpacing(8);
287
                        permForAll.addStyleName("gwt-TabPanelBottom");
288
                        permPanel.add(permForAll);
289
                        permPanel.add(readForAllNote);
290
                }
291

    
292
                TextBox path = new TextBox();
293
                path.addClickListener(new ClickListener () {
294

    
295
                        public void onClick(Widget sender) {
296
                                GSS.enableIESelection();
297
                                ((TextBox) sender).selectAll();
298
                                GSS.preventIESelection();
299
                        }
300

    
301
                });
302
                path.setText(file.getUri());
303
                path.setTitle("Use this URI for sharing this file with the world");
304
                path.setReadOnly(true);
305
                pathPanel.add(new Label("Sharing URI"));
306
                pathPanel.setSpacing(8);
307
                pathPanel.addStyleName("gwt-TabPanelBottom");
308
                pathPanel.add(path);
309
                pathPanel.setVisible(file.isReadForAll());
310
                permPanel.add(pathPanel);
311

    
312
                VersionsList verList = new VersionsList(this, images, bodies);
313
                verPanel.add(verList);
314

    
315
                vPanel.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
316
                vPanel.setSpacing(8);
317
                vPanel.addStyleName("gwt-TabPanelBottom");
318
                vPanel.add(new Label("Versioned"));
319

    
320
                vPanel.add(versioned);
321
                verPanel.add(vPanel);
322
                vPanel2.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
323
                vPanel2.setSpacing(8);
324
                vPanel2.addStyleName("gwt-TabPanelBottom");
325
                Button removeVersionsButton = new Button(images.delete().getHTML(), new ClickListener() {
326

    
327
                        public void onClick(Widget sender) {
328
                                ConfirmationDialog confirm = new ConfirmationDialog(images,"Really remove all previous versions?","Remove"){
329

    
330
                                        public void cancel() {
331
                                        }
332

    
333

    
334
                                        public void confirm() {
335
                                                FilePropertiesDialog.this.closeDialog();
336
                                                removeAllOldVersions();
337
                                        }
338

    
339
                                };
340
                                confirm.center();
341
                        }
342

    
343
                });
344
                HTML removeAllVersion = new HTML("<span>Remove all previous versions?</span>");
345
                vPanel2.add(removeAllVersion);
346
                vPanel2.add(removeVersionsButton);
347
                verPanel.add(vPanel2);
348
                if(!file.isVersioned())
349
                        vPanel2.setVisible(false);
350
                outer.add(inner);
351
                outer.add(buttons);
352
                outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
353
                outer.addStyleName("gwt-TabPanelBottom");
354

    
355
                focusPanel.setFocus(true);
356
                setWidget(outer);
357
        }
358

    
359
        /**
360
         * Retrieves all user tags from the server and updates the FlowPanel
361
         *
362
         * @param userId
363
         */
364
        private void updateTags() {
365
                GetCommand<TagsResource> tc = new GetCommand<TagsResource>(TagsResource.class, GSS.get().getCurrentUserResource().getTagsPath()) {
366

    
367
                        public void onComplete() {
368
                                allTagsContent.clear();
369
                                TagsResource tagr = getResult();
370
                                List<String> userTags = tagr.getTags();
371
                                Iterator t = userTags.iterator();
372
                                while (t.hasNext()) {
373
                                        final Button tag = new Button((String) t.next(), new ClickListener() {
374

    
375
                                                public void onClick(Widget sender) {
376
                                                        String existing = tags.getText();
377
                                                        String newTag = ((Button) sender).getText().trim();
378
                                                        // insert the new tag only if it is not in the list
379
                                                        // already
380
                                                        if (existing.indexOf(newTag + ",") == -1 && !existing.trim().endsWith(newTag))
381
                                                                tags.setText(existing.trim() + (existing.length() > 0 ? ", " : "") + newTag);
382
                                                }
383
                                        });
384
                                        allTagsContent.add(tag);
385
                                }
386
                        }
387

    
388
                        @Override
389
                        public void onError(Throwable t) {
390
                                GWT.log("", t);
391
                                GSS.get().displayError("Unable to fetch user tags");
392
                        }
393
                };
394
                DeferredCommand.addCommand(tc);
395

    
396
        }
397

    
398
        @Override
399
        @SuppressWarnings("fallthrough")
400
        public boolean onKeyDownPreview(char key, int modifiers) {
401
                // Use the popup's key preview hooks to close the dialog when either
402
                // enter or escape is pressed.
403
                switch (key) {
404
                        case KeyboardListener.KEY_ENTER:
405
                                accept();
406
                        case KeyboardListener.KEY_ESCAPE:
407
                                closeDialog();
408
                                break;
409
                }
410

    
411
                return true;
412
        }
413

    
414
        /**
415
         * Accepts any change and updates the file
416
         *
417
         * @param userId
418
         */
419
        private void accept() {
420
                permList.updatePermissionsAccordingToInput();
421
                Set<PermissionHolder> perms = permList.getPermissions();
422
                JSONObject json = new JSONObject();
423
                if (!name.getText().equals(file.getName()))
424
                        json.put("name", new JSONString(name.getText()));
425
                if (versioned.isChecked() != file.isVersioned())
426
                        json.put("versioned", JSONBoolean.getInstance(versioned.isChecked()));
427
                //only update the read for all perm if the user is the owner
428
                if (readForAll.isChecked() != file.isReadForAll())
429
                        if (file.getOwner().equals(GSS.get().getCurrentUserResource().getUsername()))
430
                                json.put("readForAll", JSONBoolean.getInstance(readForAll.isChecked()));
431
                int i = 0;
432
                if (permList.hasChanges()) {
433
                        GWT.log("Permissions change", null);
434
                        JSONArray perma = new JSONArray();
435

    
436
                        for (PermissionHolder p : perms) {
437
                                JSONObject po = new JSONObject();
438
                                if (p.getUser() != null)
439
                                        po.put("user", new JSONString(p.getUser()));
440
                                if (p.getGroup() != null)
441
                                        po.put("group", new JSONString(p.getGroup()));
442
                                po.put("read", JSONBoolean.getInstance(p.isRead()));
443
                                po.put("write", JSONBoolean.getInstance(p.isWrite()));
444
                                po.put("modifyACL", JSONBoolean.getInstance(p.isModifyACL()));
445
                                perma.set(i, po);
446
                                i++;
447
                        }
448
                        json.put("permissions", perma);
449
                }
450
                JSONArray taga = new JSONArray();
451
                i = 0;
452
                if (!tags.getText().equals(initialTagText)) {
453
                        String[] tagset = tags.getText().split(",");
454
                        for (String t : tagset) {
455
                                JSONString to = new JSONString(t);
456
                                taga.set(i, to);
457
                                i++;
458
                        }
459
                        json.put("tags", taga);
460
                }
461
                String jsonString = json.toString();
462
                if(jsonString.equals("{}")){
463
                        GWT.log("NO CHANGES", null);
464
                        return;
465
                }
466
                PostCommand cf = new PostCommand(file.getUri() + "?update=", jsonString, 200) {
467

    
468
                        public void onComplete() {
469
                                GSS.get().getFileList().updateFileCache(true, false /* do not clear selected file*/);
470
                        }
471

    
472
                        public void onError(Throwable t) {
473
                                GWT.log("", t);
474
                                if (t instanceof RestException) {
475
                                        int statusCode = ((RestException) t).getHttpStatusCode();
476
                                        if (statusCode == 405)
477
                                                GSS.get().displayError("You don't have the necessary permissions");
478
                                        else if (statusCode == 404)
479
                                                GSS.get().displayError("User in permissions does not exist");
480
                                        else if (statusCode == 409)
481
                                                GSS.get().displayError("A file with the same name already exists");
482
                                        else if (statusCode == 413)
483
                                                GSS.get().displayError("Your quota has been exceeded");
484
                                        else
485
                                                GSS.get().displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
486
                                } else
487
                                        GSS.get().displayError("System error modifying file:" + t.getMessage());
488
                        }
489

    
490
                };
491
                DeferredCommand.addCommand(cf);
492

    
493
        }
494

    
495
        private void removeAllOldVersions() {
496
                JSONObject json = new JSONObject();
497
                json.put("versioned", JSONBoolean.getInstance(false));
498
                GWT.log(json.toString(), null);
499
                PostCommand cf = new PostCommand(file.getUri() + "?update=", json.toString(), 200) {
500

    
501
                        public void onComplete() {
502
                                toggleVersioned(true);
503
                        }
504

    
505
                        public void onError(Throwable t) {
506
                                GWT.log("", t);
507
                                if (t instanceof RestException) {
508
                                        int statusCode = ((RestException) t).getHttpStatusCode();
509
                                        if (statusCode == 405)
510
                                                GSS.get().displayError("You don't have the necessary permissions");
511
                                        else if (statusCode == 404)
512
                                                GSS.get().displayError("User in permissions does not exist");
513
                                        else if (statusCode == 409)
514
                                                GSS.get().displayError("A folder with the same name already exists");
515
                                        else if (statusCode == 413)
516
                                                GSS.get().displayError("Your quota has been exceeded");
517
                                        else
518
                                                GSS.get().displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
519
                                } else
520
                                        GSS.get().displayError("System error moifying file:" + t.getMessage());
521
                        }
522
                };
523
                DeferredCommand.addCommand(cf);
524
        }
525

    
526
        private void toggleVersioned(boolean versionedValue) {
527
                JSONObject json = new JSONObject();
528
                json.put("versioned", JSONBoolean.getInstance(versionedValue));
529
                GWT.log(json.toString(), null);
530
                PostCommand cf = new PostCommand(file.getUri() + "?update=", json.toString(), 200) {
531

    
532
                        public void onComplete() {
533
                                GSS.get().getFileList().updateFileCache(true, false /* do not clear selected file*/);
534
                        }
535

    
536
                        public void onError(Throwable t) {
537
                                GWT.log("", t);
538
                                if (t instanceof RestException) {
539
                                        int statusCode = ((RestException) t).getHttpStatusCode();
540
                                        if (statusCode == 405)
541
                                                GSS.get().displayError("You don't have the necessary permissions");
542
                                        else if (statusCode == 404)
543
                                                GSS.get().displayError("User in permissions does not exist");
544
                                        else if (statusCode == 409)
545
                                                GSS.get().displayError("A folder with the same name already exists");
546
                                        else if (statusCode == 413)
547
                                                GSS.get().displayError("Your quota has been exceeded");
548
                                        else
549
                                                GSS.get().displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
550
                                } else
551
                                        GSS.get().displayError("System error moifying file:" + t.getMessage());
552
                        }
553
                };
554
                DeferredCommand.addCommand(cf);
555
        }
556

    
557
        public void selectTab(int _tab) {
558
                inner.selectTab(_tab);
559
        }
560

    
561
        /**
562
         * Enables IE selection prevention and hides the dialog
563
         * (we disable the prevention on creation of the dialog)
564
         */
565
        public void closeDialog() {
566
                GSS.preventIESelection();
567
                hide();
568
        }
569

    
570
}