Add navigation buttons to related list tables.
[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  * @author Ben Konrath <ben@bagu.org>
35  * 
36  */
37 public class AuthenticationPopup extends DecoratedPopupPanel {
38         private TextBox usernameTextBox = new TextBox();
39         private PasswordTextBox passwordTextBox = new PasswordTextBox();
40         private Label errorMessage = new Label("Username and/or password not correct.");
41         private Button okButton = new Button("OK");
42         FlexTable flexTable = new FlexTable();
43
44         private HandlerRegistration authOkHandlerRegistration;
45
46         public AuthenticationPopup() {
47                 // setup the layout
48                 flexTable.setCellSpacing(10);
49                 FlexCellFormatter cellFormatter = flexTable.getFlexCellFormatter();
50                 flexTable.setHTML(0, 0, "<b>Enter the PostgreSQL username and password.</b>");
51                 cellFormatter.setColSpan(0, 0, 2);
52                 cellFormatter.setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
53                 flexTable.setHTML(1, 0, "Username");
54                 flexTable.setWidget(1, 1, usernameTextBox);
55                 cellFormatter.setHorizontalAlignment(1, 1, HasHorizontalAlignment.ALIGN_RIGHT);
56                 flexTable.setHTML(2, 0, "Password");
57                 flexTable.setWidget(2, 1, passwordTextBox);
58                 cellFormatter.setHorizontalAlignment(2, 1, HasHorizontalAlignment.ALIGN_RIGHT);
59                 flexTable.setWidget(3, 1, okButton);
60                 cellFormatter.setHorizontalAlignment(3, 1, HasHorizontalAlignment.ALIGN_RIGHT);
61
62                 // setup the popup
63                 setGlassEnabled(true);
64                 setWidget(flexTable);
65
66                 // setup properties for the error message
67                 errorMessage.getElement().getStyle().setColor("Red");
68         }
69
70         public void setClickOkHandler(ClickHandler clickHandler) {
71                 authOkHandlerRegistration = okButton.addClickHandler(clickHandler);
72         }
73
74         public String getUsername() {
75                 return usernameTextBox.getValue();
76         }
77
78         public String getPassword() {
79                 return passwordTextBox.getValue();
80
81         }
82
83         @Override
84         public void clear() {
85                 if (authOkHandlerRegistration != null) {
86                         authOkHandlerRegistration.removeHandler();
87                         authOkHandlerRegistration = null;
88                 }
89                 usernameTextBox.setText("");
90                 passwordTextBox.setText("");
91                 setTextFieldsEnabled(true);
92                 clearError();
93         }
94
95         public void setTextFieldsEnabled(boolean enabled) {
96                 usernameTextBox.setEnabled(enabled);
97                 passwordTextBox.setEnabled(enabled);
98         }
99
100         public void setError() {
101                 flexTable.setWidget(4, 0, errorMessage);
102                 FlexCellFormatter cellFormatter = flexTable.getFlexCellFormatter();
103                 cellFormatter.setColSpan(4, 0, 2);
104                 cellFormatter.setHorizontalAlignment(4, 0, HasHorizontalAlignment.ALIGN_CENTER);
105         }
106
107         public void clearError() {
108                 if (flexTable.getRowCount() == 5)
109                         flexTable.removeRow(4);
110         }
111 }