Project

General

Profile

Download (5.46 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.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

    
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
		authListener = new AuthenticationPresenter();
66

    
67
		loginBtn.addClickListener(this);
68
		loginBtn.setClickShortcut(KeyCode.ENTER, null);
69
	}
70

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

    
75
	}
76

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

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

    
100
	}
101

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

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

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

    
121
		return mainLayout;
122
	}
123

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

    
132
		// loginPanelLayout
133
		loginPanelLayout = buildLoginPanelLayout();
134
		loginPanel.setContent(loginPanelLayout);
135

    
136
		return loginPanel;
137
	}
138

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

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

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

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

    
181
		return loginPanelLayout;
182
	}
183
}
(4-4/6)