Replace deprecated DOM.setElementAttribute().
[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.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;
39
40 public class DocumentLoginViewImpl extends Composite implements DocumentLoginView {
41
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);
46
47         interface DocumentLoginViewImplUiBinder extends UiBinder<Widget, DocumentLoginViewImpl> {
48         }
49
50         private static DocumentLoginViewImplUiBinder uiBinder = GWT.create(DocumentLoginViewImplUiBinder.class);
51         @UiField
52         VerticalPanel documentLoginFields;
53         @UiField
54         HTMLPanel documentLoginPanel;
55
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();
64
65         //private Presenter presenter;
66         
67         private HandlerRegistration authLoginButtonHandlerRegistration;
68         private HandlerRegistration authCancelButtonHandlerRegistration;
69
70         public DocumentLoginViewImpl() {
71                 initWidget(uiBinder.createAndBindUi(this));
72                 documentLoginPanel.getElement().setId("documentLoginPanel");
73                 documentLoginFields.addStyleName("documentLoginFields");
74                 
75                 documentLoginFields.add(flexTable);
76                 
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);
93
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.");
101
102                         errorMessage.setText(constants.loginNeedsHttps());
103                         setError();
104                         loginButton.setEnabled(false);
105                 }
106         }
107
108         @Override
109         public void setPresenter(final Presenter presenter) {
110                 //this.presenter = presenter;
111         }
112         
113         @Override
114         public void setClickLoginHandler(final ClickHandler clickHandler) {
115                 authLoginButtonHandlerRegistration = loginButton.addClickHandler(clickHandler);
116         }
117         
118         @Override
119         public void setClickCancelHandler(final ClickHandler clickHandler) {
120                 authCancelButtonHandlerRegistration = cancelButton.addClickHandler(clickHandler);
121         }
122
123         @Override
124         public String getUsername() {
125                 return usernameTextBox.getValue();
126         }
127
128         @Override
129         public String getPassword() {
130                 return passwordTextBox.getValue();
131
132         }
133
134         @Override
135         public void clear() {
136                 if (authLoginButtonHandlerRegistration != null) {
137                         authLoginButtonHandlerRegistration.removeHandler();
138                         authLoginButtonHandlerRegistration = null;
139                 }
140
141                 if (authCancelButtonHandlerRegistration != null) {
142                         authCancelButtonHandlerRegistration.removeHandler();
143                         authCancelButtonHandlerRegistration = null;
144                 }
145
146                 usernameTextBox.setText("");
147                 passwordTextBox.setText("");
148                 setTextFieldsEnabled(true);
149                 clearError();
150         }
151
152         @Override
153         public void setTextFieldsEnabled(final boolean enabled) {
154                 usernameTextBox.setEnabled(enabled);
155                 passwordTextBox.setEnabled(enabled);
156         }
157         
158         @Override
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);
165         }
166         
167         @Override
168         public void clearError() {
169                 if (flexTable.getRowCount() == 5) {
170                         flexTable.removeRow(4);
171                 }
172         }
173
174 }