All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.gluebooster.java.booster.basic.math.GeometryPositionConstraint Maven / Gradle / Ivy

package net.sf.gluebooster.java.booster.basic.math;

import java.awt.Rectangle;
import java.awt.Shape;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import edu.uci.ics.jung.graph.Graph;
import net.sf.gluebooster.java.booster.essentials.logging.LogBooster;

/**
 * Constraints for the positioning of geometrical objects.
 * 
 * 
 * @author CBauer
 *
 */
public class GeometryPositionConstraint {

	/**
	 * The log for infos.
	 */
	private LogBooster log = new LogBooster(getClass());

	/**
	 * These objects have additional connections to objects with a lower x
	 * (left). The objects have to be ordered according their y (lower first).
	 */
	private List edgesToLowerX;

	/**
	 * These objects have additional connections to objects with a higher x
	 * (right). The objects have to be ordered according their y (lower first).
	 */
	private List edgesToHigherX;

	/**
	 * These objects have additional connections to objects with a lower y. The
	 * objects have to be ordered according their x (lower first).
	 */
	private List edgesToLowerY;

	/**
	 * These objects have additional connections to objects with a higher y. The
	 * objects have to be ordered according their x (lower first).
	 */
	private List edgesToHigherY;

	/**
	 * The objects should be positioned within these bounds
	 */
	private Rectangle bounds;

	/**
	 * The minimum distance between objects
	 */
	private int minimumDistance;

	/**
	 * Should part results during the positioning be displayed.
	 */
	private boolean displayPartResults = false;

	public GeometryPositionConstraint() {

	}

	public GeometryPositionConstraint(List edgesToLowerX,
			List edgesToHigherX,
			List edgesToLowerY,
			List edgesToHigherY, Rectangle bounds) {
		super();
		this.edgesToLowerX = edgesToLowerX;
		this.edgesToHigherX = edgesToHigherX;
		this.edgesToLowerY = edgesToLowerY;
		this.edgesToHigherY = edgesToHigherY;
		this.bounds = bounds;
	}

	/**
	 * Returns the list. If the list is null an empty list is returned.
	 * 
	 * @param result
	 *            the returned list
	 * @return allways a list, never null
	 */
	private List getNonNullList(List result) {
		if (result == null)
			return Collections.EMPTY_LIST;
		else
			return result;
	}

	/**
	 * @return never null
	 */
	public List getEdgesToLowerX() {
		return getNonNullList(edgesToLowerX);
	}

	public void setEdgesToLowerX(List edgesToLowerX) {
		this.edgesToLowerX = edgesToLowerX;
	}

	/**
	 * @return never null
	 */
	public List getEdgesToHigherX() {
		return getNonNullList(edgesToHigherX);
	}

	public void setEdgesToHigherX(List edgesToHigherX) {
		this.edgesToHigherX = edgesToHigherX;
	}

	/**
	 * @return never null
	 */
	public List getEdgesToLowerY() {
		return getNonNullList(edgesToLowerY);
	}

	public void setEdgesToLowerY(List edgesToLowerY) {
		this.edgesToLowerY = edgesToLowerY;
	}

	/**
	 * @return never null
	 */
	public List getEdgesToHigherY() {
		return getNonNullList(edgesToHigherY);
	}

	public void setEdgesToHigherY(List edgesToHigherY) {
		this.edgesToHigherY = edgesToHigherY;
	}

	public Rectangle getBounds() {
		return bounds;
	}

	public void setBounds(Rectangle bounds) {
		this.bounds = bounds;
	}

	public int getMinimumDistance() {
		return minimumDistance;
	}

	public void setMinimumDistance(int minimumDistance) {
		this.minimumDistance = minimumDistance;
	}

	/**
	 * Returns all elements of one list without the elements of another list.
	 * 
	 * @param with
	 *            the first list with all elements
	 * @param without
	 *            the elements to be removed from the first list
	 * @return the sublist
	 */
	public List getObjectsWithButWithout(
			List with, List without) {
		ArrayList result = new ArrayList(
				with);
		result.removeAll(without);
		return result;
	}
	
	/**
	 * Gets objects that have edges to other objects with lower y but no edges
	 * to objects with higher y
	 * 
	 * @return the found objects
	 */
	public List getObjectsWithEdgesToLowerYButNotHigherY() {
		return getObjectsWithButWithout(getEdgesToLowerY(), getEdgesToHigherY());
	}

	/**
	 * Gets objects that have edges to other objects with higher y but no edges
	 * to objects with lower y
	 * 
	 * @return the found objects
	 */
	public List getObjectsWithEdgesToHigherYButNotLowerY() {
		return getObjectsWithButWithout(getEdgesToHigherY(), getEdgesToLowerY());
	}

	/**
	 * Gets objects that have edges to other objects with lower x but no edges
	 * to objects with higher x
	 * 
	 * @return the found objects
	 */
	public List getObjectsWithEdgesToLowerXButNotHigherX() {
		return getObjectsWithButWithout(getEdgesToLowerX(), getEdgesToHigherX());
	}

	/**
	 * Gets objects that have edges to other objects with higher x but no edges
	 * to objects with lower x
	 * 
	 * @return the found objects
	 */
	public List getObjectsWithEdgesToHigherXButNotLowerX() {
		return getObjectsWithButWithout(getEdgesToHigherX(), getEdgesToLowerX());
	}

	public boolean isDisplayPartResults() {
		return displayPartResults;
	}

	public void setDisplayPartResults(boolean displayPartResults) {
		this.displayPartResults = displayPartResults;
	}

	/**
	 * Has an object an edge to another object with lower x?
	 * 
	 * @param object
	 *            the object to inspect
	 * @return true if the edge exists
	 */
	public boolean hasEdgeToLowerX(ObjectToPosition object) {
		if (edgesToLowerX == null)
			return false;
		else
			return edgesToLowerX.contains(object);
	}

	/**
	 * Has an object an edge to another object with lower y?
	 * 
	 * @param object
	 *            the object to inspect
	 * @return true if the edge exists
	 */
	public boolean hasEdgeToLowerY(ObjectToPosition object) {
		if (edgesToLowerY == null)
			return false;
		else
			return edgesToLowerY.contains(object);
	}

	/**
	 * Has an object an edge to another object with higher x?
	 * 
	 * @param object
	 *            the object to inspect
	 * @return true if the edge exists
	 */
	public boolean hasEdgeToHigherX(ObjectToPosition object) {
		if (edgesToHigherX == null)
			return false;
		else
			return edgesToHigherX.contains(object);
	}

	/**
	 * Has an object an edge to another object with higher y?
	 * 
	 * @param object
	 *            the object to inspect
	 * @return true if the edge exists
	 */
	public boolean hasEdgeToHigherY(ObjectToPosition object) {
		if (edgesToHigherY == null)
			return false;
		else
			return edgesToHigherY.contains(object);
	}

	/**
	 * if one rectange is outside another add a corresponding edge for a given object.
	 * 
	 * @param ifThisRectangleisOutside
	 *            the rectangle that should be outside
	 * @param ofThisRectangle
	 *            the other rectangle
	 * @param addAnEdgeToThisObject
	 *            the object to which an edge should be created.
	 */
	public void addEdgesToLowerHigher(Rectangle ifThisRectangleisOutside,
			Rectangle ofThisRectangle, ObjectToPosition addAnEdgeToThisObject) {
		int x = ifThisRectangleisOutside.x;
		int y = ifThisRectangleisOutside.y;

		if (x < ofThisRectangle.x)
			edgesToLowerX.add(addAnEdgeToThisObject);
		if (x > ofThisRectangle.x + ofThisRectangle.width)
			edgesToHigherX.add(addAnEdgeToThisObject);
		if (y < ofThisRectangle.y)
			edgesToLowerY.add(addAnEdgeToThisObject);
		if (y > ofThisRectangle.y + ofThisRectangle.height)
			edgesToHigherY.add(addAnEdgeToThisObject);

	}

	/**
	 * Checks whether a graph is within the given constraints
	 * 
	 * @param graphToCheck
	 *            the graph to check
	 * @param throwException
	 *            should an exception be thrown if the check fails (if not only logging occurs).
	 */
	public void check(Graph graphToCheck,
			boolean throwException) {
		// check bounds
		if (bounds != null) {
			for (Shape shape : graphToCheck.getVertices()) {
				if (!bounds.contains(shape.getBounds())) {
					String message = "graph is not inside bounds " + shape
							+ " " + bounds;

					log.info(message);
					String testcode = GeometryBoostUtils.createTestSourcecode(
							(Graph) graphToCheck,
							(GeometryPositionConstraint) this);
					log.info("Testcode is\n", testcode);

					if (throwException) {
						throw new IllegalStateException(message);
					}

				}
			}
		}
	}
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy