Search in sources :

Example 31 with Component

use of com.vaadin.flow.component.Component in project flow by vaadin.

the class BeforeEvent method rerouteToError.

/**
 * Reroute to error target for given exception with given custom message.
 *
 * @param exception
 *            exception to get error target for
 * @param customMessage
 *            custom message to send to error target
 */
public void rerouteToError(Exception exception, String customMessage) {
    Optional<Class<? extends Component>> errorNavigationTarget = getSource().getRegistry().getErrorNavigationTarget(exception);
    if (errorNavigationTarget.isPresent()) {
        rerouteTargetState = new NavigationStateBuilder().withTarget(errorNavigationTarget.get()).build();
        rerouteTarget = new ErrorStateRenderer(rerouteTargetState);
        errorParameter = new ErrorParameter<>(exception, customMessage);
    } else {
        throw new RuntimeException(customMessage, exception);
    }
}
Also used : ErrorStateRenderer(com.vaadin.flow.router.internal.ErrorStateRenderer) Component(com.vaadin.flow.component.Component)

Example 32 with Component

use of com.vaadin.flow.component.Component in project flow by vaadin.

the class Router method handleExceptionNavigation.

private int handleExceptionNavigation(UI ui, Location location, Exception exception) {
    ErrorParameter<?> errorParameter = new ErrorParameter<>(exception, exception.getMessage());
    Optional<Class<? extends Component>> navigationTarget = getRegistry().getErrorNavigationTarget(errorParameter.getException());
    if (navigationTarget.isPresent()) {
        ErrorStateRenderer handler = new ErrorStateRenderer(new NavigationStateBuilder().withTarget(navigationTarget.get()).build());
        ErrorNavigationEvent navigationEvent = new ErrorNavigationEvent(this, location, ui, NavigationTrigger.PROGRAMMATIC, errorParameter);
        return handler.handle(navigationEvent);
    } else {
        throw new RuntimeException(errorParameter.getCustomMessage(), errorParameter.getException());
    }
}
Also used : ErrorStateRenderer(com.vaadin.flow.router.internal.ErrorStateRenderer) Component(com.vaadin.flow.component.Component)

Example 33 with Component

use of com.vaadin.flow.component.Component in project flow by vaadin.

the class AbstractNavigationStateRenderer method handle.

@Override
public int handle(NavigationEvent event) {
    UI ui = event.getUI();
    Class<? extends Component> routeTargetType = navigationState.getNavigationTarget();
    List<Class<? extends RouterLayout>> routeLayoutTypes = getRouterLayoutTypes(routeTargetType);
    assert routeTargetType != null;
    assert routeLayoutTypes != null;
    clearContinueNavigationAction(ui);
    RouterUtil.checkForDuplicates(routeTargetType, routeLayoutTypes);
    if (eventActionsSupported()) {
        BeforeLeaveEvent beforeNavigationDeactivating = new BeforeLeaveEvent(event, routeTargetType);
        Deque<BeforeLeaveHandler> leaveHandlers;
        if (postponed != null) {
            leaveHandlers = postponed.getLeaveObservers();
            if (!leaveHandlers.isEmpty()) {
                postponed = null;
            }
        } else {
            List<BeforeLeaveHandler> beforeLeaveHandlers = new ArrayList<>(ui.getNavigationListeners(BeforeLeaveHandler.class));
            beforeLeaveHandlers.addAll(EventUtil.collectBeforeLeaveObservers(ui.getElement()));
            leaveHandlers = new ArrayDeque<>(beforeLeaveHandlers);
        }
        TransitionOutcome transitionOutcome = executeBeforeLeaveNavigation(beforeNavigationDeactivating, leaveHandlers);
        if (transitionOutcome == TransitionOutcome.REROUTED) {
            return reroute(event, beforeNavigationDeactivating);
        } else if (transitionOutcome == TransitionOutcome.POSTPONED) {
            ContinueNavigationAction currentAction = beforeNavigationDeactivating.getContinueNavigationAction();
            currentAction.setReferences(this, event);
            storeContinueNavigationAction(ui, currentAction);
            return HttpServletResponse.SC_OK;
        }
    }
    Component componentInstance = getRouteTarget(routeTargetType, event);
    List<HasElement> chain = new ArrayList<>();
    chain.add(componentInstance);
    for (Class<? extends RouterLayout> parentType : routeLayoutTypes) {
        chain.add(getRouteTarget(parentType, event));
    }
    BeforeEnterEvent beforeNavigationActivating = new BeforeEnterEvent(event, routeTargetType);
    LocationChangeEvent locationChangeEvent = RouterUtil.createEvent(event, chain);
    notifyNavigationTarget(componentInstance, event, beforeNavigationActivating, locationChangeEvent);
    if (beforeNavigationActivating.hasRerouteTarget()) {
        return reroute(event, beforeNavigationActivating);
    }
    @SuppressWarnings("unchecked") List<RouterLayout> routerLayouts = (List<RouterLayout>) (List<?>) chain.subList(1, chain.size());
    List<BeforeEnterHandler> enterHandlers = new ArrayList<>(ui.getNavigationListeners(BeforeEnterHandler.class));
    enterHandlers.addAll(EventUtil.collectEnterObservers(componentInstance, routerLayouts));
    TransitionOutcome transitionOutcome = executeBeforeEnterNavigation(beforeNavigationActivating, enterHandlers);
    if (eventActionsSupported() && TransitionOutcome.REROUTED.equals(transitionOutcome)) {
        return reroute(event, beforeNavigationActivating);
    }
    ui.getInternals().showRouteTarget(event.getLocation(), navigationState.getResolvedPath(), componentInstance, routerLayouts);
    RouterUtil.updatePageTitle(event, componentInstance);
    int statusCode = locationChangeEvent.getStatusCode();
    validateStatusCode(statusCode, routeTargetType);
    List<AfterNavigationHandler> afterNavigationHandlers = new ArrayList<>(ui.getNavigationListeners(AfterNavigationHandler.class));
    afterNavigationHandlers.addAll(EventUtil.collectAfterNavigationObservers(componentInstance, routerLayouts));
    fireAfterNavigationListeners(new AfterNavigationEvent(locationChangeEvent), afterNavigationHandlers);
    return statusCode;
}
Also used : LocationChangeEvent(com.vaadin.flow.router.LocationChangeEvent) ArrayList(java.util.ArrayList) UI(com.vaadin.flow.component.UI) ArrayList(java.util.ArrayList) List(java.util.List) Component(com.vaadin.flow.component.Component) BeforeEnterEvent(com.vaadin.flow.router.BeforeEnterEvent) BeforeLeaveEvent(com.vaadin.flow.router.BeforeLeaveEvent) RouterLayout(com.vaadin.flow.router.RouterLayout) ContinueNavigationAction(com.vaadin.flow.router.BeforeLeaveEvent.ContinueNavigationAction) HasElement(com.vaadin.flow.component.HasElement) AfterNavigationEvent(com.vaadin.flow.router.AfterNavigationEvent)

Example 34 with Component

use of com.vaadin.flow.component.Component in project flow by vaadin.

the class AbstractNavigationStateRenderer method getRouteTarget.

/**
 * Gets the component instance to use for the given type and the
 * corresponding navigation event.
 * <p>
 * Override this method to control the creation of view instances.
 * <p>
 * By default always creates new instances.
 *
 * @param <T>
 *            the route target type
 * @param routeTargetType
 *            the class of the route target component
 * @param event
 *            the navigation event that uses the route target
 * @return an instance of the route target component
 */
@SuppressWarnings("unchecked")
static <// Non-private for testing purposes
T extends HasElement> T getRouteTarget(Class<T> routeTargetType, NavigationEvent event) {
    UI ui = event.getUI();
    Optional<HasElement> currentInstance = ui.getInternals().getActiveRouterTargetsChain().stream().filter(component -> component.getClass().equals(routeTargetType)).findAny();
    return (T) currentInstance.orElseGet(() -> Instantiator.get(ui).createRouteTarget(routeTargetType, event));
}
Also used : BeforeLeaveEvent(com.vaadin.flow.router.BeforeLeaveEvent) BeforeEnterEvent(com.vaadin.flow.router.BeforeEnterEvent) Component(com.vaadin.flow.component.Component) Deque(java.util.Deque) BeforeEvent(com.vaadin.flow.router.BeforeEvent) ArrayList(java.util.ArrayList) Route(com.vaadin.flow.router.Route) ErrorParameter(com.vaadin.flow.router.ErrorParameter) Location(com.vaadin.flow.router.Location) LocationChangeEvent(com.vaadin.flow.router.LocationChangeEvent) ContinueNavigationAction(com.vaadin.flow.router.BeforeLeaveEvent.ContinueNavigationAction) ErrorNavigationEvent(com.vaadin.flow.router.ErrorNavigationEvent) UI(com.vaadin.flow.component.UI) BeforeEnterObserver(com.vaadin.flow.router.BeforeEnterObserver) NavigationTrigger(com.vaadin.flow.router.NavigationTrigger) Instantiator(com.vaadin.flow.di.Instantiator) RouterLayout(com.vaadin.flow.router.RouterLayout) NavigationHandler(com.vaadin.flow.router.NavigationHandler) HasElement(com.vaadin.flow.component.HasElement) EventUtil(com.vaadin.flow.router.EventUtil) HttpServletResponse(javax.servlet.http.HttpServletResponse) ReflectTools(com.vaadin.flow.internal.ReflectTools) List(java.util.List) AfterNavigationEvent(com.vaadin.flow.router.AfterNavigationEvent) Optional(java.util.Optional) NavigationState(com.vaadin.flow.router.NavigationState) ArrayDeque(java.util.ArrayDeque) NavigationEvent(com.vaadin.flow.router.NavigationEvent) BeforeLeaveObserver(com.vaadin.flow.router.BeforeLeaveObserver) UI(com.vaadin.flow.component.UI) HasElement(com.vaadin.flow.component.HasElement)

Example 35 with Component

use of com.vaadin.flow.component.Component in project flow by vaadin.

the class PolymerTemplateTest method assertAnotherTemplateInitialization.

private void assertAnotherTemplateInitialization(AnotherTemplateInitialization template) {
    VirtualChildrenList feature = template.getStateNode().getFeature(VirtualChildrenList.class);
    assertEquals(2, feature.size());
    Optional<Component> child = com.vaadin.flow.dom.Element.get(feature.get(0)).getComponent();
    Assert.assertTrue(child.isPresent());
    Assert.assertEquals(TemplateChild.class, child.get().getClass());
    child = com.vaadin.flow.dom.Element.get(feature.get(1)).getComponent();
    Assert.assertTrue(child.isPresent());
    Assert.assertEquals(TestPolymerTemplate.class, child.get().getClass());
}
Also used : VirtualChildrenList(com.vaadin.flow.internal.nodefeature.VirtualChildrenList) Component(com.vaadin.flow.component.Component)

Aggregations

Component (com.vaadin.flow.component.Component)66 Test (org.junit.Test)9 Element (com.vaadin.flow.dom.Element)7 UI (com.vaadin.flow.component.UI)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 HasElement (com.vaadin.flow.component.HasElement)2 NativeButton (com.vaadin.flow.component.html.NativeButton)2 VirtualChildrenList (com.vaadin.flow.internal.nodefeature.VirtualChildrenList)2 AfterNavigationEvent (com.vaadin.flow.router.AfterNavigationEvent)2 BeforeEnterEvent (com.vaadin.flow.router.BeforeEnterEvent)2 BeforeLeaveEvent (com.vaadin.flow.router.BeforeLeaveEvent)2 ContinueNavigationAction (com.vaadin.flow.router.BeforeLeaveEvent.ContinueNavigationAction)2 Location (com.vaadin.flow.router.Location)2 LocationChangeEvent (com.vaadin.flow.router.LocationChangeEvent)2 NavigationEvent (com.vaadin.flow.router.NavigationEvent)2 RouterLayout (com.vaadin.flow.router.RouterLayout)2 ErrorStateRenderer (com.vaadin.flow.router.internal.ErrorStateRenderer)2 ViewClassLocator (com.vaadin.flow.uitest.servlet.ViewClassLocator)2 Optional (java.util.Optional)2