Removed all static references to the Pithos class
[pithos-web-client] / src / gr / grnet / pithos / web / client / PermissionsAddDialog.java
1 /*
2  * Copyright 2011 GRNET S.A. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or
5  * without modification, are permitted provided that the following
6  * conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above
9  *      copyright notice, this list of conditions and the following
10  *      disclaimer.
11  *
12  *   2. Redistributions in binary form must reproduce the above
13  *      copyright notice, this list of conditions and the following
14  *      disclaimer in the documentation and/or other materials
15  *      provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  * The views and conclusions contained in the software and
31  * documentation are those of the authors and should not be
32  * interpreted as representing official policies, either expressed
33  * or implied, of GRNET S.A.
34  */
35 package gr.grnet.pithos.web.client;
36
37 import gr.grnet.pithos.web.client.rest.GetCommand;
38 import gr.grnet.pithos.web.client.rest.RestException;
39 import gr.grnet.pithos.web.client.rest.resource.GroupResource;
40 import gr.grnet.pithos.web.client.rest.resource.PermissionHolder;
41 import gr.grnet.pithos.web.client.rest.resource.UserResource;
42 import gr.grnet.pithos.web.client.rest.resource.UserSearchResource;
43
44 import java.util.List;
45
46 import com.google.gwt.core.client.GWT;
47 import com.google.gwt.dom.client.NativeEvent;
48 import com.google.gwt.event.dom.client.ClickEvent;
49 import com.google.gwt.event.dom.client.ClickHandler;
50 import com.google.gwt.event.dom.client.FocusEvent;
51 import com.google.gwt.event.dom.client.FocusHandler;
52 import com.google.gwt.event.dom.client.KeyCodes;
53 import com.google.gwt.event.dom.client.KeyUpEvent;
54 import com.google.gwt.event.dom.client.KeyUpHandler;
55 import com.google.gwt.http.client.URL;
56 import com.google.gwt.user.client.DeferredCommand;
57 import com.google.gwt.user.client.Event.NativePreviewEvent;
58 import com.google.gwt.user.client.ui.Button;
59 import com.google.gwt.user.client.ui.CheckBox;
60 import com.google.gwt.user.client.ui.DialogBox;
61 import com.google.gwt.user.client.ui.FlexTable;
62 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
63 import com.google.gwt.user.client.ui.HorizontalPanel;
64 import com.google.gwt.user.client.ui.ListBox;
65 import com.google.gwt.user.client.ui.MultiWordSuggestOracle;
66 import com.google.gwt.user.client.ui.SuggestBox;
67 import com.google.gwt.user.client.ui.VerticalPanel;
68
69 public class PermissionsAddDialog extends DialogBox {
70
71         private MultiWordSuggestOracle oracle = new MultiWordSuggestOracle();
72         private SuggestBox suggestBox = new SuggestBox(oracle);
73
74         private String selectedUser = null;
75
76         private List<GroupResource> groups;
77
78         private ListBox groupBox = new ListBox();
79
80         private CheckBox read = new CheckBox();
81
82         private CheckBox write = new CheckBox();
83
84         private CheckBox modifyACL = new CheckBox();
85
86         private final PermissionsList permList;
87
88         boolean userAdd;
89
90     private Pithos app;
91
92         public PermissionsAddDialog(Pithos _app, List<GroupResource> _groups, PermissionsList _permList, boolean _userAdd) {
93         app = _app;
94                 groups = _groups;
95                 userAdd = _userAdd;
96                 permList = _permList;
97                 
98                 groupBox.getElement().setId("addPermission.dropDown");
99                 
100                 suggestBox.getElement().setId("addPermission.textBox");
101                 
102                 read.getElement().setId("addPermission.read");
103                 
104                 write.getElement().setId("addPermission.write");
105                 
106                 modifyACL.getElement().setId("addpermission.modify");
107                 
108                 for (GroupResource group : _groups)
109                         groupBox.addItem(group.getName(), group.getName());
110                 final VerticalPanel panel = new VerticalPanel();
111                 final HorizontalPanel buttons = new HorizontalPanel();
112                 setWidget(panel);
113                 final FlexTable permTable = new FlexTable();
114                 permTable.setText(0, 0, "Users/Groups");
115                 permTable.setText(0, 1, "Read");
116                 permTable.setText(0, 2, "Write");
117                 permTable.setText(0, 3, "Modify Access");
118                 permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
119                 permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
120                 permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
121                 permTable.getFlexCellFormatter().setStyleName(0, 3, "props-toplabels");
122                 if (userAdd) {
123                         suggestBox.getTextBox().addFocusHandler(new FocusHandler() {
124
125                                 @Override
126                                 public void onFocus(FocusEvent event) {
127                                         if (selectedUser != null && selectedUser.endsWith("@"))
128                                                 updateSuggestions();
129
130                                 }
131                         });
132
133                         suggestBox.addKeyUpHandler(new KeyUpHandler() {
134
135                                 @Override
136                                 public void onKeyUp(KeyUpEvent event) {
137                                         // Ignore the arrow keys.
138                                         int keyCode = event.getNativeKeyCode();
139                                         if (keyCode == KeyCodes.KEY_UP ||
140                                                         keyCode == KeyCodes.KEY_DOWN ||
141                                                         keyCode == KeyCodes.KEY_LEFT ||
142                                                         keyCode == KeyCodes.KEY_RIGHT)
143                                                 return;
144                                         if (keyCode==KeyCodes.KEY_ESCAPE) {
145                                                 suggestBox.hideSuggestionList();
146                                                 return;
147                                         }
148                                         String text = suggestBox.getText().trim();
149                                         // Avoid useless queries for keystrokes that do not modify
150                                         // the text.
151                                         if (text.equals(selectedUser))
152                                                 return;
153                                         selectedUser = text;
154                                         // Go to the server only if the user typed the @ character.
155                                         if (selectedUser.endsWith("@"))
156                                                 updateSuggestions();
157                                 }
158                         });
159                         permTable.setWidget(1, 0, suggestBox);
160                 } else
161                         permTable.setWidget(1, 0, groupBox);
162                 permTable.setWidget(1, 1, read);
163                 permTable.setWidget(1, 2, write);
164                 permTable.setWidget(1, 3, modifyACL);
165
166                 permTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
167                 permTable.getFlexCellFormatter().setHorizontalAlignment(1, 1, HasHorizontalAlignment.ALIGN_CENTER);
168                 permTable.getFlexCellFormatter().setHorizontalAlignment(1, 2, HasHorizontalAlignment.ALIGN_CENTER);
169                 permTable.getFlexCellFormatter().setHorizontalAlignment(1, 3, HasHorizontalAlignment.ALIGN_CENTER);
170                 panel.add(permTable);
171
172                 final Button ok = new Button("OK", new ClickHandler() {
173                         @Override
174                         public void onClick(ClickEvent event) {
175                                 addPermission();
176                                 hide();
177                         }
178                 });
179                 ok.getElement().setId("addPermission.button.ok");
180                 buttons.add(ok);
181                 buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
182                 // Create the 'Cancel' button, along with a listener that hides the
183                 // dialog
184                 // when the button is clicked.
185                 final Button cancel = new Button("Cancel", new ClickHandler() {
186                         @Override
187                         public void onClick(ClickEvent event) {
188                                 hide();
189                         }
190                 });
191                 cancel.getElement().setId("addPermission.button.cancel");
192                 buttons.add(cancel);
193                 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
194                 buttons.setSpacing(8);
195                 buttons.addStyleName("pithos-TabPanelBottom");
196                 panel.add(buttons);
197                 panel.addStyleName("pithos-TabPanelBottom");
198         }
199
200         private void addPermission() {
201                 PermissionHolder perm = new PermissionHolder();
202                 if (userAdd) {
203                         selectedUser = suggestBox.getText();
204                         for(PermissionHolder p : permList.permissions)
205                                 if (selectedUser.equals(p.getUser())){
206                                         app.displayError("User already has access to the resource");
207                                         return;
208                                 }
209                         perm.setUser(selectedUser);
210                 } else {
211                         String groupId = groupBox.getValue(groupBox.getSelectedIndex());
212                         GroupResource selected = null;
213                         for (GroupResource g : groups)
214                                 if (g.getName().equals(groupId))
215                                         selected = g;
216                         if (selected == null)
217                                 return;
218                         for(PermissionHolder p : permList.permissions)
219                                 if (selected.getName().equals(p.getGroup())){
220                                         app.displayError("Group already has access to the resource");
221                                         return;
222                                 }
223                         perm.setGroup(selected.getName());
224                 }
225                 boolean readValue = read.getValue();
226                 boolean writeValue = write.getValue();
227                 boolean modifyValue = modifyACL.getValue();
228
229                 perm.setRead(readValue);
230                 perm.setWrite(writeValue);
231                 perm.setModifyACL(modifyValue);
232                 permList.addPermission(perm);
233                 permList.updateTable();
234         }
235
236         @Override
237         protected void onPreviewNativeEvent(NativePreviewEvent preview) {
238                 super.onPreviewNativeEvent(preview);
239
240                 NativeEvent evt = preview.getNativeEvent();
241                 if (evt.getType().equals("keydown"))
242                         // Use the popup's key preview hooks to close the dialog when either
243                         // enter or escape is pressed.
244                         switch (evt.getKeyCode()) {
245                                 case KeyCodes.KEY_ENTER:
246                                         addPermission();
247                                         hide();
248                                         break;
249                                 case KeyCodes.KEY_ESCAPE:
250                                         hide();
251                                         break;
252                         }
253         }
254
255
256         @Override
257         public void center() {
258                 super.center();
259                 if (userAdd)
260                         suggestBox.setFocus(true);
261         }
262
263         /**
264          * Update the list of suggestions.
265          */
266         protected void updateSuggestions() {
267                 String query = selectedUser.substring(0, selectedUser.length()-1);
268                 GWT.log("Searching for " + query, null);
269
270                 GetCommand<UserSearchResource> eg = new GetCommand<UserSearchResource>(app, UserSearchResource.class,
271                                         app.getApiPath() + "users/" + URL.encodeComponent(query), false, null) {
272
273                         @Override
274                         public void onComplete() {
275                                 suggestBox.hideSuggestionList();
276                                 oracle.clear();
277                                 UserSearchResource s = getResult();
278                                 for (UserResource user : s.getUsers()) {
279                                         GWT.log("Found " + user.getUsername(), null);
280                                         oracle.add(user.getUsername());
281                                 }
282                                 suggestBox.showSuggestionList();
283                         }
284
285                         @Override
286                         public void onError(Throwable t) {
287                                 if(t instanceof RestException)
288                                         app.displayError("Unable to perform search: "+((RestException)t).getHttpStatusText());
289                                 else
290                                         app.displayError("System error while searching for users: "+t.getMessage());
291                                 GWT.log("", t);
292                                 DisplayHelper.log(t.getMessage());
293                         }
294
295                 };
296                 DeferredCommand.addCommand(eg);
297         }
298 }