/** * 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()); }