2 * Copyright 2008, 2009 Electronic Business Systems Ltd.
4 * This file is part of GSS.
6 * GSS is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GSS is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GSS. If not, see <http://www.gnu.org/licenses/>.
19 package gr.ebs.gss.client;
21 import gr.ebs.gss.client.rest.GetCommand;
22 import gr.ebs.gss.client.rest.RestException;
23 import gr.ebs.gss.client.rest.resource.UserResource;
25 import com.google.gwt.core.client.GWT;
26 import com.google.gwt.user.client.DeferredCommand;
27 import com.google.gwt.user.client.ui.Button;
28 import com.google.gwt.user.client.ui.ClickListener;
29 import com.google.gwt.user.client.ui.DialogBox;
30 import com.google.gwt.user.client.ui.FlexTable;
31 import com.google.gwt.user.client.ui.HTML;
32 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
33 import com.google.gwt.user.client.ui.HorizontalPanel;
34 import com.google.gwt.user.client.ui.KeyboardListener;
35 import com.google.gwt.user.client.ui.TextBox;
36 import com.google.gwt.user.client.ui.VerticalPanel;
37 import com.google.gwt.user.client.ui.Widget;
41 * A dialog box that displays the user credentials for use in other client
42 * applications, such as WebDAV clients.
46 public class CredentialsDialog extends DialogBox {
48 private final String WIDTH_FIELD = "35em";
49 private final String WIDTH_TEXT = "42em";
51 private TextBox passwordBox;
54 * The 'confirm reset password' dialog box.
56 public class ConfirmResetPasswordDialog extends DialogBox {
59 * The widget's constructor.
61 * @param images the supplied images
63 private ConfirmResetPasswordDialog(MessagePanel.Images images) {
64 // Set the dialog's caption.
65 setText("Confirmation");
66 setAnimationEnabled(true);
67 // Create a VerticalPanel to contain the label and the buttons.
68 VerticalPanel outer = new VerticalPanel();
69 HorizontalPanel buttons = new HorizontalPanel();
72 text = new HTML("<table><tr><td>" + images.warn().getHTML() + "</td><td>" + "Are you sure you want to create a new WebDAV password?</td></tr></table>");
73 text.setStyleName("gss-warnMessage");
76 // Create the 'Yes' button, along with a listener that hides the dialog
77 // when the button is clicked and resets the password.
78 Button ok = new Button("Yes", new ClickListener() {
79 public void onClick(Widget sender) {
80 resetPassword(GSS.get().getCurrentUserResource().getUri());
85 buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
86 // Create the 'No' button, along with a listener that hides the
87 // dialog when the button is clicked.
88 Button cancel = new Button("No", new ClickListener() {
89 public void onClick(Widget sender) {
94 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
95 buttons.setSpacing(8);
96 buttons.setStyleName("gss-warnMessage");
97 outer.setStyleName("gss-warnMessage");
99 outer.setCellHorizontalAlignment(text, HasHorizontalAlignment.ALIGN_CENTER);
100 outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
106 public boolean onKeyDownPreview(final char key, final int modifiers) {
107 // Use the popup's key preview hooks to close the dialog when
108 // escape is pressed.
110 case KeyboardListener.KEY_ESCAPE:
121 * The widget constructor.
123 public CredentialsDialog(final MessagePanel.Images images) {
124 // Set the dialog's caption.
125 setText("User Credentials");
126 setAnimationEnabled(true);
127 // Create a VerticalPanel to contain the 'about' label and the 'OK'
129 VerticalPanel outer = new VerticalPanel();
130 Configuration conf = (Configuration) GWT.create(Configuration.class);
131 String service = conf.serviceName();
132 String webdavUrl = conf.serviceHome() + conf.webdavUrl();
133 String tokenNote = conf.tokenTTLNote();
134 // Create the text and set a style name so we can style it with CSS.
135 HTML text = new HTML("<p>These are the user credentials that are required " +
136 "for interacting with " + service + ". " +
137 "You can copy and paste the username and password in the WebDAV client" +
138 " in order to use " + service + " through the WebDAV interface, at:<br/> " +
140 "<br/>" + tokenNote + "</p>");
141 text.setStyleName("gss-AboutText");
142 text.setWidth(WIDTH_TEXT);
144 FlexTable table = new FlexTable();
145 table.setText(0, 0, "Username");
146 table.setText(1, 0, "Password");
147 table.setText(2, 0, "Token");
148 TextBox username = new TextBox();
149 final GSS app = GSS.get();
150 username.setText(app.getCurrentUserResource().getUsername());
151 username.setReadOnly(true);
152 username.setWidth(WIDTH_FIELD);
153 username.addClickListener(new ClickListener () {
155 public void onClick(Widget sender) {
156 GSS.enableIESelection();
157 ((TextBox) sender).selectAll();
158 GSS.preventIESelection();
162 table.setWidget(0, 1, username);
163 passwordBox = new TextBox();
164 passwordBox.setText(app.getWebDAVPassword());
165 passwordBox.setReadOnly(true);
166 passwordBox.setWidth(WIDTH_FIELD);
167 passwordBox.addClickListener(new ClickListener () {
169 public void onClick(Widget sender) {
170 GSS.enableIESelection();
171 ((TextBox) sender).selectAll();
172 GSS.preventIESelection();
176 table.setWidget(1, 1, passwordBox);
178 TextBox tokenBox = new TextBox();
179 tokenBox.setText(app.getToken());
180 tokenBox.setReadOnly(true);
181 tokenBox.setWidth(WIDTH_FIELD);
182 tokenBox.addClickListener(new ClickListener () {
184 public void onClick(Widget sender) {
185 GSS.enableIESelection();
186 ((TextBox) sender).selectAll();
187 GSS.preventIESelection();
191 table.setWidget(2, 1, tokenBox);
193 table.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
194 table.getFlexCellFormatter().setStyleName(0, 1, "props-values");
195 table.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
196 table.getFlexCellFormatter().setStyleName(1, 1, "props-values");
197 table.getFlexCellFormatter().setStyleName(2, 0, "props-labels");
198 table.getFlexCellFormatter().setStyleName(2, 1, "props-values");
201 // Create the 'OK' button, along with a listener that hides the dialog
202 // when the button is clicked.
203 Button confirm = new Button("Close", new ClickListener() {
205 public void onClick(Widget sender) {
210 outer.setCellHorizontalAlignment(confirm, HasHorizontalAlignment.ALIGN_CENTER);
212 // Create the 'Reset password' button, along with a listener that hides the dialog
213 // when the button is clicked.
214 Button resetPassword = new Button("Reset Password", new ClickListener() {
216 public void onClick(Widget sender) {
217 ConfirmResetPasswordDialog dlg = new ConfirmResetPasswordDialog(images);
221 outer.add(resetPassword);
222 outer.setCellHorizontalAlignment(resetPassword, HasHorizontalAlignment.ALIGN_CENTER);
229 public boolean onKeyDownPreview(char key, int modifiers) {
230 // Use the popup's key preview hooks to close the dialog when either
231 // enter or escape is pressed.
233 case KeyboardListener.KEY_ENTER:
234 case KeyboardListener.KEY_ESCAPE:
243 * Generate an RPC request to reset WebDAV password.
245 * @param userId the Uri of the user whose password will be reset
247 private void resetPassword(String userUri) {
249 if (userUri == null || userUri.length() == 0) {
250 GSS.get().displayError("Empty user Uri!");
253 GWT.log("resetPassword(" + userUri + ")", null);
254 GetCommand cg = new GetCommand(UserResource.class, userUri + "?resetWebDAV"){
257 public void onComplete() {
258 GSS.get().refreshWebDAVPassword();
259 passwordBox.setText(GSS.get().getWebDAVPassword());
263 public void onError(Throwable t) {
265 if(t instanceof RestException){
266 int statusCode = ((RestException)t).getHttpStatusCode();
267 if(statusCode == 405)
268 GSS.get().displayError("You don't have the necessary permissions");
269 else if(statusCode == 404)
270 GSS.get().displayError("Resource does not exist");
272 GSS.get().displayError("Unable to reset password:"+((RestException)t).getHttpStatusText());
275 GSS.get().displayError("System error resetting password:"+t.getMessage());
278 DeferredCommand.addCommand(cg);