1ccc7b10dfb7563c0d6eb3db0aa3be3c5e199c86
[pithos] / gss / src / gr / ebs / gss / client / CredentialsDialog.java
1 /*
2  * Copyright 2008, 2009 Electronic Business Systems Ltd.
3  *
4  * This file is part of GSS.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19 package gr.ebs.gss.client;
20
21 import com.google.gwt.core.client.GWT;
22 import com.google.gwt.user.client.ui.Button;
23 import com.google.gwt.user.client.ui.ClickListener;
24 import com.google.gwt.user.client.ui.DialogBox;
25 import com.google.gwt.user.client.ui.FlexTable;
26 import com.google.gwt.user.client.ui.HTML;
27 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
28 import com.google.gwt.user.client.ui.KeyboardListener;
29 import com.google.gwt.user.client.ui.TextBox;
30 import com.google.gwt.user.client.ui.VerticalPanel;
31 import com.google.gwt.user.client.ui.Widget;
32
33
34 /**
35  * A dialog box that displays the user credentials for use in other client
36  * applications, such as WebDAV clients.
37  *
38  * @author kman
39  */
40 public class CredentialsDialog extends DialogBox {
41
42         private final String WIDTH_FIELD = "35em";
43         private final String WIDTH_TEXT = "42em";
44
45         /**
46          * The widget constructor.
47          */
48         public CredentialsDialog() {
49                 // Set the dialog's caption.
50                 setText("User Credentials");
51                 setAnimationEnabled(true);
52                 // Create a VerticalPanel to contain the 'about' label and the 'OK'
53                 // button.
54                 VerticalPanel outer = new VerticalPanel();
55                 String token = GSS.get().getToken();
56                 if(token == null)
57                         token = "";
58                 int contextIndex = GWT.getModuleBaseURL().lastIndexOf("gss/");
59                 String webdavUrl = GWT.getModuleBaseURL().substring(0, contextIndex) + "webdav";
60                 Configuration conf = (Configuration) GWT.create(Configuration.class);
61                 String service = conf.serviceName();
62                 // Create the text and set a style name so we can style it with CSS.
63                 HTML text = new HTML("<p>These are the user credentials that are required " +
64                                 "for interacting with " + service + ". You can copy and paste them in the WebDAV " +
65                                 "client as username/password, in order to use " + service + " through the WebDAV " +
66                                 "interface, at:<br/> " + webdavUrl + "</p>");
67                 text.setStyleName("gss-AboutText");
68                 text.setWidth(WIDTH_TEXT);
69                 outer.add(text);
70                 FlexTable table = new FlexTable();
71                 table.setText(0, 0, "Username");
72                 table.setText(1, 0, "Token");
73                 TextBox username = new TextBox();
74                 username.setText(GSS.get().getCurrentUserResource().getUsername());
75                 username.setReadOnly(true);
76                 username.setWidth(WIDTH_FIELD);
77                 username.addClickListener(new ClickListener () {
78
79                         public void onClick(Widget sender) {
80                                 GSS.enableIESelection();
81                                 ((TextBox) sender).selectAll();
82                                 GSS.preventIESelection();
83                         }
84
85                 });
86                 table.setWidget(0, 1, username);
87                 TextBox tokenBox = new TextBox();
88                 tokenBox.setText(token);
89                 tokenBox.setReadOnly(true);
90                 tokenBox.setWidth(WIDTH_FIELD);
91                 tokenBox.addClickListener(new ClickListener () {
92
93                         public void onClick(Widget sender) {
94                                 GSS.enableIESelection();
95                                 ((TextBox) sender).selectAll();
96                                 GSS.preventIESelection();
97                         }
98
99                 });
100                 table.setWidget(1, 1, tokenBox);
101                 table.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
102                 table.getFlexCellFormatter().setStyleName(0, 1, "props-values");
103                 table.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
104                 table.getFlexCellFormatter().setStyleName(1, 1, "props-values");
105                 outer.add(table);
106
107                 // Create the 'OK' button, along with a listener that hides the dialog
108                 // when the button is clicked.
109                 Button confirm = new Button("Close", new ClickListener() {
110
111                         public void onClick(Widget sender) {
112                                 hide();
113                         }
114                 });
115                 outer.add(confirm);
116                 outer.setCellHorizontalAlignment(confirm, HasHorizontalAlignment.ALIGN_CENTER);
117                 outer.setSpacing(8);
118                 setWidget(outer);
119         }
120
121         @Override
122         public boolean onKeyDownPreview(char key, int modifiers) {
123                 // Use the popup's key preview hooks to close the dialog when either
124                 // enter or escape is pressed.
125                 switch (key) {
126                         case KeyboardListener.KEY_ENTER:
127                         case KeyboardListener.KEY_ESCAPE:
128                                 hide();
129                                 break;
130                 }
131                 return true;
132         }
133
134 }