2 * Copyright 2011 GRNET S.A. All rights reserved.
\r
4 * Redistribution and use in source and binary forms, with or
\r
5 * without modification, are permitted provided that the following
\r
6 * conditions are met:
\r
8 * 1. Redistributions of source code must retain the above
\r
9 * copyright notice, this list of conditions and the following
\r
12 * 2. Redistributions in binary form must reproduce the above
\r
13 * copyright notice, this list of conditions and the following
\r
14 * disclaimer in the documentation and/or other materials
\r
15 * provided with the distribution.
\r
17 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
\r
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
\r
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
\r
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
\r
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
\r
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
\r
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
\r
24 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
\r
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
\r
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
\r
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
\r
28 * POSSIBILITY OF SUCH DAMAGE.
\r
30 * The views and conclusions contained in the software and
\r
31 * documentation are those of the authors and should not be
\r
32 * interpreted as representing official policies, either expressed
\r
33 * or implied, of GRNET S.A.
\r
35 package gr.grnet.pithos.web.client;
\r
37 import gr.grnet.pithos.web.client.FilePropertiesDialog.Images;
\r
38 import gr.grnet.pithos.web.client.rest.PostCommand;
\r
39 import gr.grnet.pithos.web.client.rest.RestException;
\r
40 import gr.grnet.pithos.web.client.rest.resource.FolderResource;
\r
41 import gr.grnet.pithos.web.client.rest.resource.GroupResource;
\r
42 import gr.grnet.pithos.web.client.rest.resource.PermissionHolder;
\r
43 import gr.grnet.pithos.web.client.rest.resource.RestResourceWrapper;
\r
45 import java.util.List;
\r
46 import java.util.Set;
\r
48 import com.google.gwt.core.client.GWT;
\r
49 import com.google.gwt.dom.client.NativeEvent;
\r
50 import com.google.gwt.event.dom.client.ChangeEvent;
\r
51 import com.google.gwt.event.dom.client.ChangeHandler;
\r
52 import com.google.gwt.event.dom.client.ClickEvent;
\r
53 import com.google.gwt.event.dom.client.ClickHandler;
\r
54 import com.google.gwt.event.dom.client.KeyCodes;
\r
55 import com.google.gwt.http.client.URL;
\r
56 import com.google.gwt.i18n.client.DateTimeFormat;
\r
57 import com.google.gwt.json.client.JSONArray;
\r
58 import com.google.gwt.json.client.JSONBoolean;
\r
59 import com.google.gwt.json.client.JSONObject;
\r
60 import com.google.gwt.json.client.JSONString;
\r
61 import com.google.gwt.user.client.DeferredCommand;
\r
62 import com.google.gwt.user.client.Event.NativePreviewEvent;
\r
63 import com.google.gwt.user.client.ui.Button;
\r
64 import com.google.gwt.user.client.ui.CheckBox;
\r
65 import com.google.gwt.user.client.ui.DecoratedTabPanel;
\r
66 import com.google.gwt.user.client.ui.DialogBox;
\r
67 import com.google.gwt.user.client.ui.FlexTable;
\r
68 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
\r
69 import com.google.gwt.user.client.ui.HorizontalPanel;
\r
70 import com.google.gwt.user.client.ui.Label;
\r
71 import com.google.gwt.user.client.ui.TabPanel;
\r
72 import com.google.gwt.user.client.ui.TextBox;
\r
73 import com.google.gwt.user.client.ui.VerticalPanel;
\r
76 * The 'Folder properties' dialog box implementation.
\r
78 public class FolderPropertiesDialog extends DialogBox {
\r
80 private List<GroupResource> groups = null;
\r
82 final PermissionsList permList;
\r
84 private CheckBox readForAll;
\r
87 * The widget that holds the folderName of the folder.
\r
89 private TextBox folderName = new TextBox();
\r
92 * A flag that denotes whether the dialog will be used to create or modify a
\r
95 private final boolean create;
\r
97 final FolderResource folder;
\r
99 final TabPanel inner;
\r
102 * The widget's constructor.
\r
104 * @param images the image icons from the file properties dialog
\r
105 * @param _create true if the dialog is displayed for creating a new
\r
106 * sub-folder of the selected folder, false if it is displayed
\r
107 * for modifying the selected folder
\r
109 public FolderPropertiesDialog(Images images, boolean _create, final List<GroupResource> _groups) {
\r
110 setAnimationEnabled(true);
\r
112 // Enable IE selection for the dialog (must disable it upon closing it)
\r
113 GSS.enableIESelection();
\r
117 folder = ((RestResourceWrapper) GSS.get().getTreeView().getSelection()).getResource();
\r
118 permList = new PermissionsList(images, folder.getPermissions(), folder.getOwner());
\r
121 // Use this opportunity to set the dialog's caption.
\r
123 setText("Create folder");
\r
125 setText("Folder properties");
\r
127 // Outer contains inner and buttons
\r
128 VerticalPanel outer = new VerticalPanel();
\r
129 // Inner contains generalPanel and permPanel
\r
130 inner = new DecoratedTabPanel();
\r
131 inner.setAnimationEnabled(true);
\r
132 VerticalPanel generalPanel = new VerticalPanel();
\r
133 VerticalPanel permPanel = new VerticalPanel();
\r
134 final HorizontalPanel permForAll = new HorizontalPanel();
\r
135 final HorizontalPanel pathPanel = new HorizontalPanel();
\r
136 HorizontalPanel buttons = new HorizontalPanel();
\r
137 HorizontalPanel permButtons = new HorizontalPanel();
\r
139 inner.add(generalPanel, "General");
\r
141 inner.add(permPanel, "Sharing");
\r
142 inner.selectTab(0);
\r
144 final Label folderNameNote = new Label("Please note that slashes ('/') are not allowed in folder names.", true);
\r
145 folderNameNote.setVisible(false);
\r
146 folderNameNote.setStylePrimaryName("pithos-readForAllNote");
\r
148 FlexTable generalTable = new FlexTable();
\r
149 generalTable.setText(0, 0, "Name");
\r
150 generalTable.setText(1, 0, "Parent");
\r
151 generalTable.setText(2, 0, "Creator");
\r
152 generalTable.setText(3, 0, "Last modified");
\r
153 folderName.setText(create ? "" : folder.getName());
\r
154 folderName.getElement().setId("folderPropertiesDialog.textBox.name");
\r
155 generalTable.setWidget(0, 1, folderName);
\r
156 folderName.addChangeHandler(new ChangeHandler() {
\r
159 public void onChange(ChangeEvent event) {
\r
160 if(folderName.getText().contains("/"))
\r
161 folderNameNote.setVisible(true);
\r
163 folderNameNote.setVisible(false);
\r
170 generalTable.setText(1, 1, folder.getName());
\r
171 else if(folder.getParentName() == null)
\r
172 generalTable.setText(1, 1, "-");
\r
174 generalTable.setText(1, 1, folder.getParentName());
\r
175 generalTable.setWidget(0, 2, folderNameNote);
\r
176 generalTable.setText(2, 1, folder.getOwner());
\r
177 DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
\r
178 if(folder.getModificationDate() != null)
\r
179 generalTable.setText(3, 1, formatter.format(folder.getModificationDate()));
\r
180 generalTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
\r
181 generalTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
\r
182 generalTable.getFlexCellFormatter().setStyleName(2, 0, "props-labels");
\r
183 generalTable.getFlexCellFormatter().setStyleName(3, 0, "props-labels");
\r
184 generalTable.getFlexCellFormatter().setStyleName(0, 1, "props-values");
\r
185 generalTable.getFlexCellFormatter().setStyleName(1, 1, "props-values");
\r
186 generalTable.getFlexCellFormatter().setStyleName(2, 1, "props-values");
\r
187 generalTable.getFlexCellFormatter().setStyleName(3, 1, "props-values");
\r
188 generalTable.setCellSpacing(4);
\r
190 // Create the 'Create/Update' button, along with a listener that hides the dialog
\r
191 // when the button is clicked and quits the application.
\r
194 okLabel = "Create";
\r
196 okLabel = "Update";
\r
197 Button ok = new Button(okLabel, new ClickHandler() {
\r
199 public void onClick(ClickEvent event) {
\r
200 if(folderName.getText().contains("/"))
\r
201 folderNameNote.setVisible(true);
\r
203 folderNameNote.setVisible(false);
\r
204 createOrUpdateFolder();
\r
210 ok.getElement().setId("folderPropertiesDialog.button.ok");
\r
212 buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
\r
213 // Create the 'Cancel' button, along with a listener that hides the
\r
215 // when the button is clicked.
\r
216 Button cancel = new Button("Cancel", new ClickHandler() {
\r
218 public void onClick(ClickEvent event) {
\r
222 cancel.getElement().setId("folderPropertiesDialog.button.cancel");
\r
223 buttons.add(cancel);
\r
224 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
\r
225 buttons.setSpacing(8);
\r
226 buttons.addStyleName("pithos-TabPanelBottom");
\r
228 Button add = new Button("Add Group", new ClickHandler() {
\r
230 public void onClick(ClickEvent event) {
\r
231 PermissionsAddDialog dlg = new PermissionsAddDialog(groups, permList, false);
\r
235 add.getElement().setId("folderPropertiesDialog.button.addGroup");
\r
236 permButtons.add(add);
\r
237 permButtons.setCellHorizontalAlignment(add, HasHorizontalAlignment.ALIGN_CENTER);
\r
239 Button addUser = new Button("Add User", new ClickHandler() {
\r
241 public void onClick(ClickEvent event) {
\r
242 PermissionsAddDialog dlg = new PermissionsAddDialog(groups, permList, true);
\r
246 addUser.getElement().setId("folderPropertiesDialog.button.addUser");
\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("pithos-TabPanelBottom");
\r
254 final Label readForAllNote = new Label("When this option is enabled, the folder will be readable" +
\r
255 " by everyone. By checking this option, you are certifying that you have the right to " +
\r
256 "distribute this folder's contents and that it does not violate the Terms of Use.", true);
\r
257 readForAllNote.setVisible(false);
\r
258 readForAllNote.setStylePrimaryName("pithos-readForAllNote");
\r
260 readForAll = new CheckBox();
\r
261 readForAll.getElement().setId("folderPropertiesDialog.checkBox.public");
\r
262 readForAll.setValue(folder.isReadForAll());
\r
263 readForAll.addClickHandler(new ClickHandler() {
\r
265 public void onClick(ClickEvent event) {
\r
266 readForAllNote.setVisible(readForAll.getValue());
\r
271 generalPanel.add(generalTable);
\r
272 permPanel.add(permList);
\r
273 permPanel.add(permButtons);
\r
275 // Only show the read for all permission if the user is the owner.
\r
276 if (folder.getOwner().equals(GSS.get().getCurrentUserResource().getUsername())) {
\r
277 permForAll.add(new Label("Public"));
\r
278 permForAll.add(readForAll);
\r
279 permForAll.setSpacing(8);
\r
280 permForAll.addStyleName("pithos-TabPanelBottom");
\r
281 permForAll.add(readForAllNote);
\r
282 permPanel.add(permForAll);
\r
284 TextBox path = new TextBox();
\r
285 path.getElement().setId("folderPropertiesDialog.textBox.link");
\r
286 path.setWidth("100%");
\r
287 path.addClickHandler(new ClickHandler() {
\r
289 public void onClick(ClickEvent event) {
\r
290 GSS.enableIESelection();
\r
291 ((TextBox) event.getSource()).selectAll();
\r
292 GSS.preventIESelection();
\r
296 path.setText(folder.getUri());
\r
297 path.setTitle("Use this link for sharing the folder via e-mail, IM, etc. (crtl-C/cmd-C to copy to system clipboard)");
\r
298 path.setWidth("100%");
\r
299 path.setReadOnly(true);
\r
300 pathPanel.setWidth("100%");
\r
301 pathPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
\r
302 pathPanel.add(new Label("Link"));
\r
303 pathPanel.setSpacing(8);
\r
304 pathPanel.addStyleName("pithos-TabPanelBottom");
\r
305 pathPanel.add(path);
\r
306 permPanel.add(pathPanel);
\r
309 outer.add(buttons);
\r
310 outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
\r
311 outer.addStyleName("pithos-TabPanelBottom");
\r
316 folderName.setFocus(true);
\r
318 ok.setFocus(true);*/
\r
322 public void center() {
\r
324 folderName.setFocus(true);
\r
328 protected void onPreviewNativeEvent(NativePreviewEvent preview) {
\r
329 super.onPreviewNativeEvent(preview);
\r
331 NativeEvent evt = preview.getNativeEvent();
\r
332 if (evt.getType().equals("keydown"))
\r
333 // Use the popup's key preview hooks to close the dialog when either
\r
334 // enter or escape is pressed.
\r
335 switch (evt.getKeyCode()) {
\r
336 case KeyCodes.KEY_ENTER:
\r
338 createOrUpdateFolder();
\r
340 case KeyCodes.KEY_ESCAPE:
\r
348 * Enables IE selection prevention and hides the dialog
\r
349 * (we disable the prevention on creation of the dialog)
\r
351 public void closeDialog() {
\r
352 GSS.preventIESelection();
\r
357 * Generate an RPC request to create a new folder.
\r
359 * @param userId the ID of the user whose namespace will be searched for
\r
361 * @param _folderName the name of the folder to create
\r
363 private void createFolder() {
\r
364 String name = folderName.getText();
\r
365 if (!GSS.isValidResourceName(name)) {
\r
366 GSS.get().displayError("The folder name '" + name + "' is invalid");
\r
369 PostCommand ep = new PostCommand(folder.getUri() + "?new=" +
\r
370 URL.encodeComponent(name), "", 201) {
\r
373 public void onComplete() {
\r
375 if(folder.getUri().equals(GSS.get().getTreeView().getMyFolders().getUri())){
\r
376 GSS.get().getTreeView().updateRootNode();
\r
379 GSS.get().getTreeView().updateNodeChildren((RestResourceWrapper) GSS.get().getTreeView().getSelection());
\r
380 //GSS.get().getFolders().updateFolder((DnDTreeItem) GSS.get().getFolders().getCurrent());
\r
384 public void onError(Throwable t) {
\r
386 if(t instanceof RestException){
\r
387 int statusCode = ((RestException)t).getHttpStatusCode();
\r
388 if(statusCode == 405)
\r
389 GSS.get().displayError("You don't have the necessary" +
\r
390 " permissions or a folder with same name " +
\r
392 else if(statusCode == 404)
\r
393 GSS.get().displayError("Resource not found");
\r
395 GSS.get().displayError("Unable to create folder:" +
\r
396 ((RestException)t).getHttpStatusText());
\r
399 GSS.get().displayError("System error creating folder:" +
\r
403 DeferredCommand.addCommand(ep);
\r
408 * Upon closing the dialog by clicking OK or pressing ENTER this method does
\r
409 * the actual work of modifying folder properties or creating a new Folder
\r
410 * depending on the value of the create field
\r
414 private void createOrUpdateFolder() {
\r
422 private void updateFolder() {
\r
423 permList.updatePermissionsAccordingToInput();
\r
424 Set<PermissionHolder> perms = permList.getPermissions();
\r
425 JSONObject json = new JSONObject();
\r
426 if(!folder.getName().equals(folderName.getText()))
\r
427 json.put("name", new JSONString(folderName.getText()));
\r
428 //only update the read for all perm if the user is the owner
\r
429 if (readForAll.getValue() != folder.isReadForAll())
\r
430 if (folder.getOwner().equals(GSS.get().getCurrentUserResource().getUsername()))
\r
431 json.put("readForAll", JSONBoolean.getInstance(readForAll.getValue()));
\r
432 if (permList.hasChanges()) {
\r
433 JSONArray perma = new JSONArray();
\r
435 for(PermissionHolder p : perms){
\r
436 JSONObject po = new JSONObject();
\r
437 if(p.getUser() != null)
\r
438 po.put("user", new JSONString(p.getUser()));
\r
439 if(p.getGroup() != null)
\r
440 po.put("group", new JSONString(p.getGroup()));
\r
441 po.put("read", JSONBoolean.getInstance(p.isRead()));
\r
442 po.put("write", JSONBoolean.getInstance(p.isWrite()));
\r
443 po.put("modifyACL", JSONBoolean.getInstance(p.isModifyACL()));
\r
447 json.put("permissions", perma);
\r
448 GWT.log(json.toString(), null);
\r
450 PostCommand ep = new PostCommand(folder.getUri()+"?update=", json.toString(), 200){
\r
453 public void onComplete() {
\r
456 if(getPostBody() != null && !"".equals(getPostBody().trim())){
\r
459 FolderResource fres = ((RestResourceWrapper) GSS.get().getTreeView().getSelection()).getResource();
\r
460 String initialPath = fres.getUri();
\r
461 String newPath = getPostBody().trim();
\r
462 fres.setUri(newPath);
\r
463 ((RestResourceWrapper) GSS.get().getTreeView().getSelection()).getResource().setUri(newPath);
\r
464 ((RestResourceWrapper) GSS.get().getTreeView().getSelection()).setUri(newPath);
\r
465 GSS.get().getTreeView().updateNodeChildren(fres.getParentURI());
\r
466 if (permList.hasChanges()) {
\r
467 GSS.get().getTreeView().updateMySharedNode();
\r
470 if(folderItem.getParentItem() != null && ((DnDTreeItem)folderItem.getParentItem()).getFolderResource() != null){
\r
471 ((DnDTreeItem)folderItem.getParentItem()).getFolderResource().removeSubfolderPath(initialPath);
\r
472 ((DnDTreeItem)folderItem.getParentItem()).getFolderResource().getSubfolderPaths().add(newPath);
\r
475 //GSS.get().getFolders().updateFolder( (DnDTreeItem) GSS.get().getFolders().getCurrent());
\r
477 GSS.get().showFileList(true);
\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 or" +
\r
487 " a folder with same name already exists");
\r
488 else if(statusCode == 404)
\r
489 GSS.get().displayError("Resource not found, or user specified in sharing does not exist");
\r
491 GSS.get().displayError("Unable to update folder: "+((RestException)t).getHttpStatusText());
\r
494 GSS.get().displayError("System error moifying file: "+t.getMessage());
\r
496 //GSS.get().getFolders().updateFolder( (DnDTreeItem) GSS.get().getFolders().getCurrent());
\r
499 DeferredCommand.addCommand(ep);
\r
502 public void selectTab(int _tab) {
\r
503 inner.selectTab(_tab);
\r