Java Code Examples for com.vaadin.flow.component.Component#from()

The following examples show how to use com.vaadin.flow.component.Component#from() . 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: EventUtilTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test
public void collectBeforeNavigationObserversFromUI() throws Exception {
    UI ui = UI.getCurrent();
    Element node = ui.getElement();
    node.appendChild(new Element("main"), new Element("menu"));
    Element nested = new Element("nested");
    nested.appendChild(new Element("nested-child"),
            new Element("nested-child-2"));

    node.appendChild(nested);
    Component.from(nested, LeaveObserver.class);

    List<BeforeLeaveObserver> beforeNavigationObservers = EventUtil
            .collectBeforeLeaveObservers(ui);

    Assert.assertEquals("Wrong amount of listener instances found", 1,
            beforeNavigationObservers.size());
}
 
Example 2
Source File: EventUtilTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test
public void collectBeforeNavigationObserversFromUI_elementHasVirtualChildren()
        throws Exception {
    UI ui = UI.getCurrent();
    Element node = ui.getElement();
    node.appendChild(new Element("main"), new Element("menu"));
    Element nested = new Element("nested");
    nested.appendVirtualChild(new Element("nested-child"),
            new Element("nested-child-2"));

    node.appendChild(nested);

    node.getStateProvider().appendVirtualChild(node.getNode(),
            new Element("attached-by-id"), NodeProperties.INJECT_BY_ID,
            "id");

    Component.from(nested, LeaveObserver.class);

    List<BeforeLeaveObserver> beforeNavigationObservers = EventUtil
            .collectBeforeLeaveObservers(ui);

    Assert.assertEquals("Wrong amount of listener instances found", 1,
            beforeNavigationObservers.size());
}
 
Example 3
Source File: EventUtilTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test
public void collectAfterNavigationObservers() {
    UI ui = UI.getCurrent();

    Element menu = new Element("menu");
    menu.appendChild(new AfterObserver().getElement());

    Element node = ui.getElement();
    node.appendChild(new Element("main"), menu);
    Element nested = new Element("nested");
    nested.appendChild(new Element("nested-child"),
            new Element("nested-child-2"));

    node.appendChild(nested);
    Component.from(nested, AfterObserver.class);

    List<AfterNavigationObserver> beforeNavigationObservers = EventUtil
            .collectAfterNavigationObservers(ui);

    Assert.assertEquals("Wrong amount of listener instances found", 2,
            beforeNavigationObservers.size());
}
 
Example 4
Source File: EventUtilTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test
public void getImplementingComponents() throws Exception {
    Element node = new Element("root");
    node.appendChild(new Element("main"), new Element("menu"));
    Element nested = new Element("nested");
    nested.appendChild(new Element("nested-child"),
            new Element("nested-child-2"));

    node.appendChild(nested);
    Component.from(nested, EnterObserver.class);

    List<Element> elements = new ArrayList<>();

    EventUtil.inspectHierarchy(node, elements, element -> true);

    List<BeforeEnterObserver> listenerComponents = EventUtil
            .getImplementingComponents(elements.stream(),
                    BeforeEnterObserver.class)
            .collect(Collectors.toList());

    Assert.assertEquals("Wrong amount of listener instances found", 1,
            listenerComponents.size());
}
 
Example 5
Source File: EventUtilTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test
public void getImplementingComponents_elementHasVirtualChildren()
        throws Exception {
    Element node = new Element("root");
    node.appendChild(new Element("main"), new Element("menu"));
    Element nested = new Element("nested");
    nested.appendChild(new Element("nested-child"),
            new Element("nested-child-2"));

    node.getStateProvider().appendVirtualChild(node.getNode(), nested,
            NodeProperties.TEMPLATE_IN_TEMPLATE, "");
    Component.from(nested, EnterObserver.class);

    List<Element> elements = new ArrayList<>();

    EventUtil.inspectHierarchy(node, elements, element -> true);

    List<BeforeEnterObserver> listenerComponents = EventUtil
            .getImplementingComponents(elements.stream(),
                    BeforeEnterObserver.class)
            .collect(Collectors.toList());

    Assert.assertEquals("Wrong amount of listener instances found", 1,
            listenerComponents.size());
}
 
Example 6
Source File: EventUtilTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test
public void collectLocaleChangeObserverFromElement() throws Exception {
    Element node = new Element("root");
    node.appendChild(new Element("main"), new Element("menu"));
    Element nested = new Element("nested-locale");
    nested.appendChild(new Element("nested-child"),
            new Element("nested-child-2"));

    node.appendChild(nested);
    Component.from(nested, Locale.class);

    List<LocaleChangeObserver> beforeNavigationObservers = EventUtil
            .collectLocaleChangeObservers(node);

    Assert.assertEquals("Wrong amount of listener instances found", 1,
            beforeNavigationObservers.size());
}
 
Example 7
Source File: EventUtilTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test
public void collectLocaleChangeObserverFromElement_elementHasVirtualChildren()
        throws Exception {
    Element node = new Element("root");
    node.appendChild(new Element("main"), new Element("menu"));
    node.appendVirtualChild(new Element("main-virtual"),
            new Element("menu-virtual"));
    Element nested = new Element("nested-locale");
    nested.appendVirtualChild(new Element("nested-child"),
            new Element("nested-child-2"));

    node.appendVirtualChild(nested);
    Component.from(nested, Locale.class);

    List<LocaleChangeObserver> beforeNavigationObservers = EventUtil
            .collectLocaleChangeObservers(node);

    Assert.assertEquals("Wrong amount of listener instances found", 1,
            beforeNavigationObservers.size());
}
 
Example 8
Source File: IdMapper.java    From flow with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private void injectTemplateElement(Element element, Field field,
        Consumer<Element> beforeComponentInject) {
    Class<?> fieldType = field.getType();
    if (Component.class.isAssignableFrom(fieldType)) {
        beforeComponentInject.accept(element);
        Component component;

        Optional<Component> wrappedComponent = element.getComponent();
        if (wrappedComponent.isPresent()) {
            component = wrappedComponent.get();
        } else {
            Class<? extends Component> componentType = (Class<? extends Component>) fieldType;
            component = Component.from(element, componentType);
        }

        ReflectTools.setJavaFieldValue(template, field, component);
    } else if (Element.class.isAssignableFrom(fieldType)) {
        ReflectTools.setJavaFieldValue(template, field, element);
    } else {
        String msg = String.format(
                "The field '%s' in '%s' has an @'%s' "
                        + "annotation but the field type '%s' "
                        + "does not extend neither '%s' nor '%s'",
                field.getName(), getContainerClass().getName(),
                Id.class.getSimpleName(), fieldType.getName(),
                Component.class.getSimpleName(),
                Element.class.getSimpleName());

        throw new IllegalArgumentException(msg);
    }
}
 
Example 9
Source File: AttachExistingElementView.java    From flow with Apache License 2.0 5 votes vote down vote up
private void handleLabel(Element label) {
    attachedLabel = Component.from(label, Label.class);
    attachedLabel.setText("Client side label");
    attachedLabel.setId("label");

    add(AbstractDivView.createButton("Attach the already attached label",
            "attach-populated-label",
            event -> getElement().getStateProvider().attachExistingElement(
                    getElement().getNode(), "label", null,
                    this::handleAttachedLabel)));

    add(createButton("Remove myself on the server side", "remove-self",
            event -> event.getSource().getElement().removeFromParent()));
}
 
Example 10
Source File: AttachExistingElementView.java    From flow with Apache License 2.0 4 votes vote down vote up
private void handleHeader(Element header) {
    H1 lbl = Component.from(header, H1.class);
    lbl.setText("Client side header");
    lbl.setId("header");
}
 
Example 11
Source File: AttachExistingElementView.java    From flow with Apache License 2.0 4 votes vote down vote up
private void handleLabelInShadow(Element label) {
    Label lbl = Component.from(label, Label.class);
    lbl.setText("Client side label in shadow root");
    lbl.setId("label-in-shadow");
}