Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (17.3 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.PostCommand;
22
import gr.ebs.gss.client.rest.RestException;
23
import gr.ebs.gss.client.rest.resource.FileResource;
24
import gr.ebs.gss.client.rest.resource.GroupResource;
25
import gr.ebs.gss.client.rest.resource.PermissionHolder;
26

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

    
31
import com.google.gwt.core.client.GWT;
32
import com.google.gwt.event.dom.client.ClickEvent;
33
import com.google.gwt.event.dom.client.ClickHandler;
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.resources.client.ClientBundle;
40
import com.google.gwt.resources.client.ImageResource;
41
import com.google.gwt.user.client.Command;
42
import com.google.gwt.user.client.DeferredCommand;
43
import com.google.gwt.user.client.ui.AbstractImagePrototype;
44
import com.google.gwt.user.client.ui.Button;
45
import com.google.gwt.user.client.ui.CheckBox;
46
import com.google.gwt.user.client.ui.DecoratedTabPanel;
47
import com.google.gwt.user.client.ui.DisclosurePanel;
48
import com.google.gwt.user.client.ui.FlexTable;
49
import com.google.gwt.user.client.ui.FlowPanel;
50
import com.google.gwt.user.client.ui.FocusPanel;
51
import com.google.gwt.user.client.ui.HTML;
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.TextBox;
56
import com.google.gwt.user.client.ui.VerticalPanel;
57

    
58
/**
59
 * The 'File properties' dialog box implementation.
60
 *
61
 * @author past
62
 */
63
public class FilePropertiesDialog extends AbstractPropertiesDialog {
64

    
65
        final PermissionsList permList;
66

    
67
        private CheckBox readForAll;
68

    
69
        /**
70
         * An image bundle for this widgets images.
71
         */
72
        public interface Images extends ClientBundle,MessagePanel.Images {
73

    
74
                @Source("gr/ebs/gss/resources/edit_user.png")
75
                ImageResource permUser();
76

    
77
                @Source("gr/ebs/gss/resources/groupevent.png")
78
                ImageResource permGroup();
79

    
80
                @Source("gr/ebs/gss/resources/editdelete.png")
81
                ImageResource delete();
82

    
83
                @Source("gr/ebs/gss/resources/db_update.png")
84
                ImageResource restore();
85

    
86
                @Source("gr/ebs/gss/resources/folder_inbox.png")
87
                ImageResource download();
88
        }
89

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

    
95
        private final CheckBox versioned = new CheckBox();
96

    
97
        final FileResource file;
98

    
99
        /**
100
         * The widget's constructor.
101
         *
102
         * @param images the dialog's ImageBundle
103
         * @param groups
104
         * @param bodies
105
         */
106
        public FilePropertiesDialog(final Images images, final List<GroupResource> groups, List<FileResource> bodies) {
107

    
108
                // Set the dialog's caption.
109
                setText("File properties");
110

    
111
                file = (FileResource) GSS.get().getCurrentSelection();
112
                permList = new PermissionsList(images, file.getPermissions(), file.getOwner());
113

    
114
                // Outer contains inner and buttons.
115
                final VerticalPanel outer = new VerticalPanel();
116
                final FocusPanel focusPanel = new FocusPanel(outer);
117
                // Inner contains generalPanel and permPanel.
118
                inner = new DecoratedTabPanel();
119
                inner.setAnimationEnabled(true);
120
                final VerticalPanel generalPanel = new VerticalPanel();
121
                final VerticalPanel permPanel = new VerticalPanel();
122
                final HorizontalPanel buttons = new HorizontalPanel();
123
                final HorizontalPanel permButtons = new HorizontalPanel();
124
                final HorizontalPanel permForAll = new HorizontalPanel();
125
                final HorizontalPanel pathPanel = new HorizontalPanel();
126
                final VerticalPanel verPanel = new VerticalPanel();
127
                final HorizontalPanel vPanel = new HorizontalPanel();
128
                final HorizontalPanel vPanel2 = new HorizontalPanel();
129

    
130
                versioned.setValue(file.isVersioned());
131
                inner.add(generalPanel, "General");
132
                inner.add(permPanel, "Sharing");
133
                inner.add(verPanel, "Versions");
134
                inner.selectTab(0);
135

    
136
                final FlexTable generalTable = new FlexTable();
137
                generalTable.setText(0, 0, "Name");
138
                generalTable.setText(1, 0, "Folder");
139
                generalTable.setText(2, 0, "Owner");
140
                generalTable.setText(3, 0, "Last modified");
141
                generalTable.setText(4, 0, "Tags");
142
                name.setWidth("100%");
143
                name.setText(file.getName());
144
                generalTable.setWidget(0, 1, name);
145
                if(file.getFolderName() != null)
146
                        generalTable.setText(1, 1, file.getFolderName());
147
                else
148
                        generalTable.setText(1, 1, "-");
149
                generalTable.setText(2, 1, file.getOwner());
150
                final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
151
                generalTable.setText(3, 1, formatter.format(file.getModificationDate()));
152
                // Get the tags.
153
                StringBuffer tagsBuffer = new StringBuffer();
154
                Iterator i = file.getTags().iterator();
155
                while (i.hasNext()) {
156
                        String tag = (String) i.next();
157
                        tagsBuffer.append(tag).append(", ");
158
                }
159
                if (tagsBuffer.length() > 1)
160
                        tagsBuffer.delete(tagsBuffer.length() - 2, tagsBuffer.length() - 1);
161
                initialTagText = tagsBuffer.toString();
162
                tags.setWidth("100%");
163
                tags.setText(initialTagText);
164
                generalTable.setWidget(4, 1, tags);
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(4, 0, "props-labels");
170
                generalTable.getFlexCellFormatter().setStyleName(0, 1, "props-values");
171
                generalTable.getFlexCellFormatter().setStyleName(1, 1, "props-values");
172
                generalTable.getFlexCellFormatter().setStyleName(2, 1, "props-values");
173
                generalTable.getFlexCellFormatter().setStyleName(3, 1, "props-values");
174
                generalTable.getFlexCellFormatter().setStyleName(4, 1, "props-values");
175
                generalTable.setCellSpacing(4);
176

    
177
                // Create the 'OK' button, along with a listener that hides the dialog
178
                // when the button is clicked.
179
                final Button ok = new Button("OK", new ClickHandler() {
180
                        @Override
181
                        public void onClick(ClickEvent event) {
182
                                accept();
183
                                closeDialog();
184
                        }
185
                });
186
                buttons.add(ok);
187
                buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
188
                // Create the 'Cancel' button, along with a listener that hides the
189
                // dialog when the button is clicked.
190
                final Button cancel = new Button("Cancel", new ClickHandler() {
191
                        @Override
192
                        public void onClick(ClickEvent event) {
193
                                closeDialog();
194
                        }
195
                });
196
                buttons.add(cancel);
197
                buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
198
                buttons.setSpacing(8);
199
                buttons.addStyleName("gss-TabPanelBottom");
200

    
201
                generalPanel.add(generalTable);
202

    
203
                // Asynchronously retrieve the tags defined by this user.
204
                DeferredCommand.addCommand(new Command() {
205

    
206
                        @Override
207
                        public void execute() {
208
                                updateTags();
209
                        }
210
                });
211

    
212
                DisclosurePanel allTags = new DisclosurePanel("All tags");
213
                allTagsContent = new FlowPanel();
214
                allTagsContent.setWidth("100%");
215
                allTags.setContent(allTagsContent);
216
                generalPanel.add(allTags);
217
                generalPanel.setSpacing(4);
218

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

    
229
                final Button addUser = new Button("Add User", new ClickHandler() {
230
                        @Override
231
                        public void onClick(ClickEvent event) {
232
                                PermissionsAddDialog dlg = new PermissionsAddDialog(groups, permList, true);
233
                                dlg.center();
234
                        }
235
                });
236
                permButtons.add(addUser);
237
                permButtons.setCellHorizontalAlignment(addUser, HasHorizontalAlignment.ALIGN_CENTER);
238

    
239
                permButtons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
240
                permButtons.setSpacing(8);
241
                permButtons.addStyleName("gss-TabPanelBottom");
242

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

    
249
                readForAll = new CheckBox();
250
                readForAll.setValue(file.isReadForAll());
251
                readForAll.addClickHandler(new ClickHandler() {
252
                        @Override
253
                        public void onClick(ClickEvent event) {
254
                                readForAllNote.setVisible(readForAll.getValue());
255
                        }
256

    
257
                });
258

    
259
                permPanel.add(permList);
260
                permPanel.add(permButtons);
261
                // Only show the read for all permission if the user is the owner.
262
                if (file.getOwner().equals(GSS.get().getCurrentUserResource().getUsername())) {
263
                        permForAll.add(new Label("Public"));
264
                        permForAll.add(readForAll);
265
                        permForAll.setSpacing(8);
266
                        permForAll.addStyleName("gss-TabPanelBottom");
267
                        permForAll.add(readForAllNote);
268
                        permPanel.add(permForAll);
269
                }
270

    
271
                TextBox path = new TextBox();
272
                path.setWidth("100%");
273
                path.addClickHandler(new ClickHandler() {
274
                        @Override
275
                        public void onClick(ClickEvent event) {
276
                                GSS.enableIESelection();
277
                                ((TextBox) event.getSource()).selectAll();
278
                                GSS.preventIESelection();
279
                        }
280

    
281
                });
282
                path.setText(file.getUri());
283
                path.setTitle("Use this link for sharing the file via e-mail, IM, etc. (crtl-C/cmd-C to copy to system clipboard)");
284
                path.setWidth("100%");
285
                path.setReadOnly(true);
286
                pathPanel.setWidth("100%");
287
                pathPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
288
                pathPanel.add(new Label("Link"));
289
                pathPanel.setSpacing(8);
290
                pathPanel.addStyleName("gss-TabPanelBottom");
291
                pathPanel.add(path);
292
                permPanel.add(pathPanel);
293

    
294
                VersionsList verList = new VersionsList(this, images, bodies);
295
                verPanel.add(verList);
296

    
297
                vPanel.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
298
                vPanel.setSpacing(8);
299
                vPanel.addStyleName("gss-TabPanelBottom");
300
                vPanel.add(new Label("Versioned"));
301

    
302
                vPanel.add(versioned);
303
                verPanel.add(vPanel);
304
                vPanel2.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
305
                vPanel2.setSpacing(8);
306
                vPanel2.addStyleName("gss-TabPanelBottom");
307
                Button removeVersionsButton = new Button(AbstractImagePrototype.create(images.delete()).getHTML(), new ClickHandler() {
308
                        @Override
309
                        public void onClick(ClickEvent event) {
310
                                ConfirmationDialog confirm = new ConfirmationDialog("Really " +
311
                                                "remove all previous versions?", "Remove") {
312

    
313
                                        @Override
314
                                        public void cancel() {
315
                                        }
316

    
317
                                        @Override
318
                                        public void confirm() {
319
                                                FilePropertiesDialog.this.closeDialog();
320
                                                removeAllOldVersions();
321
                                        }
322

    
323
                                };
324
                                confirm.center();
325
                        }
326

    
327
                });
328
                HTML removeAllVersion = new HTML("<span>Remove all previous versions?</span>");
329
                vPanel2.add(removeAllVersion);
330
                vPanel2.add(removeVersionsButton);
331
                verPanel.add(vPanel2);
332
                if(!file.isVersioned())
333
                        vPanel2.setVisible(false);
334
                outer.add(inner);
335
                outer.add(buttons);
336
                outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
337
                outer.addStyleName("gss-TabPanelBottom");
338

    
339
                focusPanel.setFocus(true);
340
                setWidget(outer);
341
        }
342

    
343

    
344
        /**
345
         * Accepts any change and updates the file
346
         *
347
         */
348
        @Override
349
        protected void accept() {
350
                String newFilename = null;
351
                permList.updatePermissionsAccordingToInput();
352
                Set<PermissionHolder> perms = permList.getPermissions();
353
                JSONObject json = new JSONObject();
354
                if (!name.getText().equals(file.getName())) {
355
                        newFilename = name.getText();
356
                        json.put("name", new JSONString(newFilename));
357
                }
358
                if (versioned.getValue() != file.isVersioned())
359
                        json.put("versioned", JSONBoolean.getInstance(versioned.getValue()));
360
                //only update the read for all perm if the user is the owner
361
                if (readForAll.getValue() != file.isReadForAll())
362
                        if (file.getOwner().equals(GSS.get().getCurrentUserResource().getUsername()))
363
                                json.put("readForAll", JSONBoolean.getInstance(readForAll.getValue()));
364
                int i = 0;
365
                if (permList.hasChanges()) {
366
                        GWT.log("Permissions change", null);
367
                        JSONArray perma = new JSONArray();
368

    
369
                        for (PermissionHolder p : perms) {
370
                                JSONObject po = new JSONObject();
371
                                if (p.getUser() != null)
372
                                        po.put("user", new JSONString(p.getUser()));
373
                                if (p.getGroup() != null)
374
                                        po.put("group", new JSONString(p.getGroup()));
375
                                po.put("read", JSONBoolean.getInstance(p.isRead()));
376
                                po.put("write", JSONBoolean.getInstance(p.isWrite()));
377
                                po.put("modifyACL", JSONBoolean.getInstance(p.isModifyACL()));
378
                                perma.set(i, po);
379
                                i++;
380
                        }
381
                        json.put("permissions", perma);
382
                }
383
                JSONArray taga = new JSONArray();
384
                i = 0;
385
                if (!tags.getText().equals(initialTagText)) {
386
                        String[] tagset = tags.getText().split(",");
387
                        for (String t : tagset) {
388
                                JSONString to = new JSONString(t);
389
                                taga.set(i, to);
390
                                i++;
391
                        }
392
                        json.put("tags", taga);
393
                }
394
                String jsonString = json.toString();
395
                if(jsonString.equals("{}")){
396
                        GWT.log("NO CHANGES", null);
397
                        return;
398
                }
399
                final String newFilenameFinal = newFilename;
400
                PostCommand cf = new PostCommand(file.getUri() + "?update=", jsonString, 200) {
401

    
402
                        @Override
403
                        public void onComplete() {
404
                                GSS.get().getFileList().updateFileCache(true, false /* do not clear selected file*/, newFilenameFinal);
405
                        }
406

    
407
                        @Override
408
                        public void onError(Throwable t) {
409
                                GWT.log("", t);
410
                                if (t instanceof RestException) {
411
                                        int statusCode = ((RestException) t).getHttpStatusCode();
412
                                        if (statusCode == 405)
413
                                                GSS.get().displayError("You don't have the necessary permissions");
414
                                        else if (statusCode == 404)
415
                                                GSS.get().displayError("User in permissions does not exist");
416
                                        else if (statusCode == 409)
417
                                                GSS.get().displayError("A file with the same name already exists");
418
                                        else if (statusCode == 413)
419
                                                GSS.get().displayError("Your quota has been exceeded");
420
                                        else
421
                                                GSS.get().displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
422
                                } else
423
                                        GSS.get().displayError("System error modifying file:" + t.getMessage());
424
                        }
425

    
426
                };
427
                DeferredCommand.addCommand(cf);
428

    
429
        }
430

    
431
        private void removeAllOldVersions() {
432
                JSONObject json = new JSONObject();
433
                json.put("versioned", JSONBoolean.getInstance(false));
434
                GWT.log(json.toString(), null);
435
                PostCommand cf = new PostCommand(file.getUri() + "?update=", json.toString(), 200) {
436

    
437
                        @Override
438
                        public void onComplete() {
439
                                toggleVersioned(true);
440
                        }
441

    
442
                        @Override
443
                        public void onError(Throwable t) {
444
                                GWT.log("", t);
445
                                if (t instanceof RestException) {
446
                                        int statusCode = ((RestException) t).getHttpStatusCode();
447
                                        if (statusCode == 405)
448
                                                GSS.get().displayError("You don't have the necessary permissions");
449
                                        else if (statusCode == 404)
450
                                                GSS.get().displayError("User in permissions does not exist");
451
                                        else if (statusCode == 409)
452
                                                GSS.get().displayError("A folder with the same name already exists");
453
                                        else if (statusCode == 413)
454
                                                GSS.get().displayError("Your quota has been exceeded");
455
                                        else
456
                                                GSS.get().displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
457
                                } else
458
                                        GSS.get().displayError("System error moifying file:" + t.getMessage());
459
                        }
460
                };
461
                DeferredCommand.addCommand(cf);
462
        }
463

    
464
        private void toggleVersioned(boolean versionedValue) {
465
                JSONObject json = new JSONObject();
466
                json.put("versioned", JSONBoolean.getInstance(versionedValue));
467
                GWT.log(json.toString(), null);
468
                PostCommand cf = new PostCommand(file.getUri() + "?update=", json.toString(), 200) {
469

    
470
                        @Override
471
                        public void onComplete() {
472
                                GSS.get().getFileList().updateFileCache(true, false /* do not clear selected file*/);
473
                        }
474

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

    
497

    
498
}