2 * Copyright (C) 2010, 2011 Openismus GmbH
4 * This file is part of GWT-Glom.
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.
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
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/>.
20 package org.glom.web.client.ui;
22 import com.google.gwt.core.client.GWT;
23 import com.google.gwt.event.dom.client.ClickHandler;
24 import com.google.gwt.event.shared.HandlerRegistration;
25 import com.google.gwt.uibinder.client.UiBinder;
26 import com.google.gwt.uibinder.client.UiField;
27 import com.google.gwt.user.client.Window;
28 import com.google.gwt.user.client.ui.Button;
29 import com.google.gwt.user.client.ui.Composite;
30 import com.google.gwt.user.client.ui.FlexTable;
31 import com.google.gwt.user.client.ui.HTMLPanel;
32 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
33 import com.google.gwt.user.client.ui.Label;
34 import com.google.gwt.user.client.ui.PasswordTextBox;
35 import com.google.gwt.user.client.ui.TextBox;
36 import com.google.gwt.user.client.ui.VerticalPanel;
37 import com.google.gwt.user.client.ui.Widget;
38 import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
40 public class DocumentLoginViewImpl extends Composite implements DocumentLoginView {
42 // OnlineGlomConstants.java is generated in the target/ directory,
43 // from OnlineGlomConstants.properties
44 // by the gwt-maven-plugin's i18n (mvn:i18n) goal.
45 private final OnlineGlomConstants constants = GWT.create(OnlineGlomConstants.class);
47 interface DocumentLoginViewImplUiBinder extends UiBinder<Widget, DocumentLoginViewImpl> {
50 private static DocumentLoginViewImplUiBinder uiBinder = GWT.create(DocumentLoginViewImplUiBinder.class);
52 VerticalPanel documentLoginFields;
54 HTMLPanel documentLoginPanel;
56 //final private FlowPanel mainPanel = new FlowPanel();
57 private final TextBox usernameTextBox = new TextBox();
58 private final PasswordTextBox passwordTextBox = new PasswordTextBox();
59 private final Label errorMessage = new Label(constants.loginWrong());
60 private final Button loginButton = new Button(constants.login());
61 private final Button cancelButton = new Button(constants.cancel());
62 //TODO: ForgotPassword button.
63 FlexTable flexTable = new FlexTable();
65 //private Presenter presenter;
67 private HandlerRegistration authLoginButtonHandlerRegistration;
68 private HandlerRegistration authCancelButtonHandlerRegistration;
70 public DocumentLoginViewImpl() {
71 initWidget(uiBinder.createAndBindUi(this));
72 documentLoginPanel.getElement().setId("documentLoginPanel");
73 documentLoginFields.addStyleName("documentLoginFields");
75 documentLoginFields.add(flexTable);
77 //TODO: Use UIBinder to lay this out properly:
78 flexTable.setCellSpacing(10);
79 final FlexCellFormatter cellFormatter = flexTable.getFlexCellFormatter();
80 flexTable.setHTML(0, 0, "<b>" + constants.loginEnter() + "</b>");
81 cellFormatter.setColSpan(0, 0, 2);
82 cellFormatter.setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
83 flexTable.setHTML(1, 0, constants.loginUsername());
84 flexTable.setWidget(1, 1, usernameTextBox);
85 cellFormatter.setHorizontalAlignment(1, 1, HasHorizontalAlignment.ALIGN_RIGHT);
86 flexTable.setHTML(2, 0, constants.loginPassword());
87 flexTable.setWidget(2, 1, passwordTextBox);
88 cellFormatter.setHorizontalAlignment(2, 1, HasHorizontalAlignment.ALIGN_RIGHT);
89 flexTable.setWidget(3, 0, cancelButton);
90 cellFormatter.setHorizontalAlignment(3, 0, HasHorizontalAlignment.ALIGN_RIGHT);
91 flexTable.setWidget(3, 1, loginButton);
92 cellFormatter.setHorizontalAlignment(3, 1, HasHorizontalAlignment.ALIGN_RIGHT);
94 final String protocol = Window.Location.getProtocol();
95 if((protocol != null) && !protocol.equals("https:")) {
96 //Warn that login cannot work unless the login servlet is served via HTTPS.
97 //And actually, the entire site must be served via HTTPS,
98 //or we would violate the Same Origin Policy by mixing protocols,
99 //so this very page should have been delivered by HTTPS.
100 GWT.log("The login page arrived via http, rather than https. Refusing to log in.");
102 errorMessage.setText(constants.loginNeedsHttps());
104 loginButton.setEnabled(false);
109 public void setPresenter(final Presenter presenter) {
110 //this.presenter = presenter;
114 public void setClickLoginHandler(final ClickHandler clickHandler) {
115 authLoginButtonHandlerRegistration = loginButton.addClickHandler(clickHandler);
119 public void setClickCancelHandler(final ClickHandler clickHandler) {
120 authCancelButtonHandlerRegistration = cancelButton.addClickHandler(clickHandler);
124 public String getUsername() {
125 return usernameTextBox.getValue();
129 public String getPassword() {
130 return passwordTextBox.getValue();
135 public void clear() {
136 if (authLoginButtonHandlerRegistration != null) {
137 authLoginButtonHandlerRegistration.removeHandler();
138 authLoginButtonHandlerRegistration = null;
141 if (authCancelButtonHandlerRegistration != null) {
142 authCancelButtonHandlerRegistration.removeHandler();
143 authCancelButtonHandlerRegistration = null;
146 usernameTextBox.setText("");
147 passwordTextBox.setText("");
148 setTextFieldsEnabled(true);
153 public void setTextFieldsEnabled(final boolean enabled) {
154 usernameTextBox.setEnabled(enabled);
155 passwordTextBox.setEnabled(enabled);
159 public void setError() {
160 flexTable.setWidget(4, 0, errorMessage);
161 final FlexCellFormatter cellFormatter = flexTable.getFlexCellFormatter();
162 cellFormatter.setColSpan(4, 0, 2);
163 cellFormatter.setHorizontalAlignment(4, 0, HasHorizontalAlignment.ALIGN_LEFT);
164 errorMessage.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
168 public void clearError() {
169 if (flexTable.getRowCount() == 5) {
170 flexTable.removeRow(4);