import java.awt.event.*;
import java.util.*;
import java.awt.Color;

public class CircleModel {
  private double radius = 20;
  private boolean filled;
  private Color color;
  // List for objects to be notified of changes to model state
  private ArrayList<ActionListener> actionListenerList;

  // Accessors for properties
  public double getRadius() {
    return radius;
  }
  
  public boolean isFilled() {
    return filled;
  }
  
  public Color getColor() {
    return color;
  }

  // Mutators for properties: lead to events being generated
  // for registered actionListeneres.
  public void setRadius(double radius) {
    this.radius = radius;
    processEvent(this, "radius");
  }
 
  public void setFilled(boolean filled) {
    this.filled = filled;
    processEvent(this, "filled");
  }

  public void setColor(java.awt.Color color) {
    this.color = color;
    processEvent(this, "color");
  }

  // Registration and event processing
  public synchronized void addActionListener(ActionListener l) {
    if (actionListenerList == null)
      actionListenerList = new ArrayList<ActionListener>();
    actionListenerList.add(l);
  }

  private void processEvent(Object source, String command) {
    ArrayList list;
    ActionEvent e = new ActionEvent(source, ActionEvent.ACTION_PERFORMED, command);

    // Allow only one thread (e.g. window) at a time to execute the
    // two "synchronized" statements below.
    synchronized (this) {
      if (actionListenerList == null) return;
      list = (ArrayList)actionListenerList.clone();
    }

    for (int i = 0; i < list.size(); i++) {
      ActionListener listener = (ActionListener)list.get(i);
      listener.actionPerformed(e);
    }
  }
}
