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