// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3) fieldsfirst 
// Source File Name:   CardSelector.java

package lt.monarch.awt;

import java.awt.*;
import java.util.Vector;

// Referenced classes of package lt.monarch.awt:
//			CardSelectorListener

public class CardSelector extends Canvas
{

	private Container container;
	private String pages[];
	private boolean pageEnabled[];
	private int selectedPage;
	private Vector listeners;
	private final int tabExtraWidth = 15;
	private final int tabSlope = 5;
	private final int tabSpacing = -4;

	public CardSelector(Container container1, String as[])
	{
		listeners = new Vector();
		container = container1;
		pages = as;
		pageEnabled = new boolean[pages.length];
		for(int i = 0; i < pageEnabled.length; i++)
			pageEnabled[i] = true;

		container.setBackground(Color.lightGray);
	}

	public void addPageChangeListener(CardSelectorListener cardselectorlistener)
	{
		listeners.addElement(cardselectorlistener);
	}

	public final void disablePage(String s)
	{
		enablePage(s, false);
	}

	void drawTab(Graphics g, int i, String s, boolean flag, boolean flag1)
	{
		Rectangle rectangle = bounds();
		FontMetrics fontmetrics = g.getFontMetrics();
		int j = fontmetrics.stringWidth(s);
		byte byte0 = ((byte)(flag ? 2 : 4));
		Polygon polygon = new Polygon();
		polygon.addPoint(i, rectangle.height);
		polygon.addPoint(i + 5, byte0);
		polygon.addPoint(i + j + 15 + 5, byte0);
		polygon.addPoint(i + j + 15 + 10, rectangle.height);
		g.setColor(Color.lightGray);
		g.fillPolygon(polygon);
		g.setColor(Color.white);
		g.drawLine(i + 1, rectangle.height, i + 5 + 1, byte0);
		g.drawLine(i + 5 + 1, byte0 + 1, i + j + 15 + 5 + 1, byte0 + 1);
		g.setColor(Color.darkGray);
		g.drawLine((i + j + 15 + 5) - 1, byte0, (i + j + 15 + 10) - 1, rectangle.height);
		g.setColor(Color.black);
		g.drawPolygon(polygon);
		if(flag1)
		{
			g.setColor(Color.black);
			g.drawString(s, i + 7 + 5, byte0 + fontmetrics.getHeight());
			return;
		} else
		{
			g.setColor(Color.white);
			g.drawString(s, i + 7 + 5, byte0 + fontmetrics.getHeight());
			g.setColor(Color.gray);
			g.drawString(s, (i + 7 + 5) - 1, (byte0 + fontmetrics.getHeight()) - 1);
			return;
		}
	}

	public final void enablePage(String s)
	{
		enablePage(s, true);
	}

	public void enablePage(String s, boolean flag)
	{
		for(int i = 0; i < pages.length; i++)
			if(pages[i].equals(s))
				pageEnabled[i] = flag;

		repaint();
	}

	public boolean isFocusTraversable()
	{
		return true;
	}

	public boolean mouseDown(Event event, int i, int j)
	{
		requestFocus();
		bounds();
		Graphics g = getGraphics();
		FontMetrics fontmetrics = g.getFontMetrics();
		int k = 0;
		for(int l = 0; l < pages.length; l++)
		{
			int i1 = fontmetrics.stringWidth(pages[l]);
			k += i1 + 15 + 10 + -4;
			if(i < k)
			{
				turnToPage(l);
				return true;
			}
		}

		return false;
	}

	public void paint(Graphics g)
	{
		Rectangle rectangle = bounds();
		Rectangle rectangle1 = g.getClipRect();
		g.clearRect(rectangle1.x, rectangle1.y, rectangle1.width, rectangle1.height);
		FontMetrics fontmetrics = g.getFontMetrics();
		int i = 0;
		int j = i;
		for(int k = 0; k < pages.length; k++)
		{
			if(k != selectedPage)
				drawTab(g, i, pages[k], false, pageEnabled[k]);
			else
				j = i;
			i += -4 + tabWidth(fontmetrics, k);
		}

		drawTab(g, j, pages[selectedPage], true, true);
		g.setColor(Color.white);
		g.drawLine(0, rectangle.height - 1, j, rectangle.height - 1);
		g.setColor(Color.black);
		g.drawLine(0, rectangle.height - 2, j, rectangle.height - 2);
		j += tabWidth(fontmetrics, selectedPage);
		g.setColor(Color.white);
		g.drawLine(j, rectangle.height - 1, rectangle.width, rectangle.height - 1);
		g.setColor(Color.black);
		g.drawLine(j, rectangle.height - 2, rectangle.width, rectangle.height - 2);
	}

	public Dimension preferredSize()
	{
		Graphics g = getGraphics();
		FontMetrics fontmetrics = g.getFontMetrics();
		int i = 0;
		for(int j = 0; j < pages.length; j++)
			i += fontmetrics.stringWidth(pages[j]) + 15 + 10 + -4;

		return new Dimension(i, (fontmetrics.getHeight() * 3) / 2);
	}

	public int tabWidth(FontMetrics fontmetrics, int i)
	{
		return fontmetrics.stringWidth(pages[i]) + 15 + 10;
	}

	public void turnToPage(int i)
	{
		if(pageEnabled[i] && i != selectedPage)
		{
			selectedPage = i;
			((CardLayout)container.getLayout()).show(container, pages[i]);
			repaint();
			for(int j = 0; j < listeners.size(); j++)
				((CardSelectorListener)listeners.elementAt(j)).pageChanged(i);

		}
	}
}
