root / src / org / gss_project / gss / web / client / PermissionsAddDialog.java @ 1206:292dec4eae08
History | View | Annotate | Download (9.1 kB)
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 org.gss_project.gss.web.client; |
20 |
|
21 |
import org.gss_project.gss.web.client.rest.GetCommand; |
22 |
import org.gss_project.gss.web.client.rest.RestException; |
23 |
import org.gss_project.gss.web.client.rest.resource.GroupResource; |
24 |
import org.gss_project.gss.web.client.rest.resource.PermissionHolder; |
25 |
import org.gss_project.gss.web.client.rest.resource.UserResource; |
26 |
import org.gss_project.gss.web.client.rest.resource.UserSearchResource; |
27 |
|
28 |
import java.util.List; |
29 |
|
30 |
import com.google.gwt.core.client.GWT; |
31 |
import com.google.gwt.dom.client.NativeEvent; |
32 |
import com.google.gwt.event.dom.client.ClickEvent; |
33 |
import com.google.gwt.event.dom.client.ClickHandler; |
34 |
import com.google.gwt.event.dom.client.FocusEvent; |
35 |
import com.google.gwt.event.dom.client.FocusHandler; |
36 |
import com.google.gwt.event.dom.client.KeyCodes; |
37 |
import com.google.gwt.event.dom.client.KeyUpEvent; |
38 |
import com.google.gwt.event.dom.client.KeyUpHandler; |
39 |
import com.google.gwt.http.client.URL; |
40 |
import com.google.gwt.user.client.DeferredCommand; |
41 |
import com.google.gwt.user.client.Event.NativePreviewEvent; |
42 |
import com.google.gwt.user.client.ui.Button; |
43 |
import com.google.gwt.user.client.ui.CheckBox; |
44 |
import com.google.gwt.user.client.ui.DialogBox; |
45 |
import com.google.gwt.user.client.ui.FlexTable; |
46 |
import com.google.gwt.user.client.ui.HasHorizontalAlignment; |
47 |
import com.google.gwt.user.client.ui.HorizontalPanel; |
48 |
import com.google.gwt.user.client.ui.ListBox; |
49 |
import com.google.gwt.user.client.ui.MultiWordSuggestOracle; |
50 |
import com.google.gwt.user.client.ui.SuggestBox; |
51 |
import com.google.gwt.user.client.ui.VerticalPanel; |
52 |
|
53 |
/**
|
54 |
* @author kman
|
55 |
*/
|
56 |
public class PermissionsAddDialog extends DialogBox { |
57 |
|
58 |
private MultiWordSuggestOracle oracle = new MultiWordSuggestOracle(); |
59 |
private SuggestBox suggestBox = new SuggestBox(oracle); |
60 |
|
61 |
private String selectedUser = null; |
62 |
|
63 |
private List<GroupResource> groups; |
64 |
|
65 |
private ListBox groupBox = new ListBox(); |
66 |
|
67 |
private CheckBox read = new CheckBox(); |
68 |
|
69 |
private CheckBox write = new CheckBox(); |
70 |
|
71 |
private CheckBox modifyACL = new CheckBox(); |
72 |
|
73 |
private final PermissionsList permList; |
74 |
|
75 |
boolean userAdd;
|
76 |
|
77 |
public PermissionsAddDialog(List<GroupResource> _groups, PermissionsList _permList, boolean _userAdd) { |
78 |
groups = _groups; |
79 |
userAdd = _userAdd; |
80 |
permList = _permList; |
81 |
|
82 |
groupBox.getElement().setId("addPermission.dropDown");
|
83 |
|
84 |
suggestBox.getElement().setId("addPermission.textBox");
|
85 |
|
86 |
read.getElement().setId("addPermission.read");
|
87 |
|
88 |
write.getElement().setId("addPermission.write");
|
89 |
|
90 |
modifyACL.getElement().setId("addpermission.modify");
|
91 |
|
92 |
for (GroupResource group : _groups)
|
93 |
groupBox.addItem(group.getName(), group.getName()); |
94 |
final VerticalPanel panel = new VerticalPanel(); |
95 |
final HorizontalPanel buttons = new HorizontalPanel(); |
96 |
setWidget(panel); |
97 |
final FlexTable permTable = new FlexTable(); |
98 |
permTable.setText(0, 0, "Users/Groups"); |
99 |
permTable.setText(0, 1, "Read"); |
100 |
permTable.setText(0, 2, "Write"); |
101 |
permTable.setText(0, 3, "Modify Access"); |
102 |
permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels"); |
103 |
permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels"); |
104 |
permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels"); |
105 |
permTable.getFlexCellFormatter().setStyleName(0, 3, "props-toplabels"); |
106 |
if (userAdd) {
|
107 |
suggestBox.getTextBox().addFocusHandler(new FocusHandler() {
|
108 |
|
109 |
@Override
|
110 |
public void onFocus(FocusEvent event) { |
111 |
if (selectedUser != null && selectedUser.endsWith("@")) |
112 |
updateSuggestions(); |
113 |
|
114 |
} |
115 |
}); |
116 |
|
117 |
suggestBox.addKeyUpHandler(new KeyUpHandler() {
|
118 |
|
119 |
@Override
|
120 |
public void onKeyUp(KeyUpEvent event) { |
121 |
// Ignore the arrow keys.
|
122 |
int keyCode = event.getNativeKeyCode();
|
123 |
if (keyCode == KeyCodes.KEY_UP ||
|
124 |
keyCode == KeyCodes.KEY_DOWN || |
125 |
keyCode == KeyCodes.KEY_LEFT || |
126 |
keyCode == KeyCodes.KEY_RIGHT) |
127 |
return;
|
128 |
if (keyCode==KeyCodes.KEY_ESCAPE) {
|
129 |
suggestBox.hideSuggestionList(); |
130 |
return;
|
131 |
} |
132 |
String text = suggestBox.getText().trim();
|
133 |
// Avoid useless queries for keystrokes that do not modify
|
134 |
// the text.
|
135 |
if (text.equals(selectedUser))
|
136 |
return;
|
137 |
selectedUser = text; |
138 |
// Go to the server only if the user typed the @ character.
|
139 |
if (selectedUser.endsWith("@")) |
140 |
updateSuggestions(); |
141 |
} |
142 |
}); |
143 |
permTable.setWidget(1, 0, suggestBox); |
144 |
} else
|
145 |
permTable.setWidget(1, 0, groupBox); |
146 |
permTable.setWidget(1, 1, read); |
147 |
permTable.setWidget(1, 2, write); |
148 |
permTable.setWidget(1, 3, modifyACL); |
149 |
|
150 |
permTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels"); |
151 |
permTable.getFlexCellFormatter().setHorizontalAlignment(1, 1, HasHorizontalAlignment.ALIGN_CENTER); |
152 |
permTable.getFlexCellFormatter().setHorizontalAlignment(1, 2, HasHorizontalAlignment.ALIGN_CENTER); |
153 |
permTable.getFlexCellFormatter().setHorizontalAlignment(1, 3, HasHorizontalAlignment.ALIGN_CENTER); |
154 |
panel.add(permTable); |
155 |
|
156 |
final Button ok = new Button("OK", new ClickHandler() { |
157 |
@Override
|
158 |
public void onClick(ClickEvent event) { |
159 |
addPermission(); |
160 |
hide(); |
161 |
} |
162 |
}); |
163 |
ok.getElement().setId("addPermission.button.ok");
|
164 |
buttons.add(ok); |
165 |
buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER); |
166 |
// Create the 'Cancel' button, along with a listener that hides the
|
167 |
// dialog
|
168 |
// when the button is clicked.
|
169 |
final Button cancel = new Button("Cancel", new ClickHandler() { |
170 |
@Override
|
171 |
public void onClick(ClickEvent event) { |
172 |
hide(); |
173 |
} |
174 |
}); |
175 |
cancel.getElement().setId("addPermission.button.cancel");
|
176 |
buttons.add(cancel); |
177 |
buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER); |
178 |
buttons.setSpacing(8);
|
179 |
buttons.addStyleName("gss-TabPanelBottom");
|
180 |
panel.add(buttons); |
181 |
panel.addStyleName("gss-TabPanelBottom");
|
182 |
} |
183 |
|
184 |
private void addPermission() { |
185 |
PermissionHolder perm = new PermissionHolder();
|
186 |
if (userAdd) {
|
187 |
selectedUser = suggestBox.getText(); |
188 |
for(PermissionHolder p : permList.permissions)
|
189 |
if (selectedUser.equals(p.getUser())){
|
190 |
GSS.get().displayError("User already has access to the resource");
|
191 |
return;
|
192 |
} |
193 |
perm.setUser(selectedUser); |
194 |
} else {
|
195 |
String groupId = groupBox.getValue(groupBox.getSelectedIndex());
|
196 |
GroupResource selected = null;
|
197 |
for (GroupResource g : groups)
|
198 |
if (g.getName().equals(groupId))
|
199 |
selected = g; |
200 |
if (selected == null) |
201 |
return;
|
202 |
for(PermissionHolder p : permList.permissions)
|
203 |
if (selected.getName().equals(p.getGroup())){
|
204 |
GSS.get().displayError("Group already has access to the resource");
|
205 |
return;
|
206 |
} |
207 |
perm.setGroup(selected.getName()); |
208 |
} |
209 |
boolean readValue = read.getValue();
|
210 |
boolean writeValue = write.getValue();
|
211 |
boolean modifyValue = modifyACL.getValue();
|
212 |
|
213 |
perm.setRead(readValue); |
214 |
perm.setWrite(writeValue); |
215 |
perm.setModifyACL(modifyValue); |
216 |
permList.addPermission(perm); |
217 |
permList.updateTable(); |
218 |
} |
219 |
|
220 |
@Override
|
221 |
protected void onPreviewNativeEvent(NativePreviewEvent preview) { |
222 |
super.onPreviewNativeEvent(preview);
|
223 |
|
224 |
NativeEvent evt = preview.getNativeEvent(); |
225 |
if (evt.getType().equals("keydown")) |
226 |
// Use the popup's key preview hooks to close the dialog when either
|
227 |
// enter or escape is pressed.
|
228 |
switch (evt.getKeyCode()) {
|
229 |
case KeyCodes.KEY_ENTER:
|
230 |
addPermission(); |
231 |
hide(); |
232 |
break;
|
233 |
case KeyCodes.KEY_ESCAPE:
|
234 |
hide(); |
235 |
break;
|
236 |
} |
237 |
} |
238 |
|
239 |
|
240 |
@Override
|
241 |
public void center() { |
242 |
super.center();
|
243 |
if (userAdd)
|
244 |
suggestBox.setFocus(true);
|
245 |
} |
246 |
|
247 |
/**
|
248 |
* Update the list of suggestions.
|
249 |
*/
|
250 |
protected void updateSuggestions() { |
251 |
final GSS app = GSS.get();
|
252 |
String query = selectedUser.substring(0, selectedUser.length()-1); |
253 |
GWT.log("Searching for " + query, null); |
254 |
|
255 |
GetCommand<UserSearchResource> eg = new GetCommand<UserSearchResource>(UserSearchResource.class,
|
256 |
app.getApiPath() + "users/" + URL.encodeComponent(query), false, null) { |
257 |
|
258 |
@Override
|
259 |
public void onComplete() { |
260 |
suggestBox.hideSuggestionList(); |
261 |
oracle.clear(); |
262 |
UserSearchResource s = getResult(); |
263 |
for (UserResource user : s.getUsers()) {
|
264 |
GWT.log("Found " + user.getUsername(), null); |
265 |
oracle.add(user.getUsername()); |
266 |
} |
267 |
suggestBox.showSuggestionList(); |
268 |
} |
269 |
|
270 |
@Override
|
271 |
public void onError(Throwable t) { |
272 |
if(t instanceof RestException) |
273 |
app.displayError("Unable to perform search: "+((RestException)t).getHttpStatusText());
|
274 |
else
|
275 |
app.displayError("System error while searching for users: "+t.getMessage());
|
276 |
GWT.log("", t);
|
277 |
DisplayHelper.log(t.getMessage()); |
278 |
} |
279 |
|
280 |
}; |
281 |
DeferredCommand.addCommand(eg); |
282 |
} |
283 |
} |