/*
 * Created on Jun 25, 2003
 *
 * To change this generated comment go to 
 * Window>Preferences>Java>Code Generation>Code Template
 */
package jdraw.figures;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.util.*;

import jdraw.framework.*;

/**
 * @author Christoph
 */
public class BorderDecorator extends Decorator {
		
		
	/**
	 * @author Christoph
	 *
	 * To change this generated comment edit the template variable "typecomment":
	 * Window>Preferences>Java>Templates.
	 * To enable and disable the creation of type comments go to
	 * Window>Preferences>Java>Code Generation.
	 */
	protected class HandleDecorator implements Handle {
		Handle handle;
		
		HandleDecorator(Handle h) {
			handle = h;
		}
		/**
		 * @see jdraw.framework.Handle#getOwner()
		 */
		public Figure getOwner() {
			return BorderDecorator.this;
		}

		/**
		 * @see jdraw.framework.Handle#getBounds()
		 */
		public java.awt.Rectangle getBounds() {
			return handle.getBounds();
		}

		/**
		 * @see jdraw.framework.Handle#draw(java.awt.Graphics)
		 */
		public void draw(Graphics g) {
			handle.draw(g);
		}

		/**
		 * @see jdraw.framework.Handle#getCursor()
		 */
		public Cursor getCursor() {
			return handle.getCursor();
		}

		/**
		 * @see jdraw.framework.Handle#contains(int, int)
		 */
		public boolean contains(int x, int y) {
			return handle.contains(x, y);
		}

		/**
		 * @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) {
			handle.startInteraction(x, y, e, v);
		}

		/**
		 * @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) {
			handle.dragInteraction(x, y, e, v);
		}

		/**
		 * @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) {
			handle.stopInteraction(x, y, e, v);
		}

	}
	private static final int inset = 2;

	public BorderDecorator(Figure f) {
		component = f;
	}
	
	/* (non-Javadoc)
	 * @see jdraw.framework.Figure#draw(java.awt.Graphics)
	 */
	public void draw(Graphics g) {
		java.awt.Rectangle bounds = getBounds();
		int x = bounds.x;
		int y = bounds.y;
		int w = bounds.width;
		int h = bounds.height;
		Color c = g.getColor();
		
		component.draw(g);
		g.setColor(Color.GRAY);
		g.drawLine(x, y+h, x+w, y+h);
		g.drawLine(x+w, y, x+w, y+h);
		g.setColor(Color.WHITE);
		g.drawLine(x, y, x+w, y);
		g.drawLine(x, y+h, x, y);
	}

	/* (non-Javadoc)
	 * @see jdraw.framework.Figure#getBounds()
	 */
	public java.awt.Rectangle getBounds() {
		java.awt.Rectangle bounds = component.getBounds();
		
		return new java.awt.Rectangle(bounds.x-inset, bounds.y-inset,
																	bounds.width+2*inset, bounds.height+2*inset);
	}

	/* (non-Javadoc)
	 * @see jdraw.framework.Figure#setBounds(java.awt.Point, java.awt.Point)
	 */
	public void setBounds(Point origin, Point corner) {
		component.setBounds(
			new Point(Math.min(origin.x, corner.x) + inset,
								Math.min(origin.y, corner.y) + inset),
			new Point(Math.max(origin.x, corner.x) - inset,
								Math.max(origin.y, corner.y) - inset));
	}

	/**
	 * @see jdraw.framework.Figure#getHandles()
	 */
	public java.util.List getHandles() {
		java.util.List handles = component.getHandles();
		java.util.List ll = new LinkedList();
		Iterator it = handles.iterator();
		while(it.hasNext()) {
			Handle h = (Handle)it.next();
			ll.add(new HandleDecorator(h));
		}
		return ll;
	}

}
