

package DDrawing;


import java.awt.Color;

/**
 * <code>DComponent</code> is the mother of all objects which can be displayed by a <code>DArea</code> object, even when it would be also enough to implement the <code>DElement</code> interface to an class DComponent is abstract because the paint method has to be overridden
 */
public abstract class DComponent implements DElement{
  protected Color color;
  protected DRectangle rectangle;
  protected DParent parent;
  private boolean visible = true;

  DComponent(boolean is_rect){}

  public DComponent(){ rectangle = DRectangle.getEmpty(); }

  /**
   * returns the rectangle in which the object lies
   */
  public DRectangle getRectangle(){
    //if( rectangle == null ) rectangle = new DRectangle( DRectangle.EMPTY );
    return (DRectangle)rectangle.clone();
  }

  /**
   * sets the parent of the component, which should take care of painting the
   * component to the right time
   */
  public void setDParent( DParent parent ){
    if( this.parent != null && this.parent != parent ){
      this.parent.removeDElement( this );
      this.parent.repaint( getRectangle() );
    }
    this.parent = parent;
  }

  /**
   * returns the parent of the component
   */
  public DParent getDParent(){ return parent; }

  /**
   * invoces the parent to repaint the rectangle in which the component lies
   */
  public void repaint(){
    //System.out.println("DComponent.repaint()");
    if( parent != null ) parent.repaint( getRectangle() );
  }

  /**
   * sets the color of the component
   */
  public void setColor( Color color ){
    if( this.color == null || !color.equals( this.color ) ) {
      this.color = color;
      repaint();
    }
  }

  /**
   * returns the color of the component
   */
  public Color getColor(){ return color; }

  /**
   * sets the component visible or not
   */
  public void setVisible( boolean aFlag ){
    boolean changed = ( aFlag != visible );
    visible = aFlag;
    if( changed ) repaint();
  }

  /**
   * returns if the component should be visible when the parent shows the right
   * area
   */
  public boolean isVisible(){ return visible; }

}
