在子视图中隐藏 UI 组件

Hide UI component in sub-view

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

这是我的代码 UI class:

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 组件是 initLayout() 方法中的 topbar ,它是应用程序的主要 MenuBar() 但我希望它只对经过身份验证的用户显示,这意味着它应该隐藏在 LoginUI() 中(目前默认的视图 class)。我的搜索工作让我明白我可以使用多个 UI classes 但这最终会变得维护起来很乏味,这就是为什么我最想找到一种隐藏 topbar 特定视图中的组件 classes

您可以在 UI 中实现 ViewChangeListener,并在 beforeViewChange 中处理导航栏。由于这也可以拒绝输入 View,因此您也可以将匿名用户发送到登录名。