/** * Adds `confirm` event listener */ public Registration addConfirmListener( ComponentEventListener<ConfirmEvent> listener) { return ComponentUtil.addListener(this, ConfirmEvent.class, listener); }
/** * Creates a new component instance using the given element. * <p> * You can use this method when you have an element instance and want to use * it through the API of a {@link Component} class. * <p> * This method attaches the component instance to the element so that * {@link Element#getComponent()} returns the component instance. This means * that {@link #getParent()}, {@link #getChildren()} and other methods which * rely on {@link Element} -> {@link Component} mappings will work * correctly. * <p> * Note that only one {@link Component} can be mapped to any given * {@link Element}. * * @see Element#as(Class) * * @param <T> * the component type to create * @param element * the element to wrap * @param componentType * the component type * @return the component instance connected to the given element */ public static <T extends Component> T from(Element element, Class<T> componentType) { return ComponentUtil.componentFromElement(element, componentType, true); }
onComponentDetach(((Composite<?>) component).getContent()); fireEvent(component, detachEvent); if (parent.isPresent()) { Component parentComponent = parent.get(); boolean state = isAttachedToParent(component, parentComponent) ? checkParentChainState(parentComponent) : component.getElement().getNode().isEnabledSelf(); component.onEnabledStateChanged(state);
boolean initialAttach) { if (component instanceof Composite) { onComponentAttach(((Composite<?>) component).getContent(), initialAttach); fireEvent(component, attachEvent); && isAttachedToParent(component, parent.get())) { component.onEnabledStateChanged( component.getElement().isEnabled());
/** * Finds the first component instance in each {@link Element} subtree by * traversing the {@link Element} tree starting from the given element. * * @param element * the element to start scanning from * @param componentConsumer * a consumer which is called for each found component */ public static void findComponents(Element element, Consumer<Component> componentConsumer) { assert element != null; assert componentConsumer != null; Optional<Component> maybeComponent = element.getComponent(); if (maybeComponent.isPresent()) { Component component = maybeComponent.get(); componentConsumer.accept(component); return; } element.getChildren().forEach(childElement -> ComponentUtil .findComponents(childElement, componentConsumer)); }
event -> ComponentUtil.fireEvent(thisComponent, new ClickEvent<>(thisComponent)), key).withModifiers(keyModifiers);
private static boolean checkParentChainState(Component component) { if (!component.getElement().getNode().isEnabledSelf()) { return false; } Optional<Component> parent = component.getParent(); if (parent.isPresent()) { Component parentComponent = parent.get(); if (isAttachedToParent(component, parentComponent)) { return checkParentChainState(parentComponent); } } return true; }
/** * Gets the parent component of this component. * <p> * A component can only have one parent. * * @return an optional parent component, or an empty optional if the * component is not attached to a parent */ public Optional<Component> getParent() { // If "this" is a component inside a Composite, iterate from the // Composite downwards Optional<Component> mappedComponent = getElement().getComponent(); if (!mappedComponent.isPresent()) { throw new IllegalStateException( "You cannot use getParent() on a wrapped component. Use Component.wrapAndMap to include the component in the hierarchy"); } if (isInsideComposite(mappedComponent.get())) { Component parent = ComponentUtil.getParentUsingComposite( (Composite<?>) mappedComponent.get(), this); return Optional.of(parent); } // Find the parent component based on the first parent element which is // mapped to a component return ComponentUtil.findParentComponent(getElement().getParent()); }
/** * Gets the UI this component is attached to. * * @return an optional UI component, or an empty optional if this component * is not attached to a UI */ public Optional<UI> getUI() { Optional<Component> parent = getParent(); if (parent.isPresent()) { return parent.flatMap(Component::getUI); } else if (getElement().getParentNode() instanceof ShadowRoot) { parent = ComponentUtil.findParentComponent( ((ShadowRoot) getElement().getParentNode()).getHost()); return parent.flatMap(Component::getUI); } return Optional.empty(); }
@Override public Stream<Component> getChildren() { Builder<Component> childComponents = Stream.builder(); container.getChildren().forEach(childElement -> ComponentUtil .findComponents(childElement, childComponents::add)); return childComponents.build(); }
private void setI18n(CrudI18n i18n, boolean fireEvent) { getElement().setPropertyJson("i18n", JsonSerializer.toJson(i18n)); if (fireEvent) { ComponentUtil.fireEvent(this.grid, new CrudI18nUpdatedEvent(this, false, i18n)); } }
/** * Adds a {@code compositionstart} listener to this component. * * @param listener * the listener to add, not <code>null</code> * @return a handle that can be used for removing the listener */ default Registration addCompositionStartListener( ComponentEventListener<CompositionStartEvent> listener) { return ComponentUtil.addListener((Component) this, CompositionStartEvent.class, listener); }
/** * Gets the child components of this component. * <p> * The default implementation finds child components by traversing each * child {@link Element} tree. * <p> * If the component is injected to a PolymerTemplate using the * <code>@Id</code> annotation the getChildren method will only return * children added from the server side and will not return any children * declared in the template file. * * @see Id * * @return the child components of this component */ public Stream<Component> getChildren() { // This should not ever be called for a Composite as it will return // wrong results assert !(this instanceof Composite); if (!getElement().getComponent().isPresent()) { throw new IllegalStateException( "You cannot use getChildren() on a wrapped component. Use Component.wrapAndMap to include the component in the hierarchy"); } Builder<Component> childComponents = Stream.builder(); getElement().getChildren().forEach(childElement -> ComponentUtil .findComponents(childElement, childComponents::add)); return childComponents.build(); }
ComponentUtil.fireEvent(component, createValueChange(oldValue, fromClient));
/** * Creates a new component instance using this element. * <p> * You can use this method when you have an element instance and want to use * it through the API of a {@link Component} class. * <p> * This method makes the component instance use the underlying element but * does not attach the new component instance to the element so that * {@link Element#getComponent()} would return the component instance. This * means that {@link #getParent()}, {@link #getChildren()} and possibly * other methods which rely on {@link Element} -> {@link Component} * mappings will not work. * <p> * To also map the element to the {@link Component} instance, use * {@link Component#from(Element, Class)} * * @see Component#from(Element, Class) * * @param <T> * the component type * @param componentType * the component type * @return the component instance connected to the given element */ public <T extends Component> T as(Class<T> componentType) { return ComponentUtil.componentFromElement(this, componentType, false); }
/** * Registers a listener to be notified whenever a panel is opened or closed. * * @param listener the listener to be notified * @return a handle to the registered listener which could also be used to unregister it. */ public Registration addOpenedChangeListener( ComponentEventListener<OpenedChangeEvent> listener) { return ComponentUtil.addListener(this, OpenedChangeEvent.class, listener); }
/** * Adds a {@code compositionupdate} listener to this component. * * @param listener * the listener to add, not <code>null</code> * @return a handle that can be used for removing the listener */ default Registration addCompositionUpdateListener( ComponentEventListener<CompositionUpdateEvent> listener) { return ComponentUtil.addListener((Component) this, CompositionUpdateEvent.class, listener); }
/** * Adds a {@code compositionend} listener to this component. * * @param listener * the listener to add, not <code>null</code> * @return a handle that can be used for removing the listener */ default Registration addCompositionEndListener( ComponentEventListener<CompositionEndEvent> listener) { return ComponentUtil.addListener((Component) this, CompositionEndEvent.class, listener); }
/** * Adds `cancel` event listener */ public Registration addCancelListener( ComponentEventListener<CancelEvent> listener) { return ComponentUtil.addListener(this, CancelEvent.class, listener); }
/** * Adds `reject` event listener */ public Registration addRejectListener( ComponentEventListener<RejectEvent> listener) { return ComponentUtil.addListener(this, RejectEvent.class, listener); }