package jdraw.framework;

/**
 * The interface DrawEditor defines the interface for accessing the
 * object which contains the draw view. 
 *
 * @see DrawView
 *
 * @author  Dominik Gruntz
 * @version 2.0
 */
public interface DrawEditor {

    /**
     * Returns the editor's drawing view.
	 * @return the drawing view displayed in this editor
     */
 	public DrawView getView();
	
	/**
	 * Shows a status message in the editor's user interface
	 * @param msg the status message to be displayed
	 */
	void showStatus(String msg);

	/**
	 * Adds a menu in the edotor' user interface
	 * @param menu the menu to be added
	 */
	public void addMenu(javax.swing.JMenu menu);

	/**
	 * Removes a menu in the edotor' user interface
	 * @param menu the menu to be removed
	 */
	public void removeMenu(javax.swing.JMenu menu);
	
	/**
	 * Informs the draw editor that the tool has changed
	 */
	public void toolChanged();
}


package jdraw.framework;

import java.util.EventObject;

/**
 * An event which indicates that a draw model has changed.
 * This event is fired if new figures are added to a model or
 * if figures are removed from a model. The event is also fired
 * if one figure in a draw model changes (e.g. its size or its
 * position).
 * @see FigureListener
 *
 * @author  Dominik Gruntz
 * @version 2.0
 */

public class DrawModelEvent extends EventObject {

	/**
	 * This event indicates that a figure was added to the draw model.
	 */
	public final static int FIGURE_ADDED = 0;

	/**
	 * This event indicates that a figure was removed from the draw model.
	 */
	public final static int FIGURE_REMOVED = 1;

	/**
	 * This event indicates that a figure was changed in the draw model.
	 */
	public final static int FIGURE_CHANGED = 2;

	private Figure figure;
	private int type;
	
    /**
     * Constructs a DrawModelEvent object with the specified model.
     * @param source  model which changed
	 * @param figure  the affected figure
	 * @param type    an integer indicating the event type
	 */
    public DrawModelEvent(DrawModel source, Figure figure, int type) {
        super(source);
		this.figure = figure;
		this.type = type;
     }

    /**
     * Returns the draw model which changed.
     * @return changed model
     */
    public DrawModel getModel() {
        return (DrawModel)getSource();
    }
	
	/**
	 * Returns the figure which initiated the event.
	 * @return added, removed or changed figure (depending on event type)
	 */
	public Figure getFigure(){
		return figure;
	}
	/**
	 * Returns the event type.
	 * @return type of event
	 */
	public int getType(){
		return type;
	}
}


package jdraw.framework;

import java.awt.event.MouseEvent;

/**
 * A tool defines a mode of the drawing view, intendid to be set over a
 * tool bar. An example of a tool is the construction of new figures.
 * Whenever a new tool is activated or deactivated, the corresponding
 * methods are called. This can be used to e.g. adjust the mouse cursor
 * depending on the current tool.
 * <P>
 * All input events targeted to the drawing view are forwarded to its 
 * current tool.
 *
 * @see Figure
 *
 * @author  Dominik Gruntz
 * @version 2.0
 */
public interface DrawTool {

	/**
	 * Activates the tool for the given view. This method is called
	 * whenever the user switches to this tool. Use this method to
	 * reinitialize a tool.
	 */
	public void activate();

	/**
	 * Deactivates the tool. This method is called whenever the user
	 * switches to another tool. Use this method to do some clean-up
	 * when the tool is switched. Subclassers should always call
	 * super.deactivate.
	 */
	public void deactivate();
	 
	/**
	 * Handles mouse down events in the drawing view.
	 * @param x x coordinate of mouse position
	 * @param y y coordinate of mouse position
	 * @param e mouse event, contains state of modifiers
	 */
	public void mouseDown(int x, int y, MouseEvent e);

	/**
	 * Handles mouse drag events in the drawing view.
	 * @param x x coordinate of mouse position
	 * @param y y coordinate of mouse position
	 * @param e mouse event, contains state of modifiers
	 */
	public void mouseDrag(int x, int y, MouseEvent e);

	/**
	 * Handles mouse up in the drawing view.
	 * @param x x coordinate of mouse position
	 * @param y y coordinate of mouse position
	 * @param e mouse event, contains state of modifiers
	 */
	public void mouseUp  (int x, int y, MouseEvent e);
	
}


package jdraw.framework;

import java.awt.*;

/**
 * The class DrawView displays a graphic model. It is responsible for the
 * presentation of the model and the interaction with the user (selecting,
 * moving and dragging new figures).
 *
 * The type of new figures which are generated if a rectangle is drawn
 * is defined by the currently installed factory.
 *
 * @see DrawModel
 * @see DrawTool
 *
 * @author  Dominik Gruntz
 * @version 2.0
 */
public interface DrawView  {

	/**
	 * Returns the graphic model of this view.
	 * @return the graphic model presented by this view
	 */
	public DrawModel getModel();

	/**
	 * Returns the draw editor which contains this view.
	 * This object gains access to the editor's menu bar 
	 * and status field. 
	 * @return the draw editor
	 */
	public DrawEditor getEditor();
	
	/**
	 * Returns the currently active tool.
	 * @return active tool
	 */
	public DrawTool getTool();
	
	/**
	 * Sets the tool to be used.
	 * @param tool tool to be used by the draw view
	 */
	public void setTool(DrawTool tool);
	
	/**
	 * Sets the default tool (selection and moving of figures).
	 */
	public void setDefaultTool();
	
	
	/**
	 * Sets the current point constrainer.
	 */
	public void setConstrainer(PointConstrainer p);

	/**
	 * Gets the current grid setting.
	 */
	public PointConstrainer getConstrainer();
	
	
	/**
	 * Returns the selected figures.
	 */
	public java.util.Set getSelection();
	
		
	/**
	 * Clears the selection.
	 */
	public void clearSelection();
	
	
	
	/**
	 * Adds a figure to the selected figures.
	 */	
	public void addToSelection(Figure f);
		
	/**
	 * Removes a figure from the selected figures.
	 */	
	public void removeFromSelection(Figure f);
		
	/**
	 * Draws the graphic model. The paint method iterates over all
	 * figures in the graphic model and calls their paint method.
	 */	
	public void paint(Graphics g);

	/**
	 * Repaints this view.
	 */
	public void repaint();
	
	/**
	 * Returns the size of the drawing.
	 * @return size of the drawing
	 */
	public Dimension getSize();
	
	/**
	 * Sets the cursor of the DrawingView.
	 */
	public void setCursor(Cursor c);
	
}


package jdraw.framework;

import java.util.EventObject;

/**
 * An event which indicates that a figure event occurred in a figure.
 * This may be a change of position or a change of the figure's size.
 * @see FigureListener
 *
 * @author  Dominik Gruntz
 * @version 2.0
 */
public class FigureEvent extends EventObject {

    /**
	 * Constructs a FigureEvent object with the specified figure.
	 * @param source figure which changed
	 */
	public FigureEvent(Figure source) {
        super(source);
     }

    /**
	 * Returns the figure which changed.
	 * @return changed figure
	 */
	public Figure getFigure() {
        return (Figure)getSource();
    }

}


package jdraw.framework;

import java.util.EventListener;

/**
 * Listener interested in figure changes.
 * 
 * @see FigureEvent
 *
 * @author  Dominik Gruntz
 * @version 2.0
 */
public interface FigureListener extends EventListener {

	/**
	 * Sent when a figure has changed.
	 * @param e figure event
	 */
	public void figureChanged(FigureEvent e);
}



package jdraw.framework;

import java.util.*;

/**
 * The class DrawModel represents the model of a drawing, i.e.
 * all figures stored in a graphic. Every draw view refers to a 
 * model.
 *
 * @see DrawView
 *
 * @author  Dominik Gruntz
 * @version 2.0
 */
public interface DrawModel {
	
	/**
	 * Adds a new figure to the draw model.
	 * @param f figure to be added to draw model.
	 */
	public void addFigure(Figure f);
	
	/**
	 * Removes a given figure from the draw model.
	 * @param f figure to be removed from draw model.
	 */
	public void removeFigure(Figure f);
	
	/**
	 * 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();
		
	/**
	 * Adds the specified model listener to receive model events from 
	 * this draw model.
	 * @param listener the draw model listener.
	 * @see DrawModelListener
	 */
	public void addModelChangeListener(DrawModelListener 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. 
	 * @param listener the draw model listener.
	 * @see DrawModelListener
	 */
	public void removeModelChangeListener(DrawModelListener listener);
	
}


package jdraw.framework;

import java.util.EventListener;

/**
 * Listener interested in draw model changes.
 *
 * @author  Dominik Gruntz
 * @version 2.0
 */
public interface DrawModelListener extends EventListener {

	/**
	 * Sent when a draw model has changed.
	 * @param e draw model event
	 */
	public void modelChanged(DrawModelEvent e);
}


package jdraw.framework;

/**
 * A DrawTool factory creates an instance of a draw tool and returns an icon
 * which can be used in the tool palette of the graphics editor.
 *
 * @see DrawTool
 *
 * @author  Dominik Gruntz
 * @version 2.0
 */
public interface DrawToolFactory {
	/**
	 * Returns a draw tool.
	 */
	DrawTool createTool(DrawEditor e);
	
	/**
	 * Returns an icon which can be used to provide access to the tool with a tool bar.
	 * If no icon is available then null is returned.
	 */
	javax.swing.ImageIcon getIcon();
}


package jdraw.framework;

import java.awt.*;
import java.util.List;

/**
 * Base interface for all figures implemented in the grafic editor.
 * Every Figure-type has to implement this interface.
 *
 *
 * @author  Dominik Gruntz
 * @version 2.0
 */
public interface Figure extends java.io.Serializable, Cloneable {

	/**
	 * Changes the bounds of the figure. The figure has to adjust its size
	 * and position when this method is called, and registered figure listeners
	 * have to be notified.
	 * @param origin the new origin
	 * @param corner the new corner
	 * @see #addFigureListener
	 */
	void setBounds(Point origin, Point corner);
	
	/**
	 * draw is called when the figure has to be drawn.
	 * @param g Graphics object on which figure has to be drawn.
	 * @see java.awt.Graphics
	 */
	void draw(Graphics g);
	
	/**
	 * Moves the figure. The figure has to adjust its coordinates 
	 * when this method is called, and registered figure listeners
	 * have to be notified.
	 * @param dx move distance in x direction (argument in pixels)
	 * @param dy move distance in y direction (argument in pixels)
	 * @see #addFigureListener
	 */
	void  move(int dx, int dy);
	
	/**
	 * Tests whether the mouse coordinates are contained in the figure.
	 * contains is called when the mouse is pressed in the grafic in
	 * order to decide which figure has to be selected.
	 * @param x x-coordinate of mouse position
	 * @param y y-coordinate of mouse position
	 * @return <tt>true</tt>, if coordinates are contained in the figure, 
	 *         <tt>false</tt> otherwise
	 */
	boolean contains(int x, int y);
	
	/**
	 * Returns the bounds of a figure. The bounds of a figure  is
	 * a rectangle which contains the figure.
	 * @return bounds of the figure
	 */
	Rectangle getBounds();
	
	/**
	 * Returns the center of a figure. The center is usually the center
	 * of its bounds rectangle.
	 * @return center of the figure
	 */
	Point getCenter();
	
	/**
	 * Returns a list of handles. Handles are used to manipulate a figure.
	 * If the figure does not support handles, <tt>null</tt> may be returned
	 * as result.
	 * @return list of handles (may be null if handles are not supported)
	 * @see FigureHandle
	 */
	List  getHandles();
	
	/**
	 * Tests whether the figure is empty. This method may be called when a
	 * new figure is spawn in order to decide whether the figure should be
	 * included in the model or not. Empty figures may be discarded. This 
	 * behavior has to be implemented in the extensions of DrawTool.
	 * @return <tt>true</tt>, if figure is "empty"
	 *         <tt>false</tt> otherwise
	 * @see DrawTool
	 */
	boolean isEmpty();
	
	/**
	 * Adds the specified figure listener to receive figure events from 
	 * this figure. If listener is null, no exception is thrown and no 
	 * action is performed.
	 * @param listener the figure listener.
	 * @see FigureListener
	 */
	public void addFigureListener(FigureListener listener);
	
	/**
	 * Removes the specified figure listener so that it no longer receives
	 * figure events from this figure. 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 figure listener.
	 * @see FigureListener
	 */
	public void removeFigureListener(FigureListener listener);
	
	/**
	 * Returns a Clone of this figure
	 * @return clone of figure
	 */	
	public Object clone();
	
}


package jdraw.framework;

import java.awt.*;
import java.awt.event.MouseEvent;

/**
 * Handles are used to change a figure by direct manipulation.
 * Handles know their owning figure and they provide methods to
 * ask the handle's bounds and to track changes.
 * 
 * @see Figure
 *
 * @author  Dominik Gruntz
 * @version 2.0
 */
public interface FigureHandle {
	
	/**
	 * Gets the handle's owner.
	 * @return the figure which owns the handle
	 */
	public Figure getOwner();
	
	/**
	 * Gets the bounding box of the handle. This result depends on the
	 * current position of the figure.
	 * @return bounds of handle
	 */
	public java.awt.Rectangle getBounds();
	
	/**
	 * Draws this handle.
	 */
	public void draw(Graphics g);

	/**
	 * Returns a curser which should be displayed when the mouse is
	 * over the handle. Signals the type of operation which can be
	 * performed using this handle.
	 * <P>
	 * A default implementation may return
	 * Cursor.getDefaultCursor().
	 * @return handle's Cursor
	 */
	public Cursor getCursor();
	
	/**
	 * Tests if a point is contained in the handle.
	 * @param x x-coordinate of mouse position
	 * @param y y-coordinate of mouse position
	 * @return <tt>true</tt>, if coordinates are contained in the figure, 
	 *         <tt>false</tt> otherwise
	 */
	public boolean contains(int x, int y);
	
	/**
	 * Tracks the start of an interaction. Usually, the position
	 * where an interaction starts is stored.
	 * @param x the x position where the interaction started
	 * @param y the y position where the interaction started
	 * @param view the view in which the interaction is performed
	 */
	public void startInteraction(int x, int y, MouseEvent e, DrawView v);

	
	/**
	 * Tracks a step of a started interaction.
	 * @param x the current x position
	 * @param y the current y position
	 * @param view the view in which the interaction is performed
	 */
	public void dragInteraction(int x, int y, MouseEvent e, DrawView v);

	
	/**
	 * Tracks the end of a running interaction.
	 * @param x the current x position
	 * @param y the current y position
	 * @param view the view in which the interaction is performed
	 */
	public void stopInteraction(int x, int y, MouseEvent e, DrawView v);
}


package jdraw.framework;

import java.awt.Point;

/**
 * A PointConstrainer is used to restrict the coordinates used when the mouse
 * is clicked. The methods defined in this interface are used by the draw view.
 *
 * @author  Dominik Gruntz
 * @version 2.0
 */
public interface PointConstrainer {
	
	/**
	 * Returns constrained coordinates for p, e.g. rounded to a grid.
	 * @param p mouse coordinates
	 * @return constrained coordinates
	 */
	Point constrainPoint(Point p);
	
	/**
	 * Returns the horizontal step size when the selection is moved with the arrow keys.
	 * @param right true if selection is moved right; false otherwise
	 * @return step size in horizontal direction (positive result)
	 */
	int getStepX(boolean right);

	/**
	 * Returns the vertical step size when the selection is moved with the arrow keys.
	 * @param down true if selection is moved down; false otherwise
	 * @return step size in vertical direction (positive result)
	 */
	int getStepY(boolean down);
}





