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.http.client.URL;
32 import com.google.gwt.user.client.DeferredCommand;
33 import com.google.gwt.user.client.ui.Button;
34 import com.google.gwt.user.client.ui.CheckBox;
35 import com.google.gwt.user.client.ui.ClickListener;
36 import com.google.gwt.user.client.ui.DialogBox;
37 import com.google.gwt.user.client.ui.FlexTable;
38 import com.google.gwt.user.client.ui.FocusListenerAdapter;
39 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
40 import com.google.gwt.user.client.ui.HorizontalPanel;
41 import com.google.gwt.user.client.ui.KeyboardListener;
42 import com.google.gwt.user.client.ui.KeyboardListenerAdapter;
43 import com.google.gwt.user.client.ui.ListBox;
44 import com.google.gwt.user.client.ui.MultiWordSuggestOracle;
45 import com.google.gwt.user.client.ui.SuggestBox;
46 import com.google.gwt.user.client.ui.VerticalPanel;
47 import com.google.gwt.user.client.ui.Widget;
52 public class PermissionsAddDialog extends DialogBox {
54 private MultiWordSuggestOracle oracle = new MultiWordSuggestOracle();
55 private SuggestBox suggestBox = new SuggestBox(oracle);
57 private String selectedUser = null;
59 private List<GroupResource> groups;
61 private ListBox groupBox = new ListBox();
63 private CheckBox read = new CheckBox();
65 private CheckBox write = new CheckBox();
67 private CheckBox modifyACL = new CheckBox();
69 private final PermissionsList permList;
73 public PermissionsAddDialog(List<GroupResource> _groups, PermissionsList _permList, boolean _userAdd) {
77 for (GroupResource group : _groups)
78 groupBox.addItem(group.getName(), group.getName());
79 final VerticalPanel panel = new VerticalPanel();
80 final HorizontalPanel buttons = new HorizontalPanel();
82 final FlexTable permTable = new FlexTable();
83 permTable.setText(0, 0, "Users/Groups");
84 permTable.setText(0, 1, "Read");
85 permTable.setText(0, 2, "Write");
86 permTable.setText(0, 3, "Modify Access");
87 permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
88 permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
89 permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
90 permTable.getFlexCellFormatter().setStyleName(0, 3, "props-toplabels");
92 suggestBox.addFocusListener(new FocusListenerAdapter() {
94 public void onFocus(Widget sender) {
95 if (selectedUser != null && selectedUser.endsWith("@"))
99 suggestBox.addKeyboardListener(new KeyboardListenerAdapter() {
101 public void onKeyUp(Widget sender, char keyCode, int modifiers) {
102 // Ignore the arrow keys.
103 if (keyCode==KEY_UP || keyCode==KEY_DOWN || keyCode==KEY_LEFT || keyCode==KEY_RIGHT)
105 String text = suggestBox.getText().trim();
106 // Avoid useless queries for keystrokes that do not modify the text.
107 if (text.equals(selectedUser))
110 // Go to the server only if the user typed the @ character.
111 if (selectedUser.endsWith("@"))
115 permTable.setWidget(1, 0, suggestBox);
117 permTable.setWidget(1, 0, groupBox);
118 permTable.setWidget(1, 1, read);
119 permTable.setWidget(1, 2, write);
120 permTable.setWidget(1, 3, modifyACL);
122 permTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
123 permTable.getFlexCellFormatter().setHorizontalAlignment(1, 1, HasHorizontalAlignment.ALIGN_CENTER);
124 permTable.getFlexCellFormatter().setHorizontalAlignment(1, 2, HasHorizontalAlignment.ALIGN_CENTER);
125 permTable.getFlexCellFormatter().setHorizontalAlignment(1, 3, HasHorizontalAlignment.ALIGN_CENTER);
126 panel.add(permTable);
128 final Button ok = new Button("OK", new ClickListener() {
130 public void onClick(Widget sender) {
136 buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
137 // Create the 'Cancel' button, along with a listener that hides the
139 // when the button is clicked.
140 final Button cancel = new Button("Cancel", new ClickListener() {
142 public void onClick(Widget sender) {
147 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
148 buttons.setSpacing(8);
149 buttons.addStyleName("gwt-TabPanelBottom");
151 panel.addStyleName("gwt-TabPanelBottom");
154 private void addPermission() {
155 PermissionHolder perm = new PermissionHolder();
157 selectedUser = suggestBox.getText();
158 for(PermissionHolder p : permList.permissions)
159 if (selectedUser.equals(p.getUser())){
160 GSS.get().displayError("User already exists");
163 perm.setUser(selectedUser);
165 String groupId = groupBox.getValue(groupBox.getSelectedIndex());
166 GroupResource selected = null;
167 for (GroupResource g : groups)
168 if (g.getName().equals(groupId))
170 for(PermissionHolder p : permList.permissions)
171 if (selected.equals(p.getGroup())){
172 GSS.get().displayError("Group already exists");
175 perm.setGroup(selected.getName());
177 boolean readValue = read.isChecked();
178 boolean writeValue = write.isChecked();
179 boolean modifyValue = modifyACL.isChecked();
181 perm.setRead(readValue);
182 perm.setWrite(writeValue);
183 perm.setModifyACL(modifyValue);
184 permList.addPermission(perm);
185 permList.updateTable();
189 public boolean onKeyDownPreview(final char key, final int modifiers) {
190 // Use the popup's key preview hooks to close the dialog when either
191 // enter or escape is pressed.
193 case KeyboardListener.KEY_ENTER:
197 case KeyboardListener.KEY_ESCAPE:
205 public void center() {
208 suggestBox.setFocus(true);
212 * Update the list of suggestions.
214 protected void updateSuggestions() {
215 final GSS app = GSS.get();
216 String query = selectedUser.substring(0, selectedUser.length()-1);
217 GWT.log("Searching for " + query, null);
219 GetCommand<UserSearchResource> eg = new GetCommand<UserSearchResource>(UserSearchResource.class,
220 app.getApiPath() + "users/" + URL.encodeComponent(query)) {
223 public void onComplete() {
224 DisplayHelper.hideSuggestions(suggestBox);
226 UserSearchResource s = getResult();
227 for (UserResource user : s.getUsers()) {
228 GWT.log("Found " + user.getUsername(), null);
229 oracle.add(user.getUsername());
231 DisplayHelper.showSuggestions(suggestBox, selectedUser);
235 public void onError(Throwable t) {
236 if(t instanceof RestException)
237 app.displayError("Unable to perform search: "+((RestException)t).getHttpStatusText());
239 app.displayError("System error while searching for users: "+t.getMessage());
241 DisplayHelper.log(t.getMessage());
245 DeferredCommand.addCommand(eg);