Search in sources :

Example 1 with LocaleChangeObserver

use of com.vaadin.flow.i18n.LocaleChangeObserver in project flow by vaadin.

the class EventUtilTest method collectLocaleChangeObserverFromElement.

@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());
}
Also used : HasElement(com.vaadin.flow.component.HasElement) Element(com.vaadin.flow.dom.Element) LocaleChangeObserver(com.vaadin.flow.i18n.LocaleChangeObserver) Test(org.junit.Test)

Example 2 with LocaleChangeObserver

use of com.vaadin.flow.i18n.LocaleChangeObserver in project flow by vaadin.

the class ComponentUtil method onComponentAttach.

/**
 * Handles triggering the {@link Component#onAttach(AttachEvent) onAttach}
 * method and firing the {@link AttachEvent} for the given component when it
 * has been attached to a UI.
 *
 * @param component
 *            the component attached to a UI
 * @param initialAttach
 *            indicates if this is the first time the component (element)
 *            has been attached
 */
public static void onComponentAttach(Component component, boolean initialAttach) {
    if (component instanceof Composite) {
        onComponentAttach(((Composite<?>) component).getContent(), initialAttach);
    }
    Optional<UI> ui = component.getUI();
    if (ui.isPresent() && component instanceof LocaleChangeObserver) {
        LocaleChangeEvent localeChangeEvent = new LocaleChangeEvent(ui.get(), ui.get().getLocale());
        ((LocaleChangeObserver) component).localeChange(localeChangeEvent);
    }
    AttachEvent attachEvent = new AttachEvent(component, initialAttach);
    component.onAttach(attachEvent);
    fireEvent(component, attachEvent);
    // internal state
    if (component instanceof HasEnabled && component.getElement().isEnabled() != component.getElement().getNode().isEnabledSelf()) {
        Element parent = component.getElement().getParent();
        if (parent != null && isAttachedToParent(component.getElement(), parent)) {
            component.onEnabledStateChanged(component.getElement().isEnabled());
        }
    }
}
Also used : LocaleChangeObserver(com.vaadin.flow.i18n.LocaleChangeObserver) MapToExistingElement(com.vaadin.flow.component.Component.MapToExistingElement) Element(com.vaadin.flow.dom.Element) LocaleChangeEvent(com.vaadin.flow.i18n.LocaleChangeEvent)

Example 3 with LocaleChangeObserver

use of com.vaadin.flow.i18n.LocaleChangeObserver in project flow by vaadin.

the class EventUtilTest method collectLocaleChangeObserverFromComponentList_elementHasVirtualChildren.

@Test
public void collectLocaleChangeObserverFromComponentList_elementHasVirtualChildren() throws Exception {
    Foo foo = new Foo();
    foo.getElement().appendChild(new Locale().getElement());
    Bar bar = new Bar();
    Element nested = new Element("nested-locale");
    nested.appendChild(new Element("nested-child"), new Locale().getElement());
    bar.getElement().appendChild(new Foo().getElement(), nested);
    List<LocaleChangeObserver> beforeNavigationObservers = EventUtil.collectLocaleChangeObservers(Arrays.asList(foo, bar));
    Assert.assertEquals("Wrong amount of listener instances found", 2, beforeNavigationObservers.size());
}
Also used : HasElement(com.vaadin.flow.component.HasElement) Element(com.vaadin.flow.dom.Element) LocaleChangeObserver(com.vaadin.flow.i18n.LocaleChangeObserver) Test(org.junit.Test)

Example 4 with LocaleChangeObserver

use of com.vaadin.flow.i18n.LocaleChangeObserver in project flow by vaadin.

the class EventUtilTest method collectLocaleChangeObserverFromComponentList.

@Test
public void collectLocaleChangeObserverFromComponentList() throws Exception {
    Foo foo = new Foo();
    foo.getElement().appendChild(new Locale().getElement());
    Bar bar = new Bar();
    Element nested = new Element("nested-locale");
    nested.appendChild(new Element("nested-child"), new Locale().getElement());
    bar.getElement().appendChild(new Foo().getElement(), nested);
    List<LocaleChangeObserver> beforeNavigationObservers = EventUtil.collectLocaleChangeObservers(Arrays.asList(foo, bar));
    Assert.assertEquals("Wrong amount of listener instances found", 2, beforeNavigationObservers.size());
}
Also used : HasElement(com.vaadin.flow.component.HasElement) Element(com.vaadin.flow.dom.Element) LocaleChangeObserver(com.vaadin.flow.i18n.LocaleChangeObserver) Test(org.junit.Test)

Example 5 with LocaleChangeObserver

use of com.vaadin.flow.i18n.LocaleChangeObserver in project flow by vaadin.

the class EventUtilTest method collectLocaleChangeObserverFromElement_elementHasVirtualChildren.

@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());
}
Also used : HasElement(com.vaadin.flow.component.HasElement) Element(com.vaadin.flow.dom.Element) LocaleChangeObserver(com.vaadin.flow.i18n.LocaleChangeObserver) Test(org.junit.Test)

Aggregations

Element (com.vaadin.flow.dom.Element)5 LocaleChangeObserver (com.vaadin.flow.i18n.LocaleChangeObserver)5 HasElement (com.vaadin.flow.component.HasElement)4 Test (org.junit.Test)4 MapToExistingElement (com.vaadin.flow.component.Component.MapToExistingElement)1 LocaleChangeEvent (com.vaadin.flow.i18n.LocaleChangeEvent)1