/*
 * Created on Apr 21, 2003
 *
 * To change this generated comment go to
 * Window>Preferences>Java>Code Generation>Code Template
 */
package jdraw.std;

import java.awt.event.*;
import java.awt.*;
import jdraw.framework.*;

/**
 * @author Christoph
 */
public abstract class StdHandle implements FigureHandle {
	protected static final int WIDTH  = 6;
	protected static final int HEIGHT = 6;

	protected Point anchor;

	protected final Figure owner;

	public StdHandle(Figure owner) {
		this.owner = owner;
	}

	public Figure getOwner(){
		return owner;
	}

	/* (non-Javadoc)
	 * @see jdraw.framework.Handle#draw(java.awt.Graphics)
	 */
	public void draw(Graphics g) {
		java.awt.Rectangle h = getBounds();
		Color tmp = g.getColor();
		g.setColor(Color.WHITE);
		g.fillRect(h.x, h.y, h.width, h.height);
		g.setColor(Color.BLACK);
		g.drawRect(h.x, h.y, h.width, h.height);
	}

	/* (non-Javadoc)
	 * @see jdraw.framework.Handle#getCursor()
	 */
	public Cursor getCursor() {
		return Cursor.getDefaultCursor();
	}

	/* (non-Javadoc)
	 * @see jdraw.framework.Handle#contains(int, int)
	 */
	public boolean contains(int x, int y) {
		return getBounds().contains(x, y);
	}

	protected abstract Point getAnchor(java.awt.Rectangle r);

	/* (non-Javadoc)
	 * @see jdraw.framework.Handle#startInteraction(int, int, java.awt.event.MouseEvent, jdraw.framework.DrawView)
	 */
	public void startInteraction(int x, int y, MouseEvent e, DrawView v) {
		java.awt.Rectangle r = getOwner().getBounds();
		anchor = getAnchor(r);
		showStatus(e, v);
	}

	/* (non-Javadoc)
	 * @see jdraw.framework.Handle#dragInteraction(int, int, java.awt.event.MouseEvent, jdraw.framework.DrawView)
	 */
	public void dragInteraction(int x, int y, MouseEvent e, DrawView v) {
		showStatus(e, v);
	}

	/* (non-Javadoc)
	 * @see jdraw.framework.Handle#stopInteraction(int, int, java.awt.event.MouseEvent, jdraw.framework.DrawView)
	 */
	public void stopInteraction(int x, int y, MouseEvent e, DrawView v){
		v.getEditor().showStatus("");
	}

	private void showStatus(MouseEvent e, DrawView v) {
		v.getEditor().showStatus("w = " + owner.getBounds().width + " h = " + owner.getBounds().height);
	}

	/**
	 * North Handle
	 */
	public static class North extends StdHandle {
		private int width; 	// at anchor point

		public North(Figure owner) {
			super(owner);
		}

		public java.awt.Rectangle getBounds() {
			java.awt.Rectangle b = owner.getBounds();
			return new java.awt.Rectangle(b.x+b.width/2-WIDTH/2, b.y-HEIGHT/2, WIDTH, HEIGHT);
		}

		public void dragInteraction(int x, int y, MouseEvent e, DrawView v) {
			owner.setBounds(new Point(anchor.x-width/2, anchor.y),
											new Point(anchor.x+width/2, y));
			super.dragInteraction(x,y,e,v);
		}

		protected Point getAnchor(java.awt.Rectangle r) {
			width = r.width;
			return new Point(r.x + r.width/2, r.y + r.height);
		}

		public Cursor getCursor() {
			return Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR);
		}

	}

	/**
	 * South Handle
	 */


	/**
	 * West Handle
	 */
	public static class West extends StdHandle {
		private int height; 	// at anchor point

		public West(Figure owner) {
			super(owner);
		}

		public java.awt.Rectangle getBounds() {
			java.awt.Rectangle b = owner.getBounds();
			return new java.awt.Rectangle(b.x-WIDTH/2, b.y+b.height/2-HEIGHT/2, WIDTH, HEIGHT);
		}

		public void dragInteraction(int x, int y, MouseEvent e, DrawView v) {
			owner.setBounds(new Point(anchor.x, anchor.y-height/2),
											new Point(x, anchor.y+height/2));
			super.dragInteraction(x,y,e,v);
		}

		protected Point getAnchor(java.awt.Rectangle r) {
			height = r.height;
			return new Point(r.x + r.width, r.y+r.height/2);
		}

		public Cursor getCursor() {
			return Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR);
		}
	}

	/**
	 * East Handle
	 */


	/**
	 * NorthEast Handle
	 */
	public static class NorthEast extends StdHandle {

		public NorthEast(Figure owner) {
			super(owner);
		}

		public java.awt.Rectangle getBounds() {
			java.awt.Rectangle b = owner.getBounds();
			return new java.awt.Rectangle(b.x+b.width-WIDTH/2, b.y-HEIGHT/2, WIDTH, HEIGHT);
		}

		public void dragInteraction(int x, int y, MouseEvent e, DrawView v) {
			owner.setBounds(anchor, e.getPoint());
			super.dragInteraction(x,y,e,v);
		}

		protected Point getAnchor(java.awt.Rectangle r) {
			return new Point(r.x, r.y + r.height);
		}

		public Cursor getCursor() {
			return Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR);
		}

	}

}


/*
 * Created on Jun 25, 2003
 *
 * To change this generated comment go to
 * Window>Preferences>Java>Code Generation>Code Template
 */
package jdraw.std;

import java.awt.*;
import java.awt.event.*;

import jdraw.framework.DrawView;
import jdraw.framework.Figure;
import jdraw.framework.FigureHandle;

public class StdMoveHandle implements FigureHandle {
	private static final int NW = 0;
	private static final int SW = 1;
	private static final int SE = 2;
	private static final int NE = 3;

	private static final int WIDTH = 6;
	private static final int HEIGHT = 6;
	private final int corner;
	private Point startDrag = null;
	private Point lastDrag = null;
	private Figure owner;

	public StdMoveHandle(int corner, Figure owner) {
		this.corner = corner;
		this.owner = owner;
	}

	public Figure getOwner() {
		return owner;
	}

	public Rectangle getBounds() {
		Rectangle r = owner.getBounds();
		int cx, cy;
		switch (corner) {
		case NW: cx = r.x; cy = r.y; break;
		case SW: cx = r.x; cy = r.y + r.height; break;
		case SE: cx = r.x + r.width; cy = r.y + r.height; break;
		case NE: default: cx = r.x + r.width; cy = r.y; break;
		}
		return new Rectangle(cx-WIDTH/2, cy-HEIGHT/2, WIDTH, HEIGHT);
	}

	/* (non-Javadoc)
	 * @see jdraw.framework.Handle#draw(java.awt.Graphics)
	 */
	public void draw(Graphics g) {
		Rectangle r = getBounds();
		Color tmp = g.getColor();
		g.setColor(Color.GRAY);
		g.fillRect(r.x, r.y, r.width, r.height);
		g.setColor(Color.BLACK);
		g.drawRect(r.x, r.y, r.width, r.height);
		g.setColor(tmp);
	}

	public Cursor getCursor() {
		return Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR);
	}

	public boolean contains(int x, int y) {
		return getBounds().contains(x, y);
	}

	public void startInteraction(int x, int y, MouseEvent e, DrawView v) {
		Rectangle r = getBounds();
		startDrag = new Point((int)r.getCenterX(), (int)r.getCenterY());
		lastDrag = startDrag;
		v.getEditor().showStatus("Move - dx = 0, dy = 0");
	}

	/* (non-Javadoc)
	 * @see jdraw.framework.Handle#dragInteraction(int, int, java.awt.event.MouseEvent, jdraw.framework.DrawView)
	 */
	public void dragInteraction(int x, int y, MouseEvent e, DrawView v) {
		int dx = x - lastDrag.x;
		int dy = y - lastDrag.y;
		lastDrag = e.getPoint();
		owner.move(dx, dy);
		v.getEditor().showStatus("Move - dx = " + (x-startDrag.x) + ", dy = " + (y-startDrag.y));
	}

	/* (non-Javadoc)
	 * @see jdraw.framework.Handle#stopInteraction(int, int, java.awt.event.MouseEvent, jdraw.framework.DrawView)
	 */
	public void stopInteraction(int x, int y, MouseEvent e, DrawView v) {
		v.getEditor().showStatus("");
	}

}


/*
 * Created on Apr 12, 2003
 *
 * To change this generated comment go to
 * Window>Preferences>Java>Code Generation>Code Template
 */
package jdraw.std;

import java.util.*;

import jdraw.framework.DrawModel;
import jdraw.framework.DrawModelListener;
import jdraw.framework.*;

/**
 * @author Christoph Denzler
 */
public class StdDrawModel implements DrawModel, FigureListener {

	private List figures = new ArrayList();
	private List modelChangeListeners = new ArrayList();

	/**
	 * Adds a new figure to the draw model.
	 * @param f figure to be added to draw model.
	 */
	public void addFigure(Figure f) {
		if (!figures.contains(f)) {
			figures.add(f);
			f.addFigureListener(this);
			notifyDrawModelListeners(new DrawModelEvent(this, f, DrawModelEvent.FIGURE_ADDED));
		}
	}

	/**
	 * Removes a given figure from the draw model.
	 * @param f figure to be removed from draw model.
	 */
	public void removeFigure(Figure f) {
		if (figures.remove(f)) {
			f.removeFigureListener(this);
			notifyDrawModelListeners(new DrawModelEvent(this, f, DrawModelEvent.FIGURE_REMOVED));
		}
	}

	/**
	 * Returns an iterator which can be used to iterate over
	 * all figures of a draw model.
	 * @return iterator to iterate over the draw model
	 */
	public Iterator getFigures() {
		return figures.iterator();
	}

	/**
	 * Adds the specified model listener to receive model events from
	 * this draw model. If listener is null, no exception is thrown and no
	 * action is performed.
	 * @param listener the draw model listener.
	 * @see DrawModelListener
	 */
	public void addModelChangeListener(DrawModelListener listener) {
		modelChangeListeners.add(listener);
	}

	/**
	 * Removes the specified model listener so that it no longer receives
	 * model events from this draw model. This method performs no function,
	 * nor does it throw an exception, if the listener specified by the
	 * argument was not previously added to this figure.
	 * If listener is null, no exception is thrown and no action is performed.
	 * @param listener the draw model listener.
	 * @see DrawModelListener
	 */
	public void removeModelChangeListener(DrawModelListener listener) {
		modelChangeListeners.remove(listener);
	}

	/**
	 * Listens to the FigureEvents of all figures contained in this model.
	 * Responds to any events by notifying all DrawModelListeners.
	 *
	 * @param e a FigureEvent occurring in any Figure contained in this model.
	 * @see jdraw.framework.FigureListener#figureChanged(FigureEvent)
	 */
	public void figureChanged(FigureEvent e) {
		notifyDrawModelListeners(
			new DrawModelEvent(this, e.getFigure(), DrawModelEvent.FIGURE_CHANGED)
		);
	}

	private void notifyDrawModelListeners(DrawModelEvent e) {
		Iterator it = modelChangeListeners.iterator();
		while (it.hasNext()) {
			((DrawModelListener)it.next()).modelChanged(e);
		}
	}


}
