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