/** * Removes all contents from this component, this includes child components, * text content as well as child elements that have been added directly to * this component using the {@link Element} API. it also removes the * children that were added only at the client-side. */ default void removeAll() { getElement().removeAllChildren(); }
public static void add(HasComponents element, Component... components) { if (components == null) { throw new AssertionError(); } else { for (final Component component : components) { if (component == null) { throw new AssertionError(); } element.getElement().appendChild(component.getElement()); } } } }
/** * Adds the given components as children of this component. * * @param components * the components to add */ default void add(Component... components) { Objects.requireNonNull(components, "Components should not be null"); for (Component component : components) { Objects.requireNonNull(component, "Component to add cannot be null"); getElement().appendChild(component.getElement()); } }
/** * Adds the given component as child of this component at the specific * index. * * @param index * the index, where the component will be added. The index must * be non-negative and may not exceed the children count * @param component * the component to add, value should not be null */ default void addComponentAtIndex(int index, Component component) { Objects.requireNonNull(component, "Component should not be null"); if (index < 0) { throw new IllegalArgumentException( "Cannot add a component with a negative index"); } // The case when the index is bigger than the children count is handled // inside the method below getElement().insertChild(index, component.getElement()); }
/** * Removes the given child components from this component. * * @param components * the components to remove * @throws IllegalArgumentException * if any of the components is not a child of this component */ default void remove(Component... components) { Objects.requireNonNull(components, "Components should not be null"); for (Component component : components) { Objects.requireNonNull(component, "Component to remove cannot be null"); Element parent = component.getElement().getParent(); if (parent == null) { LoggerFactory.getLogger(HasComponents.class).debug( "Remove of a component with no parent does nothing."); return; } if (getElement().equals(parent)) { getElement().removeChild(component.getElement()); } else { throw new IllegalArgumentException("The given component (" + component + ") is not a child of this component"); } } }