package utilities;

import java.awt.*;

// This class generates a dialog box which prompts the user for 
// some sort of yes/no/cancel type of response.  
public class YesNoDialog extends DialogSuperClass{
	public static final int NO 		= 0;	// return value if choice was no
	public static final	int	YES		= 1;	// return value if choice was yes
	public static final int	CANCEL	= -1;	// return value if choice was to cancel
	
//	protected MultiLineLabel label;
	protected Label label;
		
	// the constructor for this class.  
	// Sets up the buttons in the appropriate locations with proper labels.  
	public YesNoDialog(Frame parent, String title, String message,
			String yes_label, String no_label, String cancel_label) {
		super(parent);
		setTitle(title);
		
		// Give user the message
		setLayout(new BorderLayout(15,15));	
		label = new Label(message);//, 20, 20);
		add("North", new Label(""));
		add("Center", label);
		add("West", new Label(""));
		add("East", new Label(""));
		

		// Put up button options in the dialog window.
		Panel p = new DialogButtonPanel(this);		// Create a panel to draw window items in
		p.setLayout(new FlowLayout(FlowLayout.CENTER,15,15));	
		
		
		if (yes_label != null) 	{
			p.add(fOK);
			fOK.setLabel(yes_label);
		}
		
		if (no_label  != null) 	{
			p.add(fNo);
			fNo.setLabel(no_label);
		}
		
		if (cancel_label != null) {
			p.add(fCancel);
			fCancel.setLabel(cancel_label);
		}
												
		add("South", p);
		pack();
		p.requestFocus();
		parent.getToolkit().beep();
	}
		
	// generic destroy window method
	public boolean handleEvent(Event evt) {
		if (evt.id == Event.WINDOW_DESTROY) {
			this.dispose();
			return false;
		}
		else return super.handleEvent(evt);
	}
		
    public boolean action(Event e, Object arg) {
    	double val;
    	
   		if (e.target instanceof Button) {
   			hide();
   			dispose();
   			
   			if (e.target == fOK) answer(YES);
   			else if (e.target == fNo) answer(NO);
   			else answer(CANCEL);
		}
		 
		return true;
    }
    
    // this procedure overrides the super class event handler for 
    // pressing the return key.
    public boolean keyDown(Event e, int key) {
    	
    	if (key == '\n') {
    		answer(YES);
    	}
       	return true;
    }
    
    // Cal the yes(), no(), and cancel() methods depending on user response.
    // The subclass should provide the overriding method for each yes(), no(),
    // and cancel() method.
    protected void answer(int answer) {
    	
    	switch(answer) {
    		case YES:	 doYes();  break;
    		case NO:	 doNo();   break;
    		case CANCEL: doCancel(); break;
    	}
    }
}
