dhbw.clippinggorilla.userinterface.views.MenuBar.java Source code

Java tutorial

Introduction

Here is the source code for dhbw.clippinggorilla.userinterface.views.MenuBar.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dhbw.clippinggorilla.userinterface.views;

import com.vaadin.event.ShortcutAction.KeyCode;
import com.vaadin.icons.VaadinIcons;
import com.vaadin.server.AbstractErrorMessage;
import com.vaadin.server.ErrorMessage;
import com.vaadin.server.FileResource;
import com.vaadin.server.UserError;
import com.vaadin.server.VaadinSession;
import com.vaadin.shared.Registration;
import com.vaadin.ui.Button;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.Image;
import com.vaadin.ui.Label;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI;
import com.vaadin.ui.themes.ValoTheme;
import com.vaadin.ui.MenuBar.MenuItem;
import com.vaadin.ui.Notification;
import dhbw.clippinggorilla.ClippingGorillaUI;
import dhbw.clippinggorilla.objects.user.User;
import dhbw.clippinggorilla.objects.user.UserUtils;
import dhbw.clippinggorilla.userinterface.windows.ActivateWindow;
import dhbw.clippinggorilla.userinterface.windows.ManageSourcesWindow;
import dhbw.clippinggorilla.userinterface.windows.PasswordRecoveryWindow;
import dhbw.clippinggorilla.userinterface.windows.PreferencesWindow;
import dhbw.clippinggorilla.userinterface.windows.RegisterWindow;
import dhbw.clippinggorilla.utilities.language.Language;
import dhbw.clippinggorilla.utilities.language.Word;
import dhbw.clippinggorilla.utilities.log.Log;
import dhbw.clippinggorilla.utilities.ressources.FileUtils;
import dhbw.clippinggorilla.utilities.ui.VaadinUtils;
import java.io.FileNotFoundException;
import java.util.HashMap;

/**
 * This is the class for the Menubar at the top of the screen
 *
 * @author frank
 */
public class MenuBar extends GridLayout {

    private final static HashMap<VaadinSession, MenuBar> SESSIONS = new HashMap<>();

    private final Image logo;
    private Registration logoClick;
    private final Label spacing1;
    private final Label spacing2;
    private final TextField username;
    private final PasswordField password;
    private final Button passwordForgotten;
    private final Button login;
    private final Button register;
    private final Button home;
    private final Button profile;
    private final Button groups;
    private final com.vaadin.ui.MenuBar userBar;

    public static MenuBar getCurrent() {
        return new MenuBar();
    }

    public MenuBar() {
        setColumns(7);
        setRows(1);
        addStyleName("menubar");
        setWidth("100%");
        setHeightUndefined();
        setColumnExpandRatio(1, 5);
        setSpacing(true);
        setMargin(true);

        logo = new Image();
        try {
            logo.setSource(new FileResource(FileUtils.getFile("images/logowide_small.png").toFile()));
        } catch (FileNotFoundException ex) {
            Log.error("Could not find logo!", ex);
        }
        logo.setHeight("100%");

        spacing1 = new Label();
        spacing2 = new Label();

        username = new TextField();
        username.focus();
        Language.setCustom(Word.USERNAME_OR_EMAIL, null, v -> username.setPlaceholder(v));
        username.setMaxLength(255);

        password = new PasswordField();
        Language.setCustom(Word.PASSWORD, null, v -> password.setPlaceholder(v));
        password.setMaxLength(50);

        passwordForgotten = new Button(VaadinIcons.KEY);
        Language.set(Word.PASSWORD_FORGOTTEN, passwordForgotten);
        passwordForgotten.addStyleName(ValoTheme.BUTTON_BORDERLESS_COLORED);
        passwordForgotten.addClickListener(ce -> UI.getCurrent().addWindow(PasswordRecoveryWindow.create()));

        login = new Button();
        Language.set(Word.LOGIN, login);
        register = new Button();
        login.setClickShortcut(KeyCode.ENTER, null);
        login.addClickListener(ce -> logInActions());
        login.addStyleName(ValoTheme.BUTTON_PRIMARY);
        login.setIcon(VaadinIcons.SIGN_IN);

        Language.set(Word.REGISTER, register);
        register.addClickListener(ce -> UI.getCurrent().addWindow(RegisterWindow.get()));
        register.setIcon(VaadinIcons.USER_CHECK);

        home = new Button();
        home.setIcon(VaadinIcons.HOME);
        home.addClickListener(ce -> ClippingGorillaUI.getCurrent().setMainContent(ClippingView.getCurrent()));

        profile = new Button(Language.get(Word.PROFILES));
        profile.setIcon(VaadinIcons.USER);
        Language.set(Word.PROFILES, profile);
        profile.addClickListener(
                ce -> ClippingGorillaUI.getCurrent().setMainContent(InterestProfileView.getCurrent()));

        groups = new Button(Language.get(Word.GROUPS));
        groups.setIcon(VaadinIcons.GROUP);
        Language.set(Word.GROUPS, groups);
        groups.addClickListener(ce -> ClippingGorillaUI.getCurrent().setMainContent(GroupView.getCurrent()));

        userBar = new com.vaadin.ui.MenuBar();

        addComponents(logo, spacing1, spacing2, username, password, login, register);
        SESSIONS.put(VaadinSession.getCurrent(), this);
    }

    private void logInActions() {
        String usernameOrEmail = username.getValue();
        if (usernameOrEmail.contains("@")) {
            usernameOrEmail = usernameOrEmail.toLowerCase();
        }
        HashMap<Integer, User> state = UserUtils.loginUser(usernameOrEmail, password.getValue());
        int stateInt = state.keySet().iterator().next();
        UserUtils.setCurrentUser(state.get(stateInt));
        String errorMsg = null;
        switch (stateInt) {
        case 0://successful
            password.setValue("");
            User user = state.get(stateInt);
            login.setComponentError(null);
            profile.setComponentError(null);
            groups.setComponentError(null);

            changeItems(MenuBarState.LOGIN);

            logoClick = logo.addClickListener(
                    ce -> ClippingGorillaUI.getCurrent().setMainContent(ClippingView.getCurrent()));
            userBar.removeItems();
            MenuItem userMenu = userBar.addItem(user.getFirstName().charAt(0) + ". " + user.getLastName(), null);
            MenuItem subSettingsItem = userMenu.addItem(Language.get(Word.SETTINGS), VaadinIcons.COG,
                    e -> UI.getCurrent().addWindow(PreferencesWindow.get(user)));
            Language.setCustom(Word.SETTINGS, s -> subSettingsItem.setText(s));
            MenuItem subArchiveItem = userMenu.addItem(Language.get(Word.ARCHIVE), VaadinIcons.ARCHIVE,
                    e -> ClippingGorillaUI.getCurrent().setMainContent(ArchiveView.create()));
            Language.setCustom(Word.ARCHIVE, s -> subArchiveItem.setText(s));
            if (user.getAccessLevel() > 89) {
                MenuItem subAddSourceIcon = userMenu.addItem(Language.get(Word.MANAGE_SOURCES),
                        VaadinIcons.NEWSPAPER,
                        e -> ClippingGorillaUI.getCurrent().addWindow(ManageSourcesWindow.create()));
                Language.setCustom(Word.MANAGE_SOURCES, s -> subAddSourceIcon.setText(s));
            }
            MenuItem subLogoutItem = userMenu.addItem(Language.get(Word.SIGN_OUT), VaadinIcons.POWER_OFF, e -> {
                logOutActions();
            });
            Language.setCustom(Word.SIGN_OUT, s -> subLogoutItem.setText(s));

            ClippingGorillaUI.getCurrent().setMainContent(ClippingView.getCurrent());
            break;
        case -1://not found
        case -2://not match
            password.setValue("");
            errorMsg = Language.get(Word.WRONG_USERNAME_OR_PASSWORD);
            changeItems(MenuBarState.PW_FORGOTTEN);
            break;
        case -3://not activated
            errorMsg = Language.get(Word.NEEDS_ACTIVATION);
            UI.getCurrent().addWindow(ActivateWindow.get());
            break;
        case -4://banned
            password.setValue("");
            errorMsg = Language.get(Word.BANNED);
            break;
        default:
            password.setValue("");
            errorMsg = Language.get(Word.LOGIN_FAILED);
        }
        if (errorMsg != null) {
            VaadinUtils.errorNotification(errorMsg);
            login.setComponentError(
                    new UserError(errorMsg, AbstractErrorMessage.ContentMode.TEXT, ErrorMessage.ErrorLevel.ERROR));
        }
    }

    private void logOutActions() {
        password.setValue("");
        logoClick.remove();
        changeItems(MenuBarState.NORMAL);
        UserUtils.removeCurrentUser();
        ClippingGorillaUI.getCurrent().setMainContent(AboutUsView.getCurrent());
    }

    public enum MenuBarState {
        NORMAL, PW_FORGOTTEN, LOGIN
    }

    public void changeItems(MenuBarState state) {
        switch (state) {
        case NORMAL:
            replaceComponent(getComponent(6, 0), register);
            replaceComponent(getComponent(5, 0), login);
            replaceComponent(getComponent(4, 0), password);
            replaceComponent(getComponent(3, 0), username);
            replaceComponent(getComponent(2, 0), spacing2);
            break;
        case PW_FORGOTTEN:
            replaceComponent(getComponent(6, 0), register);
            replaceComponent(getComponent(5, 0), login);
            replaceComponent(getComponent(4, 0), passwordForgotten);
            replaceComponent(getComponent(3, 0), password);
            replaceComponent(getComponent(2, 0), username);
            break;
        case LOGIN:
            replaceComponent(getComponent(6, 0), userBar);
            replaceComponent(getComponent(5, 0), groups);
            replaceComponent(getComponent(4, 0), profile);
            replaceComponent(getComponent(3, 0), home);
            replaceComponent(getComponent(2, 0), spacing2);
            break;
        }
    }

}