package org.expeditee.core; import org.expeditee.core.bounds.AxisAlignedBoxBounds; /** * Class for anchoring the position of items within the frame. * Anchoring is when the position of the item is defined relative * to the edge of the frame instead of being defined in absolute * coordinates. Can't be anchored to the left and right edge at * the same time, nor the top and bottom edge at the same time. * * @author cts16 */ public class Anchoring { /** The relative distance from the left or right edge (null means unanchored). */ private Integer _xAnchor = null; /** The relative distance from the top or bottom edge (null means unanchored). */ private Integer _yAnchor = null; /** Whether the x-anchor is relative to the left or right edge. */ private boolean _left = true; /** Whether the y-anchor is relative to the top or bottom edge. */ private boolean _top = true; public Anchoring() { } public void setXAnchor(Integer offset, boolean left) { _xAnchor = offset; _left = left; } public void setYAnchor(Integer offset, boolean top) { _yAnchor = offset; _top = top; } public void setXAnchor(Anchoring other) { if (other == null) return; setXAnchor(other._xAnchor, other._left); } public void setYAnchor(Anchoring other) { if (other == null) return; setYAnchor(other._yAnchor, other._top); } public void setLeftAnchor(Integer offset) { setXAnchor(offset, true); } public void setRightAnchor(Integer offset) { setXAnchor(offset, false); } public void setTopAnchor(Integer offset) { setYAnchor(offset, true); } public void setBottomAnchor(Integer offset) { setYAnchor(offset, false); } public void removeXAnchor() { setXAnchor(null, _left); } public void removeYAnchor() { setYAnchor(null, _top); } public void removeAllAnchors() { removeXAnchor(); removeYAnchor(); } public Integer getXPositionInBox(AxisAlignedBoxBounds box) { if (box == null || _xAnchor == null) return null; if (_left) { return box.getMinX() + _xAnchor; } else { return box.getMaxX() - _xAnchor; } } public Integer getYPositionInBox(AxisAlignedBoxBounds box) { if (box == null || _yAnchor == null) return null; if (_top) { return box.getMinY() + _yAnchor; } else { return box.getMaxY() - _yAnchor; } } public Point getPositionInBox(AxisAlignedBoxBounds box) { Integer x = getXPositionInBox(box); Integer y = getYPositionInBox(box); if (x == null || y == null) return null; return new Point(x, y); } public boolean isXAnchored() { return _xAnchor != null; } public boolean isYAnchored() { return _yAnchor != null; } public boolean isAnchored() { return isXAnchored() || isYAnchored(); } public Integer getLeftAnchor() { return _left ? _xAnchor : null; } public Integer getRightAnchor() { return !_left ? _xAnchor : null; } public Integer getTopAnchor() { return _top ? _yAnchor : null; } public Integer getBottomAnchor() { return !_top ? _yAnchor : null; } }