2 * Copyright (c) 2011 Greek Research and Technology Network
4 package gr.grnet.pithos.web.client;
6 import com.google.gwt.user.client.Window;
7 import gr.grnet.pithos.web.client.rest.PostCommand;
8 import gr.grnet.pithos.web.client.rest.RestException;
10 import com.google.gwt.core.client.GWT;
11 import com.google.gwt.dom.client.NativeEvent;
12 import com.google.gwt.event.dom.client.ClickEvent;
13 import com.google.gwt.event.dom.client.ClickHandler;
14 import com.google.gwt.event.dom.client.KeyCodes;
15 import com.google.gwt.user.client.DeferredCommand;
16 import com.google.gwt.user.client.Event.NativePreviewEvent;
17 import com.google.gwt.user.client.ui.AbstractImagePrototype;
18 import com.google.gwt.user.client.ui.Button;
19 import com.google.gwt.user.client.ui.DialogBox;
20 import com.google.gwt.user.client.ui.FlexTable;
21 import com.google.gwt.user.client.ui.HTML;
22 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
23 import com.google.gwt.user.client.ui.HorizontalPanel;
24 import com.google.gwt.user.client.ui.TextBox;
25 import com.google.gwt.user.client.ui.VerticalPanel;
29 * A dialog box that displays the user credentials for use in other client
30 * applications, such as WebDAV clients.
32 public class CredentialsDialog extends DialogBox {
34 private final String WIDTH_FIELD = "35em";
35 private final String WIDTH_TEXT = "42em";
38 * The 'confirm reset password' dialog box.
40 private class ConfirmResetPasswordDialog extends DialogBox {
43 * The widget's constructor.
45 * @param images the supplied images
47 private ConfirmResetPasswordDialog(MessagePanel.Images images) {
48 // Set the dialog's caption.
49 setText("Confirmation");
50 setAnimationEnabled(true);
51 // Create a VerticalPanel to contain the label and the buttons.
52 VerticalPanel outer = new VerticalPanel();
53 HorizontalPanel buttons = new HorizontalPanel();
56 text = new HTML("<table><tr><td>" +
57 AbstractImagePrototype.create(images.warn()).getHTML() +
58 "</td><td>" + "Are you sure you want to create a new " +
59 "WebDAV password?</td></tr></table>");
60 text.setStyleName("pithos-warnMessage");
63 // Create the 'Yes' button, along with a listener that hides the
64 // dialog when the button is clicked and resets the password.
65 Button ok = new Button("Yes", new ClickHandler() {
67 public void onClick(ClickEvent event) {
68 resetPassword(GSS.get().getCurrentUserResource().getUri());
73 buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
74 // Create the 'No' button, along with a listener that hides the
75 // dialog when the button is clicked.
76 Button cancel = new Button("No", new ClickHandler() {
78 public void onClick(ClickEvent event) {
83 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
84 buttons.setSpacing(8);
85 buttons.setStyleName("pithos-warnMessage");
86 outer.setStyleName("pithos-warnMessage");
88 outer.setCellHorizontalAlignment(text, HasHorizontalAlignment.ALIGN_CENTER);
89 outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
94 protected void onPreviewNativeEvent(NativePreviewEvent preview) {
95 super.onPreviewNativeEvent(preview);
96 NativeEvent evt = preview.getNativeEvent();
97 if (evt.getType().equals("keydown"))
98 // Use the popup's key preview hooks to close the dialog when either
99 // enter or escape is pressed.
100 switch (evt.getKeyCode()) {
101 case KeyCodes.KEY_ENTER:
102 case KeyCodes.KEY_ESCAPE:
110 private class ReauthenticateDialog extends DialogBox {
112 * The widget constructor.
114 public ReauthenticateDialog() {
115 // Set the dialog's caption.
116 setText("New Password Created");
117 setAnimationEnabled(true);
118 VerticalPanel outer = new VerticalPanel();
120 // Create the text and set a style name so we can style it with CSS.
121 HTML text = new HTML("<p>A new WebDAV password has been created." +
122 "</p><p>You will now be redirected to the initial screen" +
123 " for the changes to take effect. Choose \"Show " +
124 "Credentials\" again afterwards to see the new password.</p>");
125 text.setStyleName("pithos-AboutText");
128 // Create the 'OK' button, along with a listener that hides the
129 // dialog when the button is clicked.
130 Button confirm = new Button("Proceed", new ClickHandler() {
132 public void onClick(ClickEvent event) {
133 GSS.get().authenticateUser();
138 outer.setCellHorizontalAlignment(confirm, HasHorizontalAlignment.ALIGN_CENTER);
144 protected void onPreviewNativeEvent(NativePreviewEvent preview) {
145 super.onPreviewNativeEvent(preview);
146 NativeEvent evt = preview.getNativeEvent();
147 if (evt.getType().equals("keydown"))
148 // Use the popup's key preview hooks to close the dialog when
149 // either enter or escape is pressed.
150 switch (evt.getKeyCode()) {
151 case KeyCodes.KEY_ENTER:
152 GSS.get().authenticateUser();
155 case KeyCodes.KEY_ESCAPE:
164 * The widget constructor.
166 public CredentialsDialog(final MessagePanel.Images images) {
167 // Set the dialog's caption.
168 setText("User Credentials");
169 setAnimationEnabled(true);
170 // A VerticalPanel that contains the 'about' label and the 'OK' button.
171 VerticalPanel outer = new VerticalPanel();
172 Configuration conf = (Configuration) GWT.create(Configuration.class);
173 String service = conf.serviceName();
174 // Create the text and set a style name so we can style it with CSS.
175 HTML text = new HTML("<p>These are the user credentials that are " +
176 "required for interacting with " + service + ".");
177 text.setStyleName("pithos-AboutText");
178 text.setWidth(WIDTH_TEXT);
180 FlexTable table = new FlexTable();
181 table.setText(0, 0, "Username");
182 table.setText(1, 0, "Token");
183 TextBox username = new TextBox();
184 final GSS app = GSS.get();
185 username.setText(app.getCurrentUserResource().getUsername());
186 username.setReadOnly(true);
187 username.setWidth(WIDTH_FIELD);
188 username.addClickHandler(new ClickHandler() {
190 public void onClick(ClickEvent event) {
191 GSS.enableIESelection();
192 ((TextBox) event.getSource()).selectAll();
193 GSS.preventIESelection();
197 table.setWidget(0, 1, username);
199 TextBox tokenBox = new TextBox();
200 tokenBox.setText(app.getToken());
201 tokenBox.setReadOnly(true);
202 tokenBox.setWidth(WIDTH_FIELD);
203 tokenBox.addClickHandler(new ClickHandler() {
205 public void onClick(ClickEvent event) {
206 GSS.enableIESelection();
207 ((TextBox) event.getSource()).selectAll();
208 GSS.preventIESelection();
212 table.setWidget(1, 1, tokenBox);
214 table.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
215 table.getFlexCellFormatter().setStyleName(0, 1, "props-values");
216 table.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
217 table.getFlexCellFormatter().setStyleName(1, 1, "props-values");
220 // Create the 'OK' button, along with a listener that hides the dialog
221 // when the button is clicked.
222 Button confirm = new Button("Close", new ClickHandler() {
224 public void onClick(ClickEvent event) {
229 outer.setCellHorizontalAlignment(confirm, HasHorizontalAlignment.ALIGN_CENTER);
231 // Create the 'Reset password' button, along with a listener that hides
232 // the dialog when the button is clicked.
233 Button resetPassword = new Button("Reset Password", new ClickHandler() {
235 public void onClick(ClickEvent event) {
236 ConfirmResetPasswordDialog dlg = new ConfirmResetPasswordDialog(images);
240 outer.add(resetPassword);
241 outer.setCellHorizontalAlignment(resetPassword, HasHorizontalAlignment.ALIGN_CENTER);
248 protected void onPreviewNativeEvent(NativePreviewEvent preview) {
249 super.onPreviewNativeEvent(preview);
250 NativeEvent evt = preview.getNativeEvent();
251 if (evt.getType().equals("keydown"))
252 // Use the popup's key preview hooks to close the dialog when
253 // either enter or escape is pressed.
254 switch (evt.getKeyCode()) {
255 case KeyCodes.KEY_ENTER:
256 case KeyCodes.KEY_ESCAPE:
264 * Generate an RPC request to reset WebDAV password.
267 private void resetPassword(String userUri) {
269 if (userUri == null || userUri.length() == 0) {
270 GSS.get().displayError("Empty user Uri!");
273 GWT.log("resetPassword(" + userUri + ")", null);
274 PostCommand cg = new PostCommand(userUri + "?resetWebDAV", "", 200) {
277 public void onComplete() {
278 ReauthenticateDialog dlg = new ReauthenticateDialog();
283 public void onError(Throwable t) {
285 if(t instanceof RestException){
286 int statusCode = ((RestException)t).getHttpStatusCode();
287 if(statusCode == 405)
288 GSS.get().displayError("You don't have the necessary" +
290 else if(statusCode == 404)
291 GSS.get().displayError("Resource does not exist");
293 GSS.get().displayError("Unable to reset password:" +
294 ((RestException)t).getHttpStatusText());
297 GSS.get().displayError("System error resetting password:" +
301 DeferredCommand.addCommand(cg);