package utilities;

import java.awt.*;

public class ControlPanel extends Panel {
	protected int m_Width, m_Height;
	
	protected int m_FieldWidth = 120;
	
	private int	m_y;
	
	protected final static int MAXSPACERS = 10;
	protected final static int ITEMSPACING = 5;
	protected final static int ITEMHEIGHT = 20;
	
	protected Canvas[] m_Labels = new Canvas[20];
	protected int m_NumLabels=0;
	
	protected int[] m_Spacers = new int[MAXSPACERS];
	protected int 	m_NumSpacers = 0;
	
	protected int	m_ButtonWidth = 150;
	
	protected int	m_EditHeight;
	
	public ControlPanel(int w, int h) {
		setBackground(Color.white);
		m_Width = w;
		m_Height = h;
		m_NumSpacers = 0;
		m_FieldWidth = 40;
		m_y  = 0;
		setLayout(null);
	}
	
	public Dimension preferredSize() {
		return new Dimension(m_Width,m_Height);
	}
	
	public Dimension minimumSize() {
		return preferredSize();
	}
	
	protected void place(Component b, int x) {
		add(b);
		b.reshape(x,m_y,m_ButtonWidth,ITEMHEIGHT);
		m_y += ITEMHEIGHT + ITEMSPACING;
	}
	
	protected void place(Component b) {
		place(b, (m_Width - m_ButtonWidth)/2);
	}
	
	protected void place(TextFieldWithLabel tfwl, int x) {
		place(tfwl,x,"");
	}
	
	protected void place(TextFieldWithLabel tfwl, int x, String units) {
		TextField 	tf;
		Canvas		l;
		
		tf = tfwl.textField();
		tf.setFont(SpecialLabel.mFont);

		l = tfwl.label();
		m_Labels[m_NumLabels] = l;
		m_NumLabels++; 
		
		SpecialLabel l2 = new SpecialLabel(units);
		l2.setFont(SpecialLabel.mFont);
		
		add(tf);
		add(l);
		
		l.reshape(x-l.preferredSize().width,
						m_y,
						l.preferredSize().width,
						ITEMHEIGHT);
		tf.reshape(x + 2, m_y, m_FieldWidth,ITEMHEIGHT);
		
		if (!units.equals("")) {
			add(l2);
			l2.reshape(x+2+m_FieldWidth+1, m_y, l2.preferredSize().width+5,ITEMHEIGHT);
		}
			
		m_y += ITEMHEIGHT + ITEMSPACING;
	}
	
	protected void backUp() {
		m_y -= ITEMHEIGHT + ITEMSPACING;
	}
	
	protected void placeSpacer() {
		if (m_NumSpacers == MAXSPACERS) return;
		m_Spacers[m_NumSpacers] = m_y;
		m_y += ITEMSPACING;
		m_NumSpacers++;
		
	}
	
	protected void placeBlank() {
		m_y += ITEMHEIGHT/2;
	}
	
	public void paint(Graphics g) {
		g.setColor(Color.lightGray);
		for (int i=0; i<m_NumSpacers; i++) {
			utilities.utils.drawDashedLine(g,10,m_Spacers[i],m_Width-10,m_Spacers[i]);
		}
		super.paint(g);
	}
		
	
}