2 * Copyright 2008, 2009 Electronic Business Systems Ltd.
4 * This file is part of GSS.
6 * GSS is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GSS is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GSS. If not, see <http://www.gnu.org/licenses/>.
19 package gr.ebs.gss.client;
21 import gr.ebs.gss.client.rest.GetCommand;
22 import gr.ebs.gss.client.rest.RestException;
23 import gr.ebs.gss.client.rest.resource.GroupResource;
24 import gr.ebs.gss.client.rest.resource.PermissionHolder;
25 import gr.ebs.gss.client.rest.resource.UserResource;
26 import gr.ebs.gss.client.rest.resource.UserSearchResource;
28 import java.util.List;
30 import com.google.gwt.core.client.GWT;
31 import com.google.gwt.dom.client.NativeEvent;
32 import com.google.gwt.event.dom.client.ClickEvent;
33 import com.google.gwt.event.dom.client.ClickHandler;
34 import com.google.gwt.event.dom.client.FocusEvent;
35 import com.google.gwt.event.dom.client.FocusHandler;
36 import com.google.gwt.event.dom.client.KeyCodes;
37 import com.google.gwt.event.dom.client.KeyUpEvent;
38 import com.google.gwt.event.dom.client.KeyUpHandler;
39 import com.google.gwt.http.client.URL;
40 import com.google.gwt.user.client.DeferredCommand;
41 import com.google.gwt.user.client.Event.NativePreviewEvent;
42 import com.google.gwt.user.client.ui.Button;
43 import com.google.gwt.user.client.ui.CheckBox;
44 import com.google.gwt.user.client.ui.DialogBox;
45 import com.google.gwt.user.client.ui.FlexTable;
46 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
47 import com.google.gwt.user.client.ui.HorizontalPanel;
48 import com.google.gwt.user.client.ui.ListBox;
49 import com.google.gwt.user.client.ui.MultiWordSuggestOracle;
50 import com.google.gwt.user.client.ui.SuggestBox;
51 import com.google.gwt.user.client.ui.VerticalPanel;
56 public class PermissionsAddDialog extends DialogBox {
58 private MultiWordSuggestOracle oracle = new MultiWordSuggestOracle();
59 private SuggestBox suggestBox = new SuggestBox(oracle);
61 private String selectedUser = null;
63 private List<GroupResource> groups;
65 private ListBox groupBox = new ListBox();
67 private CheckBox read = new CheckBox();
69 private CheckBox write = new CheckBox();
71 private CheckBox modifyACL = new CheckBox();
73 private final PermissionsList permList;
77 public PermissionsAddDialog(List<GroupResource> _groups, PermissionsList _permList, boolean _userAdd) {
81 for (GroupResource group : _groups)
82 groupBox.addItem(group.getName(), group.getName());
83 final VerticalPanel panel = new VerticalPanel();
84 final HorizontalPanel buttons = new HorizontalPanel();
86 final FlexTable permTable = new FlexTable();
87 permTable.setText(0, 0, "Users/Groups");
88 permTable.setText(0, 1, "Read");
89 permTable.setText(0, 2, "Write");
90 permTable.setText(0, 3, "Modify Access");
91 permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
92 permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
93 permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
94 permTable.getFlexCellFormatter().setStyleName(0, 3, "props-toplabels");
96 suggestBox.getTextBox().addFocusHandler(new FocusHandler() {
99 public void onFocus(FocusEvent event) {
100 if (selectedUser != null && selectedUser.endsWith("@"))
106 suggestBox.addKeyUpHandler(new KeyUpHandler() {
109 public void onKeyUp(KeyUpEvent event) {
110 // Ignore the arrow keys.
111 int keyCode = event.getNativeKeyCode();
112 if (keyCode == KeyCodes.KEY_UP ||
113 keyCode == KeyCodes.KEY_DOWN ||
114 keyCode == KeyCodes.KEY_LEFT ||
115 keyCode == KeyCodes.KEY_RIGHT)
117 String text = suggestBox.getText().trim();
118 // Avoid useless queries for keystrokes that do not modify
120 if (text.equals(selectedUser))
123 // Go to the server only if the user typed the @ character.
124 if (selectedUser.endsWith("@"))
128 permTable.setWidget(1, 0, suggestBox);
130 permTable.setWidget(1, 0, groupBox);
131 permTable.setWidget(1, 1, read);
132 permTable.setWidget(1, 2, write);
133 permTable.setWidget(1, 3, modifyACL);
135 permTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
136 permTable.getFlexCellFormatter().setHorizontalAlignment(1, 1, HasHorizontalAlignment.ALIGN_CENTER);
137 permTable.getFlexCellFormatter().setHorizontalAlignment(1, 2, HasHorizontalAlignment.ALIGN_CENTER);
138 permTable.getFlexCellFormatter().setHorizontalAlignment(1, 3, HasHorizontalAlignment.ALIGN_CENTER);
139 panel.add(permTable);
141 final Button ok = new Button("OK", new ClickHandler() {
143 public void onClick(ClickEvent event) {
149 buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
150 // Create the 'Cancel' button, along with a listener that hides the
152 // when the button is clicked.
153 final Button cancel = new Button("Cancel", new ClickHandler() {
155 public void onClick(ClickEvent event) {
160 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
161 buttons.setSpacing(8);
162 buttons.addStyleName("gss-TabPanelBottom");
164 panel.addStyleName("gss-TabPanelBottom");
167 private void addPermission() {
168 PermissionHolder perm = new PermissionHolder();
170 selectedUser = suggestBox.getText();
171 for(PermissionHolder p : permList.permissions)
172 if (selectedUser.equals(p.getUser())){
173 GSS.get().displayError("User already has access to the resource");
176 perm.setUser(selectedUser);
178 String groupId = groupBox.getValue(groupBox.getSelectedIndex());
179 GroupResource selected = null;
180 for (GroupResource g : groups)
181 if (g.getName().equals(groupId))
183 if (selected == null)
185 for(PermissionHolder p : permList.permissions)
186 if (selected.getName().equals(p.getGroup())){
187 GSS.get().displayError("Group already has access to the resource");
190 perm.setGroup(selected.getName());
192 boolean readValue = read.getValue();
193 boolean writeValue = write.getValue();
194 boolean modifyValue = modifyACL.getValue();
196 perm.setRead(readValue);
197 perm.setWrite(writeValue);
198 perm.setModifyACL(modifyValue);
199 permList.addPermission(perm);
200 permList.updateTable();
204 protected void onPreviewNativeEvent(NativePreviewEvent preview) {
205 super.onPreviewNativeEvent(preview);
207 NativeEvent evt = preview.getNativeEvent();
208 if (evt.getType().equals("keydown"))
209 // Use the popup's key preview hooks to close the dialog when either
210 // enter or escape is pressed.
211 switch (evt.getKeyCode()) {
212 case KeyCodes.KEY_ENTER:
216 case KeyCodes.KEY_ESCAPE:
224 public void center() {
227 suggestBox.setFocus(true);
231 * Update the list of suggestions.
233 protected void updateSuggestions() {
234 final GSS app = GSS.get();
235 String query = selectedUser.substring(0, selectedUser.length()-1);
236 GWT.log("Searching for " + query, null);
238 GetCommand<UserSearchResource> eg = new GetCommand<UserSearchResource>(UserSearchResource.class,
239 app.getApiPath() + "users/" + URL.encodeComponent(query), false) {
242 public void onComplete() {
243 DisplayHelper.hideSuggestions(suggestBox);
245 UserSearchResource s = getResult();
246 for (UserResource user : s.getUsers()) {
247 GWT.log("Found " + user.getUsername(), null);
248 oracle.add(user.getUsername());
250 DisplayHelper.showSuggestions(suggestBox, selectedUser);
254 public void onError(Throwable t) {
255 if(t instanceof RestException)
256 app.displayError("Unable to perform search: "+((RestException)t).getHttpStatusText());
258 app.displayError("System error while searching for users: "+t.getMessage());
260 DisplayHelper.log(t.getMessage());
264 DeferredCommand.addCommand(eg);