Project

General

Profile

Download (5.36 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.cdm.vaadin.view;
2

    
3
import org.springframework.security.authentication.BadCredentialsException;
4
import org.springframework.security.core.Authentication;
5

    
6
import com.vaadin.annotations.AutoGenerated;
7
import com.vaadin.data.validator.StringLengthValidator;
8
import com.vaadin.event.ShortcutAction.KeyCode;
9
import com.vaadin.navigator.View;
10
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
11
import com.vaadin.ui.Alignment;
12
import com.vaadin.ui.Button;
13
import com.vaadin.ui.Button.ClickEvent;
14
import com.vaadin.ui.Button.ClickListener;
15
import com.vaadin.ui.CustomComponent;
16
import com.vaadin.ui.Notification;
17
import com.vaadin.ui.Panel;
18
import com.vaadin.ui.PasswordField;
19
import com.vaadin.ui.TextField;
20
import com.vaadin.ui.UI;
21
import com.vaadin.ui.VerticalLayout;
22

    
23
import eu.etaxonomy.cdm.vaadin.ui.AbstractAuthenticatedUI;
24
import eu.etaxonomy.cdm.vaadin.util.CdmVaadinSessionUtilities;
25

    
26

    
27
public class AuthenticationView extends CustomComponent implements IAuthenticationComponent, ClickListener , View {
28

    
29
	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */
30

    
31

    
32
	/**
33
	 *
34
	 */
35
	private static final long serialVersionUID = 1L;
36
	@AutoGenerated
37
	private VerticalLayout mainLayout;
38
	@AutoGenerated
39
	private Panel loginPanel;
40
	@AutoGenerated
41
	private VerticalLayout loginPanelLayout;
42
	@AutoGenerated
43
	private Button loginBtn;
44
	@AutoGenerated
45
	private PasswordField passwordField;
46
	@AutoGenerated
47
	private TextField userNameTF;
48
	private AuthenticationComponentListener authListener;
49

    
50
	/**
51
	 * The constructor should first build the main layout, set the
52
	 * composition root and then do any custom initialization.
53
	 *
54
	 * The constructor will not be automatically regenerated by the
55
	 * visual editor.
56
	 */
57
	public AuthenticationView() {
58
		buildMainLayout();
59
		setStyleName("login");
60
		setCompositionRoot(mainLayout);
61
		userNameTF.addValidator(new StringLengthValidator("It must be 3-25 characters", 3, 25, false));
62
		userNameTF.setNullRepresentation("");
63
		userNameTF.focus();
64

    
65
		loginBtn.addClickListener(this);
66
		loginBtn.setClickShortcut(KeyCode.ENTER, null);
67
	}
68

    
69
	@Override
70
	public void addListener(AuthenticationComponentListener listener) {
71
		this.authListener = listener;
72

    
73
	}
74

    
75
	@Override
76
	public void buttonClick(ClickEvent event) {
77
		Authentication authentication = null;
78
		try {
79
			authentication = authListener.login(userNameTF.getValue(), passwordField.getValue());
80
		} catch(BadCredentialsException e){
81
			Notification.show("Bad credentials", Notification.Type.ERROR_MESSAGE);
82
		}
83
		if(authentication != null && authentication.isAuthenticated()) {
84
			CdmVaadinSessionUtilities.setCurrentAttribute("authentication", authentication);
85
			// we are sure that since we are in the authentication view that the
86
			// current ui should be of type AbstractAuthenticatedUI
87
			AbstractAuthenticatedUI aaui = (AbstractAuthenticatedUI) UI.getCurrent();
88
			UI.getCurrent().getNavigator().navigateTo(aaui.getFirstViewName());
89
		}
90
	}
91

    
92
	@Override
93
	public void enter(ViewChangeEvent event) {
94
		// TODO Auto-generated method stub
95

    
96
	}
97

    
98
	@AutoGenerated
99
	private VerticalLayout buildMainLayout() {
100
		// common part: create layout
101
		mainLayout = new VerticalLayout();
102
		mainLayout.setImmediate(false);
103
		mainLayout.setWidth("100%");
104
		mainLayout.setHeight("100%");
105
		mainLayout.setMargin(false);
106

    
107
		// top-level component properties
108
		setWidth("100.0%");
109
		setHeight("100.0%");
110

    
111
		// loginPanel
112
		loginPanel = buildLoginPanel();
113
		mainLayout.addComponent(loginPanel);
114
		mainLayout.setExpandRatio(loginPanel, 1.0f);
115
		mainLayout.setComponentAlignment(loginPanel, new Alignment(48));
116

    
117
		return mainLayout;
118
	}
119

    
120
	@AutoGenerated
121
	private Panel buildLoginPanel() {
122
		// common part: create layout
123
		loginPanel = new Panel();
124
		loginPanel.setImmediate(false);
125
		loginPanel.setWidth("-1px");
126
		loginPanel.setHeight("-1px");
127

    
128
		// loginPanelLayout
129
		loginPanelLayout = buildLoginPanelLayout();
130
		loginPanel.setContent(loginPanelLayout);
131

    
132
		return loginPanel;
133
	}
134

    
135
	@AutoGenerated
136
	private VerticalLayout buildLoginPanelLayout() {
137
		// common part: create layout
138
		loginPanelLayout = new VerticalLayout();
139
		loginPanelLayout.setImmediate(false);
140
		loginPanelLayout.setWidth("-1px");
141
		loginPanelLayout.setHeight("-1px");
142
		loginPanelLayout.setMargin(true);
143
		loginPanelLayout.setSpacing(true);
144

    
145
		// userNameTF
146
		userNameTF = new TextField();
147
		userNameTF.setCaption("User Name");
148
		userNameTF.setImmediate(false);
149
		userNameTF.setWidth("-1px");
150
		userNameTF.setHeight("-1px");
151
		userNameTF.setInvalidAllowed(false);
152
		userNameTF.setRequired(true);
153
		loginPanelLayout.addComponent(userNameTF);
154
		loginPanelLayout.setComponentAlignment(userNameTF, new Alignment(48));
155

    
156
		// passwordField
157
		passwordField = new PasswordField();
158
		passwordField.setCaption("Password");
159
		passwordField.setImmediate(false);
160
		passwordField.setWidth("-1px");
161
		passwordField.setHeight("-1px");
162
		passwordField.setInvalidAllowed(false);
163
		passwordField.setRequired(true);
164
		loginPanelLayout.addComponent(passwordField);
165
		loginPanelLayout
166
				.setComponentAlignment(passwordField, new Alignment(48));
167

    
168
		// loginBtn
169
		loginBtn = new Button();
170
		loginBtn.setCaption("Login");
171
		loginBtn.setImmediate(true);
172
		loginBtn.setWidth("-1px");
173
		loginBtn.setHeight("-1px");
174
		loginPanelLayout.addComponent(loginBtn);
175
		loginPanelLayout.setComponentAlignment(loginBtn, new Alignment(48));
176

    
177
		return loginPanelLayout;
178
	}
179
}
(1-1/7)