import java.awt.*;

public class chatArea extends Frame{
myTextArea ta = new myTextArea();
TextField tf = new TextField();
Button name = null;
String pseudo = null;
openStudio ops;

   public chatArea(openStudio ops) {
     super("Chat");
     this.ops = ops;
     setBackground(Color.lightGray);
     setForeground(Color.black);
	
      GridBagLayout gb = new GridBagLayout();
      GridBagConstraints c = new GridBagConstraints();
      setFont(new Font("Helvetica", Font.PLAIN, 14));
      setLayout(gb);
      c.weightx=1.0;
      c.weighty=1.0;
      c.fill = GridBagConstraints.BOTH;      
      c.gridwidth = GridBagConstraints.REMAINDER; 
      gb.setConstraints(ta, c);
      add(ta);
      c.weighty=0.01;
      c.weightx=0.10;
      name = new Button("Name");
      c.gridwidth = GridBagConstraints.RELATIVE; 
      gb.setConstraints(name, c);
      add(name); 
      c.weightx=0.90;
      c.fill = GridBagConstraints.HORIZONTAL;      
      c.gridwidth = GridBagConstraints.REMAINDER; 
      gb.setConstraints(tf, c);
      add(tf);
	pack();
	//Dimension d = size();
	Dimension d = new Dimension(400,300);
     //pack();
     reshape(getToolkit().getScreenSize().width/2-d.width/2,
         getToolkit().getScreenSize().height/2-d.height/2,d.width,d.height);
     show();
      tf.requestFocus();
   }

   public void addText(String s){
	ta.appendText(s);
   }

   public boolean handleEvent(Event e){
	if(e.target instanceof Button){
		if(e.id==Event.ACTION_EVENT){
			pseudo = null;
			name.setLabel("Name");
		}
	}
        if(e.id == Event.WINDOW_DESTROY){
                this.hide(); // button down
        }
	else if(e.id==401){
		System.out.println("psuedo="+pseudo);
		if(e.key==10){
			if(pseudo!=null){
				System.out.println("appending to textarea");
				 ta.appendText(pseudo+"> "+tf.getText());
				 if(ops.dp.con !=null) 
                                   ops.dp.con.sendString(pseudo+"> "+tf.getText());
				 else System.out.println("ops.dp.con = null");
			}
			else {
				System.out.println("resetting name");
				pseudo = tf.getText();
				name.setLabel(pseudo);
			}
			tf.setText("");
		}
		else return false;
	}
        return true;
   }
}                     
