display message while image loads on the client viewer
[pithos] / src / gr / ebs / gss / client / PermissionsAddDialog.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.RestException;
23 import gr.ebs.gss.client.rest.resource.GroupResource;
24 import gr.ebs.gss.client.rest.resource.PermissionHolder;
25 import gr.ebs.gss.client.rest.resource.UserResource;
26 import gr.ebs.gss.client.rest.resource.UserSearchResource;
27
28 import java.util.List;
29
30 import com.google.gwt.core.client.GWT;
31 import com.google.gwt.http.client.URL;
32 import com.google.gwt.user.client.DeferredCommand;
33 import com.google.gwt.user.client.ui.Button;
34 import com.google.gwt.user.client.ui.CheckBox;
35 import com.google.gwt.user.client.ui.ClickListener;
36 import com.google.gwt.user.client.ui.DialogBox;
37 import com.google.gwt.user.client.ui.FlexTable;
38 import com.google.gwt.user.client.ui.FocusListenerAdapter;
39 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
40 import com.google.gwt.user.client.ui.HorizontalPanel;
41 import com.google.gwt.user.client.ui.KeyboardListener;
42 import com.google.gwt.user.client.ui.KeyboardListenerAdapter;
43 import com.google.gwt.user.client.ui.ListBox;
44 import com.google.gwt.user.client.ui.MultiWordSuggestOracle;
45 import com.google.gwt.user.client.ui.SuggestBox;
46 import com.google.gwt.user.client.ui.VerticalPanel;
47 import com.google.gwt.user.client.ui.Widget;
48
49 /**
50  * @author kman
51  */
52 public class PermissionsAddDialog extends DialogBox {
53
54         private MultiWordSuggestOracle oracle = new MultiWordSuggestOracle();
55         private SuggestBox suggestBox = new SuggestBox(oracle);
56
57         private String selectedUser = null;
58
59         private List<GroupResource> groups;
60
61         private ListBox groupBox = new ListBox();
62
63         private CheckBox read = new CheckBox();
64
65         private CheckBox write = new CheckBox();
66
67         private CheckBox modifyACL = new CheckBox();
68
69         private final PermissionsList permList;
70
71         boolean userAdd;
72
73         public PermissionsAddDialog(List<GroupResource> _groups, PermissionsList _permList, boolean _userAdd) {
74                 groups = _groups;
75                 userAdd = _userAdd;
76                 permList = _permList;
77                 for (GroupResource group : _groups)
78                         groupBox.addItem(group.getName(), group.getName());
79                 final VerticalPanel panel = new VerticalPanel();
80                 final HorizontalPanel buttons = new HorizontalPanel();
81                 setWidget(panel);
82                 final FlexTable permTable = new FlexTable();
83                 permTable.setText(0, 0, "Users/Groups");
84                 permTable.setText(0, 1, "Read");
85                 permTable.setText(0, 2, "Write");
86                 permTable.setText(0, 3, "Modify Access");
87                 permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
88                 permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
89                 permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
90                 permTable.getFlexCellFormatter().setStyleName(0, 3, "props-toplabels");
91                 if (userAdd) {
92                         suggestBox.addFocusListener(new FocusListenerAdapter() {
93                                 @Override
94                                 public void onFocus(Widget sender) {
95                                         if (selectedUser != null && selectedUser.endsWith("@"))
96                                                 updateSuggestions();
97                                 }
98                         });
99                         suggestBox.addKeyboardListener(new KeyboardListenerAdapter() {
100                                 @Override
101                                 public void onKeyUp(Widget sender, char keyCode, int modifiers) {
102                                         // Ignore the arrow keys.
103                                         if (keyCode==KEY_UP || keyCode==KEY_DOWN || keyCode==KEY_LEFT || keyCode==KEY_RIGHT)
104                                                 return;
105                                         String text = suggestBox.getText().trim();
106                                         // Avoid useless queries for keystrokes that do not modify the text.
107                                         if (text.equals(selectedUser))
108                                                 return;
109                                         selectedUser = text;
110                                         // Go to the server only if the user typed the @ character.
111                                         if (selectedUser.endsWith("@"))
112                                                 updateSuggestions();
113                                 }
114                         });
115                         permTable.setWidget(1, 0, suggestBox);
116                 } else
117                         permTable.setWidget(1, 0, groupBox);
118                 permTable.setWidget(1, 1, read);
119                 permTable.setWidget(1, 2, write);
120                 permTable.setWidget(1, 3, modifyACL);
121
122                 permTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
123                 permTable.getFlexCellFormatter().setHorizontalAlignment(1, 1, HasHorizontalAlignment.ALIGN_CENTER);
124                 permTable.getFlexCellFormatter().setHorizontalAlignment(1, 2, HasHorizontalAlignment.ALIGN_CENTER);
125                 permTable.getFlexCellFormatter().setHorizontalAlignment(1, 3, HasHorizontalAlignment.ALIGN_CENTER);
126                 panel.add(permTable);
127
128                 final Button ok = new Button("OK", new ClickListener() {
129
130                         public void onClick(Widget sender) {
131                                 addPermission();
132                                 hide();
133                         }
134                 });
135                 buttons.add(ok);
136                 buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
137                 // Create the 'Cancel' button, along with a listener that hides the
138                 // dialog
139                 // when the button is clicked.
140                 final Button cancel = new Button("Cancel", new ClickListener() {
141
142                         public void onClick(Widget sender) {
143                                 hide();
144                         }
145                 });
146                 buttons.add(cancel);
147                 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
148                 buttons.setSpacing(8);
149                 buttons.addStyleName("gwt-TabPanelBottom");
150                 panel.add(buttons);
151                 panel.addStyleName("gwt-TabPanelBottom");
152         }
153
154         private void addPermission() {
155                 PermissionHolder perm = new PermissionHolder();
156                 if (userAdd) {
157                         selectedUser = suggestBox.getText();
158                         for(PermissionHolder p : permList.permissions)
159                                 if (selectedUser.equals(p.getUser())){
160                                         GSS.get().displayError("User already exists");
161                                         return;
162                                 }
163                         perm.setUser(selectedUser);
164                 } else {
165                         String groupId = groupBox.getValue(groupBox.getSelectedIndex());
166                         GroupResource selected = null;
167                         for (GroupResource g : groups)
168                                 if (g.getName().equals(groupId))
169                                         selected = g;
170                         for(PermissionHolder p : permList.permissions)
171                                 if (selected.equals(p.getGroup())){
172                                         GSS.get().displayError("Group already exists");
173                                         return;
174                                 }
175                         perm.setGroup(selected.getName());
176                 }
177                 boolean readValue = read.isChecked();
178                 boolean writeValue = write.isChecked();
179                 boolean modifyValue = modifyACL.isChecked();
180
181                 perm.setRead(readValue);
182                 perm.setWrite(writeValue);
183                 perm.setModifyACL(modifyValue);
184                 permList.addPermission(perm);
185                 permList.updateTable();
186         }
187
188         @Override
189         public boolean onKeyDownPreview(final char key, final int modifiers) {
190                 // Use the popup's key preview hooks to close the dialog when either
191                 // enter or escape is pressed.
192                 switch (key) {
193                         case KeyboardListener.KEY_ENTER:
194                                 addPermission();
195                                 hide();
196                                 break;
197                         case KeyboardListener.KEY_ESCAPE:
198                                 hide();
199                                 break;
200                 }
201                 return true;
202         }
203
204         @Override
205         public void center() {
206                 super.center();
207                 if (userAdd)
208                         suggestBox.setFocus(true);
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)) {
221
222                         @Override
223                         public void onComplete() {
224                                 DisplayHelper.hideSuggestions(suggestBox);
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                                 DisplayHelper.showSuggestions(suggestBox, selectedUser);
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 }