1
|
package eu.etaxonomy.cdm.vaadin.view;
|
2
|
|
3
|
import org.springframework.security.authentication.BadCredentialsException;
|
4
|
|
5
|
import com.vaadin.annotations.AutoGenerated;
|
6
|
import com.vaadin.data.validator.StringLengthValidator;
|
7
|
import com.vaadin.event.ShortcutAction.KeyCode;
|
8
|
import com.vaadin.navigator.View;
|
9
|
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
|
10
|
import com.vaadin.server.Page;
|
11
|
import com.vaadin.server.VaadinServlet;
|
12
|
import com.vaadin.spring.annotation.SpringView;
|
13
|
import com.vaadin.ui.Alignment;
|
14
|
import com.vaadin.ui.Button;
|
15
|
import com.vaadin.ui.Button.ClickEvent;
|
16
|
import com.vaadin.ui.Button.ClickListener;
|
17
|
import com.vaadin.ui.CustomComponent;
|
18
|
import com.vaadin.ui.Notification;
|
19
|
import com.vaadin.ui.Panel;
|
20
|
import com.vaadin.ui.PasswordField;
|
21
|
import com.vaadin.ui.TextField;
|
22
|
import com.vaadin.ui.UI;
|
23
|
import com.vaadin.ui.VerticalLayout;
|
24
|
|
25
|
import eu.etaxonomy.cdm.vaadin.ui.AbstractAuthenticatedUI;
|
26
|
|
27
|
@SpringView
|
28
|
public class AuthenticationView extends CustomComponent implements IAuthenticationComponent, ClickListener , View {
|
29
|
|
30
|
/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */
|
31
|
|
32
|
|
33
|
/**
|
34
|
*
|
35
|
*/
|
36
|
private static final long serialVersionUID = 1L;
|
37
|
@AutoGenerated
|
38
|
private VerticalLayout mainLayout;
|
39
|
@AutoGenerated
|
40
|
private Panel loginPanel;
|
41
|
@AutoGenerated
|
42
|
private VerticalLayout loginPanelLayout;
|
43
|
@AutoGenerated
|
44
|
private Button loginBtn;
|
45
|
@AutoGenerated
|
46
|
private PasswordField passwordField;
|
47
|
@AutoGenerated
|
48
|
private TextField userNameTF;
|
49
|
private AuthenticationComponentListener authListener;
|
50
|
|
51
|
/**
|
52
|
* The constructor should first build the main layout, set the
|
53
|
* composition root and then do any custom initialization.
|
54
|
*
|
55
|
* The constructor will not be automatically regenerated by the
|
56
|
* visual editor.
|
57
|
*/
|
58
|
public AuthenticationView() {
|
59
|
buildMainLayout();
|
60
|
setStyleName("login");
|
61
|
setCompositionRoot(mainLayout);
|
62
|
userNameTF.addValidator(new StringLengthValidator("It must be 3-25 characters", 3, 25, false));
|
63
|
userNameTF.setNullRepresentation("");
|
64
|
userNameTF.focus();
|
65
|
|
66
|
authListener = new AuthenticationPresenter();
|
67
|
|
68
|
loginBtn.addClickListener(this);
|
69
|
loginBtn.setClickShortcut(KeyCode.ENTER, null);
|
70
|
}
|
71
|
|
72
|
@Override
|
73
|
public void addListener(AuthenticationComponentListener listener) {
|
74
|
this.authListener = listener;
|
75
|
|
76
|
}
|
77
|
|
78
|
@Override
|
79
|
public void buttonClick(ClickEvent event) {
|
80
|
boolean isAuthenticated = false;
|
81
|
try {
|
82
|
isAuthenticated = authListener.login(Page.getCurrent().getLocation(),
|
83
|
VaadinServlet.getCurrent().getServletContext().getContextPath(),
|
84
|
userNameTF.getValue(),
|
85
|
passwordField.getValue());
|
86
|
if(isAuthenticated) {
|
87
|
// we are sure that since we are in the authentication view that the
|
88
|
// current ui should be of type AbstractAuthenticatedUI
|
89
|
AbstractAuthenticatedUI aaui = (AbstractAuthenticatedUI) UI.getCurrent();
|
90
|
UI.getCurrent().getNavigator().navigateTo(aaui.getFirstViewName());
|
91
|
}
|
92
|
} catch(BadCredentialsException e){
|
93
|
Notification.show("Bad credentials", Notification.Type.ERROR_MESSAGE);
|
94
|
}
|
95
|
}
|
96
|
|
97
|
@Override
|
98
|
public void enter(ViewChangeEvent event) {
|
99
|
// TODO Auto-generated method stub
|
100
|
|
101
|
}
|
102
|
|
103
|
@AutoGenerated
|
104
|
private VerticalLayout buildMainLayout() {
|
105
|
// common part: create layout
|
106
|
mainLayout = new VerticalLayout();
|
107
|
mainLayout.setImmediate(false);
|
108
|
mainLayout.setWidth("100%");
|
109
|
mainLayout.setHeight("100%");
|
110
|
mainLayout.setMargin(false);
|
111
|
|
112
|
// top-level component properties
|
113
|
setWidth("100.0%");
|
114
|
setHeight("100.0%");
|
115
|
|
116
|
// loginPanel
|
117
|
loginPanel = buildLoginPanel();
|
118
|
mainLayout.addComponent(loginPanel);
|
119
|
mainLayout.setExpandRatio(loginPanel, 1.0f);
|
120
|
mainLayout.setComponentAlignment(loginPanel, new Alignment(48));
|
121
|
|
122
|
return mainLayout;
|
123
|
}
|
124
|
|
125
|
@AutoGenerated
|
126
|
private Panel buildLoginPanel() {
|
127
|
// common part: create layout
|
128
|
loginPanel = new Panel();
|
129
|
loginPanel.setImmediate(false);
|
130
|
loginPanel.setWidth("-1px");
|
131
|
loginPanel.setHeight("-1px");
|
132
|
|
133
|
// loginPanelLayout
|
134
|
loginPanelLayout = buildLoginPanelLayout();
|
135
|
loginPanel.setContent(loginPanelLayout);
|
136
|
|
137
|
return loginPanel;
|
138
|
}
|
139
|
|
140
|
@AutoGenerated
|
141
|
private VerticalLayout buildLoginPanelLayout() {
|
142
|
// common part: create layout
|
143
|
loginPanelLayout = new VerticalLayout();
|
144
|
loginPanelLayout.setImmediate(false);
|
145
|
loginPanelLayout.setWidth("-1px");
|
146
|
loginPanelLayout.setHeight("-1px");
|
147
|
loginPanelLayout.setMargin(true);
|
148
|
loginPanelLayout.setSpacing(true);
|
149
|
|
150
|
// userNameTF
|
151
|
userNameTF = new TextField();
|
152
|
userNameTF.setCaption("User Name");
|
153
|
userNameTF.setImmediate(false);
|
154
|
userNameTF.setWidth("-1px");
|
155
|
userNameTF.setHeight("-1px");
|
156
|
userNameTF.setInvalidAllowed(false);
|
157
|
userNameTF.setRequired(true);
|
158
|
loginPanelLayout.addComponent(userNameTF);
|
159
|
loginPanelLayout.setComponentAlignment(userNameTF, new Alignment(48));
|
160
|
|
161
|
// passwordField
|
162
|
passwordField = new PasswordField();
|
163
|
passwordField.setCaption("Password");
|
164
|
passwordField.setImmediate(false);
|
165
|
passwordField.setWidth("-1px");
|
166
|
passwordField.setHeight("-1px");
|
167
|
passwordField.setInvalidAllowed(false);
|
168
|
passwordField.setRequired(true);
|
169
|
loginPanelLayout.addComponent(passwordField);
|
170
|
loginPanelLayout
|
171
|
.setComponentAlignment(passwordField, new Alignment(48));
|
172
|
|
173
|
// loginBtn
|
174
|
loginBtn = new Button();
|
175
|
loginBtn.setCaption("Login");
|
176
|
loginBtn.setImmediate(true);
|
177
|
loginBtn.setWidth("-1px");
|
178
|
loginBtn.setHeight("-1px");
|
179
|
loginPanelLayout.addComponent(loginBtn);
|
180
|
loginPanelLayout.setComponentAlignment(loginBtn, new Alignment(48));
|
181
|
|
182
|
return loginPanelLayout;
|
183
|
}
|
184
|
}
|