/**
   Class that implements the text part
   @author Sugiharto Widjaja
   @version 2002/3/19
*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.geom.*;

public class TextView extends JPanel implements Observer
{
   /**
      Construct a TextView object
      @param aModel the data model that will be observed by TextView
      object
   */

   public TextView(DataModel aModel)
   {
      model = aModel;
      model.addObserver(this);
      this.setLayout(new GridLayout(INIT_ROW, INIT_COLUMN));
   }

   /**
      Update the text field whenever it receives a notification from
      observed object
      @param o an observable object
      @param arg the argument
   */

   public void update(Observable o, Object arg)
   {
      String comment = (String) arg;
      StringTokenizer tokenizer = new StringTokenizer(comment);
      String command = tokenizer.nextToken();
      if(command.equals("add"))
      {
         String xCoord = tokenizer.nextToken();
         String yCoord = tokenizer.nextToken();

       addPointToTextView(xCoord, yCoord);
     }
   }

   /**
      Add the point which the user click on the graph view
      @param xCoord - the x coordinate
      @param yCoord - the y coordinate
   */

   public void addPointToTextView(String xCoord, String yCoord)
   {
      final JTextField xField = new JTextField(xCoord, TEXT_SIZE);
      final JTextField yField = new JTextField(yCoord, TEXT_SIZE);
      final int rank = model.getSize() - 1;
      xField.addActionListener(new
         ActionListener()
         {
            String text = xField.getText();
            public void actionPerformed(ActionEvent event)
            {
               try
               {
                String temp = event.getActionCommand();
               int num = Integer.parseInt(temp);
              xField.setText(temp);
              model.changePoint("x", rank, num);
            }
            catch(NumberFormatException e)
            {
               xField.setText(text);
            }
            }
         });
      yField.addActionListener(new
       ActionListener()
       {
          String text = yField.getText();
          public void actionPerformed(ActionEvent event)
          {
             try
             {
                 String temp = event.getActionCommand();
                int num = Integer.parseInt(temp);
              yField.setText(temp);
              model.changePoint("y", rank, num);
            }
            catch(NumberFormatException e)
            {
               yField.setText(text);
            }
          }
         });
      this.add(xField);
      this.add(yField);
      super.setSize(getPreferredSize());
      validate();
   }

   // The DataModel object
   private DataModel model;
   // Initial number of row of the JTextField
   private final int INIT_ROW = 0;
   // Initial number of column of the JTextField
   private final int INIT_COLUMN = 2;
   // The size of the JTextField
   private final int TEXT_SIZE = 10;
}

