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);
}
}
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());
}
}
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;
}
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));
}
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());
}
Aggregations