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 folderName.getElement().setId("folderPropertiesDialog.textBox.name");
\r
140 generalTable.setWidget(0, 1, folderName);
\r
141 folderName.addChangeHandler(new ChangeHandler() {
\r
144 public void onChange(ChangeEvent event) {
\r
145 if(folderName.getText().contains("/"))
\r
146 folderNameNote.setVisible(true);
\r
148 folderNameNote.setVisible(false);
\r
155 generalTable.setText(1, 1, folder.getName());
\r
156 else if(folder.getParentName() == null)
\r
157 generalTable.setText(1, 1, "-");
\r
159 generalTable.setText(1, 1, folder.getParentName());
\r
160 generalTable.setWidget(0, 2, folderNameNote);
\r
161 generalTable.setText(2, 1, folder.getOwner());
\r
162 DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
\r
163 if(folder.getModificationDate() != null)
\r
164 generalTable.setText(3, 1, formatter.format(folder.getModificationDate()));
\r
165 generalTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
\r
166 generalTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
\r
167 generalTable.getFlexCellFormatter().setStyleName(2, 0, "props-labels");
\r
168 generalTable.getFlexCellFormatter().setStyleName(3, 0, "props-labels");
\r
169 generalTable.getFlexCellFormatter().setStyleName(0, 1, "props-values");
\r
170 generalTable.getFlexCellFormatter().setStyleName(1, 1, "props-values");
\r
171 generalTable.getFlexCellFormatter().setStyleName(2, 1, "props-values");
\r
172 generalTable.getFlexCellFormatter().setStyleName(3, 1, "props-values");
\r
173 generalTable.setCellSpacing(4);
\r
175 // Create the 'Create/Update' button, along with a listener that hides the dialog
\r
176 // when the button is clicked and quits the application.
\r
179 okLabel = "Create";
\r
181 okLabel = "Update";
\r
182 Button ok = new Button(okLabel, new ClickHandler() {
\r
184 public void onClick(ClickEvent event) {
\r
185 if(folderName.getText().contains("/"))
\r
186 folderNameNote.setVisible(true);
\r
188 folderNameNote.setVisible(false);
\r
189 createOrUpdateFolder();
\r
195 ok.getElement().setId("folderPropertiesDialog.button.ok");
\r
197 buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
\r
198 // Create the 'Cancel' button, along with a listener that hides the
\r
200 // when the button is clicked.
\r
201 Button cancel = new Button("Cancel", new ClickHandler() {
\r
203 public void onClick(ClickEvent event) {
\r
207 cancel.getElement().setId("folderPropertiesDialog.button.cancel");
\r
208 buttons.add(cancel);
\r
209 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
\r
210 buttons.setSpacing(8);
\r
211 buttons.addStyleName("gss-TabPanelBottom");
\r
213 Button add = new Button("Add Group", new ClickHandler() {
\r
215 public void onClick(ClickEvent event) {
\r
216 PermissionsAddDialog dlg = new PermissionsAddDialog(groups, permList, false);
\r
220 permButtons.add(add);
\r
221 permButtons.setCellHorizontalAlignment(add, HasHorizontalAlignment.ALIGN_CENTER);
\r
223 Button addUser = new Button("Add User", new ClickHandler() {
\r
225 public void onClick(ClickEvent event) {
\r
226 PermissionsAddDialog dlg = new PermissionsAddDialog(groups, permList, true);
\r
230 permButtons.add(addUser);
\r
231 permButtons.setCellHorizontalAlignment(addUser, HasHorizontalAlignment.ALIGN_CENTER);
\r
233 permButtons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
\r
234 permButtons.setSpacing(8);
\r
235 permButtons.addStyleName("gss-TabPanelBottom");
\r
237 final Label readForAllNote = new Label("When this option is enabled, the folder will be readable" +
\r
238 " by everyone. By checking this option, you are certifying that you have the right to " +
\r
239 "distribute this folder's contents and that it does not violate the Terms of Use.", true);
\r
240 readForAllNote.setVisible(false);
\r
241 readForAllNote.setStylePrimaryName("gss-readForAllNote");
\r
243 readForAll = new CheckBox();
\r
244 readForAll.setValue(folder.isReadForAll());
\r
245 readForAll.addClickHandler(new ClickHandler() {
\r
247 public void onClick(ClickEvent event) {
\r
248 readForAllNote.setVisible(readForAll.getValue());
\r
253 generalPanel.add(generalTable);
\r
254 permPanel.add(permList);
\r
255 permPanel.add(permButtons);
\r
257 // Only show the read for all permission if the user is the owner.
\r
258 if (folder.getOwner().equals(GSS.get().getCurrentUserResource().getUsername())) {
\r
259 permForAll.add(new Label("Public"));
\r
260 permForAll.add(readForAll);
\r
261 permForAll.setSpacing(8);
\r
262 permForAll.addStyleName("gss-TabPanelBottom");
\r
263 permForAll.add(readForAllNote);
\r
264 permPanel.add(permForAll);
\r
266 TextBox path = new TextBox();
\r
267 path.setWidth("100%");
\r
268 path.addClickHandler(new ClickHandler() {
\r
270 public void onClick(ClickEvent event) {
\r
271 GSS.enableIESelection();
\r
272 ((TextBox) event.getSource()).selectAll();
\r
273 GSS.preventIESelection();
\r
277 path.setText(folder.getUri());
\r
278 path.setTitle("Use this link for sharing the folder via e-mail, IM, etc. (crtl-C/cmd-C to copy to system clipboard)");
\r
279 path.setWidth("100%");
\r
280 path.setReadOnly(true);
\r
281 pathPanel.setWidth("100%");
\r
282 pathPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
\r
283 pathPanel.add(new Label("Link"));
\r
284 pathPanel.setSpacing(8);
\r
285 pathPanel.addStyleName("gss-TabPanelBottom");
\r
286 pathPanel.add(path);
\r
287 permPanel.add(pathPanel);
\r
290 outer.add(buttons);
\r
291 outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
\r
292 outer.addStyleName("gss-TabPanelBottom");
\r
297 folderName.setFocus(true);
\r
299 ok.setFocus(true);*/
\r
303 public void center() {
\r
305 folderName.setFocus(true);
\r
309 protected void onPreviewNativeEvent(NativePreviewEvent preview) {
\r
310 super.onPreviewNativeEvent(preview);
\r
312 NativeEvent evt = preview.getNativeEvent();
\r
313 if (evt.getType().equals("keydown"))
\r
314 // Use the popup's key preview hooks to close the dialog when either
\r
315 // enter or escape is pressed.
\r
316 switch (evt.getKeyCode()) {
\r
317 case KeyCodes.KEY_ENTER:
\r
319 createOrUpdateFolder();
\r
321 case KeyCodes.KEY_ESCAPE:
\r
329 * Enables IE selection prevention and hides the dialog
\r
330 * (we disable the prevention on creation of the dialog)
\r
332 public void closeDialog() {
\r
333 GSS.preventIESelection();
\r
338 * Generate an RPC request to create a new folder.
\r
340 * @param userId the ID of the user whose namespace will be searched for
\r
342 * @param _folderName the name of the folder to create
\r
344 private void createFolder() {
\r
345 String name = folderName.getText();
\r
346 if (!GSS.isValidResourceName(name)) {
\r
347 GSS.get().displayError("The folder name '" + name + "' is invalid");
\r
350 PostCommand ep = new PostCommand(folder.getUri() + "?new=" +
\r
351 URL.encodeComponent(name), "", 201) {
\r
354 public void onComplete() {
\r
356 GSS.get().getTreeView().updateNodeChildren((RestResourceWrapper) GSS.get().getTreeView().getSelection());
\r
357 //GSS.get().getFolders().updateFolder((DnDTreeItem) GSS.get().getFolders().getCurrent());
\r
361 public void onError(Throwable t) {
\r
363 if(t instanceof RestException){
\r
364 int statusCode = ((RestException)t).getHttpStatusCode();
\r
365 if(statusCode == 405)
\r
366 GSS.get().displayError("You don't have the necessary" +
\r
367 " permissions or a folder with same name " +
\r
369 else if(statusCode == 404)
\r
370 GSS.get().displayError("Resource not found");
\r
372 GSS.get().displayError("Unable to create folder:" +
\r
373 ((RestException)t).getHttpStatusText());
\r
376 GSS.get().displayError("System error creating folder:" +
\r
380 DeferredCommand.addCommand(ep);
\r
385 * Upon closing the dialog by clicking OK or pressing ENTER this method does
\r
386 * the actual work of modifying folder properties or creating a new Folder
\r
387 * depending on the value of the create field
\r
391 private void createOrUpdateFolder() {
\r
399 private void updateFolder() {
\r
400 permList.updatePermissionsAccordingToInput();
\r
401 Set<PermissionHolder> perms = permList.getPermissions();
\r
402 JSONObject json = new JSONObject();
\r
403 if(!folder.getName().equals(folderName.getText()))
\r
404 json.put("name", new JSONString(folderName.getText()));
\r
405 //only update the read for all perm if the user is the owner
\r
406 if (readForAll.getValue() != folder.isReadForAll())
\r
407 if (folder.getOwner().equals(GSS.get().getCurrentUserResource().getUsername()))
\r
408 json.put("readForAll", JSONBoolean.getInstance(readForAll.getValue()));
\r
409 if (permList.hasChanges()) {
\r
410 JSONArray perma = new JSONArray();
\r
412 for(PermissionHolder p : perms){
\r
413 JSONObject po = new JSONObject();
\r
414 if(p.getUser() != null)
\r
415 po.put("user", new JSONString(p.getUser()));
\r
416 if(p.getGroup() != null)
\r
417 po.put("group", new JSONString(p.getGroup()));
\r
418 po.put("read", JSONBoolean.getInstance(p.isRead()));
\r
419 po.put("write", JSONBoolean.getInstance(p.isWrite()));
\r
420 po.put("modifyACL", JSONBoolean.getInstance(p.isModifyACL()));
\r
424 json.put("permissions", perma);
\r
425 GWT.log(json.toString(), null);
\r
427 PostCommand ep = new PostCommand(folder.getUri()+"?update=", json.toString(), 200){
\r
430 public void onComplete() {
\r
433 if(getPostBody() != null && !"".equals(getPostBody().trim())){
\r
436 FolderResource fres = ((RestResourceWrapper) GSS.get().getTreeView().getSelection()).getResource();
\r
437 String initialPath = fres.getUri();
\r
438 String newPath = getPostBody().trim();
\r
439 fres.setUri(newPath);
\r
440 ((RestResourceWrapper) GSS.get().getTreeView().getSelection()).getResource().setUri(newPath);
\r
441 ((RestResourceWrapper) GSS.get().getTreeView().getSelection()).setUri(newPath);
\r
442 GSS.get().getTreeView().updateNodeChildren(fres.getParentURI());
\r
443 if (permList.hasChanges()) {
\r
444 GSS.get().getTreeView().updateMySharedNode();
\r
447 if(folderItem.getParentItem() != null && ((DnDTreeItem)folderItem.getParentItem()).getFolderResource() != null){
\r
448 ((DnDTreeItem)folderItem.getParentItem()).getFolderResource().removeSubfolderPath(initialPath);
\r
449 ((DnDTreeItem)folderItem.getParentItem()).getFolderResource().getSubfolderPaths().add(newPath);
\r
452 //GSS.get().getFolders().updateFolder( (DnDTreeItem) GSS.get().getFolders().getCurrent());
\r
454 GSS.get().showFileList(true);
\r
458 public void onError(Throwable t) {
\r
460 if(t instanceof RestException){
\r
461 int statusCode = ((RestException)t).getHttpStatusCode();
\r
462 if(statusCode == 405)
\r
463 GSS.get().displayError("You don't have the necessary permissions or" +
\r
464 " a folder with same name already exists");
\r
465 else if(statusCode == 404)
\r
466 GSS.get().displayError("Resource not found, or user specified in sharing does not exist");
\r
468 GSS.get().displayError("Unable to update folder: "+((RestException)t).getHttpStatusText());
\r
471 GSS.get().displayError("System error moifying file: "+t.getMessage());
\r
473 //GSS.get().getFolders().updateFolder( (DnDTreeItem) GSS.get().getFolders().getCurrent());
\r
476 DeferredCommand.addCommand(ep);
\r
479 public void selectTab(int _tab) {
\r
480 inner.selectTab(_tab);
\r