Revision c947e007 src/gr/grnet/pithos/web/client/PermissionsAddDialog.java
b/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.catalog.UpdateUserCatalogs; |
|
38 |
import gr.grnet.pithos.web.client.catalog.UserCatalogs; |
|
39 |
import gr.grnet.pithos.web.client.grouptree.Group; |
|
40 |
|
|
41 |
import java.util.List; |
|
42 |
|
|
43 | 37 |
import com.google.gwt.dom.client.NativeEvent; |
44 | 38 |
import com.google.gwt.event.dom.client.ClickEvent; |
45 | 39 |
import com.google.gwt.event.dom.client.ClickHandler; |
46 | 40 |
import com.google.gwt.event.dom.client.KeyCodes; |
47 | 41 |
import com.google.gwt.regexp.shared.RegExp; |
48 | 42 |
import com.google.gwt.user.client.Event.NativePreviewEvent; |
49 |
import com.google.gwt.user.client.ui.Anchor; |
|
50 |
import com.google.gwt.user.client.ui.Button; |
|
51 |
import com.google.gwt.user.client.ui.DialogBox; |
|
52 |
import com.google.gwt.user.client.ui.FlexTable; |
|
53 |
import com.google.gwt.user.client.ui.HasHorizontalAlignment; |
|
54 |
import com.google.gwt.user.client.ui.ListBox; |
|
55 |
import com.google.gwt.user.client.ui.RadioButton; |
|
56 |
import com.google.gwt.user.client.ui.TextBox; |
|
57 |
import com.google.gwt.user.client.ui.VerticalPanel; |
|
43 |
import com.google.gwt.user.client.ui.*; |
|
44 |
import gr.grnet.pithos.web.client.catalog.UpdateUserCatalogs; |
|
45 |
import gr.grnet.pithos.web.client.catalog.UserCatalogs; |
|
46 |
import gr.grnet.pithos.web.client.grouptree.Group; |
|
47 |
|
|
48 |
import java.util.List; |
|
58 | 49 |
|
59 | 50 |
public class PermissionsAddDialog extends DialogBox { |
60 | 51 |
final static RegExp EmailValidator = RegExp.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+[.][A-Z]{2,4}$", "i"); |
61 | 52 |
|
62 |
private TextBox userBox = new TextBox();
|
|
53 |
private TextBox userBox = new TextBox();
|
|
63 | 54 |
|
64 |
private ListBox groupBox = new ListBox();
|
|
55 |
private ListBox groupBox = new ListBox();
|
|
65 | 56 |
|
66 |
private RadioButton read = new RadioButton("permissions");
|
|
57 |
private RadioButton read = new RadioButton("permissions");
|
|
67 | 58 |
|
68 |
private RadioButton write = new RadioButton("permissions");
|
|
59 |
private RadioButton write = new RadioButton("permissions");
|
|
69 | 60 |
|
70 |
private PermissionsList permList;
|
|
61 |
private final PermissionsList permList;
|
|
71 | 62 |
|
72 |
boolean userAdd;
|
|
63 |
private final boolean isUser;
|
|
73 | 64 |
|
74 |
private Pithos app; |
|
65 |
private final Pithos app;
|
|
75 | 66 |
|
76 |
public PermissionsAddDialog(Pithos _app, List<Group> _groups, PermissionsList _permList, boolean _userAdd) {
|
|
77 |
app = _app;
|
|
78 |
userAdd = _userAdd;
|
|
79 |
permList = _permList;
|
|
67 |
public PermissionsAddDialog(Pithos app, List<Group> groups, PermissionsList permList, boolean isUser) {
|
|
68 |
this.app = app;
|
|
69 |
this.isUser = isUser;
|
|
70 |
this.permList = permList;
|
|
80 | 71 |
|
81 |
Anchor close = new Anchor("close");
|
|
82 |
close.addStyleName("close");
|
|
83 |
close.addClickHandler(new ClickHandler() {
|
|
84 |
|
|
85 |
@Override
|
|
86 |
public void onClick(ClickEvent event) {
|
|
87 |
hide();
|
|
88 |
}
|
|
89 |
});
|
|
90 |
setText("Add permission");
|
|
91 |
setStyleName("pithos-DialogBox");
|
|
72 |
Anchor close = new Anchor("close");
|
|
73 |
close.addStyleName("close");
|
|
74 |
close.addClickHandler(new ClickHandler() {
|
|
75 |
@Override |
|
76 |
public void onClick(ClickEvent event) {
|
|
77 |
hide();
|
|
78 |
}
|
|
79 |
});
|
|
80 |
final String dialogText = isUser ? "Add user" : "Add group";
|
|
81 |
setText(dialogText);
|
|
82 |
setStyleName("pithos-DialogBox");
|
|
92 | 83 |
|
93 | 84 |
final VerticalPanel panel = new VerticalPanel(); |
94 | 85 |
panel.add(close); |
95 | 86 |
|
96 | 87 |
VerticalPanel inner = new VerticalPanel(); |
97 |
inner.addStyleName("inner");
|
|
88 |
inner.addStyleName("inner");
|
|
98 | 89 |
|
99 | 90 |
final FlexTable permTable = new FlexTable(); |
100 |
permTable.setText(0, 0, "Users/Groups");
|
|
91 |
permTable.setText(0, 0, isUser ? "User" : "Group");
|
|
101 | 92 |
permTable.setText(0, 1, "Read Only"); |
102 | 93 |
permTable.setText(0, 2, "Read/Write"); |
103 | 94 |
permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels"); |
104 | 95 |
permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels"); |
105 | 96 |
permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels"); |
106 | 97 |
|
107 |
if (userAdd) {
|
|
98 |
if(this.isUser) {
|
|
108 | 99 |
permTable.setWidget(1, 0, userBox); |
109 | 100 |
} |
110 | 101 |
else { |
111 |
for (Group group : _groups) {
|
|
102 |
for(Group group : groups) {
|
|
112 | 103 |
groupBox.addItem(group.getName(), group.getName()); |
113 | 104 |
} |
114 | 105 |
permTable.setWidget(1, 0, groupBox); |
115 | 106 |
} |
116 |
|
|
107 |
|
|
117 | 108 |
read.setValue(true); |
118 | 109 |
permTable.setWidget(1, 1, read); |
119 | 110 |
permTable.setWidget(1, 2, write); |
... | ... | |
136 | 127 |
|
137 | 128 |
panel.add(inner); |
138 | 129 |
panel.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER); |
139 |
|
|
130 |
|
|
140 | 131 |
setWidget(panel); |
141 |
}
|
|
132 |
}
|
|
142 | 133 |
|
143 |
protected void addPermission() {
|
|
134 |
protected void addPermission() {
|
|
144 | 135 |
final boolean readValue = read.getValue(); |
145 | 136 |
final boolean writeValue = write.getValue(); |
146 | 137 |
|
147 | 138 |
String selected = null; |
148 |
if (userAdd) {
|
|
149 |
final String userDisplayName = userBox.getText().trim();
|
|
150 |
addUserPermission(userDisplayName, readValue, writeValue);
|
|
139 |
if(isUser) {
|
|
140 |
final String userDisplayName = userBox.getText().trim();
|
|
141 |
addUserPermission(userDisplayName, readValue, writeValue);
|
|
151 | 142 |
return; |
152 |
} else if (groupBox.getSelectedIndex() > -1) { |
|
153 |
String groupName = groupBox.getValue(groupBox.getSelectedIndex()); |
|
154 |
selected = app.getUserID() + ":" + groupName; |
|
155 |
} |
|
156 |
if (permList.getPermissions().get(selected) != null) { |
|
143 |
} |
|
144 |
else if(groupBox.getSelectedIndex() > -1) { |
|
145 |
String groupName = groupBox.getValue(groupBox.getSelectedIndex()); |
|
146 |
selected = app.getUserID() + ":" + groupName; |
|
147 |
} |
|
148 |
if(permList.getPermissions().get(selected) != null) { |
|
157 | 149 |
return; |
158 | 150 |
} |
159 |
if (selected == null || selected.length() == 0 || selected.equals(app.getUserID() + ":")) {
|
|
160 |
app.displayWarning("You have to select a username or group");
|
|
161 |
return;
|
|
151 |
if(selected == null || selected.length() == 0 || selected.equals(app.getUserID() + ":")) { |
|
152 |
app.displayWarning("You have to select a username or group");
|
|
153 |
return;
|
|
162 | 154 |
} |
163 | 155 |
|
164 |
permList.addPermission(selected, readValue, writeValue);
|
|
165 |
}
|
|
156 |
permList.addPermission(selected, readValue, writeValue);
|
|
157 |
}
|
|
166 | 158 |
|
167 | 159 |
private boolean alreadyHasPermission(String selected) { |
168 | 160 |
return permList.getPermissions().get(selected) != null; |
169 | 161 |
} |
170 | 162 |
|
171 | 163 |
private void addUserPermission(final String userDisplayName, final boolean readValue, final boolean writeValue) { |
172 |
if (!EmailValidator.test(userDisplayName)) {
|
|
164 |
if(!EmailValidator.test(userDisplayName)) { |
|
173 | 165 |
app.displayWarning("Username must be a valid email address"); |
174 | 166 |
return; |
175 | 167 |
} |
... | ... | |
200 | 192 |
} |
201 | 193 |
} |
202 | 194 |
|
203 |
@Override |
|
204 |
protected void onPreviewNativeEvent(NativePreviewEvent preview) { |
|
205 |
super.onPreviewNativeEvent(preview); |
|
206 |
|
|
207 |
NativeEvent evt = preview.getNativeEvent(); |
|
208 |
if (evt.getType().equals("keydown")) |
|
209 |
// Use the popup's key preview hooks to close the dialog when either |
|
210 |
// enter or escape is pressed. |
|
211 |
switch (evt.getKeyCode()) { |
|
212 |
case KeyCodes.KEY_ENTER: |
|
213 |
addPermission(); |
|
214 |
hide(); |
|
215 |
break; |
|
216 |
case KeyCodes.KEY_ESCAPE: |
|
217 |
hide(); |
|
218 |
break; |
|
219 |
} |
|
220 |
} |
|
221 |
|
|
222 |
|
|
223 |
@Override |
|
224 |
public void center() { |
|
225 |
super.center(); |
|
226 |
if (userAdd) |
|
227 |
userBox.setFocus(true); |
|
228 |
} |
|
195 |
@Override |
|
196 |
protected void onPreviewNativeEvent(NativePreviewEvent preview) { |
|
197 |
super.onPreviewNativeEvent(preview); |
|
198 |
|
|
199 |
NativeEvent evt = preview.getNativeEvent(); |
|
200 |
if(evt.getType().equals("keydown")) |
|
201 |
// Use the popup's key preview hooks to close the dialog when either |
|
202 |
// enter or escape is pressed. |
|
203 |
{ |
|
204 |
switch(evt.getKeyCode()) { |
|
205 |
case KeyCodes.KEY_ENTER: |
|
206 |
addPermission(); |
|
207 |
hide(); |
|
208 |
break; |
|
209 |
case KeyCodes.KEY_ESCAPE: |
|
210 |
hide(); |
|
211 |
break; |
|
212 |
} |
|
213 |
} |
|
214 |
} |
|
215 |
|
|
216 |
|
|
217 |
@Override |
|
218 |
public void center() { |
|
219 |
super.center(); |
|
220 |
if(isUser) { |
|
221 |
userBox.setFocus(true); |
|
222 |
} |
|
223 |
} |
|
229 | 224 |
} |
Also available in: Unified diff