2 * Copyright 2007, 2008, 2009 Electronic Business Systems Ltd.
\r
4 * This file is part of GSS.
\r
6 * GSS is free software: you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation, either version 3 of the License, or
\r
9 * (at your option) any later version.
\r
11 * GSS is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with GSS. If not, see <http://www.gnu.org/licenses/>.
\r
19 package gr.ebs.gss.client;
\r
21 import gr.ebs.gss.client.rest.GetCommand;
\r
22 import gr.ebs.gss.client.rest.PostCommand;
\r
23 import gr.ebs.gss.client.rest.RestException;
\r
24 import gr.ebs.gss.client.rest.resource.FileResource;
\r
25 import gr.ebs.gss.client.rest.resource.GroupResource;
\r
26 import gr.ebs.gss.client.rest.resource.PermissionHolder;
\r
27 import gr.ebs.gss.client.rest.resource.TagsResource;
\r
29 import java.util.Iterator;
\r
30 import java.util.List;
\r
31 import java.util.Set;
\r
33 import com.google.gwt.core.client.GWT;
\r
34 import com.google.gwt.i18n.client.DateTimeFormat;
\r
35 import com.google.gwt.json.client.JSONArray;
\r
36 import com.google.gwt.json.client.JSONBoolean;
\r
37 import com.google.gwt.json.client.JSONObject;
\r
38 import com.google.gwt.json.client.JSONString;
\r
39 import com.google.gwt.user.client.Command;
\r
40 import com.google.gwt.user.client.DeferredCommand;
\r
41 import com.google.gwt.user.client.ui.AbstractImagePrototype;
\r
42 import com.google.gwt.user.client.ui.Button;
\r
43 import com.google.gwt.user.client.ui.CheckBox;
\r
44 import com.google.gwt.user.client.ui.ClickListener;
\r
45 import com.google.gwt.user.client.ui.DialogBox;
\r
46 import com.google.gwt.user.client.ui.DisclosurePanel;
\r
47 import com.google.gwt.user.client.ui.FlexTable;
\r
48 import com.google.gwt.user.client.ui.FlowPanel;
\r
49 import com.google.gwt.user.client.ui.FocusPanel;
\r
50 import com.google.gwt.user.client.ui.HTML;
\r
51 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
\r
52 import com.google.gwt.user.client.ui.HorizontalPanel;
\r
53 import com.google.gwt.user.client.ui.KeyboardListener;
\r
54 import com.google.gwt.user.client.ui.Label;
\r
55 import com.google.gwt.user.client.ui.TabPanel;
\r
56 import com.google.gwt.user.client.ui.TextBox;
\r
57 import com.google.gwt.user.client.ui.VerticalPanel;
\r
58 import com.google.gwt.user.client.ui.Widget;
\r
61 * The 'File properties' dialog box implementation.
\r
65 public class FilePropertiesDialog extends DialogBox {
\r
67 final PermissionsList permList;
\r
69 private CheckBox readForAll;
\r
72 * An image bundle for this widgets images.
\r
74 public interface Images extends MessagePanel.Images {
\r
76 @Resource("gr/ebs/gss/resources/edit_user.png")
\r
77 AbstractImagePrototype permUser();
\r
79 @Resource("gr/ebs/gss/resources/groupevent.png")
\r
80 AbstractImagePrototype permGroup();
\r
82 @Resource("gr/ebs/gss/resources/editdelete.png")
\r
83 AbstractImagePrototype delete();
\r
85 @Resource("gr/ebs/gss/resources/db_update.png")
\r
86 AbstractImagePrototype restore();
\r
88 @Resource("gr/ebs/gss/resources/folder_inbox.png")
\r
89 AbstractImagePrototype download();
\r
93 * The widget that holds the name of the file.
\r
95 private TextBox name = new TextBox();
\r
98 * Text box with the tags associated with the file
\r
100 private TextBox tags = new TextBox();
\r
103 * A FlowPanel with all user tags
\r
105 private FlowPanel allTagsContent;
\r
107 private final CheckBox versioned = new CheckBox();
\r
109 final FileResource file;
\r
111 String initialTagText;
\r
113 final TabPanel inner;
\r
116 * The widget's constructor.
\r
118 * @param images the dialog's ImageBundle
\r
122 public FilePropertiesDialog(final Images images, final List<GroupResource> groups, List<FileResource> bodies) {
\r
124 // Enable IE selection for the dialog (must disable it upon closing it)
\r
125 GSS.enableIESelection();
\r
127 // Set the dialog's caption.
\r
128 setText("File properties");
\r
129 setAnimationEnabled(true);
\r
130 file = (FileResource) GSS.get().getCurrentSelection();
\r
131 permList = new PermissionsList(images, file.getPermissions(), file.getOwner());
\r
133 // Outer contains inner and buttons.
\r
134 final VerticalPanel outer = new VerticalPanel();
\r
135 final FocusPanel focusPanel = new FocusPanel(outer);
\r
136 // Inner contains generalPanel and permPanel.
\r
137 inner = new TabPanel();
\r
138 final VerticalPanel generalPanel = new VerticalPanel();
\r
139 final VerticalPanel permPanel = new VerticalPanel();
\r
140 final HorizontalPanel buttons = new HorizontalPanel();
\r
141 final HorizontalPanel permButtons = new HorizontalPanel();
\r
142 final HorizontalPanel permForAll = new HorizontalPanel();
\r
143 final HorizontalPanel pathPanel = new HorizontalPanel();
\r
144 final VerticalPanel verPanel = new VerticalPanel();
\r
145 final HorizontalPanel vPanel = new HorizontalPanel();
\r
146 final HorizontalPanel vPanel2 = new HorizontalPanel();
\r
148 versioned.setChecked(file.isVersioned());
\r
149 inner.add(generalPanel, "General");
\r
150 inner.add(permPanel, "Sharing");
\r
151 inner.add(verPanel, "Versions");
\r
152 inner.selectTab(0);
\r
154 final FlexTable generalTable = new FlexTable();
\r
155 generalTable.setText(0, 0, "Name");
\r
156 generalTable.setText(1, 0, "Folder");
\r
157 generalTable.setText(2, 0, "Owner");
\r
158 generalTable.setText(3, 0, "Date");
\r
159 generalTable.setText(4, 0, "Tags");
\r
160 name.setText(file.getName());
\r
161 generalTable.setWidget(0, 1, name);
\r
162 if(file.getFolderName() != null)
\r
163 generalTable.setText(1, 1, file.getFolderName());
\r
165 generalTable.setText(1, 1, "-");
\r
166 generalTable.setText(2, 1, file.getOwner());
\r
167 final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
\r
168 generalTable.setText(3, 1, formatter.format(file.getCreationDate()));
\r
170 StringBuffer tagsBuffer = new StringBuffer();
\r
171 Iterator i = file.getTags().iterator();
\r
172 while (i.hasNext()) {
\r
173 String tag = (String) i.next();
\r
174 tagsBuffer.append(tag).append(", ");
\r
176 if (tagsBuffer.length() > 1)
\r
177 tagsBuffer.delete(tagsBuffer.length() - 2, tagsBuffer.length() - 1);
\r
178 initialTagText = tagsBuffer.toString();
\r
179 tags.setText(initialTagText);
\r
180 generalTable.setWidget(4, 1, tags);
\r
181 generalTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
\r
182 generalTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
\r
183 generalTable.getFlexCellFormatter().setStyleName(2, 0, "props-labels");
\r
184 generalTable.getFlexCellFormatter().setStyleName(3, 0, "props-labels");
\r
185 generalTable.getFlexCellFormatter().setStyleName(4, 0, "props-labels");
\r
186 generalTable.getFlexCellFormatter().setStyleName(0, 1, "props-values");
\r
187 generalTable.getFlexCellFormatter().setStyleName(1, 1, "props-values");
\r
188 generalTable.getFlexCellFormatter().setStyleName(2, 1, "props-values");
\r
189 generalTable.getFlexCellFormatter().setStyleName(3, 1, "props-values");
\r
190 generalTable.getFlexCellFormatter().setStyleName(4, 1, "props-values");
\r
191 generalTable.setCellSpacing(4);
\r
193 // Create the 'OK' button, along with a listener that hides the dialog
\r
194 // when the button is clicked.
\r
195 final Button ok = new Button("OK", new ClickListener() {
\r
197 public void onClick(Widget sender) {
\r
203 buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
\r
204 // Create the 'Cancel' button, along with a listener that hides the
\r
205 // dialog when the button is clicked.
\r
206 final Button cancel = new Button("Cancel", new ClickListener() {
\r
208 public void onClick(Widget sender) {
\r
212 buttons.add(cancel);
\r
213 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
\r
214 buttons.setSpacing(8);
\r
215 buttons.addStyleName("gwt-TabPanelBottom");
\r
217 generalPanel.add(generalTable);
\r
219 // Asynchronously retrieve the tags defined by this user.
\r
220 DeferredCommand.addCommand(new Command() {
\r
222 public void execute() {
\r
227 DisclosurePanel allTags = new DisclosurePanel("All tags");
\r
228 allTagsContent = new FlowPanel();
\r
229 allTags.setContent(allTagsContent);
\r
230 generalPanel.add(allTags);
\r
231 generalPanel.setSpacing(4);
\r
233 final Button add = new Button("Add Group", new ClickListener() {
\r
235 public void onClick(Widget sender) {
\r
236 PermissionsAddDialog dlg = new PermissionsAddDialog(groups, permList, false);
\r
240 permButtons.add(add);
\r
241 permButtons.setCellHorizontalAlignment(add, HasHorizontalAlignment.ALIGN_CENTER);
\r
243 final Button addUser = new Button("Add User", new ClickListener() {
\r
245 public void onClick(Widget sender) {
\r
246 PermissionsAddDialog dlg = new PermissionsAddDialog(groups, permList, true);
\r
250 permButtons.add(addUser);
\r
251 permButtons.setCellHorizontalAlignment(addUser, HasHorizontalAlignment.ALIGN_CENTER);
\r
253 permButtons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
\r
254 permButtons.setSpacing(8);
\r
255 permButtons.addStyleName("gwt-TabPanelBottom");
\r
257 final Label readForAllNote = new Label("When this option is enabled, the file will be readable" +
\r
258 " by everyone. By checking this option, you are certifying that you have the right to " +
\r
259 "distribute this file and that it does not violate the Terms of Use.", true);
\r
260 readForAllNote.setVisible(false);
\r
261 readForAllNote.setStylePrimaryName("gss-readForAllNote");
\r
263 readForAll = new CheckBox();
\r
264 readForAll.setChecked(file.isReadForAll());
\r
265 readForAll.addClickListener(new ClickListener() {
\r
267 public void onClick(Widget sender) {
\r
268 if (readForAll.isChecked()) {
\r
269 readForAllNote.setVisible(true);
\r
270 pathPanel.setVisible(true);
\r
273 readForAllNote.setVisible(false);
\r
274 pathPanel.setVisible(false);
\r
280 permPanel.add(permList);
\r
281 permPanel.add(permButtons);
\r
282 // Only show the read for all permission if the user is the owner.
\r
283 if (file.getOwner().equals(GSS.get().getCurrentUserResource().getUsername())) {
\r
284 permForAll.add(new Label("Make Public"));
\r
285 permForAll.add(readForAll);
\r
286 permForAll.setSpacing(8);
\r
287 permForAll.addStyleName("gwt-TabPanelBottom");
\r
288 permPanel.add(permForAll);
\r
289 permPanel.add(readForAllNote);
\r
292 TextBox path = new TextBox();
\r
293 path.addClickListener(new ClickListener () {
\r
295 public void onClick(Widget sender) {
\r
296 GSS.enableIESelection();
\r
297 ((TextBox) sender).selectAll();
\r
298 GSS.preventIESelection();
\r
302 path.setText(file.getUri());
\r
303 path.setTitle("Use this URI for sharing this file with the world");
\r
304 path.setReadOnly(true);
\r
305 pathPanel.add(new Label("Sharing URI"));
\r
306 pathPanel.setSpacing(8);
\r
307 pathPanel.addStyleName("gwt-TabPanelBottom");
\r
308 pathPanel.add(path);
\r
309 pathPanel.setVisible(file.isReadForAll());
\r
310 permPanel.add(pathPanel);
\r
312 VersionsList verList = new VersionsList(this, images, bodies);
\r
313 verPanel.add(verList);
\r
315 vPanel.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
\r
316 vPanel.setSpacing(8);
\r
317 vPanel.addStyleName("gwt-TabPanelBottom");
\r
318 vPanel.add(new Label("Versioned"));
\r
320 vPanel.add(versioned);
\r
321 verPanel.add(vPanel);
\r
322 vPanel2.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
\r
323 vPanel2.setSpacing(8);
\r
324 vPanel2.addStyleName("gwt-TabPanelBottom");
\r
325 Button removeVersionsButton = new Button(images.delete().getHTML(), new ClickListener() {
\r
327 public void onClick(Widget sender) {
\r
328 ConfirmationDialog confirm = new ConfirmationDialog("Really " +
\r
329 "remove all previous versions?", "Remove") {
\r
332 public void cancel() {
\r
336 public void confirm() {
\r
337 FilePropertiesDialog.this.closeDialog();
\r
338 removeAllOldVersions();
\r
346 HTML removeAllVersion = new HTML("<span>Remove all previous versions?</span>");
\r
347 vPanel2.add(removeAllVersion);
\r
348 vPanel2.add(removeVersionsButton);
\r
349 verPanel.add(vPanel2);
\r
350 if(!file.isVersioned())
\r
351 vPanel2.setVisible(false);
\r
353 outer.add(buttons);
\r
354 outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
\r
355 outer.addStyleName("gwt-TabPanelBottom");
\r
357 focusPanel.setFocus(true);
\r
362 * Retrieves all user tags from the server and updates the FlowPanel
\r
366 private void updateTags() {
\r
367 GetCommand<TagsResource> tc = new GetCommand<TagsResource>(TagsResource.class, GSS.get().getCurrentUserResource().getTagsPath()) {
\r
370 public void onComplete() {
\r
371 allTagsContent.clear();
\r
372 TagsResource tagr = getResult();
\r
373 List<String> userTags = tagr.getTags();
\r
374 Iterator t = userTags.iterator();
\r
375 while (t.hasNext()) {
\r
376 final Button tag = new Button((String) t.next(), new ClickListener() {
\r
378 public void onClick(Widget sender) {
\r
379 String existing = tags.getText();
\r
380 String newTag = ((Button) sender).getText().trim();
\r
381 // insert the new tag only if it is not in the list
\r
383 if (existing.indexOf(newTag + ",") == -1 && !existing.trim().endsWith(newTag))
\r
384 tags.setText(existing.trim() + (existing.length() > 0 ? ", " : "") + newTag);
\r
387 allTagsContent.add(tag);
\r
392 public void onError(Throwable t) {
\r
394 GSS.get().displayError("Unable to fetch user tags");
\r
397 DeferredCommand.addCommand(tc);
\r
402 @SuppressWarnings("fallthrough")
\r
403 public boolean onKeyDownPreview(char key, int modifiers) {
\r
404 // Use the popup's key preview hooks to close the dialog when either
\r
405 // enter or escape is pressed.
\r
407 case KeyboardListener.KEY_ENTER:
\r
409 case KeyboardListener.KEY_ESCAPE:
\r
418 * Accepts any change and updates the file
\r
422 private void accept() {
\r
423 permList.updatePermissionsAccordingToInput();
\r
424 Set<PermissionHolder> perms = permList.getPermissions();
\r
425 JSONObject json = new JSONObject();
\r
426 if (!name.getText().equals(file.getName()))
\r
427 json.put("name", new JSONString(name.getText()));
\r
428 if (versioned.isChecked() != file.isVersioned())
\r
429 json.put("versioned", JSONBoolean.getInstance(versioned.isChecked()));
\r
430 //only update the read for all perm if the user is the owner
\r
431 if (readForAll.isChecked() != file.isReadForAll())
\r
432 if (file.getOwner().equals(GSS.get().getCurrentUserResource().getUsername()))
\r
433 json.put("readForAll", JSONBoolean.getInstance(readForAll.isChecked()));
\r
435 if (permList.hasChanges()) {
\r
436 GWT.log("Permissions change", null);
\r
437 JSONArray perma = new JSONArray();
\r
439 for (PermissionHolder p : perms) {
\r
440 JSONObject po = new JSONObject();
\r
441 if (p.getUser() != null)
\r
442 po.put("user", new JSONString(p.getUser()));
\r
443 if (p.getGroup() != null)
\r
444 po.put("group", new JSONString(p.getGroup()));
\r
445 po.put("read", JSONBoolean.getInstance(p.isRead()));
\r
446 po.put("write", JSONBoolean.getInstance(p.isWrite()));
\r
447 po.put("modifyACL", JSONBoolean.getInstance(p.isModifyACL()));
\r
451 json.put("permissions", perma);
\r
453 JSONArray taga = new JSONArray();
\r
455 if (!tags.getText().equals(initialTagText)) {
\r
456 String[] tagset = tags.getText().split(",");
\r
457 for (String t : tagset) {
\r
458 JSONString to = new JSONString(t);
\r
462 json.put("tags", taga);
\r
464 String jsonString = json.toString();
\r
465 if(jsonString.equals("{}")){
\r
466 GWT.log("NO CHANGES", null);
\r
469 PostCommand cf = new PostCommand(file.getUri() + "?update=", jsonString, 200) {
\r
472 public void onComplete() {
\r
473 GSS.get().getFileList().updateFileCache(true, false /* do not clear selected file*/);
\r
477 public void onError(Throwable t) {
\r
479 if (t instanceof RestException) {
\r
480 int statusCode = ((RestException) t).getHttpStatusCode();
\r
481 if (statusCode == 405)
\r
482 GSS.get().displayError("You don't have the necessary permissions");
\r
483 else if (statusCode == 404)
\r
484 GSS.get().displayError("User in permissions does not exist");
\r
485 else if (statusCode == 409)
\r
486 GSS.get().displayError("A file with the same name already exists");
\r
487 else if (statusCode == 413)
\r
488 GSS.get().displayError("Your quota has been exceeded");
\r
490 GSS.get().displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
\r
492 GSS.get().displayError("System error modifying file:" + t.getMessage());
\r
496 DeferredCommand.addCommand(cf);
\r
500 private void removeAllOldVersions() {
\r
501 JSONObject json = new JSONObject();
\r
502 json.put("versioned", JSONBoolean.getInstance(false));
\r
503 GWT.log(json.toString(), null);
\r
504 PostCommand cf = new PostCommand(file.getUri() + "?update=", json.toString(), 200) {
\r
507 public void onComplete() {
\r
508 toggleVersioned(true);
\r
512 public void onError(Throwable t) {
\r
514 if (t instanceof RestException) {
\r
515 int statusCode = ((RestException) t).getHttpStatusCode();
\r
516 if (statusCode == 405)
\r
517 GSS.get().displayError("You don't have the necessary permissions");
\r
518 else if (statusCode == 404)
\r
519 GSS.get().displayError("User in permissions does not exist");
\r
520 else if (statusCode == 409)
\r
521 GSS.get().displayError("A folder with the same name already exists");
\r
522 else if (statusCode == 413)
\r
523 GSS.get().displayError("Your quota has been exceeded");
\r
525 GSS.get().displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
\r
527 GSS.get().displayError("System error moifying file:" + t.getMessage());
\r
530 DeferredCommand.addCommand(cf);
\r
533 private void toggleVersioned(boolean versionedValue) {
\r
534 JSONObject json = new JSONObject();
\r
535 json.put("versioned", JSONBoolean.getInstance(versionedValue));
\r
536 GWT.log(json.toString(), null);
\r
537 PostCommand cf = new PostCommand(file.getUri() + "?update=", json.toString(), 200) {
\r
540 public void onComplete() {
\r
541 GSS.get().getFileList().updateFileCache(true, false /* do not clear selected file*/);
\r
545 public void onError(Throwable t) {
\r
547 if (t instanceof RestException) {
\r
548 int statusCode = ((RestException) t).getHttpStatusCode();
\r
549 if (statusCode == 405)
\r
550 GSS.get().displayError("You don't have the necessary permissions");
\r
551 else if (statusCode == 404)
\r
552 GSS.get().displayError("User in permissions does not exist");
\r
553 else if (statusCode == 409)
\r
554 GSS.get().displayError("A folder with the same name already exists");
\r
555 else if (statusCode == 413)
\r
556 GSS.get().displayError("Your quota has been exceeded");
\r
558 GSS.get().displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
\r
560 GSS.get().displayError("System error moifying file:" + t.getMessage());
\r
563 DeferredCommand.addCommand(cf);
\r
566 public void selectTab(int _tab) {
\r
567 inner.selectTab(_tab);
\r
571 * Enables IE selection prevention and hides the dialog
\r
572 * (we disable the prevention on creation of the dialog)
\r
574 public void closeDialog() {
\r
575 GSS.preventIESelection();
\r