package DDrawing;


import java.awt.*;
import java.awt.event.*;


/**
 * @author  mkron
 */
public class DMouseZoom extends DRectangle implements MouseMotionListener, MouseListener{
  DArea area;
  private DPoint start;

  public DMouseZoom( DArea area ) {
    super( 0, 0, 0, 0 );
    setVisible( false );
    this.area = area;
    area.addMouseListener( this );
    area.addMouseMotionListener( this );
    color = Color.yellow;
  }
  /**
   * Invoked when the mouse has been clicked on a component.
  */
  public void mouseClicked(MouseEvent e) {
    DRectangle r = area.getDRectangle();
    if( ( e.getModifiers() & InputEvent.BUTTON1_MASK ) > 0 ){
      DMeasures m = area.getDMeasures();
      DPoint c = m.getDPoint( e.getPoint() );
      r.x = c.x - r.width * .5;
      r.y = c.y - r.height * .5;
    }
    else {
      r.x -= r.width * .5;
      r.y -= r.height * .5;
      r.width *= 2;
      r.height *= 2;
    }
    area.setVisibleRectangle( r );
  }

  /**
   * Invoked when a mouse button has been pressed on a component.
   */
  public void mousePressed(MouseEvent e) {
    setVisible( true );
    DMeasures m = area.getDMeasures();
    DPoint p = m.getDPoint( e.getPoint() );
    start = new DPoint( p.x, p.y );
    x = p.x;
    y = p.y;
    width = 0;
    height = 0;
    area.addDElement( this );
  }

  /**
   * Invoked when a mouse button has been released on a component.
   */
  public void mouseReleased(MouseEvent e) {
    if( isVisible() )
      area.setVisibleRectangle( (DRectangle)this.clone() );
    setVisible( false );
    area.removeDElement( this );
  }

  public void mouseExited( MouseEvent e ){
    setVisible( false );
  }

  public void mouseEntered( MouseEvent e ){
    setVisible( true );
  }

  /**
   * Invoked when a mouse button is pressed on a component and then
   * dragged.  Mouse drag events will continue to be delivered to
   * the component where the first originated until the mouse button is
   * released (regardless of whether the mouse position is within the
   * bounds of the component).
   */
  public void mouseDragged(MouseEvent e){
    if( !isVisible() ) return;
    DMeasures m = area.getDMeasures();
    DPoint p = m.getDPoint( e.getPoint() );
    width = p.x - start.x;
    height = p.y - start.y;
    if( width < 0 ){
      x = p.x;
      width *= -1;
    }
    if( height < 0 ){
      y = p.y;
      height *= -1;
    }
    area.repaint();
  }

  /**
   * Invoked when the mouse button has been moved on a component
   * (with no buttons no down).
   */
  public void mouseMoved(MouseEvent e){}
}