Project

General

Profile

Download (5.62 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.common.URI;
26
import eu.etaxonomy.cdm.vaadin.component.TextFieldNFix;
27
import eu.etaxonomy.cdm.vaadin.ui.AbstractAuthenticatedUI;
28

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

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

    
34

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

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

    
68
		authListener = new AuthenticationPresenter();
69

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

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

    
78
	}
79

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

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

    
103
	}
104

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

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

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

    
124
		return mainLayout;
125
	}
126

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

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

    
139
		return loginPanel;
140
	}
141

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

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

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

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

    
184
		return loginPanelLayout;
185
	}
186
}
(4-4/11)