Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / client / FilePropertiesDialog.java @ bed4db0d

History | View | Annotate | Download (19.4 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
                        permForAll.add(readForAllNote);
289
                        permPanel.add(permForAll);
290
                }
291

    
292
                TextBox path = new TextBox();
293
                path.setWidth("100%");
294
                path.addClickListener(new ClickListener () {
295

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

    
302
                });
303
                path.setText(file.getUri());
304
                path.setTitle("Use this URI for sharing this file with the world (crtl-C/cmd-C to copy to system clipboard)");
305
                path.setWidth("100%");
306
                path.setReadOnly(true);
307
                pathPanel.setWidth("100%");
308
                pathPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
309
                pathPanel.add(new Label("Sharing URI"));
310
                pathPanel.setSpacing(8);
311
                pathPanel.addStyleName("gwt-TabPanelBottom");
312
                pathPanel.add(path);
313
                pathPanel.setVisible(file.isReadForAll());
314
                permPanel.add(pathPanel);
315

    
316
                VersionsList verList = new VersionsList(this, images, bodies);
317
                verPanel.add(verList);
318

    
319
                vPanel.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
320
                vPanel.setSpacing(8);
321
                vPanel.addStyleName("gwt-TabPanelBottom");
322
                vPanel.add(new Label("Versioned"));
323

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

    
331
                        public void onClick(Widget sender) {
332
                                ConfirmationDialog confirm = new ConfirmationDialog("Really " +
333
                                                "remove all previous versions?", "Remove") {
334

    
335
                                        @Override
336
                                        public void cancel() {
337
                                        }
338

    
339
                                        @Override
340
                                        public void confirm() {
341
                                                FilePropertiesDialog.this.closeDialog();
342
                                                removeAllOldVersions();
343
                                        }
344

    
345
                                };
346
                                confirm.center();
347
                        }
348

    
349
                });
350
                HTML removeAllVersion = new HTML("<span>Remove all previous versions?</span>");
351
                vPanel2.add(removeAllVersion);
352
                vPanel2.add(removeVersionsButton);
353
                verPanel.add(vPanel2);
354
                if(!file.isVersioned())
355
                        vPanel2.setVisible(false);
356
                outer.add(inner);
357
                outer.add(buttons);
358
                outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
359
                outer.addStyleName("gwt-TabPanelBottom");
360

    
361
                focusPanel.setFocus(true);
362
                setWidget(outer);
363
        }
364

    
365
        /**
366
         * Retrieves all user tags from the server and updates the FlowPanel
367
         *
368
         * @param userId
369
         */
370
        private void updateTags() {
371
                GetCommand<TagsResource> tc = new GetCommand<TagsResource>(TagsResource.class, GSS.get().getCurrentUserResource().getTagsPath()) {
372

    
373
                        @Override
374
                        public void onComplete() {
375
                                allTagsContent.clear();
376
                                TagsResource tagr = getResult();
377
                                List<String> userTags = tagr.getTags();
378
                                Iterator t = userTags.iterator();
379
                                while (t.hasNext()) {
380
                                        final Button tag = new Button((String) t.next(), new ClickListener() {
381

    
382
                                                public void onClick(Widget sender) {
383
                                                        String existing = tags.getText();
384
                                                        String newTag = ((Button) sender).getText().trim();
385
                                                        // insert the new tag only if it is not in the list
386
                                                        // already
387
                                                        if (existing.indexOf(newTag + ",") == -1 && !existing.trim().endsWith(newTag))
388
                                                                tags.setText(existing.trim() + (existing.length() > 0 ? ", " : "") + newTag);
389
                                                }
390
                                        });
391
                                        allTagsContent.add(tag);
392
                                }
393
                        }
394

    
395
                        @Override
396
                        public void onError(Throwable t) {
397
                                GWT.log("", t);
398
                                GSS.get().displayError("Unable to fetch user tags");
399
                        }
400
                };
401
                DeferredCommand.addCommand(tc);
402

    
403
        }
404

    
405
        @Override
406
        @SuppressWarnings("fallthrough")
407
        public boolean onKeyDownPreview(char key, int modifiers) {
408
                // Use the popup's key preview hooks to close the dialog when either
409
                // enter or escape is pressed.
410
                switch (key) {
411
                        case KeyboardListener.KEY_ENTER:
412
                                accept();
413
                        case KeyboardListener.KEY_ESCAPE:
414
                                closeDialog();
415
                                break;
416
                }
417

    
418
                return true;
419
        }
420

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

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

    
475
                        @Override
476
                        public void onComplete() {
477
                                GSS.get().getFileList().updateFileCache(true, false /* do not clear selected file*/);
478
                        }
479

    
480
                        @Override
481
                        public void onError(Throwable t) {
482
                                GWT.log("", t);
483
                                if (t instanceof RestException) {
484
                                        int statusCode = ((RestException) t).getHttpStatusCode();
485
                                        if (statusCode == 405)
486
                                                GSS.get().displayError("You don't have the necessary permissions");
487
                                        else if (statusCode == 404)
488
                                                GSS.get().displayError("User in permissions does not exist");
489
                                        else if (statusCode == 409)
490
                                                GSS.get().displayError("A file with the same name already exists");
491
                                        else if (statusCode == 413)
492
                                                GSS.get().displayError("Your quota has been exceeded");
493
                                        else
494
                                                GSS.get().displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
495
                                } else
496
                                        GSS.get().displayError("System error modifying file:" + t.getMessage());
497
                        }
498

    
499
                };
500
                DeferredCommand.addCommand(cf);
501

    
502
        }
503

    
504
        private void removeAllOldVersions() {
505
                JSONObject json = new JSONObject();
506
                json.put("versioned", JSONBoolean.getInstance(false));
507
                GWT.log(json.toString(), null);
508
                PostCommand cf = new PostCommand(file.getUri() + "?update=", json.toString(), 200) {
509

    
510
                        @Override
511
                        public void onComplete() {
512
                                toggleVersioned(true);
513
                        }
514

    
515
                        @Override
516
                        public void onError(Throwable t) {
517
                                GWT.log("", t);
518
                                if (t instanceof RestException) {
519
                                        int statusCode = ((RestException) t).getHttpStatusCode();
520
                                        if (statusCode == 405)
521
                                                GSS.get().displayError("You don't have the necessary permissions");
522
                                        else if (statusCode == 404)
523
                                                GSS.get().displayError("User in permissions does not exist");
524
                                        else if (statusCode == 409)
525
                                                GSS.get().displayError("A folder with the same name already exists");
526
                                        else if (statusCode == 413)
527
                                                GSS.get().displayError("Your quota has been exceeded");
528
                                        else
529
                                                GSS.get().displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
530
                                } else
531
                                        GSS.get().displayError("System error moifying file:" + t.getMessage());
532
                        }
533
                };
534
                DeferredCommand.addCommand(cf);
535
        }
536

    
537
        private void toggleVersioned(boolean versionedValue) {
538
                JSONObject json = new JSONObject();
539
                json.put("versioned", JSONBoolean.getInstance(versionedValue));
540
                GWT.log(json.toString(), null);
541
                PostCommand cf = new PostCommand(file.getUri() + "?update=", json.toString(), 200) {
542

    
543
                        @Override
544
                        public void onComplete() {
545
                                GSS.get().getFileList().updateFileCache(true, false /* do not clear selected file*/);
546
                        }
547

    
548
                        @Override
549
                        public void onError(Throwable t) {
550
                                GWT.log("", t);
551
                                if (t instanceof RestException) {
552
                                        int statusCode = ((RestException) t).getHttpStatusCode();
553
                                        if (statusCode == 405)
554
                                                GSS.get().displayError("You don't have the necessary permissions");
555
                                        else if (statusCode == 404)
556
                                                GSS.get().displayError("User in permissions does not exist");
557
                                        else if (statusCode == 409)
558
                                                GSS.get().displayError("A folder with the same name already exists");
559
                                        else if (statusCode == 413)
560
                                                GSS.get().displayError("Your quota has been exceeded");
561
                                        else
562
                                                GSS.get().displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
563
                                } else
564
                                        GSS.get().displayError("System error moifying file:" + t.getMessage());
565
                        }
566
                };
567
                DeferredCommand.addCommand(cf);
568
        }
569

    
570
        public void selectTab(int _tab) {
571
                inner.selectTab(_tab);
572
        }
573

    
574
        /**
575
         * Enables IE selection prevention and hides the dialog
576
         * (we disable the prevention on creation of the dialog)
577
         */
578
        public void closeDialog() {
579
                GSS.preventIESelection();
580
                hide();
581
        }
582

    
583
}