Project

General

Profile

Download (5.57 KB) Statistics
| Branch: | Tag: | Revision:
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.component.TextFieldNFix;
26
import eu.etaxonomy.cdm.vaadin.ui.AbstractAuthenticatedUI;
27

    
28
@SpringView
29
public class AuthenticationView extends CustomComponent implements IAuthenticationComponent, ClickListener , View {
30

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

    
33

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

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

    
67
		authListener = new AuthenticationPresenter();
68

    
69
		loginBtn.addClickListener(this);
70
		loginBtn.setClickShortcut(KeyCode.ENTER, null);
71
	}
72

    
73
	@Override
74
	public void addListener(AuthenticationComponentListener listener) {
75
		this.authListener = listener;
76

    
77
	}
78

    
79
	@Override
80
	public void buttonClick(ClickEvent event) {
81
	    boolean isAuthenticated = false;
82
	    try {
83
	        isAuthenticated = authListener.login(Page.getCurrent().getLocation(),
84
	                VaadinServlet.getCurrent().getServletContext().getContextPath(),
85
	                userNameTF.getValue(),
86
	                passwordField.getValue());
87
	        if(isAuthenticated) {
88
	            // we are sure that since we are in the authentication view that the
89
	            // current ui should be of type AbstractAuthenticatedUI
90
	            AbstractAuthenticatedUI aaui = (AbstractAuthenticatedUI) UI.getCurrent();
91
	            UI.getCurrent().getNavigator().navigateTo(aaui.getFirstViewName());
92
	        }
93
	    } catch(BadCredentialsException e){
94
	        Notification.show("Bad credentials", Notification.Type.ERROR_MESSAGE);
95
	    }
96
	}
97

    
98
	@Override
99
	public void enter(ViewChangeEvent event) {
100
		// TODO Auto-generated method stub
101

    
102
	}
103

    
104
	@AutoGenerated
105
	private VerticalLayout buildMainLayout() {
106
		// common part: create layout
107
		mainLayout = new VerticalLayout();
108
		mainLayout.setImmediate(false);
109
		mainLayout.setWidth("100%");
110
		mainLayout.setHeight("100%");
111
		mainLayout.setMargin(false);
112

    
113
		// top-level component properties
114
		setWidth("100.0%");
115
		setHeight("100.0%");
116

    
117
		// loginPanel
118
		loginPanel = buildLoginPanel();
119
		mainLayout.addComponent(loginPanel);
120
		mainLayout.setExpandRatio(loginPanel, 1.0f);
121
		mainLayout.setComponentAlignment(loginPanel, new Alignment(48));
122

    
123
		return mainLayout;
124
	}
125

    
126
	@AutoGenerated
127
	private Panel buildLoginPanel() {
128
		// common part: create layout
129
		loginPanel = new Panel();
130
		loginPanel.setImmediate(false);
131
		loginPanel.setWidth("-1px");
132
		loginPanel.setHeight("-1px");
133

    
134
		// loginPanelLayout
135
		loginPanelLayout = buildLoginPanelLayout();
136
		loginPanel.setContent(loginPanelLayout);
137

    
138
		return loginPanel;
139
	}
140

    
141
	@AutoGenerated
142
	private VerticalLayout buildLoginPanelLayout() {
143
		// common part: create layout
144
		loginPanelLayout = new VerticalLayout();
145
		loginPanelLayout.setImmediate(false);
146
		loginPanelLayout.setWidth("-1px");
147
		loginPanelLayout.setHeight("-1px");
148
		loginPanelLayout.setMargin(true);
149
		loginPanelLayout.setSpacing(true);
150

    
151
		// userNameTF
152
		userNameTF = new TextFieldNFix();
153
		userNameTF.setCaption("User Name");
154
		userNameTF.setImmediate(false);
155
		userNameTF.setWidth("-1px");
156
		userNameTF.setHeight("-1px");
157
		userNameTF.setInvalidAllowed(false);
158
		userNameTF.setRequired(true);
159
		loginPanelLayout.addComponent(userNameTF);
160
		loginPanelLayout.setComponentAlignment(userNameTF, new Alignment(48));
161

    
162
		// passwordField
163
		passwordField = new PasswordField();
164
		passwordField.setCaption("Password");
165
		passwordField.setImmediate(false);
166
		passwordField.setWidth("-1px");
167
		passwordField.setHeight("-1px");
168
		passwordField.setInvalidAllowed(false);
169
		passwordField.setRequired(true);
170
		loginPanelLayout.addComponent(passwordField);
171
		loginPanelLayout
172
				.setComponentAlignment(passwordField, new Alignment(48));
173

    
174
		// loginBtn
175
		loginBtn = new Button();
176
		loginBtn.setCaption("Login");
177
		loginBtn.setImmediate(true);
178
		loginBtn.setWidth("-1px");
179
		loginBtn.setHeight("-1px");
180
		loginPanelLayout.addComponent(loginBtn);
181
		loginPanelLayout.setComponentAlignment(loginBtn, new Alignment(48));
182

    
183
		return loginPanelLayout;
184
	}
185
}
(3-3/10)