/// Evolver Copyright 2000, Morgan McGuire
/// matrix@alum.mit.edu   http://graphics3d.com/matrix
import java.awt.*;

/**
 * An operator that paints its icon based on the name of the 
 * operator.
 */
public abstract class AbstractELOperator implements ELOperator
{
    /**
     * Override to change the string painted for an icon.
   */
    protected String iconString()
    {
	return name();
    }

    /**
     * Default implementation calls apply(ELMatrix, ELMatrix) since many
     * operators are implemented to not take an ELEnvironment.
     */
    public void apply(ELMatrix argument[], ELMatrix out, ELEnvironment environment) 
    {
	apply(argument, out);
    }
    
    public void apply(ELMatrix argument[], ELMatrix out)
    {
	throw new Error("EL Runtime Error: " + name() + " unimplemented");
    }

    public void paintIcon(Graphics2D graphics, int x, int y, int width, int height)
    {
	graphics.setColor(Color.black);
	String s = iconString();
	FontMetrics metrics = graphics.getFontMetrics();
	if (s.length() > 3) {
	    s = s.substring(0, 3);
	}
    
	graphics.drawString(s, x + (width - metrics.stringWidth(s)) / 2, y + (height + metrics.getHeight() / 2) / 2);
    }

    /**
     * Computes min(-1.0, max(1.0, c)).  A useful utility method to call
     * from compose().
     */
    protected final static float clamp(float c)
    {
	if (c >= 1.0) {
	    return 1.0f;
	} else if (c <= -1.0) {
	    return -1.0f;
	} else {
	    return c;
	}
    }

    public boolean hasExternalInfluences() {
	return false;
    }

    protected void checkArguments(ELMatrix argument[])
    {
	int numArguments = argument.length;
	
	if (numArguments < getMinNumArguments()) {
	    throw new Error("EL Runtime Error: " + name() + ": " + getMinNumArguments() + " arguments expected, " + 
			    numArguments + " supplied");
	}

	if (numArguments > getMaxNumArguments()) {
	    throw new Error("EL Runtime Error: " + name() + ": " + getMaxNumArguments() + " arguments expected, " + 
			    numArguments + " supplied");
	}
  }

}








