Renamed all packages to gr.grnet.pithos.*
[pithos] / web_client / src / gr / grnet / pithos / web / client / UserAddDialog.java
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.grnet.pithos.web.client;
20
21 import gr.grnet.pithos.web.client.rest.GetCommand;
22 import gr.grnet.pithos.web.client.rest.PostCommand;
23 import gr.grnet.pithos.web.client.rest.RestException;
24 import gr.grnet.pithos.web.client.rest.resource.GroupResource;
25 import gr.grnet.pithos.web.client.rest.resource.UserResource;
26 import gr.grnet.pithos.web.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("pithos-TabPanelBottom");
70                 userTable.addStyleName("pithos-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                                 if (keyCode==KeyCodes.KEY_ESCAPE) {
94                                         suggestBox.hideSuggestionList();
95                                         return;
96                                 }
97                                 String text = suggestBox.getText().trim();
98                                 // Avoid useless queries for keystrokes that do not modify the
99                                 // text.
100                                 if (text.equals(selectedUser))
101                                         return;
102                                 selectedUser = text;
103                                 // Go to the server only if the user typed the @ character.
104                                 if (selectedUser.endsWith("@"))
105                                         updateSuggestions();
106                         }
107                 });
108                 suggestBox.getElement().setId("addUser.textBox");
109         userTable.setWidget(0, 1, suggestBox);
110         panel.add(userTable);
111                 HorizontalPanel buttons = new HorizontalPanel();
112                 Button ok = new Button("OK", new ClickHandler() {
113                         @Override
114                         public void onClick(ClickEvent event) {
115                                 addUser();
116                                 hide();
117                         }
118                 });
119                 ok.getElement().setId("addUser.button.ok");
120                 buttons.add(ok);
121                 buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
122                 // Create the 'Cancel' button, along with a listener that hides the
123                 // dialog when the button is clicked.
124                 Button cancel = new Button("Cancel", new ClickHandler() {
125                         @Override
126                         public void onClick(ClickEvent event) {
127                                 hide();
128                         }
129                 });
130                 cancel.getElement().setId("addUser.button.cancel");
131                 buttons.add(cancel);
132                 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
133                 buttons.setSpacing(8);
134                 buttons.addStyleName("pithos-TabPanelBottom");
135                 panel.add(buttons);
136                 panel.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
137                 panel.addStyleName("pithos-DialogBox");
138         }
139
140         @Override
141         public void center() {
142                 super.center();
143                 suggestBox.setFocus(true);
144         }
145
146         @Override
147         protected void onPreviewNativeEvent(NativePreviewEvent preview) {
148                 super.onPreviewNativeEvent(preview);
149
150                 NativeEvent evt = preview.getNativeEvent();
151                 if (evt.getType().equals("keydown"))
152                         // Use the popup's key preview hooks to close the dialog when either
153                         // enter or escape is pressed.
154                         switch (evt.getKeyCode()) {
155                                 case KeyCodes.KEY_ENTER:
156                                         addUser();
157                                         hide();
158                                         break;
159                                 case KeyCodes.KEY_ESCAPE:
160                                         hide();
161                                         break;
162                         }
163         }
164
165         /**
166          * Generate a request to add a user to a group.
167          *
168          * @param groupName the name of the group to create
169          */
170         private void addUser() {
171                 GroupResource group = (GroupResource) GSS.get().getCurrentSelection();
172                 selectedUser = suggestBox.getText();
173                 if ( group == null ) {
174                         GSS.get().displayError("Empty group name!");
175                         return;
176                 }
177                 if ( selectedUser == null ) {
178                         GSS.get().displayError("No User Selected!");
179                         return;
180                 }
181                 PostCommand cg = new PostCommand(group.getUri()+"?name="+selectedUser, "", 201){
182                         @Override
183                         public void onComplete() {
184                                 GSS.get().getGroups().updateGroups();
185                                 GSS.get().showUserList();
186                         }
187
188                         @Override
189                         public void onError(Throwable t) {
190                                 GWT.log("", t);
191                                 if(t instanceof RestException){
192                                         int statusCode = ((RestException)t).getHttpStatusCode();
193                                         if(statusCode == 405)
194                                                 GSS.get().displayError("You don't have the necessary permissions");
195                                         else if(statusCode == 404)
196                                                 GSS.get().displayError("User does not exist");
197                                         else if(statusCode == 409)
198                                                 GSS.get().displayError("A user with the same name already exists");
199                                         else if(statusCode == 413)
200                                                 GSS.get().displayError("Your quota has been exceeded");
201                                         else
202                                                 GSS.get().displayError("Unable to add user: "+((RestException)t).getHttpStatusText());
203                                 }
204                                 else
205                                         GSS.get().displayError("System error adding user: "+t.getMessage());
206                         }
207                 };
208                 DeferredCommand.addCommand(cg);
209         }
210
211         /**
212          * Update the list of suggestions.
213          */
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);
218
219                 GetCommand<UserSearchResource> eg = new GetCommand<UserSearchResource>(UserSearchResource.class,
220                                         app.getApiPath() + "users/" + URL.encodeComponent(query), false, null) {
221
222                         @Override
223                         public void onComplete() {
224                                 suggestBox.hideSuggestionList();
225                                 oracle.clear();
226                                 UserSearchResource s = getResult();
227                                 for (UserResource user : s.getUsers()) {
228                                         GWT.log("Found " + user.getUsername(), null);
229                                         oracle.add(user.getUsername());
230                                 }
231                                 suggestBox.showSuggestionList();
232                         }
233
234                         @Override
235                         public void onError(Throwable t) {
236                                 if(t instanceof RestException)
237                                         app.displayError("Unable to perform search: "+((RestException)t).getHttpStatusText());
238                                 else
239                                         app.displayError("System error while searching for users: "+t.getMessage());
240                                 GWT.log("", t);
241                                 DisplayHelper.log(t.getMessage());
242                         }
243
244                 };
245                 DeferredCommand.addCommand(eg);
246         }
247
248 }