Search in sources :

Example 26 with Component

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

the class RouterTest method getErrorText.

private String getErrorText(Component routeNotFoundError) {
    if (routeNotFoundError.getClass() == RouteNotFoundError.class) {
        Component errorContent = routeNotFoundError.getChildren().findFirst().get();
        Assert.assertEquals(Html.class, errorContent.getClass());
        return ((Html) errorContent).getInnerHtml().toString();
    } else {
        return routeNotFoundError.getElement().getText();
    }
}
Also used : Component(com.vaadin.flow.component.Component)

Example 27 with Component

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

the class ToStringTest method testViewsElementsStringable.

@Test
public void testViewsElementsStringable() throws Exception {
    Collection<Class<? extends Component>> viewClasses = new ViewClassLocator(getClass().getClassLoader()).getAllViewClasses();
    for (Class<? extends Component> viewClass : viewClasses) {
        Component view = viewClass.newInstance();
        String string = view.getElement().toString();
        Assert.assertNotNull(string);
        Assert.assertNotEquals("", string);
    }
}
Also used : Component(com.vaadin.flow.component.Component) ViewClassLocator(com.vaadin.flow.uitest.servlet.ViewClassLocator) Test(org.junit.Test)

Example 28 with Component

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

the class PublishedServerEventHandlerRpcHandler method handleNode.

@Override
public Optional<Runnable> handleNode(StateNode node, JsonObject invocationJson) {
    assert invocationJson.hasKey(JsonConstants.RPC_TEMPLATE_EVENT_METHOD_NAME);
    String methodName = invocationJson.getString(JsonConstants.RPC_TEMPLATE_EVENT_METHOD_NAME);
    if (methodName == null) {
        throw new IllegalArgumentException("Event handler method name may not be null");
    }
    JsonValue args = invocationJson.get(JsonConstants.RPC_TEMPLATE_EVENT_ARGS);
    if (args == null) {
        args = Json.createArray();
    }
    if (args.getType() != JsonType.ARRAY) {
        throw new IllegalArgumentException("Incorrect type for method arguments: " + args.getClass());
    }
    assert node.hasFeature(ComponentMapping.class);
    Optional<Component> component = node.getFeature(ComponentMapping.class).getComponent();
    if (!component.isPresent()) {
        throw new IllegalStateException("Unable to handle RPC template event JSON message: " + "there is no component available for the target node");
    }
    invokeMethod(component.get(), component.get().getClass(), methodName, (JsonArray) args);
    return Optional.empty();
}
Also used : JsonValue(elemental.json.JsonValue) ComponentMapping(com.vaadin.flow.internal.nodefeature.ComponentMapping) Component(com.vaadin.flow.component.Component)

Example 29 with Component

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

the class RouteRegistryInitializer method onStartup.

@Override
public void onStartup(Set<Class<?>> classSet, ServletContext servletContext) throws ServletException {
    try {
        if (classSet == null) {
            RouteRegistry.getInstance(servletContext).setNavigationTargets(Collections.emptySet());
            return;
        }
        Set<Class<? extends Component>> routes = validateRouteClasses(classSet.stream());
        RouteRegistry.getInstance(servletContext).setNavigationTargets(routes);
    } catch (InvalidRouteConfigurationException irce) {
        throw new ServletException("Exception while registering Routes on servlet startup", irce);
    }
}
Also used : ServletException(javax.servlet.ServletException) InvalidRouteConfigurationException(com.vaadin.flow.server.InvalidRouteConfigurationException) Component(com.vaadin.flow.component.Component)

Example 30 with Component

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

the class AngularTemplate method tryMapComponentOrElement.

@SuppressWarnings("unchecked")
private void tryMapComponentOrElement(Field field) {
    Optional<Id> idAnnotation = AnnotationReader.getAnnotationFor(field, Id.class);
    if (!idAnnotation.isPresent()) {
        return;
    }
    String id = idAnnotation.get().value();
    Class<?> fieldType = field.getType();
    String fieldName = field.getName();
    Element element = getElementById(id).orElseThrow(() -> new IllegalArgumentException(String.format("No element with id '%s' found while binding field '%s' in '%s'", id, fieldName, getClass().getName())));
    if (element.equals(getElement())) {
        throw new IllegalArgumentException("Cannot map the root element of the template. " + "This is always mapped to the template instance itself (" + getClass().getName() + ")");
    }
    if (Component.class.isAssignableFrom(fieldType)) {
        Class<? extends Component> componentType = (Class<? extends Component>) fieldType;
        Component c = Component.from(element, componentType);
        ReflectTools.setJavaFieldValue(this, field, c);
    } else if (Element.class.isAssignableFrom(fieldType)) {
        ReflectTools.setJavaFieldValue(this, field, element);
    } else {
        throw new IllegalArgumentException(String.format("The field '%s' in '%s' has an @'%s' " + "annotation but the field type '%s' " + "does not extend neither '%s' nor '%s'", fieldName, getClass().getName(), Id.class.getSimpleName(), fieldType.getName(), Component.class.getSimpleName(), Element.class.getSimpleName()));
    }
}
Also used : Element(com.vaadin.flow.dom.Element) Id(com.vaadin.flow.component.polymertemplate.Id) 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