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.PostCommand;
23 import gr.ebs.gss.client.rest.RestException;
24 import gr.ebs.gss.client.rest.resource.GroupResource;
25 import gr.ebs.gss.client.rest.resource.UserResource;
26 import gr.ebs.gss.client.rest.resource.UserSearchResource;
28 import com.google.gwt.core.client.GWT;
29 import com.google.gwt.http.client.URL;
30 import com.google.gwt.user.client.DeferredCommand;
31 import com.google.gwt.user.client.ui.Button;
32 import com.google.gwt.user.client.ui.ClickListener;
33 import com.google.gwt.user.client.ui.DialogBox;
34 import com.google.gwt.user.client.ui.FlexTable;
35 import com.google.gwt.user.client.ui.FocusListenerAdapter;
36 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
37 import com.google.gwt.user.client.ui.HorizontalPanel;
38 import com.google.gwt.user.client.ui.KeyboardListener;
39 import com.google.gwt.user.client.ui.KeyboardListenerAdapter;
40 import com.google.gwt.user.client.ui.Label;
41 import com.google.gwt.user.client.ui.MultiWordSuggestOracle;
42 import com.google.gwt.user.client.ui.SuggestBox;
43 import com.google.gwt.user.client.ui.VerticalPanel;
44 import com.google.gwt.user.client.ui.Widget;
49 public class UserAddDialog extends DialogBox {
51 private MultiWordSuggestOracle oracle = new MultiWordSuggestOracle();
52 private SuggestBox suggestBox = new SuggestBox(oracle);
54 String selectedUser=null;
55 FlexTable userTable = new FlexTable();
58 * The widget's constructor.
60 public UserAddDialog() {
61 setAnimationEnabled(true);
63 VerticalPanel panel = new VerticalPanel();
65 panel.addStyleName("gwt-TabPanelBottom");
66 userTable.addStyleName("gss-permList");
67 userTable.setWidget(0, 0, new Label("Username:"));
68 userTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
69 suggestBox.addFocusListener(new FocusListenerAdapter() {
71 public void onFocus(Widget sender) {
72 if (selectedUser != null && selectedUser.endsWith("@"))
76 suggestBox.addKeyboardListener(new KeyboardListenerAdapter() {
78 public void onKeyUp(Widget sender, char keyCode, int modifiers) {
79 // Ignore the arrow keys.
80 if (keyCode==KEY_UP || keyCode==KEY_DOWN || keyCode==KEY_LEFT || keyCode==KEY_RIGHT)
82 String text = suggestBox.getText().trim();
83 // Avoid useless queries for keystrokes that do not modify the text.
84 if (text.equals(selectedUser))
87 // Go to the server only if the user typed the @ character.
88 if (selectedUser.endsWith("@"))
92 userTable.setWidget(0, 1, suggestBox);
94 HorizontalPanel buttons = new HorizontalPanel();
95 Button ok = new Button("OK", new ClickListener() {
97 public void onClick(Widget sender) {
103 buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
104 // Create the 'Cancel' button, along with a listener that hides the
105 // dialog when the button is clicked.
106 Button cancel = new Button("Cancel", new ClickListener() {
108 public void onClick(Widget sender) {
113 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
114 buttons.setSpacing(8);
115 buttons.addStyleName("gwt-TabPanelBottom");
117 panel.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
118 panel.addStyleName("gss-DialogBox");
122 public void center() {
124 suggestBox.setFocus(true);
128 public boolean onKeyDownPreview(final char key, final int modifiers) {
129 // Use the popup's key preview hooks to close the dialog when either
130 // enter or escape is pressed.
132 case KeyboardListener.KEY_ENTER:
136 case KeyboardListener.KEY_ESCAPE:
144 * Generate a request to add a user to a group.
146 * @param groupName the name of the group to create
148 private void addUser() {
149 GroupResource group = (GroupResource) GSS.get().getCurrentSelection();
150 selectedUser = suggestBox.getText();
151 if ( group == null ) {
152 GSS.get().displayError("Empty group name!");
155 if ( selectedUser == null ) {
156 GSS.get().displayError("No User Selected!");
159 PostCommand cg = new PostCommand(group.getUri()+"?name="+selectedUser, "", 201){
161 public void onComplete() {
162 GSS.get().getGroups().updateGroups();
163 GSS.get().showUserList();
167 public void onError(Throwable t) {
169 if(t instanceof RestException){
170 int statusCode = ((RestException)t).getHttpStatusCode();
171 if(statusCode == 405)
172 GSS.get().displayError("You don't have the necessary permissions");
173 else if(statusCode == 404)
174 GSS.get().displayError("User does not exist");
175 else if(statusCode == 409)
176 GSS.get().displayError("A user with the same name already exists");
177 else if(statusCode == 413)
178 GSS.get().displayError("Your quota has been exceeded");
180 GSS.get().displayError("Unable to add user: "+((RestException)t).getHttpStatusText());
183 GSS.get().displayError("System error adding user: "+t.getMessage());
186 DeferredCommand.addCommand(cg);
190 * Update the list of suggestions.
192 protected void updateSuggestions() {
193 final GSS app = GSS.get();
194 String query = selectedUser.substring(0, selectedUser.length()-1);
195 GWT.log("Searching for " + query, null);
197 GetCommand<UserSearchResource> eg = new GetCommand<UserSearchResource>(UserSearchResource.class,
198 app.getApiPath() + "users/" + URL.encodeComponent(query), false) {
201 public void onComplete() {
202 DisplayHelper.hideSuggestions(suggestBox);
204 UserSearchResource s = getResult();
205 for (UserResource user : s.getUsers()) {
206 GWT.log("Found " + user.getUsername(), null);
207 oracle.add(user.getUsername());
209 DisplayHelper.showSuggestions(suggestBox, selectedUser);
213 public void onError(Throwable t) {
214 if(t instanceof RestException)
215 app.displayError("Unable to perform search: "+((RestException)t).getHttpStatusText());
217 app.displayError("System error while searching for users: "+t.getMessage());
219 DisplayHelper.log(t.getMessage());
223 DeferredCommand.addCommand(eg);