root / src / gr / ebs / gss / client / UserAddDialog.java @ 6e6e914e
History | View | Annotate | Download (7.6 kB)
1 |
/*
|
---|---|
2 |
* Copyright 2008, 2009 Electronic Business Systems Ltd.
|
3 |
*
|
4 |
* This file is part of GSS.
|
5 |
*
|
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.
|
10 |
*
|
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.
|
15 |
*
|
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/>.
|
18 |
*/
|
19 |
package gr.ebs.gss.client; |
20 |
|
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; |
27 |
|
28 |
import com.google.gwt.core.client.GWT; |
29 |
import com.google.gwt.dom.client.NativeEvent; |
30 |
import com.google.gwt.event.dom.client.ClickEvent; |
31 |
import com.google.gwt.event.dom.client.ClickHandler; |
32 |
import com.google.gwt.event.dom.client.FocusEvent; |
33 |
import com.google.gwt.event.dom.client.FocusHandler; |
34 |
import com.google.gwt.event.dom.client.KeyCodes; |
35 |
import com.google.gwt.event.dom.client.KeyUpEvent; |
36 |
import com.google.gwt.event.dom.client.KeyUpHandler; |
37 |
import com.google.gwt.http.client.URL; |
38 |
import com.google.gwt.user.client.DeferredCommand; |
39 |
import com.google.gwt.user.client.Event.NativePreviewEvent; |
40 |
import com.google.gwt.user.client.ui.Button; |
41 |
import com.google.gwt.user.client.ui.DialogBox; |
42 |
import com.google.gwt.user.client.ui.FlexTable; |
43 |
import com.google.gwt.user.client.ui.HasHorizontalAlignment; |
44 |
import com.google.gwt.user.client.ui.HorizontalPanel; |
45 |
import com.google.gwt.user.client.ui.Label; |
46 |
import com.google.gwt.user.client.ui.MultiWordSuggestOracle; |
47 |
import com.google.gwt.user.client.ui.SuggestBox; |
48 |
import com.google.gwt.user.client.ui.VerticalPanel; |
49 |
|
50 |
/**
|
51 |
* @author kman
|
52 |
*/
|
53 |
public class UserAddDialog extends DialogBox { |
54 |
|
55 |
private MultiWordSuggestOracle oracle = new MultiWordSuggestOracle(); |
56 |
private SuggestBox suggestBox = new SuggestBox(oracle); |
57 |
|
58 |
String selectedUser=null; |
59 |
FlexTable userTable = new FlexTable();
|
60 |
|
61 |
/**
|
62 |
* The widget's constructor.
|
63 |
*/
|
64 |
public UserAddDialog() {
|
65 |
setAnimationEnabled(true);
|
66 |
setText("Add User");
|
67 |
VerticalPanel panel = new VerticalPanel();
|
68 |
setWidget(panel); |
69 |
panel.addStyleName("gwt-TabPanelBottom");
|
70 |
userTable.addStyleName("gss-permList");
|
71 |
userTable.setWidget(0, 0, new Label("Username:")); |
72 |
userTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels"); |
73 |
suggestBox.getTextBox().addFocusHandler(new FocusHandler() {
|
74 |
|
75 |
@Override
|
76 |
public void onFocus(FocusEvent event) { |
77 |
if (selectedUser != null && selectedUser.endsWith("@")) |
78 |
updateSuggestions(); |
79 |
} |
80 |
}); |
81 |
|
82 |
suggestBox.addKeyUpHandler(new KeyUpHandler() {
|
83 |
|
84 |
@Override
|
85 |
public void onKeyUp(KeyUpEvent event) { |
86 |
// Ignore the arrow keys.
|
87 |
int keyCode=event.getNativeKeyCode();
|
88 |
if(keyCode == KeyCodes.KEY_UP ||
|
89 |
keyCode == KeyCodes.KEY_DOWN || |
90 |
keyCode == KeyCodes.KEY_LEFT || |
91 |
keyCode == KeyCodes.KEY_RIGHT) |
92 |
return;
|
93 |
String text = suggestBox.getText().trim();
|
94 |
// Avoid useless queries for keystrokes that do not modify the
|
95 |
// text.
|
96 |
if (text.equals(selectedUser))
|
97 |
return;
|
98 |
selectedUser = text; |
99 |
// Go to the server only if the user typed the @ character.
|
100 |
if (selectedUser.endsWith("@")) |
101 |
updateSuggestions(); |
102 |
} |
103 |
}); |
104 |
userTable.setWidget(0, 1, suggestBox); |
105 |
panel.add(userTable); |
106 |
HorizontalPanel buttons = new HorizontalPanel();
|
107 |
Button ok = new Button("OK", new ClickHandler() { |
108 |
@Override
|
109 |
public void onClick(ClickEvent event) { |
110 |
addUser(); |
111 |
hide(); |
112 |
} |
113 |
}); |
114 |
buttons.add(ok); |
115 |
buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER); |
116 |
// Create the 'Cancel' button, along with a listener that hides the
|
117 |
// dialog when the button is clicked.
|
118 |
Button cancel = new Button("Cancel", new ClickHandler() { |
119 |
@Override
|
120 |
public void onClick(ClickEvent event) { |
121 |
hide(); |
122 |
} |
123 |
}); |
124 |
buttons.add(cancel); |
125 |
buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER); |
126 |
buttons.setSpacing(8);
|
127 |
buttons.addStyleName("gwt-TabPanelBottom");
|
128 |
panel.add(buttons); |
129 |
panel.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER); |
130 |
panel.addStyleName("gss-DialogBox");
|
131 |
} |
132 |
|
133 |
@Override
|
134 |
public void center() { |
135 |
super.center();
|
136 |
suggestBox.setFocus(true);
|
137 |
} |
138 |
|
139 |
@Override
|
140 |
protected void onPreviewNativeEvent(NativePreviewEvent preview) { |
141 |
super.onPreviewNativeEvent(preview);
|
142 |
|
143 |
NativeEvent evt = preview.getNativeEvent(); |
144 |
if (evt.getType().equals("keydown")) |
145 |
// Use the popup's key preview hooks to close the dialog when either
|
146 |
// enter or escape is pressed.
|
147 |
switch (evt.getKeyCode()) {
|
148 |
case KeyCodes.KEY_ENTER:
|
149 |
addUser(); |
150 |
hide(); |
151 |
break;
|
152 |
case KeyCodes.KEY_ESCAPE:
|
153 |
hide(); |
154 |
break;
|
155 |
} |
156 |
} |
157 |
|
158 |
/**
|
159 |
* Generate a request to add a user to a group.
|
160 |
*
|
161 |
* @param groupName the name of the group to create
|
162 |
*/
|
163 |
private void addUser() { |
164 |
GroupResource group = (GroupResource) GSS.get().getCurrentSelection(); |
165 |
selectedUser = suggestBox.getText(); |
166 |
if ( group == null ) { |
167 |
GSS.get().displayError("Empty group name!");
|
168 |
return;
|
169 |
} |
170 |
if ( selectedUser == null ) { |
171 |
GSS.get().displayError("No User Selected!");
|
172 |
return;
|
173 |
} |
174 |
PostCommand cg = new PostCommand(group.getUri()+"?name="+selectedUser, "", 201){ |
175 |
@Override
|
176 |
public void onComplete() { |
177 |
GSS.get().getGroups().updateGroups(); |
178 |
GSS.get().showUserList(); |
179 |
} |
180 |
|
181 |
@Override
|
182 |
public void onError(Throwable t) { |
183 |
GWT.log("", t);
|
184 |
if(t instanceof RestException){ |
185 |
int statusCode = ((RestException)t).getHttpStatusCode();
|
186 |
if(statusCode == 405) |
187 |
GSS.get().displayError("You don't have the necessary permissions");
|
188 |
else if(statusCode == 404) |
189 |
GSS.get().displayError("User does not exist");
|
190 |
else if(statusCode == 409) |
191 |
GSS.get().displayError("A user with the same name already exists");
|
192 |
else if(statusCode == 413) |
193 |
GSS.get().displayError("Your quota has been exceeded");
|
194 |
else
|
195 |
GSS.get().displayError("Unable to add user: "+((RestException)t).getHttpStatusText());
|
196 |
} |
197 |
else
|
198 |
GSS.get().displayError("System error adding user: "+t.getMessage());
|
199 |
} |
200 |
}; |
201 |
DeferredCommand.addCommand(cg); |
202 |
} |
203 |
|
204 |
/**
|
205 |
* Update the list of suggestions.
|
206 |
*/
|
207 |
protected void updateSuggestions() { |
208 |
final GSS app = GSS.get();
|
209 |
String query = selectedUser.substring(0, selectedUser.length()-1); |
210 |
GWT.log("Searching for " + query, null); |
211 |
|
212 |
GetCommand<UserSearchResource> eg = new GetCommand<UserSearchResource>(UserSearchResource.class,
|
213 |
app.getApiPath() + "users/" + URL.encodeComponent(query), false) { |
214 |
|
215 |
@Override
|
216 |
public void onComplete() { |
217 |
DisplayHelper.hideSuggestions(suggestBox); |
218 |
oracle.clear(); |
219 |
UserSearchResource s = getResult(); |
220 |
for (UserResource user : s.getUsers()) {
|
221 |
GWT.log("Found " + user.getUsername(), null); |
222 |
oracle.add(user.getUsername()); |
223 |
} |
224 |
DisplayHelper.showSuggestions(suggestBox, selectedUser); |
225 |
} |
226 |
|
227 |
@Override
|
228 |
public void onError(Throwable t) { |
229 |
if(t instanceof RestException) |
230 |
app.displayError("Unable to perform search: "+((RestException)t).getHttpStatusText());
|
231 |
else
|
232 |
app.displayError("System error while searching for users: "+t.getMessage());
|
233 |
GWT.log("", t);
|
234 |
DisplayHelper.log(t.getMessage()); |
235 |
} |
236 |
|
237 |
}; |
238 |
DeferredCommand.addCommand(eg); |
239 |
} |
240 |
|
241 |
} |