de.metas.ui.web.vaadin.MainNavigationView.java Source code

Java tutorial

Introduction

Here is the source code for de.metas.ui.web.vaadin.MainNavigationView.java

Source

package de.metas.ui.web.vaadin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestTemplate;

import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.UI;

import de.metas.ui.web.menu.MenuController;
import de.metas.ui.web.vaadin.components.navigator.MFView;
import de.metas.ui.web.vaadin.components.navigator.MFViewDisplay;
import de.metas.ui.web.vaadin.window.WindowViewProvider;
import de.metas.ui.web.window.shared.menu.MainMenuItem;
import de.metas.ui.web.window.shared.menu.MainMenuItem.MenuItemType;
import de.metas.ui.web.window.shared.menu.MenuItem;

/*
 * #%L
 * test_vaadin
 * %%
 * Copyright (C) 2016 metas GmbH
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-2.0.html>.
 * #L%
 */

@SuppressWarnings("serial")
public class MainNavigationView extends CssLayout implements MFView {
    private List<? extends MenuItem> _rootMenuItems;

    private final String restEndpointUrl = "http://localhost:8080" + MenuController.ENDPOINT;
    @Autowired
    private RestTemplate restTemplate;

    public MainNavigationView() {
        super();
        VaadinClientApplication.autowire(this);

        setSizeFull();
    }

    @Override
    public void enter(final ViewChangeEvent event) {
        final MFViewDisplay viewDisplay = MFViewDisplay.getMFViewDisplayOrNull(event);
        viewDisplay.setTitle("Main");
        viewDisplay.setMenuItemClickListener(menuItem -> runMenuItem(menuItem));
        viewDisplay.setMenuItems(() -> getMenuItems());
        viewDisplay.showMenuPanel();
    }

    @Override
    public void exit(final ViewChangeEvent event) {
        // nothing
    }

    private List<? extends MenuItem> getMenuItems() {
        if (_rootMenuItems == null) {
            final MainMenuItem rootMenuItem = restTemplate.getForObject(restEndpointUrl + "/root",
                    MainMenuItem.class);
            _rootMenuItems = rootMenuItem.getChildren();
        }

        return _rootMenuItems;
    }

    private void runMenuItem(final MenuItem menuItem) {
        final MainMenuItem mainMenuItem = MainMenuItem.cast(menuItem);
        if (mainMenuItem.getType() == MenuItemType.Window) {
            final String viewNameAndParameters = WindowViewProvider
                    .createViewNameAndParameters(mainMenuItem.getElementId());
            UI.getCurrent().getNavigator().navigateTo(viewNameAndParameters);
        } else {
            throw new UnsupportedOperationException("Menu item not supported: " + mainMenuItem);
        }

    }
}