/**
   * Inserts a widget before the specified index.
   *
   * <p>By default, each child will fill the panel. To build more interesting layouts, set child
   * widgets' layout constraints using {@link #setWidgetLeftRight(Widget, double, Style.Unit,
   * double, Style.Unit)} and related methods.
   *
   * <p>Inserting a widget in this way has no effect on the DOM structure, but can be useful for
   * other panels that wrap LayoutPanel to maintain insertion order.
   *
   * @param widget the widget to be inserted
   * @param beforeIndex the index before which it will be inserted
   * @throws IndexOutOfBoundsException if <code>beforeIndex</code> is out of range
   */
  public void insert(Widget widget, int beforeIndex) {
    // Detach new child.
    widget.removeFromParent();

    // Logical attach.
    getChildren().insert(widget, beforeIndex);

    // Physical attach.
    Layer layer = layout.attachChild(widget.getElement(), widget);
    widget.setLayoutData(layer);

    // Adopt.
    adopt(widget);

    animate(0);
  }
  /**
   * Adds a widget to the specified edge of the dock. If the widget is already a child of this
   * panel, this method behaves as though {@link #remove(Widget)} had already been called.
   *
   * @param widget the widget to be added
   * @param direction the widget's direction in the dock
   * @param before the widget before which to insert the new child, or <code>null</code> to append
   */
  protected void insert(Widget widget, Direction direction, double size, Widget before) {
    assertIsChild(before);

    // Validation.
    if (before == null) {
      assert center == null : "No widget may be added after the CENTER widget";
    } else {
      assert direction != Direction.CENTER : "A CENTER widget must always be added last";
    }

    // Detach new child.
    widget.removeFromParent();

    // Logical attach.
    WidgetCollection children = getChildren();
    if (before == null) {
      children.add(widget);
    } else {
      int index = children.indexOf(before);
      children.insert(widget, index);
    }

    if (direction == Direction.CENTER) {
      center = widget;
    }

    // Physical attach.
    Layer layer =
        layout.attachChild(
            widget.getElement(), (before != null) ? before.getElement() : null, widget);
    LayoutData data = new LayoutData(direction, size, layer);
    widget.setLayoutData(data);

    // Adopt.
    adopt(widget);

    // Update the layout.
    animate(0);
  }