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.PostCommand;
22 import gr.ebs.gss.client.rest.RestException;
24 import com.google.gwt.core.client.GWT;
25 import com.google.gwt.user.client.DeferredCommand;
26 import com.google.gwt.user.client.ui.Button;
27 import com.google.gwt.user.client.ui.ClickListener;
28 import com.google.gwt.user.client.ui.DialogBox;
29 import com.google.gwt.user.client.ui.FlexTable;
30 import com.google.gwt.user.client.ui.HTML;
31 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
32 import com.google.gwt.user.client.ui.HorizontalPanel;
33 import com.google.gwt.user.client.ui.KeyboardListener;
34 import com.google.gwt.user.client.ui.TextBox;
35 import com.google.gwt.user.client.ui.VerticalPanel;
36 import com.google.gwt.user.client.ui.Widget;
40 * A dialog box that displays the user credentials for use in other client
41 * applications, such as WebDAV clients.
45 public class CredentialsDialog extends DialogBox {
47 private final String WIDTH_FIELD = "35em";
48 private final String WIDTH_TEXT = "42em";
50 private TextBox passwordBox;
53 * The 'confirm reset password' dialog box.
55 public class ConfirmResetPasswordDialog extends DialogBox {
58 * The widget's constructor.
60 * @param images the supplied images
62 private ConfirmResetPasswordDialog(MessagePanel.Images images) {
63 // Set the dialog's caption.
64 setText("Confirmation");
65 setAnimationEnabled(true);
66 // Create a VerticalPanel to contain the label and the buttons.
67 VerticalPanel outer = new VerticalPanel();
68 HorizontalPanel buttons = new HorizontalPanel();
71 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>");
72 text.setStyleName("gss-warnMessage");
75 // Create the 'Yes' button, along with a listener that hides the dialog
76 // when the button is clicked and resets the password.
77 Button ok = new Button("Yes", new ClickListener() {
78 public void onClick(Widget sender) {
79 resetPassword(GSS.get().getCurrentUserResource().getUri());
84 buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
85 // Create the 'No' button, along with a listener that hides the
86 // dialog when the button is clicked.
87 Button cancel = new Button("No", new ClickListener() {
88 public void onClick(Widget sender) {
93 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
94 buttons.setSpacing(8);
95 buttons.setStyleName("gss-warnMessage");
96 outer.setStyleName("gss-warnMessage");
98 outer.setCellHorizontalAlignment(text, HasHorizontalAlignment.ALIGN_CENTER);
99 outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
105 public boolean onKeyDownPreview(final char key, final int modifiers) {
106 // Use the popup's key preview hooks to close the dialog when
107 // escape is pressed.
109 case KeyboardListener.KEY_ESCAPE:
120 * The widget constructor.
122 public CredentialsDialog(final MessagePanel.Images images) {
123 // Set the dialog's caption.
124 setText("User Credentials");
125 setAnimationEnabled(true);
126 // Create a VerticalPanel to contain the 'about' label and the 'OK'
128 VerticalPanel outer = new VerticalPanel();
129 Configuration conf = (Configuration) GWT.create(Configuration.class);
130 String service = conf.serviceName();
131 String webdavUrl = conf.serviceHome() + conf.webdavUrl();
132 String tokenNote = conf.tokenTTLNote();
133 // Create the text and set a style name so we can style it with CSS.
134 HTML text = new HTML("<p>These are the user credentials that are required " +
135 "for interacting with " + service + ". " +
136 "You can copy and paste the username and password in the WebDAV client" +
137 " in order to use " + service + " through the WebDAV interface, at:<br/> " +
139 "<br/>" + tokenNote + "</p>");
140 text.setStyleName("gss-AboutText");
141 text.setWidth(WIDTH_TEXT);
143 FlexTable table = new FlexTable();
144 table.setText(0, 0, "Username");
145 table.setText(1, 0, "Password");
146 table.setText(2, 0, "Token");
147 TextBox username = new TextBox();
148 final GSS app = GSS.get();
149 username.setText(app.getCurrentUserResource().getUsername());
150 username.setReadOnly(true);
151 username.setWidth(WIDTH_FIELD);
152 username.addClickListener(new ClickListener () {
154 public void onClick(Widget sender) {
155 GSS.enableIESelection();
156 ((TextBox) sender).selectAll();
157 GSS.preventIESelection();
161 table.setWidget(0, 1, username);
162 passwordBox = new TextBox();
163 passwordBox.setText(app.getWebDAVPassword());
164 passwordBox.setReadOnly(true);
165 passwordBox.setWidth(WIDTH_FIELD);
166 passwordBox.addClickListener(new ClickListener () {
168 public void onClick(Widget sender) {
169 GSS.enableIESelection();
170 ((TextBox) sender).selectAll();
171 GSS.preventIESelection();
175 table.setWidget(1, 1, passwordBox);
177 TextBox tokenBox = new TextBox();
178 tokenBox.setText(app.getToken());
179 tokenBox.setReadOnly(true);
180 tokenBox.setWidth(WIDTH_FIELD);
181 tokenBox.addClickListener(new ClickListener () {
183 public void onClick(Widget sender) {
184 GSS.enableIESelection();
185 ((TextBox) sender).selectAll();
186 GSS.preventIESelection();
190 table.setWidget(2, 1, tokenBox);
192 table.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
193 table.getFlexCellFormatter().setStyleName(0, 1, "props-values");
194 table.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
195 table.getFlexCellFormatter().setStyleName(1, 1, "props-values");
196 table.getFlexCellFormatter().setStyleName(2, 0, "props-labels");
197 table.getFlexCellFormatter().setStyleName(2, 1, "props-values");
200 // Create the 'OK' button, along with a listener that hides the dialog
201 // when the button is clicked.
202 Button confirm = new Button("Close", new ClickListener() {
204 public void onClick(Widget sender) {
209 outer.setCellHorizontalAlignment(confirm, HasHorizontalAlignment.ALIGN_CENTER);
211 // Create the 'Reset password' button, along with a listener that hides the dialog
212 // when the button is clicked.
213 Button resetPassword = new Button("Reset Password", new ClickListener() {
215 public void onClick(Widget sender) {
216 ConfirmResetPasswordDialog dlg = new ConfirmResetPasswordDialog(images);
220 outer.add(resetPassword);
221 outer.setCellHorizontalAlignment(resetPassword, HasHorizontalAlignment.ALIGN_CENTER);
228 public boolean onKeyDownPreview(char key, int modifiers) {
229 // Use the popup's key preview hooks to close the dialog when either
230 // enter or escape is pressed.
232 case KeyboardListener.KEY_ENTER:
233 case KeyboardListener.KEY_ESCAPE:
242 * Generate an RPC request to reset WebDAV password.
244 * @param userId the Uri of the user whose password will be reset
246 private void resetPassword(String userUri) {
248 if (userUri == null || userUri.length() == 0) {
249 GSS.get().displayError("Empty user Uri!");
252 GWT.log("resetPassword(" + userUri + ")", null);
253 PostCommand cg = new PostCommand(userUri + "users?resetWebDAV", "", 200) {
256 public void onComplete() {
257 GSS.get().refreshWebDAVPassword();
258 passwordBox.setText(GSS.get().getWebDAVPassword());
262 public void onError(Throwable t) {
264 if(t instanceof RestException){
265 int statusCode = ((RestException)t).getHttpStatusCode();
266 if(statusCode == 405)
267 GSS.get().displayError("You don't have the necessary permissions");
268 else if(statusCode == 404)
269 GSS.get().displayError("Resource does not exist");
271 GSS.get().displayError("Unable to reset password:"+((RestException)t).getHttpStatusText());
274 GSS.get().displayError("System error resetting password:"+t.getMessage());
277 DeferredCommand.addCommand(cg);