/** * Gets the root element of this composite. * <p> * For a composite, the root element is the same as the root element of the * content of the composite. * * @return the root element of this component */ @Override public Element getElement() { return getContent().getElement(); }
private void registerLifecycleOwner(Component owner) { assert owner != null; lifecycleOwner = owner; // since we are attached, UI should be available Registration attachRegistration = owner.addAttachListener(e -> queueBeforeExecutionCallback()); // remove shortcut listener when detached Registration detachRegistration = owner.addDetachListener(e -> removeListenerRegistration()); lifecycleRegistration = new CompoundRegistration(attachRegistration, detachRegistration); }
@Override public DefaultComponentConfigurator id(String id) { component.setId(id); return this; }
/** * 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(); }
/** * 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()); }
Optional<UI> ui = component.getUI(); if (ui.isPresent() && component instanceof LocaleChangeObserver) { LocaleChangeEvent localeChangeEvent = new LocaleChangeEvent( component.onAttach(attachEvent); fireEvent(component, attachEvent); && component.getElement().isEnabled() != component.getElement() .getNode().isEnabledSelf()) { Optional<Component> parent = component.getParent(); if (parent.isPresent() && isAttachedToParent(component, parent.get())) { component.onEnabledStateChanged( component.getElement().isEnabled());
component.onDetach(detachEvent); fireEvent(component, detachEvent); && component.getElement().isEnabled() != component.getElement() .getNode().isEnabledSelf()) { Optional<Component> parent = component.getParent(); if (parent.isPresent()) { Component parentComponent = parent.get(); boolean state = isAttachedToParent(component, parentComponent) ? checkParentChainState(parentComponent) : component.getElement().getNode().isEnabledSelf(); component.onEnabledStateChanged(state); } else { component.onEnabledStateChanged( component.getElement().isEnabled());
private void informChildrenOfStateChange(boolean enabled, Component component) { component.getChildren().forEach(child -> { child.onEnabledStateChanged( enabled ? child.getElement().isEnabled() : false); informChildrenOfStateChange(enabled, child); }); if (component.getElement().getNode() .hasFeature(VirtualChildrenList.class)) { component.getElement().getNode() .getFeatureIfInitialized(VirtualChildrenList.class) .ifPresent(list -> { final Consumer<Component> stateChangeInformer = virtual -> { virtual.onEnabledStateChanged(enabled ? virtual.getElement().isEnabled() : false); informChildrenOfStateChange(enabled, virtual); }; final Consumer<StateNode> childNodeConsumer = childNode -> Element .get(childNode).getComponent() .ifPresent(stateChangeInformer); list.forEachChild(childNodeConsumer); }); } }
/** * Assigns the tooltip to a specific component. * <p> * The tooltip is removed from the parent after the component that the tooltip is * attached is detached. * * @param component the tooltip is attached to this component * @param appended <code>true</code> the tooltip is automatically appended * to the component's father.<code>false</code>, * it is not appended. It should be added to a layout manually. */ public void attachToComponent(Component component, boolean appended) { Objects.requireNonNull(component); getElement().getNode().runWhenAttached(ui -> { ui.getPage().executeJavaScript("$0.targetElement = $1;", getElement(), component.getElement() ); }); if ( appended ){ component.getElement().getNode().runWhenAttached(ui -> { component.getElement().getParentNode().appendChild(getElement()); }); } if ( detachedRegistration != null ){ detachedRegistration.remove(); } detachedRegistration = component.addDetachListener(event -> { this.getElement().removeFromParent(); }); }
private void registerOwnerListener() { assert listenOnSupplier != null; listenOnComponent = listenOnSupplier.get(); if (listenOnComponent == null) { throw new IllegalStateException( String.format("Could register shortcut listener for %s. " + "%s<%s> supplied a null value.", this.toString(), SerializableSupplier.class.getSimpleName(), Component.class.getSimpleName())); } if (!(listenOnComponent instanceof UI)) { listenOnAttachListenerRegistration = new CompoundRegistration(); listenOnAttachListenerRegistration.addRegistration( listenOnComponent.addAttachListener( attachEvent -> updateHandlerListenerRegistration())); listenOnAttachListenerRegistration.addRegistration( listenOnComponent.addDetachListener( detachEvent -> removeListenerRegistration())); } // either the scope is an active UI, or the component is attached to an // active UI - in either case, we want to update dom registration if (listenOnComponent.getUI().isPresent()) { updateHandlerListenerRegistration(); } }
@Override public DefaultComponentConfigurator withDetachListener(ComponentEventListener<DetachEvent> listener) { component.addDetachListener(listener); return this; }
@Override public DefaultComponentConfigurator withAttachListener(ComponentEventListener<AttachEvent> listener) { component.addAttachListener(listener); return this; }
/** * Removes a panel based on the content * * @param content the non-null content of the panel to be removed */ public void remove(Component content) { Objects.requireNonNull(content, "The content of the panel to be removed cannot be null"); if (content instanceof AccordionPanel) { remove((AccordionPanel) content); return; } if (content.getParent().isPresent()) { final Optional<Component> grandParent = content.getParent().get().getParent(); if (grandParent.isPresent() && grandParent.get() instanceof AccordionPanel) { remove((AccordionPanel) grandParent.get()); return; } } throw new IllegalArgumentException("The supplied content is not a descendant of this Accordion"); }
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; }
/** * Constructor. * @param content Form composition content (not null) */ public AbstractComposable(C content) { super(); ObjectUtils.argumentNotNull(content, "Content component must be not null"); this.content = content; // compose on attach content.addAttachListener(e -> { // check compose on attach if not already composed if (!isComposed() && isComposeOnAttach()) { compose(); } }); }
@Override public DefaultComponentConfigurator withEventListener(String eventType, DomEventListener listener) { component.getElement().addEventListener(eventType, listener); return this; }
component.setId(COMPONENT_WITH_VARIANTS_ID);
/** * Sets confirmation dialog custom header element * * @param component * the component to display instead of default header text */ public void setHeader(Component component) { setHeader(component.getElement()); }
private void initView() { HasValidation field = getValidationComponent(); ((Component) field).setId("field"); add(((Component) field)); NativeButton button = new NativeButton("Make the input invalid"); button.setId("invalidate"); button.addClickListener(event -> { field.setErrorMessage("Invalidated from server"); field.setInvalid(true); }); add(button); button = new NativeButton("Make the input valid"); button.setId("validate"); button.addClickListener(event -> { field.setErrorMessage(null); field.setInvalid(false); }); add(button); }
/** * Gets the component visibility value. * * @return {@code true} if the component is visible, {@code false} otherwise */ public boolean isVisible() { return getElement().isVisible(); }