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

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

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

import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.util.Arrays;

import javax.swing.Icon;

import org.apache.commons.collections4.Transformer;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;

import net.sf.gluebooster.java.booster.essentials.awt.Painter2D;
import net.sf.gluebooster.java.booster.essentials.math.Condition;
import net.sf.gluebooster.java.booster.essentials.meta.objects.ObjectHolder;
import net.sf.gluebooster.java.booster.essentials.utils.ThrowableBoostUtils;

/**
 * Transforms operations into icons
 * 
 * @author cbauer
 *
 */
public class OperationIconTransformer implements Transformer {

	/**
	 * Painter of the (pre and post)conditions
	 */
	private Painter2D conditionPainter;

	public OperationIconTransformer() {

	}

	public OperationIconTransformer(Painter2D conditionPainter) {
		this.conditionPainter = conditionPainter;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Icon transform(Operation operation) {
		try {

			Condition precondition = operation.getPrecondition();
			Condition postcondition = operation.getPostcondition();
			Condition globalPrecondition = operation.getGlobalPrecondition();
			Condition globalPostcondition = operation.getGlobalPostcondition();

			// result
			// 0/0
			// precondition -----------------postcondition-------
			// ---------------------circle-----------------------
			// global precondition --------- global postcondition

			// it is a condition that the partial shapes are only in the positive range (and start with 0 to avoid too much empty space)
			Rectangle2D preconditionBounds = conditionPainter.getBounds2D(precondition);// new Rectangle(0, 0, 80, 20);
			Rectangle2D postconditionBounds = conditionPainter.getBounds2D(postcondition);// new Rectangle(0, 0, 80, 10);
			Rectangle2D globalPreconditionBounds = conditionPainter.getBounds2D(globalPrecondition);// new Rectangle(0, 0, 60, 20);
			Rectangle2D globalPostconditionBounds = conditionPainter.getBounds2D(globalPostcondition);// new Rectangle(0, 0, 60, 10);

			int margin = 10;
			int circleWidth = 10;

			int leftWidth = Math.max((int) preconditionBounds.getMaxX(), (int) globalPreconditionBounds.getMaxX());
			int upperHeight = Math.max((int) preconditionBounds.getMaxY(), (int) postconditionBounds.getMaxY());

			int rightX = leftWidth + margin + circleWidth + margin;
			int lowerY = upperHeight + margin + circleWidth + margin;

			Ellipse2D.Double center = new Ellipse2D.Double(leftWidth + margin, upperHeight + margin, circleWidth, circleWidth);
			Pair operationText = new ImmutablePair(AffineTransform.getTranslateInstance(0, upperHeight), operation.getOperatorName());

			Pair postconditionTransformed = new ImmutablePair(AffineTransform.getTranslateInstance(rightX, 0),
					new ImmutablePair(conditionPainter, postcondition));
			Pair globalPostconditionTransformed = new ImmutablePair(AffineTransform.getTranslateInstance(rightX, lowerY),
					new ImmutablePair(conditionPainter, globalPostcondition));
			Pair globalPreconditionTransformed = new ImmutablePair(AffineTransform.getTranslateInstance(0, lowerY),
					new ImmutablePair(conditionPainter, globalPrecondition));

			return ObjectHolder.createObjectHolder(Arrays.asList(center, operationText, new ImmutablePair(conditionPainter, precondition),
					postconditionTransformed, globalPreconditionTransformed, globalPostconditionTransformed));
		} catch (Exception ex) {
			throw ThrowableBoostUtils.toRuntimeException(ex);
		}
	}

	public Painter2D getConditionPainter() {
		return conditionPainter;
	}

	public void setConditionPainter(Painter2D conditionPainter) {
		this.conditionPainter = conditionPainter;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy