/**
   Class that implements the data model that is observable
   @author Sugiharto Widjaja
   @version 2002/3/19
*/

import java.util.*;
import java.awt.*;
import java.awt.geom.*;

public class DataModel extends Observable
{
   /**
      Construct the DataModel object
   */

   public DataModel()
   {
      pointContainer = new ArrayList();
   }

   /**
      Add the point to the array list. It will, then, notify all
      of its observers
      @param aPoint a point to be added to the array list
   */

   public void addPoint(Point2D aPoint)
   {
      pointContainer.add(aPoint);
      super.setChanged();
      super.notifyObservers("add " + (int) aPoint.getX() + " " + (int) aPoint.getY());
   }

   /**
      Change the x or the y coordinate of a point
      @param coord indicates which coordinate to change (x or y)
      @param position indicates the locatian of point to be changed
      @param newCoord the new coordinate
   */

   public void changePoint(String coord, int position, int newCoord)
   {
      Point2D point = (Point2D) pointContainer.get(position);
      double x = point.getX();
      double y = point.getY();
      if(coord.equals("x"))
         x = newCoord;
      else
         y = newCoord;
      pointContainer.set(position, new Point2D.Double(x, y));
      super.setChanged();
      super.notifyObservers("change");
  }

   /**
      Get the size of pointContainer
      @return size of pointContainer
   */

   public int getSize()
   {
      return pointContainer.size();
   }

   /**
      Get the point at a specified index of pointContainer
      @param i the index
      @return the point at index i
   */

   public Point2D getPointAt(int i)
   {
      return (Point2D) pointContainer.get(i);
   }

   // The ArrayList to store all the points
   private ArrayList pointContainer;
}

