package utilities;

import java.awt.*;

// This class is the superclass for all dialog boxes in Grapher Java.
//  it defines the different font, colors, and default item heights

public class DialogSuperClass extends Dialog {
	protected static final Color	kBackgroundColor = new Color(190,190,190);
	protected static final Color	kTextFieldColor = new Color(50,50,50);
	protected static final Color	kTextForeColor = new Color(255,255,255);
	protected static final Color	kButtonColor = new Color(150,150,150);
	protected static final int		kFontSize = 12;
	protected static final int		kPad = (int) (kFontSize / 6);
	protected static final int		kDefaultItemHeight = kFontSize + kPad;
	protected static final int		kDefaultSpacing = 3;
	
	// protected DefaultButton			fOK;
	protected Button				fOK;
	protected Button				fNo;
	protected Button				fCancel;
	
	protected static final Font	kDialogFont = new Font("Helvetica",Font.BOLD,kFontSize);
	protected FontMetrics	fDialogMetrics = getFontMetrics(kDialogFont);

	public DialogSuperClass(Frame parent) {
		super(parent,true);
		
		setLayout(null);
		setResizable(false);
		
		setBackground(kBackgroundColor);
		setFont(kDialogFont);
		
		// define fOk and fCancel buttons.  Most dialog boxes use these.
		// Note they still have to be added and reshaped in order to be
		// visible.
		fOK = new Button("OK");
		setButtonColors(fOK);
		fNo = new Button("No");
		setButtonColors(fNo);
		fCancel = new Button("Cancel");
		setButtonColors(fCancel);
		
	}

	// makes the specified textfield have the default background and foreground colors.
	// Also does a "select all" for the textfield.
	protected void setTextFieldColors(TextField tf) {
		tf.setBackground(kTextFieldColor);
		tf.setForeground(kTextForeColor);
		tf.selectAll();
	}
		
	protected void setButtonColors(Button b) {
		b.setBackground(kButtonColor);
	}
		
	// paints a round rectangle around the fOK button
	public void paintButton(Graphics g) {
		int x, y, width, height;
		
		x = fOK.location().x;
		y = fOK.location().y;
		width = fOK.size().width;
		height = fOK.size().height;
		
		g.setColor(Color.black);
		g.drawRoundRect(x-4,y-4,width+8,height+8, 8, 8);
		g.drawRoundRect(x-3,y-3,width+6,height+6, 7, 7);
		g.drawRoundRect(x-2,y-2,width+4,height+4, 6, 6);
	} 
	
	
 	// paints a border around a panel
	protected void paintBorder(Graphics g, Panel p) {
		int x,y,width,height;
		
		x = p.location().x;
		y = p.location().y;
		width = p.size().width;
		height = p.size().height;
		
		g.setColor(Color.black);
		g.drawRect(x,y,width,height);
	}

	// generic destroy window method
	public boolean handleEvent(Event evt) {
		if (evt.id == Event.WINDOW_DESTROY) {
			this.dispose();
			return false;
		}
		else return super.handleEvent(evt);
	}
	
	// the default doYes method just destroys this dialog box
	public void doYes() {
		this.hide();
	}

	// the default doNo method just destroys this dialog box
	public void doNo() {
		this.hide();
	}
	
	// the default doCancel method just destroys this dialog box
	public void doCancel() {
		this.hide();
	}
	
	
	// The default action method.  It handles the OK and Cancel buttons, calling
	// the default doOK and doCancel methods
	public boolean action(Event e, Object arg) {
		double val;
   		if (e.target instanceof Button) {
   			Button b = (Button)(e.target);
   			if (b==fOK)
   				doYes();
   			else if (b==fCancel);
   				doCancel();
		}
		return super.action(e,arg);
	}
	
	public boolean keyDown(Event e, int key) {
		if (key == '\r' || key == '\n') { // the return key defaults to OK
			doYes();
			return false;
		}
       	return super.keyDown(e,key);
    }
    
    // overrides the setBounds of this dialog to automatically center on screen
    public void setBounds(int x, int y, int w, int h) {
    	Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
    	int newx, newy;
    	newx = (screenDim.width - w)/2;
    	newy = (screenDim.height - h)/2;
    	super.setBounds(newx,newy,w,h);
    }
    
    // setBounds without centering
    public void setBoundsWithoutCentering(int x, int y, int w, int h) {
    	super.setBounds(x,y,w,h);
    }
    
}