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