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.FilePropertiesDialog.Images;
\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.FolderResource;
\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.RestResource;
\r
28 import gr.ebs.gss.client.rest.resource.RestResourceWrapper;
\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.dom.client.NativeEvent;
\r
35 import com.google.gwt.event.dom.client.ChangeEvent;
\r
36 import com.google.gwt.event.dom.client.ChangeHandler;
\r
37 import com.google.gwt.event.dom.client.ClickEvent;
\r
38 import com.google.gwt.event.dom.client.ClickHandler;
\r
39 import com.google.gwt.event.dom.client.KeyCodes;
\r
40 import com.google.gwt.http.client.URL;
\r
41 import com.google.gwt.i18n.client.DateTimeFormat;
\r
42 import com.google.gwt.json.client.JSONArray;
\r
43 import com.google.gwt.json.client.JSONBoolean;
\r
44 import com.google.gwt.json.client.JSONObject;
\r
45 import com.google.gwt.json.client.JSONString;
\r
46 import com.google.gwt.user.client.DeferredCommand;
\r
47 import com.google.gwt.user.client.Event.NativePreviewEvent;
\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.DialogBox;
\r
52 import com.google.gwt.user.client.ui.FlexTable;
\r
53 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
\r
54 import com.google.gwt.user.client.ui.HorizontalPanel;
\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
61 * The 'Folder properties' dialog box implementation.
\r
63 public class FolderPropertiesDialog extends DialogBox {
\r
65 private List<GroupResource> groups = null;
\r
67 final PermissionsList permList;
\r
69 private CheckBox readForAll;
\r
72 * The widget that holds the folderName of the folder.
\r
74 private TextBox folderName = new TextBox();
\r
77 * A flag that denotes whether the dialog will be used to create or modify a
\r
80 private final boolean create;
\r
82 final FolderResource folder;
\r
84 final TabPanel inner;
\r
87 * The widget's constructor.
\r
89 * @param images the image icons from the file properties dialog
\r
90 * @param _create true if the dialog is displayed for creating a new
\r
91 * sub-folder of the selected folder, false if it is displayed
\r
92 * for modifying the selected folder
\r
94 public FolderPropertiesDialog(Images images, boolean _create, final List<GroupResource> _groups) {
\r
95 setAnimationEnabled(true);
\r
97 // Enable IE selection for the dialog (must disable it upon closing it)
\r
98 GSS.enableIESelection();
\r
102 folder = ((RestResourceWrapper) GSS.get().getTreeView().getSelection()).getResource();
\r
103 permList = new PermissionsList(images, folder.getPermissions(), folder.getOwner());
\r
106 // Use this opportunity to set the dialog's caption.
\r
108 setText("Create folder");
\r
110 setText("Folder properties");
\r
112 // Outer contains inner and buttons
\r
113 VerticalPanel outer = new VerticalPanel();
\r
114 // Inner contains generalPanel and permPanel
\r
115 inner = new DecoratedTabPanel();
\r
116 inner.setAnimationEnabled(true);
\r
117 VerticalPanel generalPanel = new VerticalPanel();
\r
118 VerticalPanel permPanel = new VerticalPanel();
\r
119 final HorizontalPanel permForAll = new HorizontalPanel();
\r
120 final HorizontalPanel pathPanel = new HorizontalPanel();
\r
121 HorizontalPanel buttons = new HorizontalPanel();
\r
122 HorizontalPanel permButtons = new HorizontalPanel();
\r
124 inner.add(generalPanel, "General");
\r
126 inner.add(permPanel, "Sharing");
\r
127 inner.selectTab(0);
\r
129 final Label folderNameNote = new Label("Please note that slashes ('/') are not allowed in folder names.", true);
\r
130 folderNameNote.setVisible(false);
\r
131 folderNameNote.setStylePrimaryName("gss-readForAllNote");
\r
133 FlexTable generalTable = new FlexTable();
\r
134 generalTable.setText(0, 0, "Name");
\r
135 generalTable.setText(1, 0, "Parent");
\r
136 generalTable.setText(2, 0, "Creator");
\r
137 generalTable.setText(3, 0, "Last modified");
\r
138 folderName.setText(create ? "" : folder.getName());
\r
139 generalTable.setWidget(0, 1, folderName);
\r
140 folderName.addChangeHandler(new ChangeHandler() {
\r
143 public void onChange(ChangeEvent event) {
\r
144 if(folderName.getText().contains("/"))
\r
145 folderNameNote.setVisible(true);
\r
147 folderNameNote.setVisible(false);
\r
154 generalTable.setText(1, 1, folder.getName());
\r
155 else if(folder.getParentName() == null)
\r
156 generalTable.setText(1, 1, "-");
\r
158 generalTable.setText(1, 1, folder.getParentName());
\r
159 generalTable.setWidget(0, 2, folderNameNote);
\r
160 generalTable.setText(2, 1, folder.getOwner());
\r
161 DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
\r
162 if(folder.getModificationDate() != null)
\r
163 generalTable.setText(3, 1, formatter.format(folder.getModificationDate()));
\r
164 generalTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
\r
165 generalTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
\r
166 generalTable.getFlexCellFormatter().setStyleName(2, 0, "props-labels");
\r
167 generalTable.getFlexCellFormatter().setStyleName(3, 0, "props-labels");
\r
168 generalTable.getFlexCellFormatter().setStyleName(0, 1, "props-values");
\r
169 generalTable.getFlexCellFormatter().setStyleName(1, 1, "props-values");
\r
170 generalTable.getFlexCellFormatter().setStyleName(2, 1, "props-values");
\r
171 generalTable.getFlexCellFormatter().setStyleName(3, 1, "props-values");
\r
172 generalTable.setCellSpacing(4);
\r
174 // Create the 'Create/Update' button, along with a listener that hides the dialog
\r
175 // when the button is clicked and quits the application.
\r
178 okLabel = "Create";
\r
180 okLabel = "Update";
\r
181 Button ok = new Button(okLabel, new ClickHandler() {
\r
183 public void onClick(ClickEvent event) {
\r
184 if(folderName.getText().contains("/"))
\r
185 folderNameNote.setVisible(true);
\r
187 folderNameNote.setVisible(false);
\r
188 createOrUpdateFolder();
\r
195 buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
\r
196 // Create the 'Cancel' button, along with a listener that hides the
\r
198 // when the button is clicked.
\r
199 Button cancel = new Button("Cancel", new ClickHandler() {
\r
201 public void onClick(ClickEvent event) {
\r
205 buttons.add(cancel);
\r
206 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
\r
207 buttons.setSpacing(8);
\r
208 buttons.addStyleName("gss-TabPanelBottom");
\r
210 Button add = new Button("Add Group", new ClickHandler() {
\r
212 public void onClick(ClickEvent event) {
\r
213 PermissionsAddDialog dlg = new PermissionsAddDialog(groups, permList, false);
\r
217 permButtons.add(add);
\r
218 permButtons.setCellHorizontalAlignment(add, HasHorizontalAlignment.ALIGN_CENTER);
\r
220 Button addUser = new Button("Add User", new ClickHandler() {
\r
222 public void onClick(ClickEvent event) {
\r
223 PermissionsAddDialog dlg = new PermissionsAddDialog(groups, permList, true);
\r
227 permButtons.add(addUser);
\r
228 permButtons.setCellHorizontalAlignment(addUser, HasHorizontalAlignment.ALIGN_CENTER);
\r
230 permButtons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
\r
231 permButtons.setSpacing(8);
\r
232 permButtons.addStyleName("gss-TabPanelBottom");
\r
234 final Label readForAllNote = new Label("When this option is enabled, the folder will be readable" +
\r
235 " by everyone. By checking this option, you are certifying that you have the right to " +
\r
236 "distribute this folder's contents and that it does not violate the Terms of Use.", true);
\r
237 readForAllNote.setVisible(false);
\r
238 readForAllNote.setStylePrimaryName("gss-readForAllNote");
\r
240 readForAll = new CheckBox();
\r
241 readForAll.setValue(folder.isReadForAll());
\r
242 readForAll.addClickHandler(new ClickHandler() {
\r
244 public void onClick(ClickEvent event) {
\r
245 readForAllNote.setVisible(readForAll.getValue());
\r
250 generalPanel.add(generalTable);
\r
251 permPanel.add(permList);
\r
252 permPanel.add(permButtons);
\r
254 // Only show the read for all permission if the user is the owner.
\r
255 if (folder.getOwner().equals(GSS.get().getCurrentUserResource().getUsername())) {
\r
256 permForAll.add(new Label("Public"));
\r
257 permForAll.add(readForAll);
\r
258 permForAll.setSpacing(8);
\r
259 permForAll.addStyleName("gss-TabPanelBottom");
\r
260 permForAll.add(readForAllNote);
\r
261 permPanel.add(permForAll);
\r
263 TextBox path = new TextBox();
\r
264 path.setWidth("100%");
\r
265 path.addClickHandler(new ClickHandler() {
\r
267 public void onClick(ClickEvent event) {
\r
268 GSS.enableIESelection();
\r
269 ((TextBox) event.getSource()).selectAll();
\r
270 GSS.preventIESelection();
\r
274 path.setText(folder.getUri());
\r
275 path.setTitle("Use this link for sharing the folder via e-mail, IM, etc. (crtl-C/cmd-C to copy to system clipboard)");
\r
276 path.setWidth("100%");
\r
277 path.setReadOnly(true);
\r
278 pathPanel.setWidth("100%");
\r
279 pathPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
\r
280 pathPanel.add(new Label("Link"));
\r
281 pathPanel.setSpacing(8);
\r
282 pathPanel.addStyleName("gss-TabPanelBottom");
\r
283 pathPanel.add(path);
\r
284 permPanel.add(pathPanel);
\r
287 outer.add(buttons);
\r
288 outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
\r
289 outer.addStyleName("gss-TabPanelBottom");
\r
294 folderName.setFocus(true);
\r
296 ok.setFocus(true);*/
\r
300 public void center() {
\r
302 folderName.setFocus(true);
\r
306 protected void onPreviewNativeEvent(NativePreviewEvent preview) {
\r
307 super.onPreviewNativeEvent(preview);
\r
309 NativeEvent evt = preview.getNativeEvent();
\r
310 if (evt.getType().equals("keydown"))
\r
311 // Use the popup's key preview hooks to close the dialog when either
\r
312 // enter or escape is pressed.
\r
313 switch (evt.getKeyCode()) {
\r
314 case KeyCodes.KEY_ENTER:
\r
316 createOrUpdateFolder();
\r
318 case KeyCodes.KEY_ESCAPE:
\r
326 * Enables IE selection prevention and hides the dialog
\r
327 * (we disable the prevention on creation of the dialog)
\r
329 public void closeDialog() {
\r
330 GSS.preventIESelection();
\r
335 * Generate an RPC request to create a new folder.
\r
337 * @param userId the ID of the user whose namespace will be searched for
\r
339 * @param _folderName the name of the folder to create
\r
341 private void createFolder() {
\r
342 String name = folderName.getText();
\r
343 if (!GSS.isValidResourceName(name)) {
\r
344 GSS.get().displayError("The folder name '" + name + "' is invalid");
\r
347 PostCommand ep = new PostCommand(folder.getUri() + "?new=" +
\r
348 URL.encodeComponent(name), "", 201) {
\r
351 public void onComplete() {
\r
353 GSS.get().getTreeView().updateNodeChildren(folder);
\r
354 //GSS.get().getFolders().updateFolder((DnDTreeItem) GSS.get().getFolders().getCurrent());
\r
358 public void onError(Throwable t) {
\r
360 if(t instanceof RestException){
\r
361 int statusCode = ((RestException)t).getHttpStatusCode();
\r
362 if(statusCode == 405)
\r
363 GSS.get().displayError("You don't have the necessary" +
\r
364 " permissions or a folder with same name " +
\r
366 else if(statusCode == 404)
\r
367 GSS.get().displayError("Resource not found");
\r
369 GSS.get().displayError("Unable to create folder:" +
\r
370 ((RestException)t).getHttpStatusText());
\r
373 GSS.get().displayError("System error creating folder:" +
\r
377 DeferredCommand.addCommand(ep);
\r
382 * Upon closing the dialog by clicking OK or pressing ENTER this method does
\r
383 * the actual work of modifying folder properties or creating a new Folder
\r
384 * depending on the value of the create field
\r
388 private void createOrUpdateFolder() {
\r
396 private void updateFolder() {
\r
397 permList.updatePermissionsAccordingToInput();
\r
398 Set<PermissionHolder> perms = permList.getPermissions();
\r
399 JSONObject json = new JSONObject();
\r
400 if(!folder.getName().equals(folderName.getText()))
\r
401 json.put("name", new JSONString(folderName.getText()));
\r
402 //only update the read for all perm if the user is the owner
\r
403 if (readForAll.getValue() != folder.isReadForAll())
\r
404 if (folder.getOwner().equals(GSS.get().getCurrentUserResource().getUsername()))
\r
405 json.put("readForAll", JSONBoolean.getInstance(readForAll.getValue()));
\r
406 if (permList.hasChanges()) {
\r
407 JSONArray perma = new JSONArray();
\r
409 for(PermissionHolder p : perms){
\r
410 JSONObject po = new JSONObject();
\r
411 if(p.getUser() != null)
\r
412 po.put("user", new JSONString(p.getUser()));
\r
413 if(p.getGroup() != null)
\r
414 po.put("group", new JSONString(p.getGroup()));
\r
415 po.put("read", JSONBoolean.getInstance(p.isRead()));
\r
416 po.put("write", JSONBoolean.getInstance(p.isWrite()));
\r
417 po.put("modifyACL", JSONBoolean.getInstance(p.isModifyACL()));
\r
421 json.put("permissions", perma);
\r
422 GWT.log(json.toString(), null);
\r
424 PostCommand ep = new PostCommand(folder.getUri()+"?update=", json.toString(), 200){
\r
427 public void onComplete() {
\r
430 if(getPostBody() != null && !"".equals(getPostBody().trim())){
\r
433 FolderResource fres = ((RestResourceWrapper) GSS.get().getTreeView().getSelection()).getResource();
\r
434 String initialPath = fres.getUri();
\r
435 String newPath = getPostBody().trim();
\r
436 fres.setUri(newPath);
\r
437 GSS.get().getTreeView().updateNodeChildren(fres.getParentURI());
\r
439 if(folderItem.getParentItem() != null && ((DnDTreeItem)folderItem.getParentItem()).getFolderResource() != null){
\r
440 ((DnDTreeItem)folderItem.getParentItem()).getFolderResource().removeSubfolderPath(initialPath);
\r
441 ((DnDTreeItem)folderItem.getParentItem()).getFolderResource().getSubfolderPaths().add(newPath);
\r
444 //GSS.get().getFolders().updateFolder( (DnDTreeItem) GSS.get().getFolders().getCurrent());
\r
446 GSS.get().showFileList(true);
\r
450 public void onError(Throwable t) {
\r
452 if(t instanceof RestException){
\r
453 int statusCode = ((RestException)t).getHttpStatusCode();
\r
454 if(statusCode == 405)
\r
455 GSS.get().displayError("You don't have the necessary permissions or" +
\r
456 " a folder with same name already exists");
\r
457 else if(statusCode == 404)
\r
458 GSS.get().displayError("Resource not found, or user specified in sharing does not exist");
\r
460 GSS.get().displayError("Unable to update folder: "+((RestException)t).getHttpStatusText());
\r
463 GSS.get().displayError("System error moifying file: "+t.getMessage());
\r
465 //GSS.get().getFolders().updateFolder( (DnDTreeItem) GSS.get().getFolders().getCurrent());
\r
468 DeferredCommand.addCommand(ep);
\r
471 public void selectTab(int _tab) {
\r
472 inner.selectTab(_tab);
\r