Project

General

Profile

Download (5.34 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.server.VaadinSession;
12
import com.vaadin.ui.Alignment;
13
import com.vaadin.ui.Button;
14
import com.vaadin.ui.Button.ClickEvent;
15
import com.vaadin.ui.Button.ClickListener;
16
import com.vaadin.ui.CustomComponent;
17
import com.vaadin.ui.Notification;
18
import com.vaadin.ui.Panel;
19
import com.vaadin.ui.PasswordField;
20
import com.vaadin.ui.TextField;
21
import com.vaadin.ui.UI;
22
import com.vaadin.ui.VerticalLayout;
23

    
24
import eu.etaxonomy.cdm.vaadin.ui.AbstractAuthenticatedUI;
25

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

    
28
	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */
29
	
30
	
31
	/**
32
	 * 
33
	 */
34
	private static final long serialVersionUID = 1L;
35
	@AutoGenerated
36
	private VerticalLayout mainLayout;
37
	@AutoGenerated
38
	private Panel loginPanel;
39
	@AutoGenerated
40
	private VerticalLayout loginPanelLayout;
41
	@AutoGenerated
42
	private Button loginBtn;
43
	@AutoGenerated
44
	private PasswordField passwordField;
45
	@AutoGenerated
46
	private TextField userNameTF;
47
	private AuthenticationComponentListener authListener;
48
	
49
	/**
50
	 * The constructor should first build the main layout, set the
51
	 * composition root and then do any custom initialization.
52
	 *
53
	 * The constructor will not be automatically regenerated by the
54
	 * visual editor.
55
	 */
56
	public AuthenticationView() {
57
		buildMainLayout();
58
		setCompositionRoot(mainLayout);
59
		userNameTF.addValidator(new StringLengthValidator("It must be 3-25 characters", 3, 25, false));
60
		userNameTF.setNullRepresentation("");
61
		userNameTF.focus();
62
		
63
		loginBtn.addClickListener(this);
64
		loginBtn.setClickShortcut(KeyCode.ENTER, null);
65
	}
66

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

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

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

    
96
	@AutoGenerated
97
	private VerticalLayout buildMainLayout() {
98
		// common part: create layout
99
		mainLayout = new VerticalLayout();
100
		mainLayout.setImmediate(false);
101
		mainLayout.setWidth("100%");
102
		mainLayout.setHeight("100%");
103
		mainLayout.setMargin(false);
104
		
105
		// top-level component properties
106
		setWidth("100.0%");
107
		setHeight("100.0%");
108
		
109
		// loginPanel
110
		loginPanel = buildLoginPanel();
111
		mainLayout.addComponent(loginPanel);
112
		mainLayout.setExpandRatio(loginPanel, 1.0f);
113
		mainLayout.setComponentAlignment(loginPanel, new Alignment(48));
114
		
115
		return mainLayout;
116
	}
117

    
118
	@AutoGenerated
119
	private Panel buildLoginPanel() {
120
		// common part: create layout
121
		loginPanel = new Panel();
122
		loginPanel.setImmediate(false);
123
		loginPanel.setWidth("-1px");
124
		loginPanel.setHeight("-1px");
125
		
126
		// loginPanelLayout
127
		loginPanelLayout = buildLoginPanelLayout();
128
		loginPanel.setContent(loginPanelLayout);
129
		
130
		return loginPanel;
131
	}
132

    
133
	@AutoGenerated
134
	private VerticalLayout buildLoginPanelLayout() {
135
		// common part: create layout
136
		loginPanelLayout = new VerticalLayout();
137
		loginPanelLayout.setImmediate(false);
138
		loginPanelLayout.setWidth("-1px");
139
		loginPanelLayout.setHeight("-1px");
140
		loginPanelLayout.setMargin(true);
141
		loginPanelLayout.setSpacing(true);
142
		
143
		// userNameTF
144
		userNameTF = new TextField();
145
		userNameTF.setCaption("User Name");
146
		userNameTF.setImmediate(false);
147
		userNameTF.setWidth("-1px");
148
		userNameTF.setHeight("-1px");
149
		userNameTF.setInvalidAllowed(false);
150
		userNameTF.setRequired(true);
151
		loginPanelLayout.addComponent(userNameTF);
152
		loginPanelLayout.setComponentAlignment(userNameTF, new Alignment(48));
153
		
154
		// passwordField
155
		passwordField = new PasswordField();
156
		passwordField.setCaption("Password");
157
		passwordField.setImmediate(false);
158
		passwordField.setWidth("-1px");
159
		passwordField.setHeight("-1px");
160
		passwordField.setInvalidAllowed(false);
161
		passwordField.setRequired(true);
162
		loginPanelLayout.addComponent(passwordField);
163
		loginPanelLayout
164
				.setComponentAlignment(passwordField, new Alignment(48));
165
		
166
		// loginBtn
167
		loginBtn = new Button();
168
		loginBtn.setCaption("Login");
169
		loginBtn.setImmediate(true);
170
		loginBtn.setWidth("-1px");
171
		loginBtn.setHeight("-1px");
172
		loginPanelLayout.addComponent(loginBtn);
173
		loginPanelLayout.setComponentAlignment(loginBtn, new Alignment(48));
174
		
175
		return loginPanelLayout;
176
	}
177
}
(1-1/2)