package primitives.frames;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.util.Enumeration ;
import java.util.Vector;
/**
* this class encapsulates a TextBox which displays text of a possible enumeration
* of texts.
* The user can switch the display between the possible texts by clicking an appropriate button.
*the texts are read from the values of the parameters:frame<i>i</i> from the applet Tag,
* The first text is the value of frame0.
* Set the name of the reciever applet (must be instance of FrameListener) in the reciever parameter 
*of the applet Tag.
*@see FrameListener.
*@author Dori Eldar. 
*/
public class TextBox extends Applet implements MouseListener{
	/**
	*Panel containing TextArea.
	*/
	FrameAreaEx framed;

	/**
	*Vector containing the texts from the applet Tag parameters: frame<i>i</i>
	*/
	Vector textFrames;
    /**
	* name of the reciever read from the reciever parameter.
	*/
	String recieverName;
	/**
	*The current index of the text shown in the TextArea component.
	*/
	int currentFrame;
	/**
	* The object which recieves notification by calling its FrameListener.changeFrame.
	*The reciever should be an instance of a FrameLIstener.
	*/
	Applet reciever;

   
	private final static int Next=0;
	private final static int Back=1;
	private final static int Restart=2;
	private final static int Current=3;

	Button[] buttons = new Button[4];
	private final static String[] bLabels = {"Next","Back","Restart","Restart Current"};

	/**
	*Creates 2 panels one for the buttons and one for the TextArea
	* and reads the texts to the textFrames object.
	*/
	public void init() {
     GridBagLayout gridBag = new GridBagLayout();
     GridBagConstraints c = new GridBagConstraints();
	 for (int i=0;i<4;i++){
		 buttons[i] = new Button(bLabels[i]);
		 buttons[i].addMouseListener(this);
	 }
    setLayout(gridBag);
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1.0;
		c.weighty = 1.0;
		c.gridwidth = GridBagConstraints.REMAINDER;
		framed = new FrameAreaEx();
	        gridBag.setConstraints(framed, c);
		add(framed);
		c.gridwidth = GridBagConstraints.RELATIVE;
		c.weighty = 0.0;
		ExPanel bPanel = new ExPanel(buttons,new FlowLayout(FlowLayout.CENTER));
		gridBag.setConstraints(bPanel, c);
		add(bPanel);
        validate();
		framed.setTextFont(1);
			int i=0;
		String value = null;
		textFrames = new Vector(1,1);
		do {
			value = getParameter("frame"+i++);
			textFrames.addElement(value);
		}while(value!=null);
		textFrames.removeElementAt(textFrames.size()-1);
		if(textFrames.size()>0)
			framed.text.setText((String)textFrames.firstElement());
		currentFrame = 0;
		buttons[Restart]. setEnabled(false);
		buttons[Back].setEnabled(false);
		
	}
	/**
	*clears components
	*/
	public void destroy(){
		for(int i=0;i<4;i++){
			buttons[i].removeMouseListener(this);
			buttons[i] = null;
		}
		framed = null;
		textFrames = null;
		recieverName = null;
		reciever = null;
	}
	/**
	*displays the first text of the textFrame object.
	*and calls the mouseclick method.
	*@see #mouseClicked
	*/
	public void start(){
	
		recieverName = getParameter("reciever");
	
	   try{
		mouseClicked(new MouseEvent(buttons[Current],ActionEvent.ACTION_PERFORMED,1000,0,0,0,1,false));
	   }catch(Exception e){}
	}
	/**
	*Sets the reciever to null.
	*/
	public void stop(){

		recieverName = null;
		reciever = null;

	}
    /**
	*Locates the FrameListener object with the name of the recieverName field,
	* if successfull calls its changeFrame method and sets the text displayed according to
	*the user action.
	*/
	public void mouseClicked(MouseEvent e){
		
		if (reciever==null)
		  try{
				reciever = getAppletContext().getApplet(recieverName);
		  }catch(NullPointerException n){}
		int oldFrame = currentFrame;
		if ((((Button)e.getComponent()).getLabel()==bLabels[Next])&&(currentFrame<textFrames.size()-1)){
			currentFrame++;
		}
		else if	((((Button)e.getComponent()).getLabel()==bLabels[Back])&&(currentFrame>0)){
			currentFrame--;
		}
		else if (((Button)e.getComponent()).getLabel()==bLabels[Restart]) currentFrame=0;
		if (reciever!=null){
			if(reciever instanceof FramesListener){
				try{
					((FramesListener)reciever).changeFrame(currentFrame);
				}catch(Exception ex){
					currentFrame = oldFrame;
				}
				framed.text.setText((String)textFrames.elementAt(currentFrame));
				framed.text.setCaretPosition(1);
				buttons[Restart].setEnabled(currentFrame>0);
				buttons[Back].setEnabled(currentFrame>0);
				buttons[Next].setEnabled(currentFrame<textFrames.size()-1); 
			}else{
				currentFrame = oldFrame;
			}	
		}else currentFrame = oldFrame;
	}
	/**
	*Not implemented.
	*/
	public void mousePressed(MouseEvent e){
	}
	/**
	*Not implemented.
	*/
	public void mouseReleased(MouseEvent e){
	}
	/**
	*Not implemented.
	*/
	public void mouseEntered(MouseEvent e){
	}
	/**
	*Not implemented.
	*/
	public void mouseExited(MouseEvent e){
	}

}
		
/**
*A panel which contains a TextArea for the use of a TextBox object.
*/		
class FrameAreaEx extends Panel{
	public TextArea text;
    public FrameAreaEx() {
        super();
		setLayout(new GridLayout(1,0));
		text = new TextArea("",1, 10,TextArea.SCROLLBARS_VERTICAL_ONLY );
        text.setEditable(false);
		add(text);
        validate();
    }
	/**
	*Sets the size of the font which appears in the TextArea.
	*@param size the requested increment of the font size.
	*/
	protected void setTextFont(int size){
		Font font = text.getFont();
		text.setFont(new Font(font.getName(),font.getStyle(),font.getSize()+size));
	}
    public Insets getInsets() {
        return new Insets(4,4,5,5);
    }
    /**
	*Draws a 3D rectangle as the border of this panel.
	*@param g the Grphics to draw on.
	*/
    public void paint(Graphics g) {
        Dimension d = getSize();
        g.setColor(Color.gray);
        g.draw3DRect(0, 0, d.width - 1, d.height - 1, true);
        g.draw3DRect(3, 3, d.width - 7, d.height - 7, false);
    }
	/**
	*@exception java.Throwable.
	*/
	public void finalize() throws Throwable{
		removeAll();
		text = null;
	}
}

