DetailsActivity: Check for authentication here too.
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / client / ui / AuthenticationPopup.java
1 /*
2  * Copyright (C) 2011 Openismus GmbH
3  *
4  * This file is part of GWT-Glom.
5  *
6  * GWT-Glom is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * GWT-Glom is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with GWT-Glom.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 package org.glom.web.client.ui;
21
22 import com.google.gwt.event.dom.client.ClickHandler;
23 import com.google.gwt.event.shared.HandlerRegistration;
24 import com.google.gwt.user.client.ui.Button;
25 import com.google.gwt.user.client.ui.DecoratedPopupPanel;
26 import com.google.gwt.user.client.ui.FlexTable;
27 import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
28 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
29 import com.google.gwt.user.client.ui.Label;
30 import com.google.gwt.user.client.ui.PasswordTextBox;
31 import com.google.gwt.user.client.ui.TextBox;
32
33 /**
34  *
35  */
36 public class AuthenticationPopup extends DecoratedPopupPanel {
37         private final TextBox usernameTextBox = new TextBox();
38         private final PasswordTextBox passwordTextBox = new PasswordTextBox();
39         private final Label errorMessage = new Label("Username and/or password not correct.");
40         private final Button okButton = new Button("OK");
41         FlexTable flexTable = new FlexTable();
42
43         private HandlerRegistration authOkHandlerRegistration;
44
45         public AuthenticationPopup() {
46                 // setup the layout
47                 flexTable.setCellSpacing(10);
48                 final FlexCellFormatter cellFormatter = flexTable.getFlexCellFormatter();
49                 flexTable.setHTML(0, 0, "<b>Enter the PostgreSQL username and password.</b>");
50                 cellFormatter.setColSpan(0, 0, 2);
51                 cellFormatter.setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
52                 flexTable.setHTML(1, 0, "Username");
53                 flexTable.setWidget(1, 1, usernameTextBox);
54                 cellFormatter.setHorizontalAlignment(1, 1, HasHorizontalAlignment.ALIGN_RIGHT);
55                 flexTable.setHTML(2, 0, "Password");
56                 flexTable.setWidget(2, 1, passwordTextBox);
57                 cellFormatter.setHorizontalAlignment(2, 1, HasHorizontalAlignment.ALIGN_RIGHT);
58                 flexTable.setWidget(3, 1, okButton);
59                 cellFormatter.setHorizontalAlignment(3, 1, HasHorizontalAlignment.ALIGN_RIGHT);
60
61                 // setup the popup
62                 setGlassEnabled(true);
63                 setWidget(flexTable);
64
65                 // setup properties for the error message
66                 errorMessage.getElement().getStyle().setColor("Red");
67         }
68
69         public void setClickOkHandler(final ClickHandler clickHandler) {
70                 authOkHandlerRegistration = okButton.addClickHandler(clickHandler);
71         }
72
73         public String getUsername() {
74                 return usernameTextBox.getValue();
75         }
76
77         public String getPassword() {
78                 return passwordTextBox.getValue();
79
80         }
81
82         @Override
83         public void clear() {
84                 if (authOkHandlerRegistration != null) {
85                         authOkHandlerRegistration.removeHandler();
86                         authOkHandlerRegistration = null;
87                 }
88                 usernameTextBox.setText("");
89                 passwordTextBox.setText("");
90                 setTextFieldsEnabled(true);
91                 clearError();
92         }
93
94         public void setTextFieldsEnabled(final boolean enabled) {
95                 usernameTextBox.setEnabled(enabled);
96                 passwordTextBox.setEnabled(enabled);
97         }
98
99         public void setError() {
100                 flexTable.setWidget(4, 0, errorMessage);
101                 final FlexCellFormatter cellFormatter = flexTable.getFlexCellFormatter();
102                 cellFormatter.setColSpan(4, 0, 2);
103                 cellFormatter.setHorizontalAlignment(4, 0, HasHorizontalAlignment.ALIGN_CENTER);
104         }
105
106         public void clearError() {
107                 if (flexTable.getRowCount() == 5) {
108                         flexTable.removeRow(4);
109                 }
110         }
111 }