com.vaadin.flow.dom.Element.getComponent()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(10.1k)|赞(0)|评价(0)|浏览(137)

本文整理了Java中com.vaadin.flow.dom.Element.getComponent()方法的一些代码示例,展示了Element.getComponent()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Element.getComponent()方法的具体详情如下:
包路径:com.vaadin.flow.dom.Element
类名称:Element
方法名:getComponent

Element.getComponent介绍

[英]Gets the component this element has been mapped to, if any.
[中]获取此元素已映射到的组件(如果有)。

代码示例

代码示例来源:origin: com.vaadin/flow-server

/**
 * Gets the innermost mapped component for the element.
 * <p>
 * This returns {@link Element#getComponent()} if something else than a
 * {@link Composite} is mapped to the element. If a {@link Composite} is
 * mapped to the element, finds the innermost content of the
 * {@link Composite} chain.
 *
 * @param element
 *            the element which is mapped to a component
 * @return the innermost component mapped to the element
 */
public static Component getInnermostComponent(Element element) {
  assert element.getComponent().isPresent();
  Component component = element.getComponent().get();
  if (component instanceof Composite) {
    return ComponentUtil
        .getInnermostComponent((Composite<?>) component);
  }
  return component;
}

代码示例来源:origin: com.vaadin/vaadin-upload-flow

private Component getComponentAtSlot(String slot) {
  return getElement().getChildren()
      .filter(child -> slot.equals(child.getAttribute("slot")))
      .filter(child -> child.getComponent().isPresent())
      .map(child -> child.getComponent().get()).findFirst()
      .orElse(null);
}

代码示例来源:origin: com.vaadin/flow-server

/**
 * Collect elements with Component implementing listener of type T.
 *
 * @param elementStream
 *            collected elements
 * @param type
 *            class type to filter by
 * @param <T>
 *            type that is used in filtering
 * @return stream of components implementing T
 */
public static <T> Stream<T> getImplementingComponents(
    Stream<Element> elementStream, Class<T> type) {
  return elementStream.flatMap(
      o -> o.getComponent().map(Stream::of).orElseGet(Stream::empty))
      .filter(component -> type
          .isAssignableFrom(component.getClass()))
      .map(component -> (T) component);
}

代码示例来源:origin: com.vaadin/vaadin-text-field-flow

/**
   * Gets the first child component of the parent that is in the specified
   * slot.
   * 
   * @param parent
   *            the component to get child from, not {@code null}
   * @param slot
   *            the name of the slot inside the parent, not {@code null}
   * @return a child component of the parent in the specified slot, or
   *         {@code null} if none is found
   */
  public static Component getChildInSlot(HasElement parent, String slot) {
    Optional<Element> element = getElementsInSlot(parent, slot).findFirst();
    if (element.isPresent()) {
      return element.get().getComponent().get();
    }
    return null;
  }
}

代码示例来源:origin: com.vaadin/vaadin-accordion-flow

private static Optional<AccordionPanel> getOpenedPanel(Accordion accordion, Integer index) {
  return index == null || index >= accordion.getChildren().count() ? Optional.empty() :
      accordion.getElement().getChild(index).getComponent().map(AccordionPanel.class::cast);
}

代码示例来源:origin: com.vaadin/flow-server

/**
 * Finds the first component by traversing upwards in the element hierarchy,
 * starting from the given element.
 *
 * @param element
 *            the element from which to begin the search
 * @return optional of the component, empty if no component is found
 */
public static Optional<Component> findParentComponent(Element element) {
  Element mappedElement = element;
  while (mappedElement != null
      && !mappedElement.getComponent().isPresent()) {
    mappedElement = mappedElement.getParent();
  }
  if (mappedElement == null) {
    return Optional.empty();
  }
  return Optional.of(getInnermostComponent(mappedElement));
}

代码示例来源:origin: com.vaadin/flow-server

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

代码示例来源:origin: com.vaadin/flow-server

@SuppressWarnings("unchecked")
private void injectTemplateElement(Element element, Field field) {
  Class<?> fieldType = field.getType();
  if (Component.class.isAssignableFrom(fieldType)) {
    attachComponentIfUses(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(), templateClass.getName(),
        Id.class.getSimpleName(), fieldType.getName(),
        Component.class.getSimpleName(),
        Element.class.getSimpleName());
    throw new IllegalArgumentException(msg);
  }
}

代码示例来源:origin: com.vaadin/flow-server

Optional<Component> currentComponent = element.getComponent();
if (currentComponent.isPresent()) {

代码示例来源:origin: com.vaadin/flow-data

/**
   * Gets the index of the child element that represents the given item.
   * 
   * @param item
   *            the item to look for
   * @return the index of the child element that represents the item, or -1 if
   *         the item is not found
   */
  default int getItemPosition(T item) {
    if (item == null) {
      return -1;
    }
    return IntStream.range(0, getElement().getChildCount()).filter(i -> {
      Optional<Component> c = getElement().getChild(i).getComponent();
      return c.isPresent() && c.get() instanceof ItemComponent
          && item.equals(((ItemComponent<?>) c.get()).getItem());
    }).findFirst().orElse(-1);
  }
}

代码示例来源:origin: com.vaadin/flow-server

/**
 * Sets the content for this composite and attaches it to the element.
 * <p>
 * This method must only be called once.
 *
 * @param content
 *            the content for the composite
 */
private void setContent(T content) {
  assert content.getElement().getComponent()
      .isPresent() : "Composite should never be attached to an element which is not attached to a component";
  assert this.content == null : "Content has already been initialized";
  this.content = content;
  Element element = content.getElement();
  // Always replace the composite reference as this will be called from
  // inside out, so the end result is that the element refers to the
  // outermost composite in the probably rare case that multiple
  // composites are nested
  ElementUtil.setComponent(element, this);
}

代码示例来源:origin: com.vaadin/flow-server

/**
 * Sets the enabled state of the element.
 *
 * @param enabled
 *            the enabled state
 * @return the element
 */
public Element setEnabled(final boolean enabled) {
  getNode().setEnabled(enabled);
  Optional<Component> componentOptional = getComponent();
  if (componentOptional.isPresent()) {
    Component component = componentOptional.get();
    component.onEnabledStateChanged(enabled);
    informChildrenOfStateChange(enabled, component);
  }
  return getSelf();
}

代码示例来源:origin: com.vaadin/flow-server

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

代码示例来源:origin: com.vaadin/flow-server

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

代码示例来源:origin: com.vaadin/vaadin-app-layout-flow

@Override
public void showRouterLayoutContent(HasElement content) {
  Component component = content.getElement().getComponent().get();
  String target = null;
  if (component instanceof RouteNotFoundError) {
    getAppLayoutMenu().selectMenuItem(null);
  } else {
    target = UI.getCurrent().getRouter()
        .getUrl(component.getClass());
    getAppLayoutMenu().getMenuItemTargetingRoute(target)
        .ifPresent(item -> getAppLayoutMenu().selectMenuItem(item, false));
  }
  beforeNavigate(target, content);
  getAppLayout().setContent(content.getElement());
  afterNavigate(target, content);
}

代码示例来源:origin: com.vaadin/flow-server

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);
        });
  }
}

相关文章

微信公众号

最新文章

更多