Java source code of 'jhplot.jadraw.JaKey'

package jhplot.jadraw;

import graph.RTextLine;

import japlot.JaKeyOptionsPanel;
import japlot.jaxodraw.*;

import java.awt.BasicStroke;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Color;
import org.freehep.graphics2d.VectorGraphics;
import java.awt.Shape;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.FontMetrics;

import jhplot.HPlotJa;

import jplot.Translate;
import jplot.GPoints;

/**
 * Defines a key with the text
 * 
 * @author S.Chekanov (ANL)
 * 
 */

public class JaKey extends JaObject {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/** The input text string representing this label object. */
	private String textString;
	private RTextLine rtext;
	private String stext; // short version

	/** The font of this label object. */
	private Font textFont;
	private int rotAngle;
	private String[] availableFonts;
	private int key;
	private Color keycolor;
	private double keyspace;
	private float penwidth;
	private int padX, padY;
	private int type;

	/**
	 * Constructor: sets the width, height, relative width and height to a
	 * default value of 2.
	 */
	public JaKey() {
		setRelw(2);
		setRelh(2);
		setColor(Color.black);
		this.textFont = new Font("Dialog", Font.BOLD, 16);
		// this.textFont= new Font("Utopia", Font.BOLD, 16);
		rtext = new RTextLine();
		key = 0;
		keyspace = 1.1;
		keycolor = Color.black;
		penwidth = 1.0f;
		padX = 0;
		padY = 0;
		type = HPlotJa.Key;
	}

	/**
	 * Build a line
	 * 
	 * @param key
	 *          key identification (1-12, 20, 21, 22)
	 * @param text
	 *           text string         
	 * 
	 * @param x1
	 *            starting x
	 * @param y1
	 *            starting y
	
	 * @param what
	 *            if "NDC", coordinate in NDC (from 0 to 1), if "USER"
	 *            coordinates are in the user system given by the axes.
	 */
	public JaKey(int key, String text, double x1, double y1, String what) {

		setRelw(2);
		setRelh(2);
		setColor(Color.black);
		this.textFont = new Font("Dialog", Font.BOLD, 16);
		// this.textFont= new Font("Utopia", Font.BOLD, 16);
		rtext = new RTextLine();
		this.key = key;
		setText(text);
		keyspace = 1.1;
		keycolor = Color.black;
		penwidth = 1.0f;
		padX = 0;
		padY = 0;
		type = HPlotJa.Key;

		setLocation(x1, y1, what);

	}

	/**
	 * Set type of this object
	 * 
	 * @param type
	 *            type
	 */
	public void setType(int type) {
		this.type = type;
	}

	/**
	 * Get type of this object
	 * 
	 * @return type
	 */
	public int getType() {
		return type;
	}

	/**
	 * Set pad ID in X
	 * 
	 * @param pad
	 *            pad id
	 */
	public void setPadX(int pad) {
		this.padX = pad;
	}

	/**
	 * Get pad ID in X
	 * 
	 * @return pad ID
	 */
	public int getPadX() {
		return padX;
	}

	/**
	 * Set pad ID in Y
	 * 
	 * @param pad
	 *            pad id
	 */
	public void setPadY(int pad) {
		this.padY = pad;
	}

	/**
	 * Get pad ID in Y
	 * 
	 * @return pad ID
	 */
	public int getPadY() {
		return padY;
	}

	// Bean getter and setter methods

	/**
	 * Returns the textFont property of this text object.
	 * 
	 * @return The textFont property of this text object.
	 */
	public final Font getFont() {
		return textFont;
	}

	/**
	 * Get current key symbol.
	 * 
	 * @return
	 */
	public final int getKey() {
		return key;
	}

	/**
	 * Set the key symbol (1-12, 20,21,22)
	 * 
	 * @param key
	 *            key symbol
	 */
	public final void setKey(int key) {
		this.key = key;
	}

	/**
	 * Get space between a text and the key
	 * 
	 * @return space in NDC coordinate system
	 */
	public final double getKeySpace() {
		return keyspace;
	}

	/**
	 * Set a space between a key and a text
	 * 
	 * @param space
	 *            space between key and the text in NDC
	 */
	public final void setKeySpace(double space) {
		this.keyspace = space;
	}

	/**
	 * Get key color
	 * 
	 * @return key color
	 */
	public final Color getKeyColor() {
		return keycolor;
	}

	/**
	 * Set the key color
	 * 
	 * @param color
	 *            key color
	 */
	public final void setKeyColor(Color color) {
		this.keycolor = color;
	}

	/**
	 * Get pen width used to draw the key
	 * 
	 * @return pen width
	 */
	public float getPenWidth() {
		return penwidth;
	}

	/**
	 * Set a pen width used to draw the key
	 * 
	 * @param penwidth
	 */
	public void setPenWidth(float penwidth) {
		this.penwidth = penwidth;
	}

	/**
	 * Sets the textFont property of this text object.
	 * 
	 * @param font
	 *            The textFont property of this text object.
	 */
	public final void setFont(Font font) {
		this.textFont = font;
		rtext.setFont(this.textFont);
	}

	/**
	 * Returns the rotAngle property of this text object.
	 * 
	 * @return The rotAngle property of this text object.
	 */
	public final int getRotAngle() {
		return rotAngle;
	}

	/**
	 * Sets the rotAngle property of this text object.
	 * 
	 * @param angle
	 *            The rotAngle property of this text object.
	 */
	public final void setRotAngle(int angle) {
		this.rotAngle = angle;
	}

	/**
	 * Returns the textString property of this text object.
	 * 
	 * @return The textString property of this text object.
	 */
	public final String getText() {
		return textString;
	}

	/**
	 * Parse the text
	 */
	private final void parseText() {
		rtext.setFont(getFont());
		rtext.setColor(getColor());
		rtext.setText(Translate.decode(textString));
		stext = Translate.shrink(textString);
	}

	/**
	 * Sets the textString property of this text object.
	 * 
	 * @param string
	 *            The textString property of this text object.
	 */
	public final void setText(String string) {
		this.textString = string;
		parseText();
	}

	/**
	 * Returns an exact copy of this label.
	 * 
	 * @return A copy of this label.
	 */
	public final JaObject copy() {
		JaText temp = new JaText();
		temp.setX(getX());
		temp.setY(getY());
		temp.setRotAngle(getRotAngle());
		temp.setPadX(padX);
		temp.setPadY(padY);
		temp.setColor(getColor());
		temp.setText(textString);
		temp.setFont(getFont());
		temp.setBoundingBox(getBoundingBox());

		return temp;
	}

	/**
	 * Returns true if all serializable variables of this JaObject and those of
	 * the specified one are equal.
	 * 
	 * @param comp
	 *            A JaObject to compare with.
	 * @return True if the objects are equal, false otherwise.
	 */
	public final boolean isCopy(JaObject comp) {
		boolean isCopy = false;

		if (comp instanceof JaText) {
			JaText text = (JaText) comp;
			if ((text.getX() == getX()) && (text.getY() == getY())
					&& (text.getRotAngle() == getRotAngle())
					&& text.getColor().equals(getColor())
					&& text.getText().equals(getText())
					&& text.getFont().equals(getFont())) {
				isCopy = true;
			}
		}

		return isCopy;

	}

	/**
	 * Determines where on this JaObject a mouse click has ocurred.
	 * 
	 * @param clickX
	 *            The x position of the point where the mouse click ocurred.
	 * @param clickY
	 *            The y position of the point where the mouse click ocurred.
	 * @param editmode
	 *            The current edit mode as defined in JaxoMainPanel.
	 * @return An integer specifying whether the click ocurred on one of the
	 *         handles and if yes, on which.
	 */
	public final int getGrabbedHandle(int clickX, int clickY, int editmode) {
		int length = JaObject.LENGTH;

		if ((editmode == MOVE) || (editmode == COPY)) {
			if ((clickX >= (getX() - (length / 2)))
					&& (clickX <= (getX() + (length / 2)))
					&& (clickY >= (getY() - (length / 2)))
					&& (clickY <= (getY() + (length / 2)))) {
				return SELECT_BODY;
			} else {
				return SELECT_NONE;
			}
		} else if (editmode == RESIZE) {
			return SELECT_NONE;
		}

		return SELECT_NONE;
	}

	/**
	 * The method that draws this label.
	 * 
	 * @param drawToScreen
	 *            Indicates whether the drawing happens on the screen, or
	 *            elsewhere (eg, a file or a printer)
	 * @param g2
	 *            The graphics context where the label has to be drawn.
	 */
	public final void jaxoDraw(VectorGraphics g2, boolean drawToScreen) {
		if (textString.equals("")) {
			return;
		}

		Font baseFont = getFont();
		rtext.setFont(getFont());
		float x = (float) getX();
		float y = (float) getY();
		double theta = Math.toRadians(rotAngle);
		g2.rotate(theta, x, y);
		rtext.setRotation((int) rotAngle);

		FontMetrics fm = g2.getFontMetrics(baseFont);
		float sH = (float) (0.5 * fm.getHeight());

		float xspace = (float) (sH * keyspace);
		float ypos = y - (float) (0.55 * sH);

		g2.setColor(keycolor);
		float shift = 0;
		if (key >= 20 )
			shift = 1.5f * sH;
		if (key < 13 && key > -1)
			shift = 1.0f * sH;

		shift = shift + xspace;
		g2.setStroke(new BasicStroke(penwidth));
		GPoints.drawPointType(key, g2, x, ypos, sH);

		rtext.setColor(getColor());
		rtext.draw(g2, (int) (x + xspace), (int) y);
		g2.rotate(-theta, x, y);

		FontRenderContext frc = g2.getFontRenderContext();
		TextLayout layout = new TextLayout(stext, baseFont, frc);

		Rectangle2D bb = layout.getBounds();
		GeneralPath gp = new GeneralPath((Shape) bb);
		AffineTransform at = AffineTransform.getRotateInstance(theta);
		gp.transform(at);
		bb = gp.getBounds();

		double[] bbox = { bb.getMinX() + getX() - shift, bb.getMinY() + getY(),
				bb.getMaxX() + getX() + (shift), bb.getMaxY() + getY() + 5 };
		setBoundingBox(bbox);

	}

	/**
	 * Draws the handles of this text object.
	 * 
	 * @param g2
	 *            The current graphics context.
	 */
	public final void drawHandles(VectorGraphics g2) {
		int length = JaObject.LENGTH;

		g2.setColor(JaxoColor.RED);
		g2.setStroke(new BasicStroke(1.0f));
		g2.drawRect(getX() - (length / 2), getY() - (length / 2), length,
				length);

		if (isMarked()) {
			g2.setColor(JaxoColor.GRAYSCALE150);
			g2.fillRect(getX() - (length / 2) + 1, getY() - (length / 2) + 1,
					length - 1, length - 1);
		}
	}

	/**
	 * The LaTeX command that is necessary to draw the given label using the
	 * axodraw.sty package. This is not used for label.
	 * 
	 * @param scale
	 *            A scale factor to translate Java coordinates to LaTeX
	 *            coordinates.
	 * @param canvasDim
	 *            The current dimension of the canvas.
	 * @return A string that appears as a comment in the Latex file, indicating
	 *         that there is a postscript text here.
	 */
	public final String latexCommand(float scale, Dimension canvasDim) {
		String command = "% There is a postscript text here!";

		return command;
	}

	/**
	 * Rescales this label by the scale factor scale, keeping the point (orx,
	 * ory) fixed.
	 * 
	 * @param orx
	 *            The x-coordinate of the fixed point
	 * @param ory
	 *            The y-coordinate of the fixed point
	 * @param scale
	 *            The scale parameter
	 */
	public final void rescaleObject(int orx, int ory, float scale) {
		Point2D newP = this.scalePoint(orx, ory, scale, this.getX(), this
				.getY());

		this.setX((int) Math.round(newP.getX()));
		this.setY((int) Math.round(newP.getY()));
	}

	/**
	 * Returns the line width of this label. This is not used for label.
	 * 
	 * @return Returns "%", corresponding to an empty comment line in LaTeX.
	 */
	public final String latexWidth() {
		String lineWidth = "%";

		return lineWidth;
	}

	/**
	 * Brings up the edit panel that allows to change the parameters of this
	 * object.
	 * 
	 * @return True if the editing actually changed the object, false if the
	 *         object has not been changed.
	 */
	public final boolean editPanel() {
		if (availableFonts == null) {
			availableFonts = JaxoStaticCanvasMethods.getSystemFonts();
		}
		JaKeyOptionsPanel textop = new JaKeyOptionsPanel(this, availableFonts);

		return textop.hasChanged();
	}

	/**
	 * Sets the string array of available fonts.
	 * 
	 * @param fonts
	 *            The string array of available fonts.
	 */
	public final void setAvailableFonts(String[] fonts) {
		this.availableFonts = fonts;
	}

	public final void print() {

		System.out.println("JaText object");
		System.out.println("     -      text: " + getText());
		System.out.println("     -     (x,y): " + Integer.toString(getX())
				+ "," + Integer.toString(getY()));
		System.out.println("     - NDC (x,y): " + Double.toString(getXndc())
				+ "," + Double.toString(getYndc()));

	}

}