@Override public void addLayoutComponent(Component aComponent, Object aConstraints) { if (aConstraints instanceof MarginConstraints) { MarginConstraints constraints = (MarginConstraints) aConstraints; // horizontal check if (constraints.getWidth() == null && (constraints.getLeft() == null || constraints.getRight() == null)) { throw new IllegalStateException(MarginConstraints.HORIZONTAL_VALUE_MISSING); } else if (constraints.getWidth() != null && constraints.getLeft() == null && constraints.getRight() == null) { throw new IllegalStateException(MarginConstraints.HORIZONTAL_VALUE_MISSING); } // vertical check if (constraints.getHeight() == null && (constraints.getTop() == null || constraints.getBottom() == null)) { throw new IllegalStateException(MarginConstraints.HORIZONTAL_VALUE_MISSING); } else if (constraints.getHeight() != null && constraints.getTop() == null && constraints.getBottom() == null) { throw new IllegalStateException(MarginConstraints.HORIZONTAL_VALUE_MISSING); } layouted.put(aComponent, constraints); } else { throw new IllegalArgumentException("The constraints must be instance of MarginConstraints"); } }
public static void ajustLeft(java.awt.Component aChild, int aValue) { assert aChild.getParent().getLayout() instanceof MarginLayout; MarginLayout layout = (MarginLayout) aChild.getParent().getLayout(); MarginConstraints anchors = layout.getLayoutConstraints(aChild); int containerWidth = aChild.getParent().getWidth(); int childWidth = aChild.getWidth(); if (anchors.getLeft() != null && anchors.getWidth() != null) { anchors.getLeft().setPlainValue(aValue, containerWidth); } else if (anchors.getWidth() != null && anchors.getRight() != null) { anchors.getRight().setPlainValue(containerWidth - aValue - childWidth, containerWidth); } else if (anchors.getLeft() != null && anchors.getRight() != null) { anchors.getLeft().setPlainValue(aValue, containerWidth); anchors.getRight().setPlainValue(containerWidth - aValue - childWidth, containerWidth); } aChild.getParent().revalidate(); aChild.getParent().repaint(); }