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, "Last modified");
\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.getModificationDate()));
\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 permForAll.add(readForAllNote);
\r
289 permPanel.add(permForAll);
\r
292 TextBox path = new TextBox();
\r
293 path.setWidth("100%");
\r
294 path.addClickListener(new ClickListener () {
\r
296 public void onClick(Widget sender) {
\r
297 GSS.enableIESelection();
\r
298 ((TextBox) sender).selectAll();
\r
299 GSS.preventIESelection();
\r
303 path.setText(file.getUri());
\r
304 path.setTitle("Use this URI for sharing this file with the world (crtl-C/cmd-C to copy to system clipboard)");
\r
305 path.setWidth("100%");
\r
306 path.setReadOnly(true);
\r
307 pathPanel.setWidth("100%");
\r
308 pathPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
\r
309 pathPanel.add(new Label("Sharing URI"));
\r
310 pathPanel.setSpacing(8);
\r
311 pathPanel.addStyleName("gwt-TabPanelBottom");
\r
312 pathPanel.add(path);
\r
313 pathPanel.setVisible(file.isReadForAll());
\r
314 permPanel.add(pathPanel);
\r
316 VersionsList verList = new VersionsList(this, images, bodies);
\r
317 verPanel.add(verList);
\r
319 vPanel.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
\r
320 vPanel.setSpacing(8);
\r
321 vPanel.addStyleName("gwt-TabPanelBottom");
\r
322 vPanel.add(new Label("Versioned"));
\r
324 vPanel.add(versioned);
\r
325 verPanel.add(vPanel);
\r
326 vPanel2.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
\r
327 vPanel2.setSpacing(8);
\r
328 vPanel2.addStyleName("gwt-TabPanelBottom");
\r
329 Button removeVersionsButton = new Button(images.delete().getHTML(), new ClickListener() {
\r
331 public void onClick(Widget sender) {
\r
332 ConfirmationDialog confirm = new ConfirmationDialog("Really " +
\r
333 "remove all previous versions?", "Remove") {
\r
336 public void cancel() {
\r
340 public void confirm() {
\r
341 FilePropertiesDialog.this.closeDialog();
\r
342 removeAllOldVersions();
\r
350 HTML removeAllVersion = new HTML("<span>Remove all previous versions?</span>");
\r
351 vPanel2.add(removeAllVersion);
\r
352 vPanel2.add(removeVersionsButton);
\r
353 verPanel.add(vPanel2);
\r
354 if(!file.isVersioned())
\r
355 vPanel2.setVisible(false);
\r
357 outer.add(buttons);
\r
358 outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
\r
359 outer.addStyleName("gwt-TabPanelBottom");
\r
361 focusPanel.setFocus(true);
\r
366 * Retrieves all user tags from the server and updates the FlowPanel
\r
370 private void updateTags() {
\r
371 GetCommand<TagsResource> tc = new GetCommand<TagsResource>(TagsResource.class, GSS.get().getCurrentUserResource().getTagsPath()) {
\r
374 public void onComplete() {
\r
375 allTagsContent.clear();
\r
376 TagsResource tagr = getResult();
\r
377 List<String> userTags = tagr.getTags();
\r
378 Iterator t = userTags.iterator();
\r
379 while (t.hasNext()) {
\r
380 final Button tag = new Button((String) t.next(), new ClickListener() {
\r
382 public void onClick(Widget sender) {
\r
383 String existing = tags.getText();
\r
384 String newTag = ((Button) sender).getText().trim();
\r
385 // insert the new tag only if it is not in the list
\r
387 if (existing.indexOf(newTag + ",") == -1 && !existing.trim().endsWith(newTag))
\r
388 tags.setText(existing.trim() + (existing.length() > 0 ? ", " : "") + newTag);
\r
391 allTagsContent.add(tag);
\r
396 public void onError(Throwable t) {
\r
398 GSS.get().displayError("Unable to fetch user tags");
\r
401 DeferredCommand.addCommand(tc);
\r
406 @SuppressWarnings("fallthrough")
\r
407 public boolean onKeyDownPreview(char key, int modifiers) {
\r
408 // Use the popup's key preview hooks to close the dialog when either
\r
409 // enter or escape is pressed.
\r
411 case KeyboardListener.KEY_ENTER:
\r
413 case KeyboardListener.KEY_ESCAPE:
\r
422 * Accepts any change and updates the file
\r
426 private void accept() {
\r
427 permList.updatePermissionsAccordingToInput();
\r
428 Set<PermissionHolder> perms = permList.getPermissions();
\r
429 JSONObject json = new JSONObject();
\r
430 if (!name.getText().equals(file.getName()))
\r
431 json.put("name", new JSONString(name.getText()));
\r
432 if (versioned.isChecked() != file.isVersioned())
\r
433 json.put("versioned", JSONBoolean.getInstance(versioned.isChecked()));
\r
434 //only update the read for all perm if the user is the owner
\r
435 if (readForAll.isChecked() != file.isReadForAll())
\r
436 if (file.getOwner().equals(GSS.get().getCurrentUserResource().getUsername()))
\r
437 json.put("readForAll", JSONBoolean.getInstance(readForAll.isChecked()));
\r
439 if (permList.hasChanges()) {
\r
440 GWT.log("Permissions change", null);
\r
441 JSONArray perma = new JSONArray();
\r
443 for (PermissionHolder p : perms) {
\r
444 JSONObject po = new JSONObject();
\r
445 if (p.getUser() != null)
\r
446 po.put("user", new JSONString(p.getUser()));
\r
447 if (p.getGroup() != null)
\r
448 po.put("group", new JSONString(p.getGroup()));
\r
449 po.put("read", JSONBoolean.getInstance(p.isRead()));
\r
450 po.put("write", JSONBoolean.getInstance(p.isWrite()));
\r
451 po.put("modifyACL", JSONBoolean.getInstance(p.isModifyACL()));
\r
455 json.put("permissions", perma);
\r
457 JSONArray taga = new JSONArray();
\r
459 if (!tags.getText().equals(initialTagText)) {
\r
460 String[] tagset = tags.getText().split(",");
\r
461 for (String t : tagset) {
\r
462 JSONString to = new JSONString(t);
\r
466 json.put("tags", taga);
\r
468 String jsonString = json.toString();
\r
469 if(jsonString.equals("{}")){
\r
470 GWT.log("NO CHANGES", null);
\r
473 PostCommand cf = new PostCommand(file.getUri() + "?update=", jsonString, 200) {
\r
476 public void onComplete() {
\r
477 GSS.get().getFileList().updateFileCache(true, false /* do not clear selected file*/);
\r
481 public void onError(Throwable t) {
\r
483 if (t instanceof RestException) {
\r
484 int statusCode = ((RestException) t).getHttpStatusCode();
\r
485 if (statusCode == 405)
\r
486 GSS.get().displayError("You don't have the necessary permissions");
\r
487 else if (statusCode == 404)
\r
488 GSS.get().displayError("User in permissions does not exist");
\r
489 else if (statusCode == 409)
\r
490 GSS.get().displayError("A file with the same name already exists");
\r
491 else if (statusCode == 413)
\r
492 GSS.get().displayError("Your quota has been exceeded");
\r
494 GSS.get().displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
\r
496 GSS.get().displayError("System error modifying file:" + t.getMessage());
\r
500 DeferredCommand.addCommand(cf);
\r
504 private void removeAllOldVersions() {
\r
505 JSONObject json = new JSONObject();
\r
506 json.put("versioned", JSONBoolean.getInstance(false));
\r
507 GWT.log(json.toString(), null);
\r
508 PostCommand cf = new PostCommand(file.getUri() + "?update=", json.toString(), 200) {
\r
511 public void onComplete() {
\r
512 toggleVersioned(true);
\r
516 public void onError(Throwable t) {
\r
518 if (t instanceof RestException) {
\r
519 int statusCode = ((RestException) t).getHttpStatusCode();
\r
520 if (statusCode == 405)
\r
521 GSS.get().displayError("You don't have the necessary permissions");
\r
522 else if (statusCode == 404)
\r
523 GSS.get().displayError("User in permissions does not exist");
\r
524 else if (statusCode == 409)
\r
525 GSS.get().displayError("A folder with the same name already exists");
\r
526 else if (statusCode == 413)
\r
527 GSS.get().displayError("Your quota has been exceeded");
\r
529 GSS.get().displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
\r
531 GSS.get().displayError("System error moifying file:" + t.getMessage());
\r
534 DeferredCommand.addCommand(cf);
\r
537 private void toggleVersioned(boolean versionedValue) {
\r
538 JSONObject json = new JSONObject();
\r
539 json.put("versioned", JSONBoolean.getInstance(versionedValue));
\r
540 GWT.log(json.toString(), null);
\r
541 PostCommand cf = new PostCommand(file.getUri() + "?update=", json.toString(), 200) {
\r
544 public void onComplete() {
\r
545 GSS.get().getFileList().updateFileCache(true, false /* do not clear selected file*/);
\r
549 public void onError(Throwable t) {
\r
551 if (t instanceof RestException) {
\r
552 int statusCode = ((RestException) t).getHttpStatusCode();
\r
553 if (statusCode == 405)
\r
554 GSS.get().displayError("You don't have the necessary permissions");
\r
555 else if (statusCode == 404)
\r
556 GSS.get().displayError("User in permissions does not exist");
\r
557 else if (statusCode == 409)
\r
558 GSS.get().displayError("A folder with the same name already exists");
\r
559 else if (statusCode == 413)
\r
560 GSS.get().displayError("Your quota has been exceeded");
\r
562 GSS.get().displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
\r
564 GSS.get().displayError("System error moifying file:" + t.getMessage());
\r
567 DeferredCommand.addCommand(cf);
\r
570 public void selectTab(int _tab) {
\r
571 inner.selectTab(_tab);
\r
575 * Enables IE selection prevention and hides the dialog
\r
576 * (we disable the prevention on creation of the dialog)
\r
578 public void closeDialog() {
\r
579 GSS.preventIESelection();
\r