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.FilePropertiesDialog.Images;
\r
22 import gr.ebs.gss.client.dnd.DnDTreeItem;
\r
23 import gr.ebs.gss.client.rest.PostCommand;
\r
24 import gr.ebs.gss.client.rest.RestException;
\r
25 import gr.ebs.gss.client.rest.resource.FolderResource;
\r
26 import gr.ebs.gss.client.rest.resource.GroupResource;
\r
27 import gr.ebs.gss.client.rest.resource.PermissionHolder;
\r
29 import java.util.List;
\r
30 import java.util.Set;
\r
32 import com.google.gwt.core.client.GWT;
\r
33 import com.google.gwt.dom.client.NativeEvent;
\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.KeyCodes;
\r
37 import com.google.gwt.http.client.URL;
\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.user.client.DeferredCommand;
\r
44 import com.google.gwt.user.client.Event.NativePreviewEvent;
\r
45 import com.google.gwt.user.client.ui.Button;
\r
46 import com.google.gwt.user.client.ui.CheckBox;
\r
47 import com.google.gwt.user.client.ui.DecoratedTabPanel;
\r
48 import com.google.gwt.user.client.ui.DialogBox;
\r
49 import com.google.gwt.user.client.ui.FlexTable;
\r
50 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
\r
51 import com.google.gwt.user.client.ui.HorizontalPanel;
\r
52 import com.google.gwt.user.client.ui.Label;
\r
53 import com.google.gwt.user.client.ui.TabPanel;
\r
54 import com.google.gwt.user.client.ui.TextBox;
\r
55 import com.google.gwt.user.client.ui.VerticalPanel;
\r
58 * The 'Folder properties' dialog box implementation.
\r
60 public class FolderPropertiesDialog extends DialogBox {
\r
62 private List<GroupResource> groups = null;
\r
64 final PermissionsList permList;
\r
66 private CheckBox readForAll;
\r
69 * The widget that holds the folderName of the folder.
\r
71 private TextBox folderName = new TextBox();
\r
74 * A flag that denotes whether the dialog will be used to create or modify a
\r
77 private final boolean create;
\r
79 final FolderResource folder;
\r
81 final TabPanel inner;
\r
84 * The widget's constructor.
\r
86 * @param images the image icons from the file properties dialog
\r
87 * @param _create true if the dialog is displayed for creating a new
\r
88 * sub-folder of the selected folder, false if it is displayed
\r
89 * for modifying the selected folder
\r
91 public FolderPropertiesDialog(Images images, boolean _create, final List<GroupResource> _groups) {
\r
92 setAnimationEnabled(true);
\r
94 // Enable IE selection for the dialog (must disable it upon closing it)
\r
95 GSS.enableIESelection();
\r
98 DnDTreeItem folderItem = (DnDTreeItem)GSS.get().getFolders().getCurrent();
\r
99 folder = folderItem.getFolderResource();
\r
100 permList = new PermissionsList(images, folder.getPermissions(), folder.getOwner());
\r
103 // Use this opportunity to set the dialog's caption.
\r
105 setText("Create folder");
\r
107 setText("Folder properties");
\r
109 // Outer contains inner and buttons
\r
110 VerticalPanel outer = new VerticalPanel();
\r
111 // Inner contains generalPanel and permPanel
\r
112 inner = new DecoratedTabPanel();
\r
113 inner.setAnimationEnabled(true);
\r
114 VerticalPanel generalPanel = new VerticalPanel();
\r
115 VerticalPanel permPanel = new VerticalPanel();
\r
116 final HorizontalPanel permForAll = new HorizontalPanel();
\r
117 final HorizontalPanel pathPanel = new HorizontalPanel();
\r
118 HorizontalPanel buttons = new HorizontalPanel();
\r
119 HorizontalPanel permButtons = new HorizontalPanel();
\r
121 inner.add(generalPanel, "General");
\r
123 inner.add(permPanel, "Sharing");
\r
124 inner.selectTab(0);
\r
126 FlexTable generalTable = new FlexTable();
\r
127 generalTable.setText(0, 0, "Name");
\r
128 generalTable.setText(1, 0, "Parent");
\r
129 generalTable.setText(2, 0, "Creator");
\r
130 generalTable.setText(3, 0, "Last modified");
\r
131 folderName.setText(create ? "" : folder.getName());
\r
132 generalTable.setWidget(0, 1, folderName);
\r
134 generalTable.setText(1, 1, folder.getName());
\r
135 else if(folder.getParentName() == null)
\r
136 generalTable.setText(1, 1, "-");
\r
138 generalTable.setText(1, 1, folder.getParentName());
\r
139 generalTable.setText(2, 1, folder.getOwner());
\r
140 DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
\r
141 if(folder.getModificationDate() != null)
\r
142 generalTable.setText(3, 1, formatter.format(folder.getModificationDate()));
\r
143 generalTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
\r
144 generalTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
\r
145 generalTable.getFlexCellFormatter().setStyleName(2, 0, "props-labels");
\r
146 generalTable.getFlexCellFormatter().setStyleName(3, 0, "props-labels");
\r
147 generalTable.getFlexCellFormatter().setStyleName(0, 1, "props-values");
\r
148 generalTable.getFlexCellFormatter().setStyleName(1, 1, "props-values");
\r
149 generalTable.getFlexCellFormatter().setStyleName(2, 1, "props-values");
\r
150 generalTable.getFlexCellFormatter().setStyleName(3, 1, "props-values");
\r
151 generalTable.setCellSpacing(4);
\r
153 // Create the 'Create/Update' button, along with a listener that hides the dialog
\r
154 // when the button is clicked and quits the application.
\r
157 okLabel = "Create";
\r
159 okLabel = "Update";
\r
160 Button ok = new Button(okLabel, new ClickHandler() {
\r
162 public void onClick(ClickEvent event) {
\r
164 createOrUpdateFolder();
\r
170 buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
\r
171 // Create the 'Cancel' button, along with a listener that hides the
\r
173 // when the button is clicked.
\r
174 Button cancel = new Button("Cancel", new ClickHandler() {
\r
176 public void onClick(ClickEvent event) {
\r
180 buttons.add(cancel);
\r
181 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
\r
182 buttons.setSpacing(8);
\r
183 buttons.addStyleName("gss-TabPanelBottom");
\r
185 Button add = new Button("Add Group", new ClickHandler() {
\r
187 public void onClick(ClickEvent event) {
\r
188 PermissionsAddDialog dlg = new PermissionsAddDialog(groups, permList, false);
\r
192 permButtons.add(add);
\r
193 permButtons.setCellHorizontalAlignment(add, HasHorizontalAlignment.ALIGN_CENTER);
\r
195 Button addUser = new Button("Add User", new ClickHandler() {
\r
197 public void onClick(ClickEvent event) {
\r
198 PermissionsAddDialog dlg = new PermissionsAddDialog(groups, permList, true);
\r
202 permButtons.add(addUser);
\r
203 permButtons.setCellHorizontalAlignment(addUser, HasHorizontalAlignment.ALIGN_CENTER);
\r
205 permButtons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
\r
206 permButtons.setSpacing(8);
\r
207 permButtons.addStyleName("gss-TabPanelBottom");
\r
209 final Label readForAllNote = new Label("When this option is enabled, the folder will be readable" +
\r
210 " by everyone. By checking this option, you are certifying that you have the right to " +
\r
211 "distribute this folder's contents and that it does not violate the Terms of Use.", true);
\r
212 readForAllNote.setVisible(false);
\r
213 readForAllNote.setStylePrimaryName("gss-readForAllNote");
\r
215 readForAll = new CheckBox();
\r
216 readForAll.setValue(folder.isReadForAll());
\r
217 readForAll.addClickHandler(new ClickHandler() {
\r
219 public void onClick(ClickEvent event) {
\r
220 readForAllNote.setVisible(readForAll.getValue());
\r
225 generalPanel.add(generalTable);
\r
226 permPanel.add(permList);
\r
227 permPanel.add(permButtons);
\r
229 // Only show the read for all permission if the user is the owner.
\r
230 if (folder.getOwner().equals(GSS.get().getCurrentUserResource().getUsername())) {
\r
231 permForAll.add(new Label("Public"));
\r
232 permForAll.add(readForAll);
\r
233 permForAll.setSpacing(8);
\r
234 permForAll.addStyleName("gss-TabPanelBottom");
\r
235 permForAll.add(readForAllNote);
\r
236 permPanel.add(permForAll);
\r
238 TextBox path = new TextBox();
\r
239 path.setWidth("100%");
\r
240 path.addClickHandler(new ClickHandler() {
\r
242 public void onClick(ClickEvent event) {
\r
243 GSS.enableIESelection();
\r
244 ((TextBox) event.getSource()).selectAll();
\r
245 GSS.preventIESelection();
\r
249 path.setText(folder.getUri());
\r
250 path.setTitle("Use this link for sharing the folder via e-mail, IM, etc. (crtl-C/cmd-C to copy to system clipboard)");
\r
251 path.setWidth("100%");
\r
252 path.setReadOnly(true);
\r
253 pathPanel.setWidth("100%");
\r
254 pathPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
\r
255 pathPanel.add(new Label("Link"));
\r
256 pathPanel.setSpacing(8);
\r
257 pathPanel.addStyleName("gss-TabPanelBottom");
\r
258 pathPanel.add(path);
\r
259 permPanel.add(pathPanel);
\r
262 outer.add(buttons);
\r
263 outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
\r
264 outer.addStyleName("gss-TabPanelBottom");
\r
269 folderName.setFocus(true);
\r
271 ok.setFocus(true);*/
\r
275 public void center() {
\r
277 folderName.setFocus(true);
\r
281 protected void onPreviewNativeEvent(NativePreviewEvent preview) {
\r
282 super.onPreviewNativeEvent(preview);
\r
284 NativeEvent evt = preview.getNativeEvent();
\r
285 if (evt.getType().equals("keydown"))
\r
286 // Use the popup's key preview hooks to close the dialog when either
\r
287 // enter or escape is pressed.
\r
288 switch (evt.getKeyCode()) {
\r
289 case KeyCodes.KEY_ENTER:
\r
291 createOrUpdateFolder();
\r
293 case KeyCodes.KEY_ESCAPE:
\r
301 * Enables IE selection prevention and hides the dialog
\r
302 * (we disable the prevention on creation of the dialog)
\r
304 public void closeDialog() {
\r
305 GSS.preventIESelection();
\r
310 * Generate an RPC request to create a new folder.
\r
312 * @param userId the ID of the user whose namespace will be searched for
\r
314 * @param _folderName the name of the folder to create
\r
316 private void createFolder() {
\r
317 PostCommand ep = new PostCommand(folder.getUri() + "?new=" +
\r
318 URL.encodeComponent(folderName.getText()), "", 201) {
\r
321 public void onComplete() {
\r
322 GSS.get().getFolders().updateFolder((DnDTreeItem) GSS.get().getFolders().getCurrent());
\r
326 public void onError(Throwable t) {
\r
328 if(t instanceof RestException){
\r
329 int statusCode = ((RestException)t).getHttpStatusCode();
\r
330 if(statusCode == 405)
\r
331 GSS.get().displayError("You don't have the necessary" +
\r
332 " permissions or a folder with same name " +
\r
334 else if(statusCode == 404)
\r
335 GSS.get().displayError("Resource not found");
\r
337 GSS.get().displayError("Unable to create folder:" +
\r
338 ((RestException)t).getHttpStatusText());
\r
341 GSS.get().displayError("System error creating folder:" +
\r
345 DeferredCommand.addCommand(ep);
\r
350 * Upon closing the dialog by clicking OK or pressing ENTER this method does
\r
351 * the actual work of modifying folder properties or creating a new Folder
\r
352 * depending on the value of the create field
\r
356 private void createOrUpdateFolder() {
\r
364 private void updateFolder() {
\r
365 permList.updatePermissionsAccordingToInput();
\r
366 Set<PermissionHolder> perms = permList.getPermissions();
\r
367 JSONObject json = new JSONObject();
\r
368 if(!folder.getName().equals(folderName.getText()))
\r
369 json.put("name", new JSONString(folderName.getText()));
\r
370 //only update the read for all perm if the user is the owner
\r
371 if (readForAll.getValue() != folder.isReadForAll())
\r
372 if (folder.getOwner().equals(GSS.get().getCurrentUserResource().getUsername()))
\r
373 json.put("readForAll", JSONBoolean.getInstance(readForAll.getValue()));
\r
374 if (permList.hasChanges()) {
\r
375 JSONArray perma = new JSONArray();
\r
377 for(PermissionHolder p : perms){
\r
378 JSONObject po = new JSONObject();
\r
379 if(p.getUser() != null)
\r
380 po.put("user", new JSONString(p.getUser()));
\r
381 if(p.getGroup() != null)
\r
382 po.put("group", new JSONString(p.getGroup()));
\r
383 po.put("read", JSONBoolean.getInstance(p.isRead()));
\r
384 po.put("write", JSONBoolean.getInstance(p.isWrite()));
\r
385 po.put("modifyACL", JSONBoolean.getInstance(p.isModifyACL()));
\r
389 json.put("permissions", perma);
\r
390 GWT.log(json.toString(), null);
\r
392 PostCommand ep = new PostCommand(folder.getUri()+"?update=", json.toString(), 200){
\r
395 public void onComplete() {
\r
396 if(getPostBody() != null && !"".equals(getPostBody().trim())){
\r
397 DnDTreeItem folderItem = (DnDTreeItem) GSS.get().getFolders().getCurrent();
\r
398 FolderResource fres = folderItem.getFolderResource();
\r
399 String initialPath = fres.getUri();
\r
400 String newPath = getPostBody().trim();
\r
401 fres.setUri(newPath);
\r
403 if(folderItem.getParentItem() != null && ((DnDTreeItem)folderItem.getParentItem()).getFolderResource() != null){
\r
404 ((DnDTreeItem)folderItem.getParentItem()).getFolderResource().removeSubfolderPath(initialPath);
\r
405 ((DnDTreeItem)folderItem.getParentItem()).getFolderResource().getSubfolderPaths().add(newPath);
\r
408 GSS.get().getFolders().updateFolder( (DnDTreeItem) GSS.get().getFolders().getCurrent());
\r
409 GSS.get().showFileList(true);
\r
413 public void onError(Throwable t) {
\r
415 if(t instanceof RestException){
\r
416 int statusCode = ((RestException)t).getHttpStatusCode();
\r
417 if(statusCode == 405)
\r
418 GSS.get().displayError("You don't have the necessary permissions or" +
\r
419 " a folder with same name already exists");
\r
420 else if(statusCode == 404)
\r
421 GSS.get().displayError("Resource not found, or user specified in sharing does not exist");
\r
423 GSS.get().displayError("Unable to update folder: "+((RestException)t).getHttpStatusText());
\r
426 GSS.get().displayError("System error moifying file: "+t.getMessage());
\r
427 GSS.get().getFolders().updateFolder( (DnDTreeItem) GSS.get().getFolders().getCurrent());
\r
430 DeferredCommand.addCommand(ep);
\r
433 public void selectTab(int _tab) {
\r
434 inner.selectTab(_tab);
\r