// Path: src/main/java/de/dhbw/humbuch/event/LoginEvent.java
// public class LoginEvent {
// 	private final String message;
// 
// 	public LoginEvent(String message) {
// 		this.message = message;
// 	}
// 	
// 	public String getMessage() {
// 		return message;
// 	}
// }
// 
// Path: src/main/java/de/dhbw/humbuch/viewmodel/LoginViewModel.java
// public class LoginViewModel {
// 
// 	private final static Logger LOG = LoggerFactory.getLogger(MainUI.class);
// 	
// 	public interface IsLoggedIn extends State<Boolean> {}
// 
// 	public interface DoLogout extends ActionHandler {}
// 	public interface DoLogin extends ActionHandler {}
// 	
// 	private DAO<User> daoUser;
// 	private EventBus eventBus;
// 	private Properties properties;
// 
// 	@ProvidesState(IsLoggedIn.class)
// 	public final BasicState<Boolean> isLoggedIn = new BasicState<Boolean>(Boolean.class);
// 	
// 	/**
// 	 * Constructor
// 	 * 
// 	 * @param daoUser
// 	 * @param properties
// 	 * @param eventBus
// 	 */
// 	@Inject
// 	public LoginViewModel(DAO<User> daoUser, Properties properties, EventBus eventBus) {
// 		this.properties = properties;
// 		this.eventBus = eventBus;
// 		this.daoUser = daoUser;
// 		updateLoginStatus();
// 	}
// 	
// 	private void updateLoginStatus() {
// 		isLoggedIn.set(properties.currentUser.get() != null);
// 	}
// 
// 	/**
// 	 * Validates {@code username} and {@code password}. Fires an event if something is wrong.<br>
// 	 * Sets the logged in {@link User} in the corresponding {@link Properties} state
// 	 * 
// 	 * @param username
// 	 * @param password
// 	 */
// 	@HandlesAction(DoLogin.class)
// 	public void doLogin(String username, String password) {
// 		properties.currentUser.set(null);
// 		updateLoginStatus();
// 		try {
// 			if (username.equals("") || password.equals("")) {
// 				eventBus.post(new LoginEvent("Bitte geben Sie einen Nutzernamen und Passwort an."));
// 				return;
// 			} else {
// 				List<User> user = (List<User>) daoUser.findAllWithCriteria(Restrictions.eq("username", username));
// 				if(!user.isEmpty()) {
// 						if(PasswordHash.validatePassword(password, user.get(0).getPassword())) {
// 							properties.currentUser.set(user.get(0));
// 							updateLoginStatus();
// 							return;
// 						}
// 				}
// 				eventBus.post(new LoginEvent("Username oder Passwort stimmen nicht überein."));
// 			}
// 		} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
// 			eventBus.post(new LoginEvent("Fehler bei Login. Bitte kontaktieren Sie einen Entwickler."));
// 			LOG.warn(e.getMessage());
// 			return;
// 		}
// 	}
// 
// 	@HandlesAction(DoLogout.class)
// 	public void doLogout(Object obj) {
// 		properties.currentUser.set(null);
// 		updateLoginStatus();
// 	}
// 
// }
// 
// Path: src/main/java/de/dhbw/humbuch/viewmodel/LoginViewModel.java
// public interface DoLogin extends ActionHandler {}
// 
// Path: src/main/java/de/dhbw/humbuch/viewmodel/LoginViewModel.java
// public interface IsLoggedIn extends State<Boolean> {}


import java.util.NoSuchElementException;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import com.google.inject.Inject;
import com.vaadin.event.ShortcutAction.KeyCode;
import com.vaadin.event.ShortcutListener;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import de.davherrmann.mvvm.BasicState;
import de.davherrmann.mvvm.StateChangeListener;
import de.davherrmann.mvvm.ViewModelComposer;
import de.davherrmann.mvvm.annotations.BindAction;
import de.davherrmann.mvvm.annotations.BindState;
import de.dhbw.humbuch.event.LoginEvent;
import de.dhbw.humbuch.viewmodel.LoginViewModel;
import de.dhbw.humbuch.viewmodel.LoginViewModel.DoLogin;
import de.dhbw.humbuch.viewmodel.LoginViewModel.IsLoggedIn;
package de.dhbw.humbuch.view;




/**
 * Provides the UI for the login and displays error messages, if the user uses
 * wrong credentials
 * 
 * @author Johannes Idelhauser
 */
public class LoginView extends VerticalLayout implements View {
	private static final long serialVersionUID = 5187769743375079627L;

	private VerticalLayout loginLayout;
	private CssLayout loginPanel;

	private TextField username = new TextField("Username");
	private PasswordField password = new PasswordField("Passwort");
	@BindAction(value = DoLogin.class, source = { "username", "password" })
	private Button btnLogin = new Button("Login");

// Path: src/main/java/de/dhbw/humbuch/event/LoginEvent.java
// public class LoginEvent {
// 	private final String message;
// 
// 	public LoginEvent(String message) {
// 		this.message = message;
// 	}
// 	
// 	public String getMessage() {
// 		return message;
// 	}
// }
// 
// Path: src/main/java/de/dhbw/humbuch/viewmodel/LoginViewModel.java
// public class LoginViewModel {
// 
// 	private final static Logger LOG = LoggerFactory.getLogger(MainUI.class);
// 	
// 	public interface IsLoggedIn extends State<Boolean> {}
// 
// 	public interface DoLogout extends ActionHandler {}
// 	public interface DoLogin extends ActionHandler {}
// 	
// 	private DAO<User> daoUser;
// 	private EventBus eventBus;
// 	private Properties properties;
// 
// 	@ProvidesState(IsLoggedIn.class)
// 	public final BasicState<Boolean> isLoggedIn = new BasicState<Boolean>(Boolean.class);
// 	
// 	/**
// 	 * Constructor
// 	 * 
// 	 * @param daoUser
// 	 * @param properties
// 	 * @param eventBus
// 	 */
// 	@Inject
// 	public LoginViewModel(DAO<User> daoUser, Properties properties, EventBus eventBus) {
// 		this.properties = properties;
// 		this.eventBus = eventBus;
// 		this.daoUser = daoUser;
// 		updateLoginStatus();
// 	}
// 	
// 	private void updateLoginStatus() {
// 		isLoggedIn.set(properties.currentUser.get() != null);
// 	}
// 
// 	/**
// 	 * Validates {@code username} and {@code password}. Fires an event if something is wrong.<br>
// 	 * Sets the logged in {@link User} in the corresponding {@link Properties} state
// 	 * 
// 	 * @param username
// 	 * @param password
// 	 */
// 	@HandlesAction(DoLogin.class)
// 	public void doLogin(String username, String password) {
// 		properties.currentUser.set(null);
// 		updateLoginStatus();
// 		try {
// 			if (username.equals("") || password.equals("")) {
// 				eventBus.post(new LoginEvent("Bitte geben Sie einen Nutzernamen und Passwort an."));
// 				return;
// 			} else {
// 				List<User> user = (List<User>) daoUser.findAllWithCriteria(Restrictions.eq("username", username));
// 				if(!user.isEmpty()) {
// 						if(PasswordHash.validatePassword(password, user.get(0).getPassword())) {
// 							properties.currentUser.set(user.get(0));
// 							updateLoginStatus();
// 							return;
// 						}
// 				}
// 				eventBus.post(new LoginEvent("Username oder Passwort stimmen nicht überein."));
// 			}
// 		} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
// 			eventBus.post(new LoginEvent("Fehler bei Login. Bitte kontaktieren Sie einen Entwickler."));
// 			LOG.warn(e.getMessage());
// 			return;
// 		}
// 	}
// 
// 	@HandlesAction(DoLogout.class)
// 	public void doLogout(Object obj) {
// 		properties.currentUser.set(null);
// 		updateLoginStatus();
// 	}
// 
// }
// 
// Path: src/main/java/de/dhbw/humbuch/viewmodel/LoginViewModel.java
// public interface DoLogin extends ActionHandler {}
// 
// Path: src/main/java/de/dhbw/humbuch/viewmodel/LoginViewModel.java
// public interface IsLoggedIn extends State<Boolean> {}


// Path: src/main/java/de/dhbw/humbuch/view/LoginView.java
import java.util.NoSuchElementException;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import com.google.inject.Inject;
import com.vaadin.event.ShortcutAction.KeyCode;
import com.vaadin.event.ShortcutListener;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import de.davherrmann.mvvm.BasicState;
import de.davherrmann.mvvm.StateChangeListener;
import de.davherrmann.mvvm.ViewModelComposer;
import de.davherrmann.mvvm.annotations.BindAction;
import de.davherrmann.mvvm.annotations.BindState;
import de.dhbw.humbuch.event.LoginEvent;
import de.dhbw.humbuch.viewmodel.LoginViewModel;
import de.dhbw.humbuch.viewmodel.LoginViewModel.DoLogin;
import de.dhbw.humbuch.viewmodel.LoginViewModel.IsLoggedIn;

package de.dhbw.humbuch.view;




/**
 * Provides the UI for the login and displays error messages, if the user uses
 * wrong credentials
 * 
 * @author Johannes Idelhauser
 */
public class LoginView extends VerticalLayout implements View {
	private static final long serialVersionUID = 5187769743375079627L;

	private VerticalLayout loginLayout;
	private CssLayout loginPanel;

	private TextField username = new TextField("Username");
	private PasswordField password = new PasswordField("Passwort");
	@BindAction(value = DoLogin.class, source = { "username", "password" })
	private Button btnLogin = new Button("Login");

	@BindState(IsLoggedIn.class)