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