package ckelling.baukasten;

import java.awt.*;
import java.lang.*;
import java.util.*;


// Versionsgeschichte
// 0.1.0, 27.02.97
// 0.2.0, 24.07.97
/**
 *	Überschrift für Stufen einer Pipeline
 *
 *  Zeigt zwei Zeilen Text an:
 *  Name der Stufe und in ihr enthaltener Befehl.
 *
 *	@author		Carsten Kelling
 *	@version	0.2.0, 24.07.97
 */
public class PipelineStageLabel extends RechnerKomponente
{
    public final static int	DEFAULT_WIDTH	= 200;
    public final static int	DEFAULT_HEIGHT	= 40;
	private int			WIDTH;
	private int			HEIGHT;

	private String		title[];
	private Point		titleCoord[];
	private Point		start;
	private Point		leftPort;
	private Point		rightPort;
	private String		grabMode;

	private boolean		hasBeenActivated;

	private Rechner		parent;


	public PipelineStageLabel(String label1, String label2, int x1, int y1, String str2, Rechner par)
	{
		this(label1, label2, x1, y1, DEFAULT_WIDTH, DEFAULT_HEIGHT, str2, par);
	}

	public PipelineStageLabel(String label1, String label2, int x1, int y1, int width, int height, String str2, Rechner par)
	{
		parent = par;
		title = new String[2];
		title[0] = label1;
		title[1] = label2;
		grabMode = str2;

		reshape(x1, y1, width, height);

		activated = false;
		hasBeenActivated = false;
	} /* end PipelineStageLabel */


	public void paint(Graphics g)
	{
		// Hintergrund loeschen
		g.setColor(parent.BACKGROUND);
		g.fillRect(start.x, start.y, WIDTH, HEIGHT + 2);

		if (activated)
		{
			g.setColor(parent.PIPELINE_STAGE_COLOR);

			g.drawRect(start.x, start.y, 1, HEIGHT);
			g.drawRect(start.x, leftPort.y, WIDTH, 1);
			g.drawRect(rightPort.x - 1, start.y, 1, HEIGHT);

			g.drawString(title[0], titleCoord[0].x, titleCoord[0].y);

			g.setColor(parent.MIPS_COLOR);

			g.drawString(title[1], titleCoord[1].x, titleCoord[1].y);
		}
	} /* end paint */


	public void paintActivated(Graphics g)
	{
		if (activated)
			paint(g);
		else if (hasBeenActivated)
		{
			hasBeenActivated = false;
			paint(g);
		}
	} /* end paintActivated */


	public void reshape(int x, int y, int width, int height)
	{
		reshape(x, y, width, height, grabMode);
	}

	public void reshape(int x, int y, int width, int height, String newGrabMode)
	{
		WIDTH = width; HEIGHT = height;
		setCoordinates(x, y, newGrabMode);
	}

	public void setCoordinates(int x1, int y1, String str2)
	{
		if (str2.equalsIgnoreCase("left"))
		{
			leftPort = new Point(x1, y1);
			rightPort = new Point(x1 + WIDTH, y1);
			start = new Point(x1, y1 - HEIGHT/2);
		}
		else if (str2.equalsIgnoreCase("right"))
		{
			leftPort = new Point(x1 - WIDTH, y1);
			rightPort = new Point(x1, y1);
			start = new Point(x1 - WIDTH, y1 - HEIGHT/2);
		}
		else if (str2.equalsIgnoreCase("top"))
		{
			leftPort = new Point(x1 - WIDTH/2, y1 + HEIGHT/2);
			rightPort = new Point(x1 + WIDTH/2, y1 + HEIGHT/2);
			start = new Point(x1 - WIDTH/2, y1);
		}
		else if (str2.equalsIgnoreCase("bottom"))
		{
			leftPort = new Point(x1 - WIDTH/2, y1 - HEIGHT/2);
			rightPort = new Point(x1 + WIDTH/2, y1 - HEIGHT/2);
			start = new Point(x1 - WIDTH/2, y1 - HEIGHT);
		}
		else  // "leftTop"
		{
			leftPort = new Point(x1, y1 + HEIGHT/2);
			rightPort = new Point(x1 + WIDTH, y1 + HEIGHT/2);
			start = new Point(x1, y1);
		}

		setLabelCoordinates();

	} /* end setCoordinates */

	private void setLabelCoordinates()
	{
		titleCoord = new Point[title.length];
		int lineWidth;
		int lineHeight = parent.getHeight(parent.DIALOGFONT);
		int y2 = start.y + HEIGHT/2 - 4;
		for (int i = 0; i < title.length; i++)
		{
			lineWidth = parent.stringWidth(parent.DIALOGFONT, title[i]);
			titleCoord[i] = new Point(start.x + (WIDTH - lineWidth)/2, y2);
			y2 = y2 + lineHeight + 6;
		}
	}


	public Point getCoordinates(String str)
	{
		if (str.equalsIgnoreCase("left"))
			return leftPort;
		else if (str.equalsIgnoreCase("right"))
			return rightPort;
		else if (str.equalsIgnoreCase("top"))
			return new Point(start.x + WIDTH/2, start.y);
		else if (str.equalsIgnoreCase("bottom"))
			return new Point(start.x + WIDTH/2, start.y + HEIGHT);
		else  // "leftTop"
			return start;
	} /* end getCoordinates for PipelineStageLabel */



	public void activate()
	{
		activate(title[0]);
	}

	public void activate(String str)
	{
		if ( (! activationLocked) && (str != null) )
		{
			title[0] = str;
			titleCoord[0].x = start.x + (WIDTH - parent.stringWidth(parent.DIALOGFONT, title[0]))/2;
			activated = true;
			hasBeenActivated = true;
		}
	}

	public synchronized void deactivate()
	{
		if (! activationLocked)
		{
			activated = false;
		}
	}

	public synchronized void blink(boolean blinker)
	{
	}


	public String[] getPossibleQualifiers()
	{
		String[] q = {"left", "right", "top", "bottom", "leftTop"};
		return q;
	}

	public void setLabel(String[] newLabel)
	{
		if (newLabel != null)
		{
			for (int i = 0; i < Math.min(title.length, newLabel.length); i++)
				title[i] = newLabel[i];

			setLabelCoordinates();
		}
	}

	public String[] getLabel()
	{
		return title;
	}

	public boolean intersectsWith(Point p)
	{
		return ( (start.x <= p.x) && (start.x + WIDTH >= p.x) && (start.y <= p.y) && (start.y + HEIGHT >= p.y) );
	}

	public String getInfoTipText(Point p)
	{
		return new String("");
	}

	/**
	 * Ohne Funktion
	 */
	public void setValue(int foo) {}

	/**
	 * Ohne Funktion
	 */
	public int getValue() { return -1; }

	public long getMaxValue()
	{
		return Rechner.DEFAULT_MAXVALUE;
	}

} /* end PipelineStageLabel */

