在子视图中隐藏UI组件

时间:2015-11-10 08:32:15

标签: java vaadin vaadin7

我正在尝试找到在特定子视图中隐藏UI组件的最佳方法,但让其在其他子视图中可见。

这是我的UI类的代码:

public class DCSAdminUI extends UI {

    private static final long serialVersionUID = 1L;

    VerticalLayout root = new VerticalLayout();
    CssLayout content = new CssLayout();
    private MenuBar nav = new MenuBar();
    private MenuBar userNav = new MenuBar();
    private Navigator navigator;

    public static final String PERSISTENCE_UNIT = "mystery";

    public static DCSAdminUI getCurrent() {
        return (DCSAdminUI) UI.getCurrent();
    }

    public static DCSAdminUI getApplication() {
        return (DCSAdminUI) getApplication();
    }

    @Override
    protected void init(VaadinRequest request) {

        Page.getCurrent().setTitle("Admin");

        initLayout();
        initNav();
        initUserNav();
        getUserOrg();

        navigator = new Navigator(this, content);

        LoginUI loginView = new LoginUI();
        navigator.addView("", loginView);
        navigator.addView(ActivitiesUI.VIEW_NAME.toLowerCase(), new ActivitiesUI());
        navigator.addView(BookingsUI.VIEW_NAME.toLowerCase(), new BookingsUI());
        navigator.addView(OperatorsUI.VIEW_NAME.toLowerCase(), new OperatorsUI());
        navigator.addView(TeamUI.VIEW_NAME.toLowerCase(), new TeamUI());
        navigator.addView(OrganisationsUI.VIEW_NAME.toLowerCase(), new OrganisationsUI());
        navigator.addView(PlayersUI.VIEW_NAME.toLowerCase(), new PlayersUI());


        navigator.addViewChangeListener(new ViewChangeListener() {

            private static final long serialVersionUID = 1L;

            @Override
            public boolean beforeViewChange(ViewChangeEvent event) {
                return true;
            }

            @Override
            public void afterViewChange(ViewChangeEvent event) {
                if (event.getViewName() == null || event.getViewName().equals("")) {
                    updateNavSelection("activities");
                } else {
                    updateNavSelection(event.getViewName());
                }
            }
        }); 

    }

    private void initLayout() {
        root.setSizeFull();
        setContent(root);

        HorizontalLayout topbar = new HorizontalLayout();
        topbar.addStyleName("topbar");
        topbar.setWidth("100%");
        topbar.setSpacing(true);
        root.addComponent(topbar);

        content.setSizeFull();
        root.addComponent(content);
        root.setExpandRatio(content, 1);

        nav.addStyleName("nav");
        topbar.addComponent(nav);

        userNav.addStyleName("user-nav");
        topbar.addComponent(userNav);
        topbar.setComponentAlignment(userNav, Alignment.TOP_CENTER);
    }

    private void initNav() {
        nav.addItem(ActivitiesUI.VIEW_NAME.toUpperCase(Locale.GERMANY), navCommand).setCheckable(true);
        nav.addItem(BookingsUI.VIEW_NAME.toUpperCase(Locale.GERMANY), navCommand).setCheckable(true);
        nav.addItem(PlayersUI.VIEW_NAME.toUpperCase(Locale.GERMANY), navCommand).setCheckable(true);
        nav.addItem(TeamUI.VIEW_NAME.toUpperCase(Locale.GERMANY), navCommand).setCheckable(true);
        nav.addItem(OperatorsUI.VIEW_NAME.toUpperCase(Locale.GERMANY), navCommand).setCheckable(true);
        nav.addItem(OrganisationsUI.VIEW_NAME.toUpperCase(Locale.GERMANY), navCommand).setCheckable(true);
    }

    private void initUserNav() {
        MenuItem username = userNav.addItem("DCS User", FontAwesome.CHEVRON_DOWN, null);
        username.addItem("Edit Profile", null);
        username.addItem("Settings", null);
        username.addSeparator();
        username.addItem("Logout", null);
    }

    private Command navCommand = new Command() {

        private static final long serialVersionUID = 1L;

        @Override
        public void menuSelected(MenuItem selectedItem) {
            updateNavSelection(selectedItem.getText());
            navigator.navigateTo(selectedItem.getText().toLowerCase());
        }
    };

    private void updateNavSelection(String selectedItem) {
        for (MenuItem item : nav.getItems()) {
            item.setChecked(item.getText().toLowerCase()
                    .equals(selectedItem.toLowerCase()));
        }
    }

    void navigateTo(String menuItem) {
        for (MenuItem item : nav.getItems()) {
            if (item.getText().toLowerCase().equals(menuItem.toLowerCase())) {
                navCommand.menuSelected(item);
                return;
            }
        }
    }

}

此处讨论的UI组件topbar方法中的initLayout()是应用程序的主要MenuBar(),但我希望这仅针对经过身份验证的用户显示,这意味着它应该隐藏在LoginUI()(现在是默认的View类)中。我的搜索努力让我明白我可以使用多个UI类,但这最终会变得乏味,这就是为什么我想要找到一种方法来隐藏特定View类中的topbar组件< / p>

1 个答案:

答案 0 :(得分:0)

您可以在用户界面中实施ViewChangeListener并在beforeViewChange中处理导航栏。由于这也可以拒绝输入View,您也可以向用户发送登录信息。