com.vaadin.flow.router.RouterLayout Java Examples

The following examples show how to use com.vaadin.flow.router.RouterLayout. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractRouteRegistryInitializer.java    From flow with Apache License 2.0 6 votes vote down vote up
private void validateRouteImplementation(Class<?> route,
        Class<?> implementation) {
    Route annotation = route.getAnnotation(Route.class);
    if (annotation != null && !UI.class.equals(annotation.layout())) {
        if (implementation.isAssignableFrom(route)) {
            throw new InvalidRouteLayoutConfigurationException(String
                    .format("%s needs to be the top parent layout '%s' not '%s'",
                            implementation.getSimpleName(),
                            RouteUtil.getTopParentLayout(route,
                                    RouteUtil.resolve(route, annotation))
                                    .getName(),
                            route.getName()));
        }

        List<Class<? extends RouterLayout>> parentLayouts = RouteUtil
                .getParentLayouts(route,
                        RouteUtil.resolve(route, annotation));
        Class<? extends RouterLayout> topParentLayout = RouteUtil
                .getTopParentLayout(route,
                        RouteUtil.resolve(route, annotation));

        validateParentImplementation(parentLayouts, topParentLayout,
                implementation);
    }
}
 
Example #2
Source File: RouteRegistryTestBase.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test
public void registeredParentLayouts_changingListDoesntChangeRegistration() {
    getInitializationRegistry().clean();

    List<Class<? extends RouterLayout>> parentChain = new ArrayList<>(
            Arrays.asList(MiddleLayout.class, MainLayout.class));

    getInitializationRegistry().setRoute("version", MyRoute.class,
            parentChain);

    parentChain.remove(MainLayout.class);

    Assert.assertEquals(
            "'version' should return two parents even when original list is changed",
            2, getTestedRegistry().getRouteLayouts("version", MyRoute.class)
                    .size());
}
 
Example #3
Source File: AbstractRouteRegistry.java    From flow with Apache License 2.0 6 votes vote down vote up
private void populateRegisteredRoutes(ConfiguredRoutes configuration,
        List<RouteData> registeredRoutes, Class<? extends Component> target,
        String template) {
    List<RouteAliasData> routeAliases = new ArrayList<>();

    configuration.getRoutePaths(target).stream().filter(
            routePathTemplate -> !routePathTemplate.equals(template))
            .forEach(
                    aliasRoutePathTemplate -> routeAliases
                            .add(new RouteAliasData(
                                    getParentLayouts(configuration,
                                            aliasRoutePathTemplate),
                                    aliasRoutePathTemplate,
                                    configuration.getParameters(
                                            aliasRoutePathTemplate),
                                    target)));
    List<Class<? extends RouterLayout>> parentLayouts = getParentLayouts(
            configuration, template);
    RouteData route = new RouteData(parentLayouts, template,
            configuration.getParameters(template), target, routeAliases);
    registeredRoutes.add(route);
}
 
Example #4
Source File: RouteUtil.java    From flow with Apache License 2.0 6 votes vote down vote up
private static Class<? extends RouterLayout> recurseToTopLayout(
        Class<? extends RouterLayout> layout) {
    Optional<ParentLayout> parentLayout = AnnotationReader
            .getAnnotationFor(layout, ParentLayout.class);

    if (parentLayout.isPresent()) {
        return recurseToTopLayout(parentLayout.get().value());
    }
    return layout;
}
 
Example #5
Source File: RouteUtil.java    From flow with Apache License 2.0 6 votes vote down vote up
private static List<String> getParentRoutePrefixes(Class<?> component,
        Supplier<Class<? extends RouterLayout>> routerLayoutSupplier) {
    List<String> list = new ArrayList<>();

    Optional<ParentLayout> parentLayout = AnnotationReader
            .getAnnotationFor(component, ParentLayout.class);
    Optional<RoutePrefix> routePrefix = AnnotationReader
            .getAnnotationFor(component, RoutePrefix.class);

    routePrefix.ifPresent(prefix -> list.add(prefix.value()));

    // break chain on an absolute RoutePrefix or Route
    if (routePrefix.isPresent() && routePrefix.get().absolute()) {
        return list;
    }

    Class<? extends RouterLayout> routerLayout = routerLayoutSupplier.get();
    if (routerLayout != null && !routerLayout.equals(UI.class)) {
        list.addAll(getParentRoutePrefixes(routerLayout, () -> null));
    } else if (parentLayout.isPresent()) {
        list.addAll(getParentRoutePrefixes(parentLayout.get().value(),
                () -> null));
    }

    return list;
}
 
Example #6
Source File: RouteUtil.java    From flow with Apache License 2.0 6 votes vote down vote up
/**
 * Get parent layouts for navigation target according to the {@link Route}
 * or {@link RouteAlias} annotation.
 *
 * @param component
 *            navigation target to get parents for
 * @param path
 *            path used to get navigation target so we know which annotation
 *            to handle
 * @return parent layouts for target
 */
public static List<Class<? extends RouterLayout>> getParentLayouts(
        Class<?> component, String path) {
    final List<Class<? extends RouterLayout>> list = new ArrayList<>();

    Optional<Route> route = AnnotationReader.getAnnotationFor(component,
            Route.class);
    List<RouteAlias> routeAliases = AnnotationReader
            .getAnnotationsFor(component, RouteAlias.class);
    if (route.isPresent()
            && path.equals(getRoutePath(component, route.get()))
            && !route.get().layout().equals(UI.class)) {
        list.addAll(collectRouteParentLayouts(route.get().layout()));
    } else {

        Optional<RouteAlias> matchingRoute = getMatchingRouteAlias(
                component, path, routeAliases);
        if (matchingRoute.isPresent()) {
            list.addAll(collectRouteParentLayouts(
                    matchingRoute.get().layout()));
        }
    }

    return list;
}
 
Example #7
Source File: ConfigureRoutes.java    From flow with Apache License 2.0 6 votes vote down vote up
/**
 * Set a new {@link RouteTarget} for the given template.
 * <p>
 * Note! this will override any previous value.
 *
 * @param template
 *            template for which to set route target for
 * @param navigationTarget
 *            navigation target to add
 * @param parentChain
 *            chain of parent layouts that should be used with this target
 */
public void setRoute(String template,
        Class<? extends Component> navigationTarget,
        List<Class<? extends RouterLayout>> parentChain) {

    template = PathUtil.trimPath(template);

    final RouteTarget target = new RouteTarget(navigationTarget,
            parentChain);

    getRouteModel().addRoute(template, target);

    if (!hasRouteTarget(navigationTarget)) {
        setTargetRoute(navigationTarget, template);
    }

    getTargetRouteModelMap().computeIfAbsent(navigationTarget,
            aClass -> RouteModel.create(true));
    getTargetRouteModelMap().get(navigationTarget).addRoute(template,
            target);

    getRoutesMap().put(template, target);
}
 
Example #8
Source File: RouteUtilTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test
public void expected_parent_layouts_are_found_for_route() {
    List<Class<? extends RouterLayout>> parentLayouts = RouteUtil
            .getParentLayouts(BaseRouteWithParentPrefixAndRouteAlias.class,
                    "parent");

    Assert.assertThat(
            "Get parent layouts for route \"\" with parent prefix \"parent\" gave wrong result.",
            parentLayouts, IsIterableContainingInOrder
                    .contains(new Class[] { RoutePrefixParent.class }));

    parentLayouts = RouteUtil.getParentLayouts(RootWithParents.class, "");

    Assert.assertThat(
            "Expected to receive MiddleParent and Parent classes as parents.",
            parentLayouts, IsIterableContainingInOrder.contains(
                    new Class[] { MiddleParent.class, Parent.class }));
}
 
Example #9
Source File: RouteUtilTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test
public void absolute_route_gets_expected_parent_layouts() {
    List<Class<? extends RouterLayout>> parentLayouts = RouteUtil
            .getParentLayouts(AbsoluteRoute.class, "single");

    Assert.assertThat(
            "Get parent layouts for route \"\" with parent prefix \"parent\" gave wrong result.",
            parentLayouts, IsIterableContainingInOrder
                    .contains(new Class[] { RoutePrefixParent.class }));

    parentLayouts = RouteUtil.getParentLayouts(AbsoluteCenterRoute.class,
            "absolute/child");

    Assert.assertThat(
            "Expected to receive MiddleParent and Parent classes as parents.",
            parentLayouts,
            IsIterableContainingInOrder.contains(new Class[] {
                    AbsoluteCenterParent.class, RoutePrefixParent.class }));
}
 
Example #10
Source File: RouteUtilTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test
public void abolute_route_alias_gets_expected_parent_layouts() {
    List<Class<? extends RouterLayout>> parentLayouts = RouteUtil
            .getParentLayouts(AbsoluteRoute.class, "alias");

    Assert.assertThat(
            "Get parent layouts for route \"\" with parent prefix \"parent\" gave wrong result.",
            parentLayouts, IsIterableContainingInOrder
                    .contains(new Class[] { RoutePrefixParent.class }));

    parentLayouts = RouteUtil.getParentLayouts(AbsoluteCenterRoute.class,
            "absolute/alias");

    Assert.assertThat(
            "Expected to receive MiddleParent and Parent classes as parents.",
            parentLayouts,
            IsIterableContainingInOrder.contains(new Class[] {
                    AbsoluteCenterParent.class, RoutePrefixParent.class }));

}
 
Example #11
Source File: RouteUtilTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test // 3424
public void top_layout_resolves_correctly_for_route_parent() {
    Class<? extends RouterLayout> topParentLayout = RouteUtil
            .getTopParentLayout(MultiTarget.class, "");
    Assert.assertEquals(
            "@Route path should have gotten Parent as top parent layout",
            Parent.class, topParentLayout);

    topParentLayout = RouteUtil.getTopParentLayout(MultiTarget.class,
            "alias");
    Assert.assertEquals(
            "@RouteAlias path should have gotten Parent as top parent layout",
            Parent.class, topParentLayout);

    topParentLayout = RouteUtil.getTopParentLayout(SubLayout.class,
            "parent/sub");
    Assert.assertEquals(
            "SubLayout using MultiTarget as parent should have gotten RoutePrefixParent as top parent layout",
            RoutePrefixParent.class, topParentLayout);

}
 
Example #12
Source File: AbstractRouteRegistryInitializer.java    From flow with Apache License 2.0 6 votes vote down vote up
/**
 * Validate PWA annotations of the potential route classes stream, search
 * for properly annotated PWA class and return it, or null if none existing.
 *
 * @param routeClasses
 *            potential route classes
 * @return a PWA -annotated class, or null if none exist.
 */
@SuppressWarnings("unchecked")
protected Class<?> validatePwaClass(Stream<Class<?>> routeClasses) {
    pwaClass = null;
    routeClasses.forEach(route -> {
        // check and validate route pwa annotation
        validatePwa(route);

        Route routeAnnotation = route.getAnnotation(Route.class);

        if (!UI.class.equals(routeAnnotation.layout())) {
            Class<? extends RouterLayout> topParentLayout = RouteUtil
                    .getTopParentLayout(route,
                            RouteUtil.resolve(route, routeAnnotation));
            // check and validate top parent layout pwa annotation
            validatePwa(topParentLayout);
        }
    });
    return pwaClass;
}
 
Example #13
Source File: AbstractRouteRegistryInitializer.java    From flow with Apache License 2.0 6 votes vote down vote up
private void validateParentAnnotation(
        List<Class<? extends RouterLayout>> parentLayouts,
        Class<? extends RouterLayout> topParentLayout,
        Class<? extends Annotation> annotation) {
    Supplier<Stream<Class<? extends RouterLayout>>> streamSupplier = () -> parentLayouts
            .stream()
            .filter(layout -> layout.isAnnotationPresent(annotation));
    if (streamSupplier.get().count() > 1) {
        throw new InvalidRouteLayoutConfigurationException("Only one "
                + annotation.getSimpleName()
                + " annotation is supported for navigation chain and should be on the top most level. Offending classes in chain: "
                + streamSupplier.get().map(Class::getName)
                        .collect(Collectors.joining(", ")));
    }

    streamSupplier.get().findFirst().ifPresent(layout -> {
        if (!layout.equals(topParentLayout)) {
            throw new InvalidRouteLayoutConfigurationException(String
                    .format("%s annotation should be on the top most route layout '%s'. Offending class: '%s'",
                            annotation.getSimpleName(),
                            topParentLayout.getName(), layout.getName()));
        }
    });
}
 
Example #14
Source File: AbstractRouteRegistryInitializer.java    From flow with Apache License 2.0 6 votes vote down vote up
private void validateRouteAliasAnnotation(Class<?> route, RouteAlias alias,
        Class<? extends Annotation> annotation) {
    if (!UI.class.equals(alias.layout())) {
        if (route.isAnnotationPresent(annotation)) {
            throw new InvalidRouteLayoutConfigurationException(String
                    .format("%s annotation needs to be on the top parent layout '%s' not on '%s'",
                            annotation.getSimpleName(),
                            RouteUtil.getTopParentLayout(route,
                                    alias.value()).getName(),
                            route.getName()));
        }

        List<Class<? extends RouterLayout>> parentLayouts = RouteUtil
                .getParentLayouts(route, alias.value());
        Class<? extends RouterLayout> topParentLayout = RouteUtil
                .getTopParentLayout(route, alias.value());

        validateParentAnnotation(parentLayouts, topParentLayout,
                annotation);
    }
}
 
Example #15
Source File: AbstractRouteRegistryInitializer.java    From flow with Apache License 2.0 6 votes vote down vote up
private void validateParentImplementation(
        List<Class<? extends RouterLayout>> parentLayouts,
        Class<? extends RouterLayout> topParentLayout,
        Class<?> implementation) {
    Supplier<Stream<Class<? extends RouterLayout>>> streamSupplier = () -> parentLayouts
            .stream().filter(implementation::isAssignableFrom);
    if (streamSupplier.get().count() > 1) {
        throw new InvalidRouteLayoutConfigurationException("Only one "
                + implementation.getSimpleName()
                + " implementation is supported for navigation chain and should be on the top most level. Offending classes in chain: "
                + streamSupplier.get().map(Class::getName)
                        .collect(Collectors.joining(", ")));
    }

    streamSupplier.get().findFirst().ifPresent(layout -> {
        if (!layout.equals(topParentLayout)) {
            throw new InvalidRouteLayoutConfigurationException(String
                    .format("%s implementation should be the top most route layout '%s'. Offending class: '%s'",
                            implementation.getSimpleName(),
                            topParentLayout.getName(), layout.getName()));
        }
    });

}
 
Example #16
Source File: RouteUtilTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test // 3424
public void parent_layouts_resolve_correctly_for_route_parent() {
    List<Class<? extends RouterLayout>> parentLayouts = RouteUtil
            .getParentLayouts(MultiTarget.class, "");

    Assert.assertThat(
            "Get parent layouts for route \"\" gave wrong result.",
            parentLayouts, IsIterableContainingInOrder
                    .contains(new Class[] { Parent.class }));

    parentLayouts = RouteUtil.getParentLayouts(MultiTarget.class, "alias");

    Assert.assertThat(
            "Get parent layouts for routeAlias \"alias\" gave wrong result.",
            parentLayouts, IsIterableContainingInOrder.contains(
                    new Class[] { MiddleParent.class, Parent.class }));

    parentLayouts = RouteUtil.getParentLayouts(SubLayout.class,
            "parent/sub");

    Assert.assertThat(
            "Get parent layouts for route \"parent/sub\" with parent Route + ParentLayout gave wrong result.",
            parentLayouts,
            IsIterableContainingInOrder.contains(new Class[] {
                    MultiTarget.class, RoutePrefixParent.class }));
}
 
Example #17
Source File: NavigationStateRendererTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test
public void getRouterLayoutForMulipleLayers() throws Exception {
    NavigationStateRenderer childRenderer = new NavigationStateRenderer(
            navigationStateFromTarget(ChildConfiguration.class));
    RouteConfiguration.forRegistry(router.getRegistry())
            .setAnnotatedRoute(ChildConfiguration.class);

    List<Class<? extends RouterLayout>> routerLayoutTypes = childRenderer
            .getRouterLayoutTypes(ChildConfiguration.class, router);

    Assert.assertEquals("Not all expected layouts were found", 2,
            routerLayoutTypes.size());
    Assert.assertEquals("Wrong class found as first in array",
            MiddleLayout.class, routerLayoutTypes.get(0));
    Assert.assertEquals("Wrong class found as second in array",
            RouteParentLayout.class, routerLayoutTypes.get(1));
}
 
Example #18
Source File: AbstractRouteRegistryInitializer.java    From flow with Apache License 2.0 6 votes vote down vote up
private void validateRouteAliasImplementation(Class<?> route,
        RouteAlias alias, Class<?> implementation) {
    if (!UI.class.equals(alias.layout())) {
        if (PageConfigurator.class.isAssignableFrom(route)) {
            throw new InvalidRouteLayoutConfigurationException(String
                    .format("%s needs to be the top parent layout '%s' not '%s'",
                            implementation.getSimpleName(),
                            RouteUtil.getTopParentLayout(route,
                                    alias.value()).getName(),
                            route.getName()));
        }

        List<Class<? extends RouterLayout>> parentLayouts = RouteUtil
                .getParentLayouts(route, alias.value());
        Class<? extends RouterLayout> topParentLayout = RouteUtil
                .getTopParentLayout(route, alias.value());

        validateParentImplementation(parentLayouts, topParentLayout,
                implementation);
    }
}
 
Example #19
Source File: RouteUtilTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void top_parent_layout_for_absolute_route_parent() {
    Class<? extends RouterLayout> parent = RouteUtil.getTopParentLayout(
            AbsoluteCenterRoute.class, "absolute/child");

    Assert.assertNotNull("Didn't find any parent for route", parent);
    Assert.assertEquals("Received wrong parent class.",
            RoutePrefixParent.class, parent);
}
 
Example #20
Source File: AbstractNavigationStateRenderer.java    From flow with Apache License 2.0 5 votes vote down vote up
private static boolean isPreserveOnRefreshTarget(
        Class<? extends Component> routeTargetType,
        List<Class<? extends RouterLayout>> routeLayoutTypes) {
    return routeTargetType.isAnnotationPresent(PreserveOnRefresh.class)
            || routeLayoutTypes.stream().anyMatch(layoutType -> layoutType
                    .isAnnotationPresent(PreserveOnRefresh.class));
}
 
Example #21
Source File: SessionRouteRegistryTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void registeredParentLayouts_changingListDoesntChangeRegistration() {
    SessionRouteRegistry registry = getRegistry(session);

    List<Class<? extends RouterLayout>> parentChain = new ArrayList<>(
            Arrays.asList(MiddleLayout.class, MainLayout.class));

    registry.setRoute("version", MyRoute.class, parentChain);

    parentChain.remove(MainLayout.class);

    Assert.assertEquals(
            "'version' should return two parents even when original list is changed",
            2, registry.getRouteLayouts("version", MyRoute.class).size());
}
 
Example #22
Source File: SessionRouteRegistryTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void registeredParentLayouts_returnedListInSameOrder() {
    SessionRouteRegistry registry = getRegistry(session);

    List<Class<? extends RouterLayout>> parentChain = new ArrayList<>(
            Arrays.asList(MiddleLayout.class, MainLayout.class));

    registry.setRoute("version", MyRoute.class, parentChain);

    Assert.assertArrayEquals(
            "Registry should return parent layouts in the same order as set.",
            parentChain.toArray(),
            registry.getRouteLayouts("version", MyRoute.class).toArray());
}
 
Example #23
Source File: AbstractRouteRegistry.java    From flow with Apache License 2.0 5 votes vote down vote up
@Override
public void setRoute(String path,
        Class<? extends Component> navigationTarget,
        List<Class<? extends RouterLayout>> parentChain) {
    configureWithFullTemplate(path, navigationTarget,
            (configuration, fullTemplate) -> configuration
                    .setRoute(fullTemplate, navigationTarget, parentChain));
}
 
Example #24
Source File: RouteRegistryTestBase.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void registeredParentLayouts_returnedListInSameOrder() {
    getInitializationRegistry().clean();

    List<Class<? extends RouterLayout>> parentChain = new ArrayList<>(
            Arrays.asList(MiddleLayout.class, MainLayout.class));

    getInitializationRegistry().setRoute("version", MyRoute.class,
            parentChain);

    Assert.assertArrayEquals(
            "Registry should return parent layouts in the same order as set.",
            parentChain.toArray(), getTestedRegistry()
                    .getRouteLayouts("version", MyRoute.class).toArray());
}
 
Example #25
Source File: RouteUtilTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void top_parent_layout_should_be_found_for_base_route() {
    Class<? extends RouterLayout> parent = RouteUtil.getTopParentLayout(
            BaseRouteWithParentPrefixAndRouteAlias.class, "parent");

    Assert.assertNotNull("Didn't find any parent for route", parent);
    Assert.assertEquals("Received wrong parent class.",
            RoutePrefixParent.class, parent);
}
 
Example #26
Source File: RouteUtilTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void top_parent_layout_should_be_found_for_non_base_route() {
    Class<? extends RouterLayout> parent = RouteUtil.getTopParentLayout(
            RouteWithParentPrefixAndRouteAlias.class, "parent/flow");

    Assert.assertNotNull("Didn't find any parent for route", parent);
    Assert.assertEquals("Received wrong parent class.",
            RoutePrefixParent.class, parent);
}
 
Example #27
Source File: RouteUtilTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void no_top_parent_layout_for_route_alias() {
    Class<? extends RouterLayout> parent = RouteUtil.getTopParentLayout(
            BaseRouteWithParentPrefixAndRouteAlias.class, "alias");

    Assert.assertNull("Found parent for RouteAlias without parent.",
            parent);
}
 
Example #28
Source File: RouteUtilTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void top_parent_layout_for_route_alias() {
    Class<? extends RouterLayout> parent = RouteUtil.getTopParentLayout(
            RouteAliasWithParentPrefix.class, "aliasparent/alias");

    Assert.assertNotNull("Didn't find any parent for route", parent);
    Assert.assertEquals("Received wrong parent class.",
            RouteAliasPrefixParent.class, parent);
}
 
Example #29
Source File: RouteUtilTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void top_parent_layout_for_absolute_route() {
    Class<? extends RouterLayout> parent = RouteUtil
            .getTopParentLayout(AbsoluteRoute.class, "single");

    Assert.assertNotNull("Didn't find any parent for route", parent);
    Assert.assertEquals("Received wrong parent class.",
            RoutePrefixParent.class, parent);
}
 
Example #30
Source File: RouteUtilTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void top_parent_layout_for_absolute_route_alias() {
    Class<? extends RouterLayout> parent = RouteUtil
            .getTopParentLayout(AbsoluteRoute.class, "alias");

    Assert.assertNotNull("Didn't find any parent for route", parent);
    Assert.assertEquals("Received wrong parent class.",
            RoutePrefixParent.class, parent);
}