Revision 9fce5629 web_client/src/gr/grnet/pithos/web/client/PermissionsAddDialog.java
b/web_client/src/gr/grnet/pithos/web/client/PermissionsAddDialog.java | ||
---|---|---|
34 | 34 |
*/ |
35 | 35 |
package gr.grnet.pithos.web.client; |
36 | 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; |
|
37 |
import com.google.gwt.user.client.ui.TextBox; |
|
38 |
import gr.grnet.pithos.web.client.foldertree.Group; |
|
43 | 39 |
|
44 | 40 |
import java.util.List; |
45 | 41 |
|
46 |
import com.google.gwt.core.client.GWT; |
|
47 | 42 |
import com.google.gwt.dom.client.NativeEvent; |
48 | 43 |
import com.google.gwt.event.dom.client.ClickEvent; |
49 | 44 |
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 | 45 |
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 | 46 |
import com.google.gwt.user.client.Event.NativePreviewEvent; |
58 | 47 |
import com.google.gwt.user.client.ui.Button; |
59 | 48 |
import com.google.gwt.user.client.ui.CheckBox; |
... | ... | |
62 | 51 |
import com.google.gwt.user.client.ui.HasHorizontalAlignment; |
63 | 52 |
import com.google.gwt.user.client.ui.HorizontalPanel; |
64 | 53 |
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 | 54 |
import com.google.gwt.user.client.ui.VerticalPanel; |
68 | 55 |
|
69 | 56 |
public class PermissionsAddDialog extends DialogBox { |
70 | 57 |
|
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; |
|
58 |
private TextBox userBox = new TextBox(); |
|
77 | 59 |
|
78 | 60 |
private ListBox groupBox = new ListBox(); |
79 | 61 |
|
... | ... | |
81 | 63 |
|
82 | 64 |
private CheckBox write = new CheckBox(); |
83 | 65 |
|
84 |
private CheckBox modifyACL = new CheckBox(); |
|
85 |
|
|
86 |
private final PermissionsList permList; |
|
66 |
private PermissionsList permList; |
|
87 | 67 |
|
88 | 68 |
boolean userAdd; |
89 | 69 |
|
90 | 70 |
private Pithos app; |
91 | 71 |
|
92 |
public PermissionsAddDialog(Pithos _app, List<GroupResource> _groups, PermissionsList _permList, boolean _userAdd) {
|
|
72 |
public PermissionsAddDialog(Pithos _app, List<Group> _groups, PermissionsList _permList, boolean _userAdd) { |
|
93 | 73 |
app = _app; |
94 |
groups = _groups; |
|
95 | 74 |
userAdd = _userAdd; |
96 | 75 |
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 | 76 |
|
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"); |
|
77 |
final VerticalPanel panel = new VerticalPanel(); |
|
78 |
|
|
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.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels"); |
|
84 |
permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels"); |
|
85 |
permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels"); |
|
86 |
|
|
87 |
if (userAdd) { |
|
88 |
permTable.setWidget(1, 0, userBox); |
|
89 |
} |
|
90 |
else { |
|
91 |
for (Group group : _groups) |
|
92 |
groupBox.addItem(group.getName(), group.getName()); |
|
93 |
permTable.setWidget(1, 0, groupBox); |
|
94 |
} |
|
95 |
|
|
96 |
permTable.setWidget(1, 1, read); |
|
97 |
permTable.setWidget(1, 2, write); |
|
98 |
|
|
99 |
permTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels"); |
|
100 |
permTable.getFlexCellFormatter().setHorizontalAlignment(1, 1, HasHorizontalAlignment.ALIGN_CENTER); |
|
101 |
permTable.getFlexCellFormatter().setHorizontalAlignment(1, 2, HasHorizontalAlignment.ALIGN_CENTER); |
|
102 |
panel.add(permTable); |
|
103 |
|
|
104 |
final HorizontalPanel buttons = new HorizontalPanel(); |
|
105 |
final Button ok = new Button("OK", new ClickHandler() { |
|
106 |
@Override |
|
107 |
public void onClick(ClickEvent event) { |
|
108 |
addPermission(); |
|
109 |
hide(); |
|
110 |
} |
|
111 |
}); |
|
112 |
buttons.add(ok); |
|
113 |
buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER); |
|
114 |
// Create the 'Cancel' button, along with a listener that hides the |
|
115 |
// dialog |
|
116 |
// when the button is clicked. |
|
117 |
final Button cancel = new Button("Cancel", new ClickHandler() { |
|
118 |
@Override |
|
119 |
public void onClick(ClickEvent event) { |
|
120 |
hide(); |
|
121 |
} |
|
122 |
}); |
|
123 |
buttons.add(cancel); |
|
124 |
buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER); |
|
125 |
buttons.setSpacing(8); |
|
126 |
buttons.addStyleName("pithos-TabPanelBottom"); |
|
127 |
|
|
128 |
panel.add(buttons); |
|
129 |
panel.addStyleName("pithos-TabPanelBottom"); |
|
130 |
|
|
131 |
setWidget(panel); |
|
198 | 132 |
} |
199 | 133 |
|
200 | 134 |
private void addPermission() { |
201 |
PermissionHolder perm = new PermissionHolder();
|
|
135 |
String selected = null;
|
|
202 | 136 |
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); |
|
137 |
selected = userBox.getText(); |
|
210 | 138 |
} 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()); |
|
139 |
String groupName = groupBox.getValue(groupBox.getSelectedIndex()); |
|
140 |
selected = app.getUsername() + ":" + groupName; |
|
224 | 141 |
} |
142 |
if (permList.getPermissions().get(selected) != null) { |
|
143 |
app.displayError((userAdd ? "User " : "Group ") + "already has access to the resource"); |
|
144 |
return; |
|
145 |
} |
|
225 | 146 |
boolean readValue = read.getValue(); |
226 | 147 |
boolean writeValue = write.getValue(); |
227 |
boolean modifyValue = modifyACL.getValue(); |
|
228 | 148 |
|
229 |
perm.setRead(readValue); |
|
230 |
perm.setWrite(writeValue); |
|
231 |
perm.setModifyACL(modifyValue); |
|
232 |
permList.addPermission(perm); |
|
233 |
permList.updateTable(); |
|
149 |
permList.addPermission(selected, readValue, writeValue); |
|
234 | 150 |
} |
235 | 151 |
|
236 | 152 |
@Override |
... | ... | |
257 | 173 |
public void center() { |
258 | 174 |
super.center(); |
259 | 175 |
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); |
|
176 |
userBox.setFocus(true); |
|
297 | 177 |
} |
298 | 178 |
} |
Also available in: Unified diff