com.wintindustries.pfserver.interfaces.view.dashboard.DashboardMenu.java Source code

Java tutorial

Introduction

Here is the source code for com.wintindustries.pfserver.interfaces.view.dashboard.DashboardMenu.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 com.wintindustries.pfserver.interfaces.view.dashboard;

import java.util.Collection;

import com.google.gwt.thirdparty.guava.common.eventbus.Subscribe;
import com.vaadin.data.Container;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.util.HierarchicalContainer;
import com.vaadin.data.util.ObjectProperty;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.event.ItemClickEvent.ItemClickListener;
import com.vaadin.event.dd.DragAndDropEvent;
import com.vaadin.event.dd.DropHandler;
import com.vaadin.event.dd.acceptcriteria.AcceptCriterion;
import com.vaadin.server.FontAwesome;
import com.vaadin.server.Page;
import com.vaadin.server.Resource;
import com.vaadin.server.Responsive;
import com.vaadin.server.ThemeResource;
import com.vaadin.server.VaadinSession;
import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.ui.AbstractSelect;
import com.vaadin.ui.AbstractSelect.AcceptItem;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Component;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.DragAndDropWrapper;
import com.vaadin.ui.DragAndDropWrapper.DragStartMode;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.MenuBar;
import com.vaadin.ui.MenuBar.Command;
import com.vaadin.ui.MenuBar.MenuItem;
import com.vaadin.ui.Notification;
import com.vaadin.ui.Table;
import com.vaadin.ui.Tree;
import com.vaadin.ui.UI;
import com.vaadin.ui.themes.ValoTheme;
import com.wintindustries.pdffilter.PFData.PFDataSource;
import com.wintindustries.pdffilter.PFDocument.PFFile;
import com.wintindustries.pdffilter.PFLocation.PFFolder;
import com.wintindustries.pdffilter.PFLocation.PFLocation;
import com.wintindustries.pdffilter.PF_DATABASE_ENTITIES.PFUser;
import com.wintindustries.pdffilter.pfcore.PFCore;
import com.wintindustries.pfserver.MyUI;
import com.wintindustries.pfserver.event.DashboardEvent.NotificationsCountUpdatedEvent;
import com.wintindustries.pfserver.event.DashboardEvent.ProfileUpdatedEvent;
import com.wintindustries.pfserver.event.DashboardEvent.ReportsCountUpdatedEvent;
import com.wintindustries.pfserver.event.DashboardEventBus;
import com.wintindustries.pfserver.interfaces.container.PFFolderContainer;
import com.wintindustries.pfserver.interfaces.container.PFFolderProxy;
import com.wintindustries.pfserver.interfaces.container.PFFolderProxyContainer;
import com.wintindustries.pfserver.view.dashboard.testView.createnewFoldertest;
import com.wintindustries.pdffilter.PFData.PFSessionManager;
import com.wintindustries.pfserver.interfaces.container.components.PFSearch.PFSearchBar;

import java.util.Set;
import javax.transaction.Transactional;

/**
 * A responsive menu component providing user information and the controls for
 * primary navigation between the views.
 */
@SuppressWarnings({ "serial", "unchecked" })
public final class DashboardMenu extends CustomComponent {

    public static final String ID = "dashboard-menu";
    public static final String REPORTS_BADGE_ID = "dashboard-menu-reports-badge";
    public static final String NOTIFICATIONS_BADGE_ID = "dashboard-menu-notifications-badge";
    private static final String STYLE_VISIBLE = "valo-menu-visible";
    private Label notificationsBadge;
    private Label reportsBadge;
    private MenuItem settingsItem;
    Component folderUI;

    public DashboardMenu() {
        setPrimaryStyleName("valo-menu");
        setId(ID);
        setSizeUndefined();

        // There's only one DashboardMenu per UI so this doesn't need to be
        // unregistered from the UI-scoped DashboardEventBus.
        DashboardEventBus.register(this);

        //  setCompositionRoot(buildContent());
        Responsive.makeResponsive(this);
    }

    public void buildMenu() {
        setCompositionRoot(buildContent());
    }

    private Component buildContent() {
        final CssLayout menuContent = new CssLayout();
        menuContent.addStyleName("sidebar");
        menuContent.addStyleName(ValoTheme.MENU_PART);
        menuContent.addStyleName("no-vertical-drag-hints");
        menuContent.addStyleName("no-horizontal-drag-hints");
        menuContent.setWidth(null);
        menuContent.setHeight("100%");

        menuContent.addComponent(buildTitle());
        menuContent.addComponent(buildUserMenu());
        menuContent.addComponent(buildToggleButton());
        menuContent.addComponent(new PFSearchBar());

        reloadFolderUI();
        //   menuContent.addComponent(buildMenuItems());
        menuContent.addComponent(folderUI);

        return menuContent;
    }

    public void reloadFolderUI() {
        folderUI = buildFolderUI();
    }

    private Component buildFolderUI() {
        final CssLayout folderUIComponent = new CssLayout();
        folderUIComponent.setWidth("100%");
        folderUIComponent.addComponent(buildFolderTree());
        return folderUIComponent;

    }

    private Component buildTitle() {
        Label logo = new Label("PFServer <strong>Dashboard</strong>", ContentMode.HTML);
        logo.setSizeUndefined();
        HorizontalLayout logoWrapper = new HorizontalLayout(logo);
        logoWrapper.setComponentAlignment(logo, Alignment.MIDDLE_CENTER);
        logoWrapper.addStyleName("valo-menu-title");
        return logoWrapper;
    }

    private PFUser getCurrentUser() {
        return (PFUser) VaadinSession.getCurrent().getAttribute(PFUser.class.getName());
    }

    private Component buildUserMenu() {
        final MenuBar settings = new MenuBar();
        settings.addStyleName("user-menu");
        final PFUser user = getCurrentUser();
        settingsItem = settings.addItem("", new ThemeResource("img/profile-pic-300px.jpg"), null);
        //    updateUserName(null);
        settingsItem.addItem("Edit Profile", new Command() {
            @Override
            public void menuSelected(final MenuItem selectedItem) {
                //   ProfilePreferencesWindow.open(user, false);
            }
        });
        settingsItem.addItem("Preferences", new Command() {
            @Override
            public void menuSelected(final MenuItem selectedItem) {
                //   ProfilePreferencesWindow.open(user, true);
            }
        });
        settingsItem.addSeparator();
        settingsItem.addItem("Sign Out", new Command() {
            @Override
            public void menuSelected(final MenuItem selectedItem) {
                //   DashboardEventBus.post(new UserLoggedOutEvent());
            }
        });
        return settings;
    }

    private Component buildToggleButton() {
        Button valoMenuToggleButton = new Button("Menu", new ClickListener() {
            @Override
            public void buttonClick(final ClickEvent event) {
                if (getCompositionRoot().getStyleName().contains(STYLE_VISIBLE)) {
                    getCompositionRoot().removeStyleName(STYLE_VISIBLE);
                } else {
                    getCompositionRoot().addStyleName(STYLE_VISIBLE);
                }
            }
        });
        valoMenuToggleButton.setIcon(FontAwesome.LIST);
        valoMenuToggleButton.addStyleName("valo-menu-toggle");
        valoMenuToggleButton.addStyleName(ValoTheme.BUTTON_BORDERLESS);
        valoMenuToggleButton.addStyleName(ValoTheme.BUTTON_SMALL);
        return valoMenuToggleButton;
    }

    private Component buildFolderTree() {

        CssLayout treeLayout = new CssLayout();
        treeLayout.setWidth("100%");
        if (treeLayout.getComponentCount() > 0) {
            treeLayout.removeAllComponents();
        }

        System.out.println("Init Dataasource");

        for (PFDataSource source : PFCore.core.getDataSources()) {
            System.out.println("NEW TREE");

            Label label = new Label(source.getName(), ContentMode.HTML);
            label.setPrimaryStyleName("valo-menu-subtitle");
            label.addStyleName("h4");

            label.setSizeUndefined();
            treeLayout.addComponent(label);
            //      label.setIcon(FontAwesome.LAPTOP);
            treeLayout.addStyleName("valo-menuitems");

            Tree tree = new Tree();
            //tree.setIcon(FontAwesome.LAPTOP);
            // tree.setWidth("100%");

            // container.setItemSorter(new l);

            source.Session().OpenSession();
            source.Session().getSession().beginTransaction();
            source.Session().getSession().getTransaction().setTimeout(4);

            System.out.println(": " + source.getDatabase().getSessionManager());

            Set<PFFolder> rootFolders = PFLocation.getRootDirectoriesFromDatabase(source.getDatabase());

            System.out.println(": " + source.getDatabase().getSessionManager());

            final PFFolderProxyContainer container = new PFFolderProxyContainer(rootFolders, source);

            container.setSortAlphabetic(true);
            tree.setContainerDataSource(container);
            tree.setItemCaptionMode(AbstractSelect.ItemCaptionMode.PROPERTY);
            tree.setItemCaptionPropertyId("Name");
            tree.setItemIconPropertyId("Icon");

            ItemClickListener treeclick;
            treeclick = new ItemClickListener() {

                @Override
                public void itemClick(final ItemClickEvent event) {

                    //  Notification note = new Notification("Notificaton", event.getItemId().toString(), Notification.Type.ERROR_MESSAGE);
                    // note.show(Page.getCurrent());
                    PFFolderProxy proxyFolder = (PFFolderProxy) event.getItemId();
                    MyUI.getPFNavigator().navigateTo(MyUI.PAGE_FOLDERVIEW + "/" + proxyFolder.getIdentifier());
                }
            };

            tree.addItemClickListener(treeclick);

            // Allow all nodes to have children
            //   .dashboard-menu-folder-wrapper
            // all these wrappers are a hacky workaround to a Vaadin CSS glitch
            CssLayout treeWrapper = new CssLayout();
            treeWrapper.setStyleName(".dashboard-menu-folder-wrapper");
            treeWrapper.addComponent(tree);
            treeWrapper.setWidth("40px");
            tree.setWidth("800px");
            tree.setStyleName(".dashboard-menu-folder-tree");

            treeLayout.addComponent(treeWrapper);
            source.Session().getSession().getTransaction().commit();
            source.getDatabase().getSessionManager().CloseSession();
        }

        return treeLayout;

    }

    private Component buildMenuItems() {
        CssLayout menuItemsLayout = new CssLayout();
        menuItemsLayout.addStyleName("valo-menuitems");

        for (final DashboardViewType view : DashboardViewType.values()) {
            Component menuItemComponent = new ValoMenuItemButton(view);

            /*
            if (view == DashboardViewType.REPORTS) {
            // Add drop target to reports button
            DragAndDropWrapper reports = new DragAndDropWrapper(
                    menuItemComponent);
            reports.setSizeUndefined();
            reports.setDragStartMode(DragStartMode.NONE);
            reports.setDropHandler(new DropHandler() {
                
                @Override
                public void drop(final DragAndDropEvent event) {
                    UI.getCurrent()
                            .getNavigator()
                            .navigateTo(
                                    DashboardViewType.REPORTS.getViewName());
                    Table table = (Table) event.getTransferable()
                            .getSourceComponent();
                    DashboardEventBus.post(new TransactionReportEvent(
                            (Collection<Transaction>) table.getValue()));
                }
                
                @Override
                public AcceptCriterion getAcceptCriterion() {
                    return AcceptItem.ALL;
                }
                
            });
            menuItemComponent = reports;
            }*/

            if (view == DashboardViewType.DASHBOARD) {
                notificationsBadge = new Label();
                notificationsBadge.setId(NOTIFICATIONS_BADGE_ID);
                menuItemComponent = buildBadgeWrapper(menuItemComponent, notificationsBadge);
            }
            if (view == DashboardViewType.REPORTS) {
                reportsBadge = new Label();
                reportsBadge.setId(REPORTS_BADGE_ID);
                menuItemComponent = buildBadgeWrapper(menuItemComponent, reportsBadge);
            }

            menuItemsLayout.addComponent(menuItemComponent);
        }
        return menuItemsLayout;

    }

    private Component buildBadgeWrapper(final Component menuItemButton, final Component badgeLabel) {
        CssLayout dashboardWrapper = new CssLayout(menuItemButton);
        dashboardWrapper.addStyleName("badgewrapper");
        dashboardWrapper.addStyleName(ValoTheme.MENU_ITEM);
        badgeLabel.addStyleName(ValoTheme.MENU_BADGE);
        badgeLabel.setWidthUndefined();
        badgeLabel.setVisible(false);
        dashboardWrapper.addComponent(badgeLabel);
        return dashboardWrapper;
    }

    @Override
    public void attach() {
        super.attach();
        updateNotificationsCount(null);
    }

    /*
     @Subscribe
     public void postViewChange(final PostViewChangeEvent event) {
    // After a successful view change the menu can be hidden in mobile view.
    getCompositionRoot().removeStyleName(STYLE_VISIBLE);
     }
     */

    @Subscribe
    public void updateNotificationsCount(final NotificationsCountUpdatedEvent event) {
        /*  int unreadNotificationsCount = DashboardUI.getDataProvider()
            .getUnreadNotificationsCount();
          notificationsBadge.setValue(String.valueOf(unreadNotificationsCount));
          notificationsBadge.setVisible(unreadNotificationsCount > 0);
          */
    }

    @Subscribe
    public void updateReportsCount(final ReportsCountUpdatedEvent event) {
        reportsBadge.setValue(String.valueOf(event.getCount()));
        reportsBadge.setVisible(event.getCount() > 0);
    }

    /*
        @Subscribe
        public void updateUserName(final ProfileUpdatedEvent event) {
    User user = getCurrentUser();
    settingsItem.setText(user.getFirstName() + " " + user.getLastName());
        }
    */
    public final class ValoMenuItemButton extends Button {

        private static final String STYLE_SELECTED = "selected";

        private final DashboardViewType view;

        public ValoMenuItemButton(final DashboardViewType view) {
            this.view = view;
            setPrimaryStyleName("valo-menu-item");
            setIcon(view.getIcon());
            setCaption(view.getViewName().substring(0, 1).toUpperCase() + view.getViewName().substring(1));
            DashboardEventBus.register(this);
            addClickListener(new ClickListener() {
                @Override
                public void buttonClick(final ClickEvent event) {
                    UI.getCurrent().getNavigator().navigateTo(view.getViewName());
                }
            });

        }
        /*
        @Subscribe
        public void postViewChange(final PostViewChangeEvent event) {
        removeStyleName(STYLE_SELECTED);
        if (event.getView() == view) {
            addStyleName(STYLE_SELECTED);
        }
        }
        */
    }
}