public void removeComponent(RADVisualComponent<?> radComp, int index) {
    // first store constraints in the meta component
    LayoutConstraints<?> constr = layoutDelegate.getConstraints(index);
    if (constr != null) {
      radComp.setLayoutConstraints(layoutDelegate.getClass(), constr);
    }
    // remove the component from layout
    layoutDelegate.removeComponent(index);
    // remove the component instance from the primary container instance
    if (!layoutDelegate.removeComponentFromContainer(
        getPrimaryContainer(),
        getPrimaryContainerDelegate(),
        (Component)
            radComp
                .getBeanInstance())) { // layout delegate does not support removing individual
                                       // components,
      // so we clear the container and add the remaining components again
      layoutDelegate.clearContainer(getPrimaryContainer(), getPrimaryContainerDelegate());

      RADVisualComponent<?>[] radComps = radContainer.getSubComponents();
      if (radComps.length > 1) {
        // we rely on that radcomp was not removed from the model yet
        Component[] comps = new Component[radComps.length - 1];
        for (int i = 0; i < radComps.length; i++) {
          if (i != index) {
            Component comp = (Component) radComps[i].getBeanInstance();
            comps[i < index ? i : i - 1] = comp;
          }
        }
        layoutDelegate.addComponentsToContainer(
            getPrimaryContainer(), getPrimaryContainerDelegate(), comps, 0);
      }
    }
  }
  // copy layout delegate from another container
  public void copyLayoutDelegateFrom(
      LayoutSupportManager sourceLayoutSupport, RADVisualComponent<?>[] newRadComps) {
    LayoutSupportDelegate sourceDelegate = sourceLayoutSupport.getLayoutDelegate();

    int componentCount = sourceDelegate.getComponentCount();

    Container cont = getPrimaryContainer();
    Container contDel = getPrimaryContainerDelegate();

    if (layoutDelegate != null) {
      removeLayoutDelegate(false);
    }

    Component[] primaryComps = new Component[componentCount];

    for (int i = 0; i < componentCount; i++) {
      RADVisualComponent<?> radComp = newRadComps[i];
      primaryComps[i] = (Component) radComp.getBeanInstance();
    }

    LayoutSupportDelegate newDelegate = sourceDelegate.cloneLayoutSupport(this, newRadComps);

    newDelegate.setLayoutToContainer(cont, contDel);
    newDelegate.addComponentsToContainer(cont, contDel, primaryComps, 0);

    layoutDelegate = newDelegate;

    // Ensure correct propagation of copied properties (issue 50011, 72351)
    try {
      layoutDelegate.acceptContainerLayoutChange(null);
    } catch (PropertyVetoException pvex) {
      // should not happen
    }
  }
 // Формирует список MarginConstraints
 protected final void gatherConstraints() {
   selectedConstraints = new ArrayList<>();
   for (RADVisualComponent<?> vc : selected) {
     LayoutSupportDelegate lsd = vc.getParentLayoutSupport().getLayoutDelegate();
     assert lsd instanceof MarginLayoutSupport;
     MarginLayoutConstraints mlc =
         (MarginLayoutConstraints) lsd.getConstraints(vc.getComponentIndex());
     selectedConstraints.add(mlc);
   }
 }
 // components adding/removing
 public void injectComponents(
     RADVisualComponent<?>[] components, LayoutConstraints<?>[] aConstraints, int index) {
   if (index <= -1) {
     index = layoutDelegate.getComponentCount();
   }
   layoutDelegate.addComponents(components, aConstraints, index);
   for (RADVisualComponent<?> component : components) {
     component.resetConstraintsProperties();
   }
 }
 public static LayoutConstraints<?> storeConstraints(RADVisualComponent<?> radComp) {
   LayoutSupportManager layoutSupport = radComp.getParentLayoutSupport();
   if (layoutSupport != null) {
     LayoutConstraints<?> constr = layoutSupport.getConstraints(radComp);
     if (constr != null) {
       radComp.setLayoutConstraints(layoutSupport.getLayoutDelegate().getClass(), constr);
     }
     return constr;
   } else {
     return null;
   }
 }
 public LayoutConstraints<?> getConstraints(RADVisualComponent<?> radComp) {
   if (layoutDelegate != null) {
     int index = radComp.getComponentIndex(); // radContainer.getIndexOf(radComp);
     return index >= 0 && index < layoutDelegate.getComponentCount()
         ? layoutDelegate.getConstraints(index)
         : null;
   } else {
     return null;
   }
 }
  @Override
  public void componentLayoutChanged(int index, PropertyChangeEvent ev)
      throws PropertyVetoException {
    RADVisualComponent<?> radComp = radContainer.getSubComponent(index);

    if (ev != null && ev.getPropertyName() != null) {
      layoutDelegate.acceptComponentLayoutChange(index, ev);

      FormModel formModel = radContainer.getFormModel();
      formModel.fireComponentLayoutChanged(
          radComp, ev.getPropertyName(), ev.getOldValue(), ev.getNewValue());

      if (radComp.getNodeReference() != null) // propagate the change to node
      {
        radComp
            .getNodeReference()
            .firePropertyChangeHelper(ev.getPropertyName(), ev.getOldValue(), ev.getNewValue());
      }
    } else {
      if (radComp.getNodeReference() != null) // propagate the change to node
      {
        radComp.getNodeReference().fireComponentPropertySetsChange();
      }
      radComp.resetConstraintsProperties();
    }
  }
  /**
   * Выравнивание выбранных компонентов в выбранном направлении.
   *
   * @param dimension направление выравнивания
   * @param alignment требуемое выравнивание
   * @throws java.lang.Exception
   */
  public void align(int dimension, int alignment) throws Exception {
    if (selected != null && selected.size() > 1) { // выбрано по крайней мере 2 компонента
      assert selected.size() == selectedConstraints.size();
      assert container != null;
      Dimension parentSize = container.getBeanInstance().getSize();
      if (dimension == 0) { // выравнирание в горизонтальном направлении
        if (alignment == LayoutConstants.LEADING) { // выравнивание влево
          // ищем самый левый компонент
          MarginConstraints first = findLeftCompConstraints(parentSize.width);
          int leftNew = calcLeft(first, parentSize.width); // новая левая координата
          // выравниваем остальные компоненты по найденому
          for (int i = 0; i < selectedConstraints.size(); i++) {
            MarginLayoutConstraints mlc = selectedConstraints.get(i);
            MarginConstraints mc = mlc.getConstraintsObject();
            RADVisualComponent<?> vc = selected.get(i);
            if (mc != first) {
              int leftOld = calcLeft(mc, parentSize.width); // старая левая координата
              int rightOld = calcRight(mc, parentSize.width); // старая правая координата
              if (mc.getLeft() != null) {
                Margin newLeft = mc.getLeft().copy();
                newLeft.setPlainValue(leftNew, parentSize.width); // выравниваем левую координату
                mlc.getMLeft().setValue(newLeft);
                if (mc.getRight()
                    != null) { // ширина не используется. нужно устанавливать правую координату
                  Margin newRight = mc.getRight().copy();
                  newRight.setPlainValue(
                      rightOld + (leftOld - leftNew),
                      parentSize.width); // изменяем правую координату
                  mlc.getMRight().setValue(newRight);
                }
              } else {
                Margin newRight = mc.getRight().copy();
                newRight.setPlainValue(
                    rightOld + (leftOld - leftNew), parentSize.width); // изменяем правую координату
                mlc.getMRight().setValue(newRight);
              }
            }
            // отметить компонент как invalid
            vc.getBeanInstance().invalidate();
          }
        }
        if (alignment == LayoutConstants.TRAILING) { // выравнивание вправо
          // ищем самый правый компонент
          MarginConstraints first = findRightCompConstraints(parentSize.width);
          int rightNew = calcRight(first, parentSize.width); // новая правая координата
          // выравниваем остальные компоненты по найденому
          for (int i = 0; i < selectedConstraints.size(); i++) {
            MarginLayoutConstraints mlc = selectedConstraints.get(i);
            MarginConstraints mc = mlc.getConstraintsObject();
            RADVisualComponent<?> vc = selected.get(i);
            if (mc != first) {
              int leftOld = calcLeft(mc, parentSize.width); // старая левая координата
              int rightOld = calcRight(mc, parentSize.width); // старая правая координата
              // выравнивание
              if (mc.getLeft() != null) {
                Margin newLeft = mc.getLeft().copy();
                newLeft.setPlainValue(
                    leftOld + (rightOld - rightNew), parentSize.width); // изменяем левую координату
                mlc.getMLeft().setValue(newLeft);
                if (mc.getRight() != null) // ширина не используется
                {
                  Margin newRight = mc.getRight().copy();
                  newRight.setPlainValue(
                      rightNew, parentSize.width); // выравниваем правая координату
                  //   }
                  mlc.getMRight().setValue(newRight);
                }
              } else {
                Margin newRight = mc.getRight().copy();
                newRight.setPlainValue(rightNew, parentSize.width); // выравниваем правая координату
                mlc.getMRight().setValue(newRight);
              }
            }
            // отметить компонент как invalid
            vc.getBeanInstance().invalidate();
          }
        }
        if (alignment == LayoutConstants.CENTER) { // выравнивание по центру
          // ищем самый левый компонент
          MarginConstraints firstL = findLeftCompConstraints(parentSize.width);
          int leftNew = calcLeft(firstL, parentSize.width); // новая левая координата
          // ищем самый правый компонент
          MarginConstraints firstR = findRightCompConstraints(parentSize.width);
          int rightNew = calcRight(firstR, parentSize.width); // новая правая координата;
          int centerNew = (parentSize.width - rightNew + leftNew) / 2; // новый центр
          // выравниваем остальные компоненты по найденому
          for (int i = 0; i < selectedConstraints.size(); i++) {
            MarginLayoutConstraints mlc = selectedConstraints.get(i);
            MarginConstraints mc = mlc.getConstraintsObject();
            RADVisualComponent<?> vc = selected.get(i);

            int leftOld = calcLeft(mc, parentSize.width); // старая левая координата
            int rightOld = calcRight(mc, parentSize.width); // старая правая координата
            int centerOld = (parentSize.width - rightOld + leftOld) / 2;
            // выравнивание
            int sm = centerNew - centerOld;
            if (mc.getLeft() != null) {
              Margin newLeft = mc.getLeft().copy();
              newLeft.setPlainValue(leftOld + sm, parentSize.width); // изменяем левую координату
              mlc.getMLeft().setValue(newLeft);
              if (mc.getRight() != null) // ширина не используется
              {
                Margin newRight = mc.getRight().copy();
                newRight.setPlainValue(
                    rightOld - sm, parentSize.width); // выравниваем правая координату
                mlc.getMRight().setValue(newRight);
              }
            } else {
              Margin newRight = mc.getRight().copy();
              newRight.setPlainValue(
                  rightOld - sm, parentSize.width); // выравниваем правая координату
              mlc.getMRight().setValue(newRight);
            }
            // отметить компонент как invalid
            vc.getBeanInstance().invalidate();
          }
        }
      }
      if (dimension == 1) { // выравнивание в вертикальном направлении
        if (alignment == LayoutConstants.LEADING) { // выравнивание вверх
          // ищем самый верхний компонент
          MarginConstraints first = findTopCompConstraints(parentSize.height);
          int topNew = calcTop(first, parentSize.height); // новая верхняя координата
          // выравниваем остальные компоненты по найденому
          for (int i = 0; i < selectedConstraints.size(); i++) {
            MarginLayoutConstraints mlc = selectedConstraints.get(i);
            MarginConstraints mc = mlc.getConstraintsObject();
            RADVisualComponent<?> vc = selected.get(i);
            if (mc != first) {
              int topOld = calcTop(mc, parentSize.height); // старая левая координата
              int bottomOld = calcBottom(mc, parentSize.height); // старая правая координата
              if (mc.getTop() != null) {
                Margin newTop = mc.getTop().copy();
                newTop.setPlainValue(topNew, parentSize.height); // выравниваем левую координату
                mlc.getMTop().setValue(newTop);
                if (mc.getBottom()
                    != null) { // ширина не используется. нужно устанавливать правую координату
                  Margin newBottom = mc.getBottom().copy();
                  newBottom.setPlainValue(
                      bottomOld + (topOld - topNew),
                      parentSize.height); // изменяем правую координату
                  mlc.getMBottom().setValue(newBottom);
                }
              } else {
                Margin newBottom = mc.getBottom().copy();
                newBottom.setPlainValue(
                    bottomOld + (topOld - topNew), parentSize.height); // изменяем правую координату
                mlc.getMBottom().setValue(newBottom);
              }
            }
            // отметить компонент как invalid
            vc.getBeanInstance().invalidate();
          }
        }
        if (alignment == LayoutConstants.TRAILING) { // выравнивание вниз
          // ищем самый нижный компонент
          MarginConstraints first = findBottomCompConstraints(parentSize.height);
          int bottomNew = calcBottom(first, parentSize.height); // новая нижняя координата
          // выравниваем остальные компоненты по найденому
          for (int i = 0; i < selectedConstraints.size(); i++) {
            MarginLayoutConstraints mlc = selectedConstraints.get(i);
            MarginConstraints mc = mlc.getConstraintsObject();
            RADVisualComponent<?> vc = selected.get(i);
            if (mc != null) {
              int topOld = calcTop(mc, parentSize.height); // старая верхняя координата
              int bottomOld = calcBottom(mc, parentSize.height); // старая нижняя координата
              // выравнивание
              if (mc.getTop() != null) {
                Margin newTop = mc.getTop().copy();
                newTop.setPlainValue(
                    topOld + (bottomOld - bottomNew),
                    parentSize.height); // изменяем левую координату
                mlc.getMTop().setValue(newTop);
                if (mc.getBottom() != null) { // ширина не используется
                  Margin newBottom = mc.getBottom().copy();
                  newBottom.setPlainValue(
                      bottomNew, parentSize.height); // выравниваем правая координату
                  mlc.getMBottom().setValue(newBottom);
                }
              } else {
                Margin newBottom = mc.getBottom().copy();
                newBottom.setPlainValue(
                    bottomNew, parentSize.height); // выравниваем правая координату
                mlc.getMBottom().setValue(newBottom);
              }
            }
            // отметить компонент как invalid
            vc.getBeanInstance().invalidate();
          }
        }
        if (alignment == LayoutConstants.CENTER) { // выравнивание по центру
          // ищем самый верхний компонент
          MarginConstraints firstT = findTopCompConstraints(parentSize.height);
          int topNew = calcTop(firstT, parentSize.height); // новая верхняя координата
          // ищем самый нижний компонент
          MarginConstraints firstR = findBottomCompConstraints(parentSize.height);
          int bottomNew = calcBottom(firstR, parentSize.height); // новая верхняя координата
          int centerNew = (parentSize.height - bottomNew + topNew) / 2; // новый центр
          // выравниваем остальные компоненты по найденому
          for (int i = 0; i < selectedConstraints.size(); i++) {
            MarginLayoutConstraints mlc = selectedConstraints.get(i);
            MarginConstraints mc = mlc.getConstraintsObject();
            RADVisualComponent<?> vc = selected.get(i);
            int topOld = calcTop(mc, parentSize.height); // старая верхняя координата
            int bottomOld = calcBottom(mc, parentSize.height); // старая нижняя координата
            int centerOld = (parentSize.height - bottomOld + topOld) / 2;
            // выравнивание
            int sm = centerNew - centerOld;
            if (mc.getTop() != null) {
              Margin newTop = mc.getTop().copy();
              newTop.setPlainValue(topOld + sm, parentSize.height); // изменяем верхняя координату
              mlc.getMTop().setValue(newTop);
              if (mc.getBottom() != null) // ширина не используется
              {
                Margin newBottom = mc.getBottom().copy();
                newBottom.setPlainValue(
                    bottomOld - sm, parentSize.height); // выравниваем нижняя координату
                mlc.getMBottom().setValue(newBottom);
              }
            } else {
              Margin newBottom = mc.getBottom().copy();
              newBottom.setPlainValue(
                  bottomOld - sm, parentSize.height); // выравниваем нижняя координату
              mlc.getMBottom().setValue(newBottom);
            }
            // отметить компонент как invalid
            vc.getBeanInstance().invalidate();
          }
        }
      }
    }
  }
 /**
  * Установка или сброс ширины/высоты компонента
  *
  * @param dimension направление: 0 - ширина, 1 - высота
  * @throws java.lang.Exception
  */
 public void resize(int dimension) throws Exception {
   if (selectedConstraints != null
       && !selectedConstraints.isEmpty()) { // выбрано по крайней мере 1 компонент
     assert selectedConstraints.size() == selected.size();
     for (int i = 0; i < selectedConstraints.size(); i++) {
       MarginLayoutConstraints mlc = selectedConstraints.get(i);
       MarginConstraints mc = mlc.getConstraintsObject();
       RADVisualComponent<?> vc = selected.get(i);
       Dimension parentSize = parentSize(vc);
       if (dimension == 0) { // горизонталь
         if (mc.getWidth() != null) { // Ширина используется. Cбрасываем.
           int w = mc.getWidth().calcPlainValue(parentSize.width);
           if (mc.getLeft() == null) {
             assert mc.getRight() != null;
             // Устанавливаем лево
             Margin newLeft = new Margin(0, true);
             newLeft.setPlainValue(
                 parentSize.width - mc.getRight().calcPlainValue(parentSize.width) - w,
                 parentSize.width);
             mlc.getMLeft().setValue(newLeft);
           } else {
             // Устанавливаем право
             Margin oldRight = mc.getRight();
             Margin newRight = new Margin(0, oldRight != null ? oldRight.absolute : true);
             newRight.setPlainValue(
                 parentSize.width - mc.getLeft().calcPlainValue(parentSize.width) - w,
                 parentSize.width);
             if (!newRight.isEqual(oldRight)) {
               mlc.getMRight().setValue(newRight);
             }
           }
           // сбрасываем ширину
           mlc.getMWidth().setValue(null);
         } else { // Ширина не используется. Устанавливаем
           int left = mc.getLeft().calcPlainValue(parentSize.width); // считываем левую координату
           int right =
               mc.getRight().calcPlainValue(parentSize.width); // считываем правую координату
           int w = parentSize.width - left - right;
           // устанавливаем ширину
           mlc.getMWidth().setValue(new Margin(w, true));
           mlc.getMRight().setValue(null);
         }
       } else if (dimension == 1) {
         if (mc.getHeight() != null) { // высота используется. сбрасываем
           int h = mc.getHeight().calcPlainValue(parentSize.height); // считываем высоту
           if (mc.getTop() == null) {
             Margin newTop = new Margin(0, true);
             newTop.setPlainValue(
                 parentSize.height - mc.getBottom().calcPlainValue(parentSize.height) - h,
                 parentSize.height);
             mlc.getMTop().setValue(newTop);
           } else {
             Margin oldBottom = mc.getBottom();
             Margin newBottom = new Margin(0, oldBottom != null ? oldBottom.absolute : true);
             newBottom.setPlainValue(
                 parentSize.height - mc.getTop().calcPlainValue(parentSize.height) - h,
                 parentSize.height);
             if (!newBottom.isEqual(oldBottom)) {
               mlc.getMBottom().setValue(newBottom);
             }
           }
           // сбрасываем высоту
           mlc.getMHeight().setValue(null);
         } else { // Высота не используется. Устанавливаем
           assert mc.getTop() != null && mc.getBottom() != null;
           int top = mc.getTop().calcPlainValue(parentSize.height); // считываем верхнюю координату
           int bottom =
               mc.getBottom().calcPlainValue(parentSize.height); // считываем нижнюю координату
           int h = parentSize.height - top - bottom;
           // устанавливаем высоту
           mlc.getMHeight().setValue(new Margin(h, true));
           mlc.getMBottom().setValue(null);
         }
       }
       // отметить компонент как invalid
       vc.getBeanInstance().invalidate();
     }
   }
 }
 protected Dimension parentSize(RADVisualComponent<?> aVisualComponent) {
   RADVisualContainer<?> radParent = aVisualComponent.getParentComponent();
   Container parentComp = radParent.getBeanInstance();
   Dimension parentSize = parentComp.getSize();
   return parentSize;
 }
 public LayoutConstraints<?> getStoredConstraints(RADVisualComponent<?> radComp) {
   return radComp.getLayoutConstraints(layoutDelegate.getClass());
 }