ignore case when examining the file extension for guessing the mime-type
[pithos] / src / gr / ebs / gss / 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.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.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;
45
46 /**
47  * @author kman
48  */
49 public class UserAddDialog extends DialogBox {
50
51         private MultiWordSuggestOracle oracle = new MultiWordSuggestOracle();
52         private SuggestBox suggestBox = new SuggestBox(oracle);
53
54         String selectedUser=null;
55         FlexTable userTable = new FlexTable();
56
57         /**
58          * The widget's constructor.
59          */
60         public UserAddDialog() {
61                 setAnimationEnabled(true);
62                 setText("Add User");
63                 VerticalPanel panel = new VerticalPanel();
64                 setWidget(panel);
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() {
70                         @Override
71                         public void onFocus(Widget sender) {
72                                 if (selectedUser != null && selectedUser.endsWith("@"))
73                                         updateSuggestions();
74                         }
75                 });
76                 suggestBox.addKeyboardListener(new KeyboardListenerAdapter() {
77                         @Override
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)
81                                         return;
82                                 String text = suggestBox.getText().trim();
83                                 // Avoid useless queries for keystrokes that do not modify the text.
84                                 if (text.equals(selectedUser))
85                                         return;
86                                 selectedUser = text;
87                                 // Go to the server only if the user typed the @ character.
88                                 if (selectedUser.endsWith("@"))
89                                         updateSuggestions();
90                         }
91                 });
92         userTable.setWidget(0, 1, suggestBox);
93         panel.add(userTable);
94                 HorizontalPanel buttons = new HorizontalPanel();
95                 Button ok = new Button("OK", new ClickListener() {
96
97                         public void onClick(Widget sender) {
98                                 addUser();
99                                 hide();
100                         }
101                 });
102                 buttons.add(ok);
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() {
107
108                         public void onClick(Widget sender) {
109                                 hide();
110                         }
111                 });
112                 buttons.add(cancel);
113                 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
114                 buttons.setSpacing(8);
115                 buttons.addStyleName("gwt-TabPanelBottom");
116                 panel.add(buttons);
117                 panel.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
118                 panel.addStyleName("gss-DialogBox");
119         }
120
121         @Override
122         public void center() {
123                 super.center();
124                 suggestBox.setFocus(true);
125         }
126
127         @Override
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.
131                 switch (key) {
132                         case KeyboardListener.KEY_ENTER:
133                                 hide();
134                                 addUser();
135                                 break;
136                         case KeyboardListener.KEY_ESCAPE:
137                                 hide();
138                                 break;
139                 }
140                 return true;
141         }
142
143         /**
144          * Generate a request to add a user to a group.
145          *
146          * @param groupName the name of the group to create
147          */
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!");
153                         return;
154                 }
155                 if ( selectedUser == null ) {
156                         GSS.get().displayError("No User Selected!");
157                         return;
158                 }
159                 PostCommand cg = new PostCommand(group.getUri()+"?name="+selectedUser, "", 201){
160                         @Override
161                         public void onComplete() {
162                                 GSS.get().getGroups().updateGroups();
163                                 GSS.get().showUserList();
164                         }
165
166                         @Override
167                         public void onError(Throwable t) {
168                                 GWT.log("", 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");
179                                         else
180                                                 GSS.get().displayError("Unable to add user: "+((RestException)t).getHttpStatusText());
181                                 }
182                                 else
183                                         GSS.get().displayError("System error adding user: "+t.getMessage());
184                         }
185                 };
186                 DeferredCommand.addCommand(cg);
187         }
188
189         /**
190          * Update the list of suggestions.
191          */
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);
196
197                 GetCommand<UserSearchResource> eg = new GetCommand<UserSearchResource>(UserSearchResource.class,
198                                         app.getApiPath() + "users/" + URL.encodeComponent(query)) {
199
200                         @Override
201                         public void onComplete() {
202                                 DisplayHelper.hideSuggestions(suggestBox);
203                                 oracle.clear();
204                                 UserSearchResource s = getResult();
205                                 for (UserResource user : s.getUsers()) {
206                                         GWT.log("Found " + user.getUsername(), null);
207                                         oracle.add(user.getUsername());
208                                 }
209                                 DisplayHelper.showSuggestions(suggestBox, selectedUser);
210                         }
211
212                         @Override
213                         public void onError(Throwable t) {
214                                 if(t instanceof RestException)
215                                         app.displayError("Unable to perform search: "+((RestException)t).getHttpStatusText());
216                                 else
217                                         app.displayError("System error while searching for users: "+t.getMessage());
218                                 GWT.log("", t);
219                                 DisplayHelper.log(t.getMessage());
220                         }
221
222                 };
223                 DeferredCommand.addCommand(eg);
224         }
225
226 }