Turn the authenication popup into a real Place.
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / client / ui / DocumentLoginViewImpl.java
1 /*
2  * Copyright (C) 2010, 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.Composite;
26 import com.google.gwt.user.client.ui.FlexTable;
27 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
28 import com.google.gwt.user.client.ui.Label;
29 import com.google.gwt.user.client.ui.PasswordTextBox;
30 import com.google.gwt.user.client.ui.TextBox;
31 import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
32
33 public class DocumentLoginViewImpl extends Composite implements DocumentLoginView {
34
35         //final private FlowPanel mainPanel = new FlowPanel();
36         private final TextBox usernameTextBox = new TextBox();
37         private final PasswordTextBox passwordTextBox = new PasswordTextBox();
38         private final Label errorMessage = new Label("Username and/or password not correct."); //TODO: Translate
39         private final Button loginButton = new Button("Login"); //TODO: Translate
40         private final Button cancelButton = new Button("Cancel"); //TODO: Translate
41         //TODO: ForgotPassword button.
42         FlexTable flexTable = new FlexTable();
43         private Presenter presenter;
44         
45         private HandlerRegistration authLoginButtonHandlerRegistration;
46         private HandlerRegistration authCancelButtonHandlerRegistration;
47
48         public DocumentLoginViewImpl() {
49                 initWidget(flexTable);
50                 
51                 flexTable.setCellSpacing(10);
52                 final FlexCellFormatter cellFormatter = flexTable.getFlexCellFormatter();
53                 flexTable.setHTML(0, 0, "<b>Enter the PostgreSQL username and password.</b>");
54                 cellFormatter.setColSpan(0, 0, 2);
55                 cellFormatter.setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
56                 flexTable.setHTML(1, 0, "Username");
57                 flexTable.setWidget(1, 1, usernameTextBox);
58                 cellFormatter.setHorizontalAlignment(1, 1, HasHorizontalAlignment.ALIGN_RIGHT);
59                 flexTable.setHTML(2, 0, "Password");
60                 flexTable.setWidget(2, 1, passwordTextBox);
61                 cellFormatter.setHorizontalAlignment(2, 1, HasHorizontalAlignment.ALIGN_RIGHT);
62                 flexTable.setWidget(3, 0, cancelButton);
63                 cellFormatter.setHorizontalAlignment(3, 0, HasHorizontalAlignment.ALIGN_RIGHT);
64                 flexTable.setWidget(3, 1, loginButton);
65                 cellFormatter.setHorizontalAlignment(3, 1, HasHorizontalAlignment.ALIGN_RIGHT);
66         }
67
68         @Override
69         public void setPresenter(final Presenter presenter) {
70                 this.presenter = presenter;
71         }
72         
73         @Override
74         public void setClickLoginHandler(final ClickHandler clickHandler) {
75                 authLoginButtonHandlerRegistration = loginButton.addClickHandler(clickHandler);
76         }
77         
78         @Override
79         public void setClickCancelHandler(final ClickHandler clickHandler) {
80                 authCancelButtonHandlerRegistration = cancelButton.addClickHandler(clickHandler);
81         }
82
83         @Override
84         public String getUsername() {
85                 return usernameTextBox.getValue();
86         }
87
88         @Override
89         public String getPassword() {
90                 return passwordTextBox.getValue();
91
92         }
93
94         @Override
95         public void clear() {
96                 if (authLoginButtonHandlerRegistration != null) {
97                         authLoginButtonHandlerRegistration.removeHandler();
98                         authLoginButtonHandlerRegistration = null;
99                 }
100
101                 if (authCancelButtonHandlerRegistration != null) {
102                         authCancelButtonHandlerRegistration.removeHandler();
103                         authCancelButtonHandlerRegistration = null;
104                 }
105
106                 usernameTextBox.setText("");
107                 passwordTextBox.setText("");
108                 setTextFieldsEnabled(true);
109                 clearError();
110         }
111
112         @Override
113         public void setTextFieldsEnabled(final boolean enabled) {
114                 usernameTextBox.setEnabled(enabled);
115                 passwordTextBox.setEnabled(enabled);
116         }
117         
118         @Override
119         public void setError() {
120                 flexTable.setWidget(4, 0, errorMessage);
121                 final FlexCellFormatter cellFormatter = flexTable.getFlexCellFormatter();
122                 cellFormatter.setColSpan(4, 0, 2);
123                 cellFormatter.setHorizontalAlignment(4, 0, HasHorizontalAlignment.ALIGN_CENTER);
124         }
125         
126         @Override
127         public void clearError() {
128                 if (flexTable.getRowCount() == 5) {
129                         flexTable.removeRow(4);
130                 }
131         }
132
133 }