2 * Copyright 2007, 2008, 2009, 2010 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.PostCommand;
\r
22 import gr.ebs.gss.client.rest.RestException;
\r
23 import gr.ebs.gss.client.rest.resource.FileResource;
\r
24 import gr.ebs.gss.client.rest.resource.GroupResource;
\r
25 import gr.ebs.gss.client.rest.resource.PermissionHolder;
\r
27 import java.util.Iterator;
\r
28 import java.util.List;
\r
29 import java.util.Set;
\r
31 import com.google.gwt.core.client.GWT;
\r
32 import com.google.gwt.event.dom.client.ChangeEvent;
\r
33 import com.google.gwt.event.dom.client.ChangeHandler;
\r
34 import com.google.gwt.event.dom.client.ClickEvent;
\r
35 import com.google.gwt.event.dom.client.ClickHandler;
\r
36 import com.google.gwt.event.dom.client.KeyDownEvent;
\r
37 import com.google.gwt.event.dom.client.KeyDownHandler;
\r
38 import com.google.gwt.i18n.client.DateTimeFormat;
\r
39 import com.google.gwt.json.client.JSONArray;
\r
40 import com.google.gwt.json.client.JSONBoolean;
\r
41 import com.google.gwt.json.client.JSONObject;
\r
42 import com.google.gwt.json.client.JSONString;
\r
43 import com.google.gwt.resources.client.ClientBundle;
\r
44 import com.google.gwt.resources.client.ImageResource;
\r
45 import com.google.gwt.user.client.Command;
\r
46 import com.google.gwt.user.client.DeferredCommand;
\r
47 import com.google.gwt.user.client.ui.AbstractImagePrototype;
\r
48 import com.google.gwt.user.client.ui.Button;
\r
49 import com.google.gwt.user.client.ui.CheckBox;
\r
50 import com.google.gwt.user.client.ui.DecoratedTabPanel;
\r
51 import com.google.gwt.user.client.ui.DisclosurePanel;
\r
52 import com.google.gwt.user.client.ui.FlexTable;
\r
53 import com.google.gwt.user.client.ui.FlowPanel;
\r
54 import com.google.gwt.user.client.ui.FocusPanel;
\r
55 import com.google.gwt.user.client.ui.HTML;
\r
56 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
\r
57 import com.google.gwt.user.client.ui.HorizontalPanel;
\r
58 import com.google.gwt.user.client.ui.Label;
\r
59 import com.google.gwt.user.client.ui.TextBox;
\r
60 import com.google.gwt.user.client.ui.VerticalPanel;
\r
63 * The 'File properties' dialog box implementation.
\r
67 public class FilePropertiesDialog extends AbstractPropertiesDialog {
\r
69 final PermissionsList permList;
\r
71 private CheckBox readForAll;
\r
74 * An image bundle for this widgets images.
\r
76 public interface Images extends ClientBundle,MessagePanel.Images {
\r
78 @Source("gr/ebs/gss/resources/edit_user.png")
\r
79 ImageResource permUser();
\r
81 @Source("gr/ebs/gss/resources/groupevent.png")
\r
82 ImageResource permGroup();
\r
84 @Source("gr/ebs/gss/resources/editdelete.png")
\r
85 ImageResource delete();
\r
87 @Source("gr/ebs/gss/resources/db_update.png")
\r
88 ImageResource restore();
\r
90 @Source("gr/ebs/gss/resources/folder_inbox.png")
\r
91 ImageResource download();
\r
95 * The widget that holds the name of the file.
\r
97 private TextBox name = new TextBox();
\r
99 private final CheckBox versioned = new CheckBox();
\r
101 final FileResource file;
\r
103 private String userFullName;
\r
106 * The widget's constructor.
\r
108 * @param images the dialog's ImageBundle
\r
112 public FilePropertiesDialog(final Images images, final List<GroupResource> groups, List<FileResource> bodies, String _userFullName) {
\r
114 // Set the dialog's caption.
\r
115 setText("File properties");
\r
117 file = (FileResource) GSS.get().getCurrentSelection();
\r
118 userFullName = _userFullName;
\r
119 permList = new PermissionsList(images, file.getPermissions(), file.getOwner());
\r
122 // Outer contains inner and buttons.
\r
123 final VerticalPanel outer = new VerticalPanel();
\r
124 final FocusPanel focusPanel = new FocusPanel(outer);
\r
125 // Inner contains generalPanel and permPanel.
\r
126 inner = new DecoratedTabPanel();
\r
127 inner.setAnimationEnabled(true);
\r
128 final VerticalPanel generalPanel = new VerticalPanel();
\r
129 final VerticalPanel permPanel = new VerticalPanel();
\r
130 final HorizontalPanel buttons = new HorizontalPanel();
\r
131 final HorizontalPanel permButtons = new HorizontalPanel();
\r
132 final HorizontalPanel permForAll = new HorizontalPanel();
\r
133 final HorizontalPanel pathPanel = new HorizontalPanel();
\r
134 final VerticalPanel verPanel = new VerticalPanel();
\r
135 final HorizontalPanel vPanel = new HorizontalPanel();
\r
136 final HorizontalPanel vPanel2 = new HorizontalPanel();
\r
138 versioned.setValue(file.isVersioned());
\r
139 inner.add(generalPanel, "General");
\r
140 inner.add(permPanel, "Sharing");
\r
141 inner.add(verPanel, "Versions");
\r
142 inner.selectTab(0);
\r
144 final Label fileNameNote = new Label("Please note that slashes ('/') are not allowed in file names.", true);
\r
145 fileNameNote.setVisible(false);
\r
146 fileNameNote.setStylePrimaryName("gss-readForAllNote");
\r
148 final FlexTable generalTable = new FlexTable();
\r
149 generalTable.setText(0, 0, "Name");
\r
150 generalTable.setText(1, 0, "Folder");
\r
151 generalTable.setText(2, 0, "Owner");
\r
152 generalTable.setText(3, 0, "Last modified");
\r
153 generalTable.setText(4, 0, "Tags");
\r
154 name.setWidth("100%");
\r
155 name.setText(file.getName());
\r
156 generalTable.setWidget(0, 1, name);
\r
157 name.addChangeHandler(new ChangeHandler() {
\r
160 public void onChange(ChangeEvent event) {
\r
161 if(name.getText().contains("/"))
\r
162 fileNameNote.setVisible(true);
\r
164 fileNameNote.setVisible(false);
\r
169 if(file.getFolderName() != null)
\r
170 generalTable.setText(1, 1, file.getFolderName());
\r
172 generalTable.setText(1, 1, "-");
\r
173 generalTable.setWidget(0, 2, fileNameNote);
\r
174 generalTable.setText(2, 1,userFullName);
\r
176 final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
\r
177 generalTable.setText(3, 1, formatter.format(file.getModificationDate()));
\r
179 StringBuffer tagsBuffer = new StringBuffer();
\r
180 Iterator i = file.getTags().iterator();
\r
181 while (i.hasNext()) {
\r
182 String tag = (String) i.next();
\r
183 tagsBuffer.append(tag).append(", ");
\r
185 if (tagsBuffer.length() > 1)
\r
186 tagsBuffer.delete(tagsBuffer.length() - 2, tagsBuffer.length() - 1);
\r
187 initialTagText = tagsBuffer.toString();
\r
188 tags.setWidth("100%");
\r
189 tags.setText(initialTagText);
\r
190 generalTable.setWidget(4, 1, tags);
\r
191 generalTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
\r
192 generalTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
\r
193 generalTable.getFlexCellFormatter().setStyleName(2, 0, "props-labels");
\r
194 generalTable.getFlexCellFormatter().setStyleName(3, 0, "props-labels");
\r
195 generalTable.getFlexCellFormatter().setStyleName(4, 0, "props-labels");
\r
196 generalTable.getFlexCellFormatter().setStyleName(0, 1, "props-values");
\r
197 generalTable.getFlexCellFormatter().setStyleName(1, 1, "props-values");
\r
198 generalTable.getFlexCellFormatter().setStyleName(2, 1, "props-values");
\r
199 generalTable.getFlexCellFormatter().setStyleName(3, 1, "props-values");
\r
200 generalTable.getFlexCellFormatter().setStyleName(4, 1, "props-values");
\r
201 generalTable.setCellSpacing(4);
\r
203 // Create the 'OK' button, along with a listener that hides the dialog
\r
204 // when the button is clicked.
\r
205 final Button ok = new Button("OK", new ClickHandler() {
\r
207 public void onClick(ClickEvent event) {
\r
208 if(name.getText().contains("/"))
\r
209 fileNameNote.setVisible(true);
\r
211 fileNameNote.setVisible(true);
\r
218 buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
\r
219 // Create the 'Cancel' button, along with a listener that hides the
\r
220 // dialog when the button is clicked.
\r
221 final Button cancel = new Button("Cancel", new ClickHandler() {
\r
223 public void onClick(ClickEvent event) {
\r
227 buttons.add(cancel);
\r
228 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
\r
229 buttons.setSpacing(8);
\r
230 buttons.addStyleName("gss-TabPanelBottom");
\r
232 generalPanel.add(generalTable);
\r
234 // Asynchronously retrieve the tags defined by this user.
\r
235 DeferredCommand.addCommand(new Command() {
\r
238 public void execute() {
\r
243 DisclosurePanel allTags = new DisclosurePanel("All tags");
\r
244 allTagsContent = new FlowPanel();
\r
245 allTagsContent.setWidth("100%");
\r
246 allTags.setContent(allTagsContent);
\r
247 generalPanel.add(allTags);
\r
248 generalPanel.setSpacing(4);
\r
250 final Button add = new Button("Add Group", new ClickHandler() {
\r
252 public void onClick(ClickEvent event) {
\r
253 PermissionsAddDialog dlg = new PermissionsAddDialog(groups, permList, false);
\r
257 permButtons.add(add);
\r
258 permButtons.setCellHorizontalAlignment(add, HasHorizontalAlignment.ALIGN_CENTER);
\r
260 final Button addUser = new Button("Add User", new ClickHandler() {
\r
262 public void onClick(ClickEvent event) {
\r
263 PermissionsAddDialog dlg = new PermissionsAddDialog(groups, permList, true);
\r
267 permButtons.add(addUser);
\r
268 permButtons.setCellHorizontalAlignment(addUser, HasHorizontalAlignment.ALIGN_CENTER);
\r
270 permButtons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
\r
271 permButtons.setSpacing(8);
\r
272 permButtons.addStyleName("gss-TabPanelBottom");
\r
274 final Label readForAllNote = new Label("When this option is enabled, the file will be readable" +
\r
275 " by everyone. By checking this option, you are certifying that you have the right to " +
\r
276 "distribute this file and that it does not violate the Terms of Use.", true);
\r
277 readForAllNote.setVisible(false);
\r
278 readForAllNote.setStylePrimaryName("gss-readForAllNote");
\r
280 readForAll = new CheckBox();
\r
281 readForAll.setValue(file.isReadForAll());
\r
282 readForAll.addClickHandler(new ClickHandler() {
\r
284 public void onClick(ClickEvent event) {
\r
285 readForAllNote.setVisible(readForAll.getValue());
\r
290 permPanel.add(permList);
\r
291 permPanel.add(permButtons);
\r
292 // Only show the read for all permission if the user is the owner.
\r
293 if (file.getOwner().equals(GSS.get().getCurrentUserResource().getUsername())) {
\r
294 permForAll.add(new Label("Public"));
\r
295 permForAll.add(readForAll);
\r
296 permForAll.setSpacing(8);
\r
297 permForAll.addStyleName("gss-TabPanelBottom");
\r
298 permForAll.add(readForAllNote);
\r
299 permPanel.add(permForAll);
\r
302 TextBox path = new TextBox();
\r
303 path.setWidth("100%");
\r
304 path.addClickHandler(new ClickHandler() {
\r
306 public void onClick(ClickEvent event) {
\r
307 GSS.enableIESelection();
\r
308 ((TextBox) event.getSource()).selectAll();
\r
309 GSS.preventIESelection();
\r
313 path.setText(file.getUri());
\r
314 path.setTitle("Use this link for sharing the file via e-mail, IM, etc. (crtl-C/cmd-C to copy to system clipboard)");
\r
315 path.setWidth("100%");
\r
316 path.setReadOnly(true);
\r
317 pathPanel.setWidth("100%");
\r
318 pathPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
\r
319 pathPanel.add(new Label("Link"));
\r
320 pathPanel.setSpacing(8);
\r
321 pathPanel.addStyleName("gss-TabPanelBottom");
\r
322 pathPanel.add(path);
\r
323 permPanel.add(pathPanel);
\r
325 VersionsList verList = new VersionsList(this, images, bodies);
\r
326 verPanel.add(verList);
\r
328 vPanel.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
\r
329 vPanel.setSpacing(8);
\r
330 vPanel.addStyleName("gss-TabPanelBottom");
\r
331 vPanel.add(new Label("Versioned"));
\r
333 vPanel.add(versioned);
\r
334 verPanel.add(vPanel);
\r
335 vPanel2.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
\r
336 vPanel2.setSpacing(8);
\r
337 vPanel2.addStyleName("gss-TabPanelBottom");
\r
338 Button removeVersionsButton = new Button(AbstractImagePrototype.create(images.delete()).getHTML(), new ClickHandler() {
\r
340 public void onClick(ClickEvent event) {
\r
341 ConfirmationDialog confirm = new ConfirmationDialog("Really " +
\r
342 "remove all previous versions?", "Remove") {
\r
345 public void cancel() {
\r
349 public void confirm() {
\r
350 FilePropertiesDialog.this.closeDialog();
\r
351 removeAllOldVersions();
\r
359 HTML removeAllVersion = new HTML("<span>Remove all previous versions?</span>");
\r
360 vPanel2.add(removeAllVersion);
\r
361 vPanel2.add(removeVersionsButton);
\r
362 verPanel.add(vPanel2);
\r
363 if(!file.isVersioned())
\r
364 vPanel2.setVisible(false);
\r
366 outer.add(buttons);
\r
367 outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
\r
368 outer.addStyleName("gss-TabPanelBottom");
\r
370 focusPanel.setFocus(true);
\r
376 * Accepts any change and updates the file
\r
380 protected void accept() {
\r
381 String newFilename = null;
\r
382 permList.updatePermissionsAccordingToInput();
\r
383 Set<PermissionHolder> perms = permList.getPermissions();
\r
384 JSONObject json = new JSONObject();
\r
385 if (!name.getText().equals(file.getName())) {
\r
386 newFilename = name.getText();
\r
387 json.put("name", new JSONString(newFilename));
\r
389 if (versioned.getValue() != file.isVersioned())
\r
390 json.put("versioned", JSONBoolean.getInstance(versioned.getValue()));
\r
391 //only update the read for all perm if the user is the owner
\r
392 if (readForAll.getValue() != file.isReadForAll())
\r
393 if (file.getOwner().equals(GSS.get().getCurrentUserResource().getUsername()))
\r
394 json.put("readForAll", JSONBoolean.getInstance(readForAll.getValue()));
\r
396 if (permList.hasChanges()) {
\r
397 GWT.log("Permissions change", null);
\r
398 JSONArray perma = new JSONArray();
\r
400 for (PermissionHolder p : perms) {
\r
401 JSONObject po = new JSONObject();
\r
402 if (p.getUser() != null)
\r
403 po.put("user", new JSONString(p.getUser()));
\r
404 if (p.getGroup() != null)
\r
405 po.put("group", new JSONString(p.getGroup()));
\r
406 po.put("read", JSONBoolean.getInstance(p.isRead()));
\r
407 po.put("write", JSONBoolean.getInstance(p.isWrite()));
\r
408 po.put("modifyACL", JSONBoolean.getInstance(p.isModifyACL()));
\r
412 json.put("permissions", perma);
\r
414 JSONArray taga = new JSONArray();
\r
416 if (!tags.getText().equals(initialTagText)) {
\r
417 String[] tagset = tags.getText().split(",");
\r
418 for (String t : tagset) {
\r
419 JSONString to = new JSONString(t);
\r
423 json.put("tags", taga);
\r
425 String jsonString = json.toString();
\r
426 if(jsonString.equals("{}")){
\r
427 GWT.log("NO CHANGES", null);
\r
430 final String newFilenameFinal = newFilename;
\r
431 PostCommand cf = new PostCommand(file.getUri() + "?update=", jsonString, 200) {
\r
434 public void onComplete() {
\r
435 GSS.get().getFileList().updateFileCache(true, false /* do not clear selected file*/, newFilenameFinal);
\r
439 public void onError(Throwable t) {
\r
441 if (t instanceof RestException) {
\r
442 int statusCode = ((RestException) t).getHttpStatusCode();
\r
443 if (statusCode == 405)
\r
444 GSS.get().displayError("You don't have the necessary permissions");
\r
445 else if (statusCode == 404)
\r
446 GSS.get().displayError("User in permissions does not exist");
\r
447 else if (statusCode == 409)
\r
448 GSS.get().displayError("A file with the same name already exists");
\r
449 else if (statusCode == 413)
\r
450 GSS.get().displayError("Your quota has been exceeded");
\r
452 GSS.get().displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
\r
454 GSS.get().displayError("System error modifying file:" + t.getMessage());
\r
458 DeferredCommand.addCommand(cf);
\r
462 private void removeAllOldVersions() {
\r
463 JSONObject json = new JSONObject();
\r
464 json.put("versioned", JSONBoolean.getInstance(false));
\r
465 GWT.log(json.toString(), null);
\r
466 PostCommand cf = new PostCommand(file.getUri() + "?update=", json.toString(), 200) {
\r
469 public void onComplete() {
\r
470 toggleVersioned(true);
\r
474 public void onError(Throwable t) {
\r
476 if (t instanceof RestException) {
\r
477 int statusCode = ((RestException) t).getHttpStatusCode();
\r
478 if (statusCode == 405)
\r
479 GSS.get().displayError("You don't have the necessary permissions");
\r
480 else if (statusCode == 404)
\r
481 GSS.get().displayError("User in permissions does not exist");
\r
482 else if (statusCode == 409)
\r
483 GSS.get().displayError("A folder with the same name already exists");
\r
484 else if (statusCode == 413)
\r
485 GSS.get().displayError("Your quota has been exceeded");
\r
487 GSS.get().displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
\r
489 GSS.get().displayError("System error moifying file:" + t.getMessage());
\r
492 DeferredCommand.addCommand(cf);
\r
495 private void toggleVersioned(boolean versionedValue) {
\r
496 JSONObject json = new JSONObject();
\r
497 json.put("versioned", JSONBoolean.getInstance(versionedValue));
\r
498 GWT.log(json.toString(), null);
\r
499 PostCommand cf = new PostCommand(file.getUri() + "?update=", json.toString(), 200) {
\r
502 public void onComplete() {
\r
503 GSS.get().getFileList().updateFileCache(true, false /* do not clear selected file*/);
\r
507 public void onError(Throwable t) {
\r
509 if (t instanceof RestException) {
\r
510 int statusCode = ((RestException) t).getHttpStatusCode();
\r
511 if (statusCode == 405)
\r
512 GSS.get().displayError("You don't have the necessary permissions");
\r
513 else if (statusCode == 404)
\r
514 GSS.get().displayError("User in permissions does not exist");
\r
515 else if (statusCode == 409)
\r
516 GSS.get().displayError("A folder with the same name already exists");
\r
517 else if (statusCode == 413)
\r
518 GSS.get().displayError("Your quota has been exceeded");
\r
520 GSS.get().displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
\r
522 GSS.get().displayError("System error moifying file:" + t.getMessage());
\r
525 DeferredCommand.addCommand(cf);
\r