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

net.sf.gluebooster.java.booster.basic.transformation.CallableToShape Maven / Gradle / Ivy

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

import java.awt.Rectangle;
import java.awt.Shape;

import net.sf.gluebooster.java.booster.essentials.eventsCommands.Callable;
import net.sf.gluebooster.java.booster.essentials.eventsCommands.CallableAbstraction;

/**
 * Transformation to a shape.
 * 
 * @defaultParamText name name of the callable
 * 
 * @author cbauer
 *
 * @param 
 */
public class CallableToShape extends CallableAbstraction {

	/**
	 * Create a string within a rectangle
	 */
	private static final String TYPE_RECTANGLE_AROUND_STRING = "rectangle around string";

	/**
	 * Converts objects to strings. Used for type TYPE_RECTANGLE_AROUND_STRING
	 */
	private Callable toString;

	/**
	 * Used by tests
	 */
	private CallableToShape() {

	}

	/**
	 * 
	 * @param type
	 *            type of the callable
	 */
	private CallableToShape(Object name, Object type) {
		super(name, type);
	}

	/**
	 * Creates a callable that creates a rectangle wide enough to contain a given string (the call parameter)
	 * 
	 * @param toStringTransformer
	 *            creates the string of the rectangle
	 * @return the created transformer
	 */
	public static  CallableAbstraction createRectangleForString(Object name,
			Callable toStringTransformer) {
		CallableToShape result = new CallableToShape(name, TYPE_RECTANGLE_AROUND_STRING);
		result.toString = toStringTransformer;
		return result;
	}

	@Override
	protected Result callImpl(Object... parameters) throws Exception {
		if (parameters.length == 0) {
			return null;
		}
		Object input = parameters[0];
		if (input == null) {
			return null;
		}

		Object type = getType();
		if (TYPE_RECTANGLE_AROUND_STRING.equals(type)) {
			return (Result) new Rectangle(toString.call(input).length() * 7, 20); // TODO adapt to real size of string
		} else {
			throw new IllegalStateException("Type not supported: " + type);

		}
	}



}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy