root / src / gr / grnet / pithos / web / client / AddUserDialog.java @ cd2dd1fe
History | View | Annotate | Download (7.3 kB)
1 |
/*
|
---|---|
2 |
* Copyright 2011-2012 GRNET S.A. All rights reserved.
|
3 |
*
|
4 |
* Redistribution and use in source and binary forms, with or
|
5 |
* without modification, are permitted provided that the following
|
6 |
* conditions are met:
|
7 |
*
|
8 |
* 1. Redistributions of source code must retain the above
|
9 |
* copyright notice, this list of conditions and the following
|
10 |
* disclaimer.
|
11 |
*
|
12 |
* 2. Redistributions in binary form must reproduce the above
|
13 |
* copyright notice, this list of conditions and the following
|
14 |
* disclaimer in the documentation and/or other materials
|
15 |
* provided with the distribution.
|
16 |
*
|
17 |
* THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
18 |
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
19 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
20 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
21 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
22 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
23 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
24 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
25 |
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
26 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
27 |
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
28 |
* POSSIBILITY OF SUCH DAMAGE.
|
29 |
*
|
30 |
* The views and conclusions contained in the software and
|
31 |
* documentation are those of the authors and should not be
|
32 |
* interpreted as representing official policies, either expressed
|
33 |
* or implied, of GRNET S.A.
|
34 |
*/
|
35 |
package gr.grnet.pithos.web.client; |
36 |
|
37 |
import com.google.gwt.core.client.GWT; |
38 |
import com.google.gwt.core.client.Scheduler; |
39 |
import com.google.gwt.dom.client.NativeEvent; |
40 |
import com.google.gwt.event.dom.client.ClickEvent; |
41 |
import com.google.gwt.event.dom.client.ClickHandler; |
42 |
import com.google.gwt.event.dom.client.KeyCodes; |
43 |
import com.google.gwt.event.dom.client.KeyDownEvent; |
44 |
import com.google.gwt.http.client.Response; |
45 |
import com.google.gwt.http.client.URL; |
46 |
import com.google.gwt.user.client.Event.NativePreviewEvent; |
47 |
import com.google.gwt.user.client.ui.*; |
48 |
import gr.grnet.pithos.web.client.grouptree.Group; |
49 |
import gr.grnet.pithos.web.client.rest.PostRequest; |
50 |
import gr.grnet.pithos.web.client.rest.RestException; |
51 |
|
52 |
/**
|
53 |
* The 'Folder properties' dialog box implementation.
|
54 |
*/
|
55 |
public class AddUserDialog extends DialogBox { |
56 |
|
57 |
protected Pithos app;
|
58 |
|
59 |
Group group;
|
60 |
/**
|
61 |
* The widget that holds the folderName of the folder.
|
62 |
*/
|
63 |
TextBox userName = new TextBox();
|
64 |
|
65 |
final VerticalPanel inner;
|
66 |
|
67 |
/**
|
68 |
* The widget's constructor.
|
69 |
*/
|
70 |
public AddUserDialog(final Pithos app, Group _group) { |
71 |
this.app = app;
|
72 |
this.group = _group;
|
73 |
|
74 |
Anchor close = new Anchor("close"); |
75 |
close.addStyleName("close");
|
76 |
close.addClickHandler(new ClickHandler() {
|
77 |
|
78 |
@Override
|
79 |
public void onClick(ClickEvent event) { |
80 |
hide(); |
81 |
} |
82 |
}); |
83 |
|
84 |
setGlassEnabled(true);
|
85 |
setStyleName("pithos-DialogBox");
|
86 |
|
87 |
// Enable IE selection for the dialog (must disable it upon closing it)
|
88 |
Pithos.enableIESelection(); |
89 |
|
90 |
// Use this opportunity to set the dialog's caption.
|
91 |
setText("Add user");
|
92 |
|
93 |
// Outer contains inner and buttons
|
94 |
VerticalPanel outer = new VerticalPanel();
|
95 |
outer.add(close); |
96 |
// Inner contains generalPanel and permPanel
|
97 |
inner = new VerticalPanel();
|
98 |
inner.addStyleName("inner");
|
99 |
|
100 |
VerticalPanel generalPanel = new VerticalPanel();
|
101 |
FlexTable generalTable = new FlexTable();
|
102 |
generalTable.setText(0, 0, "Username"); |
103 |
|
104 |
generalTable.setWidget(0, 1, userName); |
105 |
|
106 |
generalTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels"); |
107 |
generalTable.getFlexCellFormatter().setStyleName(0, 1, "props-values"); |
108 |
generalTable.setCellSpacing(4);
|
109 |
generalPanel.add(generalTable); |
110 |
inner.add(generalPanel); |
111 |
|
112 |
outer.add(inner); |
113 |
|
114 |
// Create the 'Create/Update' button, along with a listener that hides the dialog
|
115 |
// when the button is clicked and quits the application.
|
116 |
String okLabel = "Create"; |
117 |
final Button ok = new Button(okLabel, new ClickHandler() { |
118 |
@Override
|
119 |
public void onClick(ClickEvent event) { |
120 |
addUser(); |
121 |
closeDialog(); |
122 |
} |
123 |
}); |
124 |
ok.addStyleName("button");
|
125 |
outer.add(ok); |
126 |
outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER); |
127 |
|
128 |
setWidget(outer); |
129 |
} |
130 |
|
131 |
@Override
|
132 |
public void center() { |
133 |
super.center();
|
134 |
userName.setFocus(true);
|
135 |
} |
136 |
|
137 |
@Override
|
138 |
protected void onPreviewNativeEvent(NativePreviewEvent preview) { |
139 |
super.onPreviewNativeEvent(preview);
|
140 |
|
141 |
NativeEvent evt = preview.getNativeEvent(); |
142 |
if(evt.getType().equals(KeyDownEvent.getType().getName()))
|
143 |
// Use the popup's key preview hooks to close the dialog when either
|
144 |
// enter or escape is pressed.
|
145 |
{ |
146 |
switch(evt.getKeyCode()) {
|
147 |
case KeyCodes.KEY_ENTER:
|
148 |
addUser(); |
149 |
closeDialog(); |
150 |
break;
|
151 |
case KeyCodes.KEY_ESCAPE:
|
152 |
closeDialog(); |
153 |
break;
|
154 |
} |
155 |
} |
156 |
} |
157 |
|
158 |
|
159 |
/**
|
160 |
* Enables IE selection prevention and hides the dialog
|
161 |
* (we disable the prevention on creation of the dialog)
|
162 |
*/
|
163 |
public void closeDialog() { |
164 |
Pithos.preventIESelection(); |
165 |
hide(); |
166 |
} |
167 |
|
168 |
/**
|
169 |
* Generate an RPC request to create a new folder.
|
170 |
*/
|
171 |
void addUser() {
|
172 |
String name = userName.getText().trim();
|
173 |
if(name.length() == 0) { |
174 |
return;
|
175 |
} |
176 |
if(!Const.EMAIL_REGEX.test(name)) {
|
177 |
app.displayWarning("Username must be a valid email address");
|
178 |
return;
|
179 |
} |
180 |
|
181 |
group.addMember(name); |
182 |
String path = "?update="; |
183 |
PostRequest updateGroup = new PostRequest(app.getApiPath(), app.getUserID(), path) {
|
184 |
|
185 |
@Override
|
186 |
public void onSuccess(Resource result) { |
187 |
app.updateGroupNode(group); |
188 |
} |
189 |
|
190 |
@Override
|
191 |
public void onError(Throwable t) { |
192 |
GWT.log("", t);
|
193 |
app.setError(t); |
194 |
if(t instanceof RestException) { |
195 |
app.displayError("Unable to update group:" + ((RestException) t).getHttpStatusText());
|
196 |
} |
197 |
else {
|
198 |
app.displayError("System error updating group:" + t.getMessage());
|
199 |
} |
200 |
} |
201 |
|
202 |
@Override
|
203 |
protected void onUnauthorized(Response response) { |
204 |
app.sessionExpired(); |
205 |
} |
206 |
}; |
207 |
updateGroup.setHeader(Const.X_AUTH_TOKEN, app.getUserToken()); |
208 |
String groupMembers = ""; |
209 |
for(String u : group.getMembers()) { |
210 |
groupMembers += (URL.encodePathSegment(u) + ","); |
211 |
} |
212 |
updateGroup.setHeader(Const.X_ACCOUNT_GROUP_ + URL.encodePathSegment(group.getName()), groupMembers);
|
213 |
Scheduler.get().scheduleDeferred(updateGroup); |
214 |
} |
215 |
} |