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.ExecuteGet;
\r
22 import gr.ebs.gss.client.rest.ExecutePost;
\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.FocusListener;
\r
50 import com.google.gwt.user.client.ui.FocusPanel;
\r
51 import com.google.gwt.user.client.ui.HTML;
\r
52 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
\r
53 import com.google.gwt.user.client.ui.HorizontalPanel;
\r
54 import com.google.gwt.user.client.ui.KeyboardListener;
\r
55 import com.google.gwt.user.client.ui.Label;
\r
56 import com.google.gwt.user.client.ui.TabPanel;
\r
57 import com.google.gwt.user.client.ui.TextBox;
\r
58 import com.google.gwt.user.client.ui.VerticalPanel;
\r
59 import com.google.gwt.user.client.ui.Widget;
\r
62 * The 'File properties' dialog box implementation.
\r
66 public class FilePropertiesDialog extends DialogBox {
\r
68 final PermissionsList permList;
\r
70 private CheckBox readForAll;
\r
73 * An image bundle for this widgets images.
\r
75 public interface Images extends MessagePanel.Images {
\r
77 @Resource("gr/ebs/gss/resources/edit_user.png")
\r
78 AbstractImagePrototype permUser();
\r
80 @Resource("gr/ebs/gss/resources/groupevent.png")
\r
81 AbstractImagePrototype permGroup();
\r
83 @Resource("gr/ebs/gss/resources/editdelete.png")
\r
84 AbstractImagePrototype delete();
\r
86 @Resource("gr/ebs/gss/resources/db_update.png")
\r
87 AbstractImagePrototype restore();
\r
89 @Resource("gr/ebs/gss/resources/folder_inbox.png")
\r
90 AbstractImagePrototype download();
\r
94 * The widget that holds the name of the file.
\r
96 private TextBox name = new TextBox();
\r
99 * Text box with the tags associated with the file
\r
101 private TextBox tags = new TextBox();
\r
104 * A FlowPanel with all user tags
\r
106 private FlowPanel allTagsContent;
\r
108 private final CheckBox versioned = new CheckBox();
\r
110 final FileResource file;
\r
112 String initialTagText;
\r
114 final TabPanel inner;
\r
117 * The widget's constructor.
\r
119 * @param images the dialog's ImageBundle
\r
123 public FilePropertiesDialog(final Images images, final List<GroupResource> groups, List<FileResource> bodies) {
\r
124 // Set the dialog's caption.
\r
125 setText("File properties");
\r
126 setAnimationEnabled(true);
\r
127 file = (FileResource) GSS.get().getCurrentSelection();
\r
128 permList = new PermissionsList(images, file.getPermissions(), file.getOwner());
\r
130 // Outer contains inner and buttons.
\r
131 final VerticalPanel outer = new VerticalPanel();
\r
132 final FocusPanel focusPanel = new FocusPanel(outer);
\r
133 // Inner contains generalPanel and permPanel.
\r
134 inner = new TabPanel();
\r
135 final VerticalPanel generalPanel = new VerticalPanel();
\r
136 final VerticalPanel permPanel = new VerticalPanel();
\r
137 final HorizontalPanel buttons = new HorizontalPanel();
\r
138 final HorizontalPanel permButtons = new HorizontalPanel();
\r
139 final HorizontalPanel permForAll = new HorizontalPanel();
\r
140 final HorizontalPanel pathPanel = new HorizontalPanel();
\r
141 final VerticalPanel verPanel = new VerticalPanel();
\r
142 final HorizontalPanel vPanel = new HorizontalPanel();
\r
143 final HorizontalPanel vPanel2 = new HorizontalPanel();
\r
145 versioned.setChecked(file.isVersioned());
\r
146 inner.add(generalPanel, "General");
\r
147 inner.add(permPanel, "Sharing");
\r
148 inner.add(verPanel, "Versions");
\r
149 inner.selectTab(0);
\r
151 final FlexTable generalTable = new FlexTable();
\r
152 generalTable.setText(0, 0, "Name");
\r
153 generalTable.setText(1, 0, "Folder");
\r
154 generalTable.setText(2, 0, "Owner");
\r
155 generalTable.setText(3, 0, "Date");
\r
156 generalTable.setText(4, 0, "Tags");
\r
157 name.setText(file.getName());
\r
158 generalTable.setWidget(0, 1, name);
\r
159 if(file.getFolderName() != null)
\r
160 generalTable.setText(1, 1, file.getFolderName());
\r
162 generalTable.setText(1, 1, "-");
\r
163 generalTable.setText(2, 1, file.getOwner());
\r
164 final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
\r
165 generalTable.setText(3, 1, formatter.format(file.getCreationDate()));
\r
167 StringBuffer tagsBuffer = new StringBuffer();
\r
168 Iterator i = file.getTags().iterator();
\r
169 while (i.hasNext()) {
\r
170 String tag = (String) i.next();
\r
171 tagsBuffer.append(tag).append(", ");
\r
173 if (tagsBuffer.length() > 1)
\r
174 tagsBuffer.delete(tagsBuffer.length() - 2, tagsBuffer.length() - 1);
\r
175 initialTagText = tagsBuffer.toString();
\r
176 tags.setText(initialTagText);
\r
177 generalTable.setWidget(4, 1, tags);
\r
178 generalTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
\r
179 generalTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
\r
180 generalTable.getFlexCellFormatter().setStyleName(2, 0, "props-labels");
\r
181 generalTable.getFlexCellFormatter().setStyleName(3, 0, "props-labels");
\r
182 generalTable.getFlexCellFormatter().setStyleName(4, 0, "props-labels");
\r
183 generalTable.getFlexCellFormatter().setStyleName(0, 1, "props-values");
\r
184 generalTable.getFlexCellFormatter().setStyleName(1, 1, "props-values");
\r
185 generalTable.getFlexCellFormatter().setStyleName(2, 1, "props-values");
\r
186 generalTable.getFlexCellFormatter().setStyleName(3, 1, "props-values");
\r
187 generalTable.getFlexCellFormatter().setStyleName(4, 1, "props-values");
\r
188 generalTable.setCellSpacing(4);
\r
190 // Create the 'OK' button, along with a listener that hides the dialog
\r
191 // when the button is clicked.
\r
192 final Button ok = new Button("OK", new ClickListener() {
\r
194 public void onClick(Widget sender) {
\r
200 buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
\r
201 // Create the 'Cancel' button, along with a listener that hides the
\r
202 // dialog when the button is clicked.
\r
203 final Button cancel = new Button("Cancel", new ClickListener() {
\r
205 public void onClick(Widget sender) {
\r
209 buttons.add(cancel);
\r
210 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
\r
211 buttons.setSpacing(8);
\r
212 buttons.addStyleName("gwt-TabPanelBottom");
\r
214 generalPanel.add(generalTable);
\r
216 // Asynchronously retrieve the tags defined by this user.
\r
217 DeferredCommand.addCommand(new Command() {
\r
219 public void execute() {
\r
224 DisclosurePanel allTags = new DisclosurePanel("All tags");
\r
225 allTagsContent = new FlowPanel();
\r
226 allTags.setContent(allTagsContent);
\r
227 generalPanel.add(allTags);
\r
228 generalPanel.setSpacing(4);
\r
230 final Button add = new Button("Add Group", new ClickListener() {
\r
232 public void onClick(Widget sender) {
\r
233 PermissionsAddDialog dlg = new PermissionsAddDialog(groups, permList, false);
\r
237 permButtons.add(add);
\r
238 permButtons.setCellHorizontalAlignment(add, HasHorizontalAlignment.ALIGN_CENTER);
\r
240 final Button addUser = new Button("Add User", new ClickListener() {
\r
242 public void onClick(Widget sender) {
\r
243 PermissionsAddDialog dlg = new PermissionsAddDialog(groups, permList, true);
\r
247 permButtons.add(addUser);
\r
248 permButtons.setCellHorizontalAlignment(addUser, HasHorizontalAlignment.ALIGN_CENTER);
\r
250 permButtons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
\r
251 permButtons.setSpacing(8);
\r
252 permButtons.addStyleName("gwt-TabPanelBottom");
\r
254 final Label readForAllNote = new Label("When this option is enabled, the file will be readable" +
\r
255 " by everyone. By checking this option, you are certifying that you have the right to " +
\r
256 "distribute this file and that it does not violate the Terms of Use.", true);
\r
257 readForAllNote.setVisible(false);
\r
258 readForAllNote.setStylePrimaryName("gss-readForAllNote");
\r
260 readForAll = new CheckBox();
\r
261 readForAll.setChecked(file.isReadForAll());
\r
262 readForAll.addClickListener(new ClickListener() {
\r
264 public void onClick(Widget sender) {
\r
265 if (readForAll.isChecked()) {
\r
266 readForAllNote.setVisible(true);
\r
267 pathPanel.setVisible(true);
\r
270 readForAllNote.setVisible(false);
\r
271 pathPanel.setVisible(false);
\r
277 permPanel.add(permList);
\r
278 permPanel.add(permButtons);
\r
279 // Only show the read for all permission if the user is the owner.
\r
280 if (file.getOwner().equals(GSS.get().getCurrentUserResource().getUsername())) {
\r
281 permForAll.add(new Label("Make Public"));
\r
282 permForAll.add(readForAll);
\r
283 permForAll.setSpacing(8);
\r
284 permForAll.addStyleName("gwt-TabPanelBottom");
\r
285 permPanel.add(permForAll);
\r
286 permPanel.add(readForAllNote);
\r
289 TextBox path = new TextBox();
\r
290 path.addFocusListener(new FocusListener() {
\r
291 public void onFocus(Widget sender) {
\r
292 ((TextBox) sender).selectAll();
\r
294 public void onLostFocus(Widget sender) {
\r
295 ((TextBox) sender).setSelectionRange(0, 0);
\r
298 path.setText(file.getPath());
\r
299 path.setTitle("Use this URI for sharing this file with the world");
\r
300 path.setReadOnly(true);
\r
301 pathPanel.add(new Label("Sharing URI"));
\r
302 pathPanel.setSpacing(8);
\r
303 pathPanel.addStyleName("gwt-TabPanelBottom");
\r
304 pathPanel.add(path);
\r
305 pathPanel.setVisible(file.isReadForAll());
\r
306 permPanel.add(pathPanel);
\r
308 VersionsList verList = new VersionsList(this, images, bodies);
\r
309 verPanel.add(verList);
\r
311 vPanel.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
\r
312 vPanel.setSpacing(8);
\r
313 vPanel.addStyleName("gwt-TabPanelBottom");
\r
314 vPanel.add(new Label("Versioned"));
\r
316 vPanel.add(versioned);
\r
317 verPanel.add(vPanel);
\r
318 vPanel2.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
\r
319 vPanel2.setSpacing(8);
\r
320 vPanel2.addStyleName("gwt-TabPanelBottom");
\r
321 Button removeVersionsButton = new Button(images.delete().getHTML(), new ClickListener() {
\r
323 public void onClick(Widget sender) {
\r
324 ConfirmationDialog confirm = new ConfirmationDialog(images,"Really remove all previous versions?","Remove"){
\r
326 public void cancel() {
\r
330 public void confirm() {
\r
331 FilePropertiesDialog.this.hide();
\r
332 removeAllOldVersions();
\r
340 HTML removeAllVersion = new HTML("<span>Remove all previous versions?</span>");
\r
341 vPanel2.add(removeAllVersion);
\r
342 vPanel2.add(removeVersionsButton);
\r
343 verPanel.add(vPanel2);
\r
344 if(!file.isVersioned())
\r
345 vPanel2.setVisible(false);
\r
347 outer.add(buttons);
\r
348 outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
\r
349 outer.addStyleName("gwt-TabPanelBottom");
\r
351 focusPanel.setFocus(true);
\r
356 * Retrieves all user tags from the server and updates the FlowPanel
\r
360 private void updateTags() {
\r
361 ExecuteGet<TagsResource> tc = new ExecuteGet<TagsResource>(TagsResource.class, GSS.get().getCurrentUserResource().getTagsPath()) {
\r
363 public void onComplete() {
\r
364 allTagsContent.clear();
\r
365 TagsResource tagr = getResult();
\r
366 List<String> userTags = tagr.getTags();
\r
367 Iterator t = userTags.iterator();
\r
368 while (t.hasNext()) {
\r
369 final Button tag = new Button((String) t.next(), new ClickListener() {
\r
371 public void onClick(Widget sender) {
\r
372 String existing = tags.getText();
\r
373 String newTag = ((Button) sender).getText().trim();
\r
374 // insert the new tag only if it is not in the list
\r
376 if (existing.indexOf(newTag + ",") == -1 && !existing.trim().endsWith(newTag))
\r
377 tags.setText(existing.trim() + (existing.length() > 0 ? ", " : "") + newTag);
\r
380 allTagsContent.add(tag);
\r
385 public void onError(Throwable t) {
\r
387 GSS.get().displayError("Unable to fetch user tags");
\r
390 DeferredCommand.addCommand(tc);
\r
395 @SuppressWarnings("fallthrough")
\r
396 public boolean onKeyDownPreview(char key, int modifiers) {
\r
397 // Use the popup's key preview hooks to close the dialog when either
\r
398 // enter or escape is pressed.
\r
400 case KeyboardListener.KEY_ENTER:
\r
402 case KeyboardListener.KEY_ESCAPE:
\r
411 * Accepts any change and updates the file
\r
415 private void accept() {
\r
416 permList.updatePermissionsAccordingToInput();
\r
417 Set<PermissionHolder> perms = permList.getPermissions();
\r
418 JSONObject json = new JSONObject();
\r
419 if (!name.getText().equals(file.getName()))
\r
420 json.put("name", new JSONString(name.getText()));
\r
421 if (versioned.isChecked() != file.isVersioned())
\r
422 json.put("versioned", JSONBoolean.getInstance(versioned.isChecked()));
\r
423 //only update the read for all perm if the user is the owner
\r
424 if (readForAll.isChecked() != file.isReadForAll())
\r
425 if (file.getOwner().equals(GSS.get().getCurrentUserResource().getUsername()))
\r
426 json.put("readForAll", JSONBoolean.getInstance(readForAll.isChecked()));
\r
428 if (permList.hasChanges()) {
\r
429 GWT.log("Permissions change", null);
\r
430 JSONArray perma = new JSONArray();
\r
432 for (PermissionHolder p : perms) {
\r
433 JSONObject po = new JSONObject();
\r
434 if (p.getUser() != null)
\r
435 po.put("user", new JSONString(p.getUser()));
\r
436 if (p.getGroup() != null)
\r
437 po.put("group", new JSONString(p.getGroup()));
\r
438 po.put("read", JSONBoolean.getInstance(p.isRead()));
\r
439 po.put("write", JSONBoolean.getInstance(p.isWrite()));
\r
440 po.put("modifyACL", JSONBoolean.getInstance(p.isModifyACL()));
\r
444 json.put("permissions", perma);
\r
446 JSONArray taga = new JSONArray();
\r
448 if (!tags.getText().equals(initialTagText)) {
\r
449 String[] tagset = tags.getText().split(",");
\r
450 for (String t : tagset) {
\r
451 JSONString to = new JSONString(t);
\r
455 json.put("tags", taga);
\r
457 String jsonString = json.toString();
\r
458 if(jsonString.equals("{}")){
\r
459 GWT.log("NO CHANGES", null);
\r
462 ExecutePost cf = new ExecutePost(file.getPath() + "?update=", jsonString, 200) {
\r
464 public void onComplete() {
\r
465 GSS.get().getFileList().updateFileCache(true, false /* do not clear selected file*/);
\r
468 public void onError(Throwable t) {
\r
470 if (t instanceof RestException) {
\r
471 int statusCode = ((RestException) t).getHttpStatusCode();
\r
472 if (statusCode == 405)
\r
473 GSS.get().displayError("You don't have the necessary permissions");
\r
474 else if (statusCode == 404)
\r
475 GSS.get().displayError("User in permissions does not exist");
\r
476 else if (statusCode == 409)
\r
477 GSS.get().displayError("A file with the same name already exists");
\r
478 else if (statusCode == 413)
\r
479 GSS.get().displayError("Your quota has been exceeded");
\r
481 GSS.get().displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
\r
483 GSS.get().displayError("System error modifying file:" + t.getMessage());
\r
487 DeferredCommand.addCommand(cf);
\r
491 private void removeAllOldVersions() {
\r
492 JSONObject json = new JSONObject();
\r
493 json.put("versioned", JSONBoolean.getInstance(false));
\r
494 GWT.log(json.toString(), null);
\r
495 ExecutePost cf = new ExecutePost(file.getPath() + "?update=", json.toString(), 200) {
\r
497 public void onComplete() {
\r
498 toggleVersioned(true);
\r
501 public void onError(Throwable t) {
\r
503 if (t instanceof RestException) {
\r
504 int statusCode = ((RestException) t).getHttpStatusCode();
\r
505 if (statusCode == 405)
\r
506 GSS.get().displayError("You don't have the necessary permissions");
\r
507 else if (statusCode == 404)
\r
508 GSS.get().displayError("User in permissions does not exist");
\r
509 else if (statusCode == 409)
\r
510 GSS.get().displayError("A folder with the same name already exists");
\r
511 else if (statusCode == 413)
\r
512 GSS.get().displayError("Your quota has been exceeded");
\r
514 GSS.get().displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
\r
516 GSS.get().displayError("System error moifying file:" + t.getMessage());
\r
519 DeferredCommand.addCommand(cf);
\r
522 private void toggleVersioned(boolean versionedValue) {
\r
523 JSONObject json = new JSONObject();
\r
524 json.put("versioned", JSONBoolean.getInstance(versionedValue));
\r
525 GWT.log(json.toString(), null);
\r
526 ExecutePost cf = new ExecutePost(file.getPath() + "?update=", json.toString(), 200) {
\r
528 public void onComplete() {
\r
529 GSS.get().getFileList().updateFileCache(true, false /* do not clear selected file*/);
\r
532 public void onError(Throwable t) {
\r
534 if (t instanceof RestException) {
\r
535 int statusCode = ((RestException) t).getHttpStatusCode();
\r
536 if (statusCode == 405)
\r
537 GSS.get().displayError("You don't have the necessary permissions");
\r
538 else if (statusCode == 404)
\r
539 GSS.get().displayError("User in permissions does not exist");
\r
540 else if (statusCode == 409)
\r
541 GSS.get().displayError("A folder with the same name already exists");
\r
542 else if (statusCode == 413)
\r
543 GSS.get().displayError("Your quota has been exceeded");
\r
545 GSS.get().displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
\r
547 GSS.get().displayError("System error moifying file:" + t.getMessage());
\r
550 DeferredCommand.addCommand(cf);
\r
553 public void selectTab(int _tab) {
\r
554 inner.selectTab(_tab);
\r