org.jhotdraw.draw
Class CreationTool

java.lang.Object
  extended by org.jhotdraw.beans.AbstractBean
      extended by org.jhotdraw.draw.AbstractTool
          extended by org.jhotdraw.draw.CreationTool
All Implemented Interfaces:
java.awt.event.KeyListener, java.awt.event.MouseListener, java.awt.event.MouseMotionListener, java.io.Serializable, java.lang.Cloneable, java.util.EventListener, Tool
Direct Known Subclasses:
ImageTool, SVGCreateFromFileTool, TextAreaCreationTool, TextCreationTool

public class CreationTool
extends AbstractTool

A tool to create new figures. The figure to be created is specified by a prototype.

To create a figure using the CreationTool, the user does the following mouse gestures on a DrawingView:

  1. Press the mouse button over the DrawingView. This defines the start point of the Figure bounds.
  2. Drag the mouse while keeping the mouse button pressed, and then release the mouse button. This defines the end point of the Figure bounds.
The CreationTool works well with most figures that fit into a rectangular shape or that concist of a single straight line. For figures that need additional editing after these mouse gestures, the use of a specialized creation tool is recommended. For example the TextTool allows to enter the text into a TextFigure after the user has performed the mouse gestures.

Alltough the mouse gestures might be fitting for the creation of a connection, the CreationTool is not suited for the creation of a ConnectionFigure. Use the ConnectionTool for this type of figures instead.

Design pattern:
Name: Prototype.
Role: Client.
Partners: Figure as Prototype.

Version:
2.4 2008-05-24 Made all private variables protected. Use crosshair cursor for creation tool.
2.2 2007-08-22 Added property 'toolDoneAfterCreation'.
2.1.1 2006-07-20 Minimal size treshold was enforced too eagerly.
2.1 2006-07-15 Changed to create prototype creation from class presentationName.
2.0 2006-01-14 Changed to support double precision coordinates.
1.0 2003-12-01 Derived from JHotDraw 5.4b1.
Author:
Werner Randelshofer
See Also:
Serialized Form

Field Summary
protected  Figure createdFigure
          The created figure.
private  boolean isToolDoneAfterCreation
          If this is set to false, the CreationTool does not fire toolDone after a new Figure has been created.
protected  java.awt.Dimension minimalSize
          We set the figure to this minimal size, if it is smaller than the minimal size treshold.
protected  java.awt.Dimension minimalSizeTreshold
          Treshold for which we create a larger shape of a minimal size.
protected  java.lang.String presentationName
          A localized name for this tool.
protected  Figure prototype
          The prototype for new figures.
protected  java.util.Map<AttributeKey,java.lang.Object> prototypeAttributes
          Attributes to be applied to the created ConnectionFigure.
 
Fields inherited from class org.jhotdraw.draw.AbstractTool
anchor, editor, isWorking, listenerList
 
Fields inherited from class org.jhotdraw.beans.AbstractBean
propertySupport
 
Constructor Summary
CreationTool(Figure prototype)
          Creates a new instance with the specified prototype but without an attribute set.
CreationTool(Figure prototype, java.util.Map<AttributeKey,java.lang.Object> attributes)
          Creates a new instance with the specified prototype but without an attribute set.
CreationTool(Figure prototype, java.util.Map<AttributeKey,java.lang.Object> attributes, java.lang.String name)
          Deprecated. This constructor might go away, because the name parameter is not used.
CreationTool(java.lang.String prototypeClassName)
          Creates a new instance.
CreationTool(java.lang.String prototypeClassName, java.util.Map<AttributeKey,java.lang.Object> attributes)
           
CreationTool(java.lang.String prototypeClassName, java.util.Map<AttributeKey,java.lang.Object> attributes, java.lang.String name)
           
 
Method Summary
 void activate(DrawingEditor editor)
          Activates the tool for the given editor.
protected  Figure createFigure()
           
protected  void creationFinished(Figure createdFigure)
          This method allows subclasses to do perform additonal user interactions after the new figure has been created.
 void deactivate(DrawingEditor editor)
          Deactivates the tool.
protected  Figure getAddedFigure()
           
protected  Figure getCreatedFigure()
           
 Figure getPrototype()
           
 boolean isToolDoneAfterCreation()
          Returns true, if this tool fires toolDone immediately after a new figure has been created.
 void mouseDragged(java.awt.event.MouseEvent evt)
           
 void mousePressed(java.awt.event.MouseEvent evt)
           
 void mouseReleased(java.awt.event.MouseEvent evt)
           
 void setToolDoneAfterCreation(boolean newValue)
          If this is set to false, the CreationTool does not fire toolDone after a new Figure has been created.
 void updateCursor(DrawingView view, java.awt.Point p)
           
 
Methods inherited from class org.jhotdraw.draw.AbstractTool
addToolListener, addUndoableEditListener, constrainPoint, constrainPoint, createActionMap, createInputMap, draw, editCopy, editCut, editDelete, editDuplicate, editPaste, fireAreaInvalidated, fireAreaInvalidated, fireToolDone, fireToolStarted, getActionMap, getDrawing, getEditor, getInputMap, getToolTipText, getView, isActive, keyPressed, keyReleased, keyTyped, mouseClicked, mouseEntered, mouseExited, mouseMoved, removeToolListener, removeUndoableEditListener, setActionMap, setInputMap, supportsHandleInteraction, viewToDrawing
 
Methods inherited from class org.jhotdraw.beans.AbstractBean
addPropertyChangeListener, addPropertyChangeListener, clone, firePropertyChange, firePropertyChange, firePropertyChange, removePropertyChangeListener, removePropertyChangeListener
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

prototypeAttributes

protected java.util.Map<AttributeKey,java.lang.Object> prototypeAttributes
Attributes to be applied to the created ConnectionFigure. These attributes override the default attributes of the DrawingEditor.


presentationName

protected java.lang.String presentationName
A localized name for this tool. The presentationName is displayed by the UndoableEdit.


minimalSizeTreshold

protected java.awt.Dimension minimalSizeTreshold
Treshold for which we create a larger shape of a minimal size.


minimalSize

protected java.awt.Dimension minimalSize
We set the figure to this minimal size, if it is smaller than the minimal size treshold.


prototype

protected Figure prototype
The prototype for new figures.


createdFigure

protected Figure createdFigure
The created figure.


isToolDoneAfterCreation

private boolean isToolDoneAfterCreation
If this is set to false, the CreationTool does not fire toolDone after a new Figure has been created. This allows to create multiple figures consecutively.

Constructor Detail

CreationTool

public CreationTool(java.lang.String prototypeClassName)
Creates a new instance.


CreationTool

public CreationTool(java.lang.String prototypeClassName,
                    java.util.Map<AttributeKey,java.lang.Object> attributes)

CreationTool

public CreationTool(java.lang.String prototypeClassName,
                    java.util.Map<AttributeKey,java.lang.Object> attributes,
                    java.lang.String name)

CreationTool

public CreationTool(Figure prototype)
Creates a new instance with the specified prototype but without an attribute set. The CreationTool clones this prototype each time a new Figure needs to be created. When a new Figure is created, the CreationTool applies the default attributes from the DrawingEditor to it.

Parameters:
prototype - The prototype used to create a new Figure.

CreationTool

public CreationTool(Figure prototype,
                    java.util.Map<AttributeKey,java.lang.Object> attributes)
Creates a new instance with the specified prototype but without an attribute set. The CreationTool clones this prototype each time a new Figure needs to be created. When a new Figure is created, the CreationTool applies the default attributes from the DrawingEditor to it, and then it applies the attributes to it, that have been supplied in this constructor.

Parameters:
prototype - The prototype used to create a new Figure.
attributes - The CreationTool applies these attributes to the prototype after having applied the default attributes from the DrawingEditor.

CreationTool

public CreationTool(Figure prototype,
                    java.util.Map<AttributeKey,java.lang.Object> attributes,
                    java.lang.String name)
Deprecated. This constructor might go away, because the name parameter is not used.

Creates a new instance with the specified prototype and attribute set.

Parameters:
prototype - The prototype used to create a new Figure.
attributes - The CreationTool applies these attributes to the prototype after having applied the default attributes from the DrawingEditor.
name - The name parameter is currently not used.
Method Detail

getPrototype

public Figure getPrototype()

activate

public void activate(DrawingEditor editor)
Description copied from interface: Tool
Activates the tool for the given editor. This method is called whenever the user switches to this tool.

Specified by:
activate in interface Tool
Overrides:
activate in class AbstractTool

deactivate

public void deactivate(DrawingEditor editor)
Description copied from interface: Tool
Deactivates the tool. This method is called whenever the user switches to another tool.

Specified by:
deactivate in interface Tool
Overrides:
deactivate in class AbstractTool

mousePressed

public void mousePressed(java.awt.event.MouseEvent evt)
Specified by:
mousePressed in interface java.awt.event.MouseListener
Overrides:
mousePressed in class AbstractTool

mouseDragged

public void mouseDragged(java.awt.event.MouseEvent evt)

mouseReleased

public void mouseReleased(java.awt.event.MouseEvent evt)
Specified by:
mouseReleased in interface java.awt.event.MouseListener
Overrides:
mouseReleased in class AbstractTool

createFigure

protected Figure createFigure()

getCreatedFigure

protected Figure getCreatedFigure()

getAddedFigure

protected Figure getAddedFigure()

creationFinished

protected void creationFinished(Figure createdFigure)
This method allows subclasses to do perform additonal user interactions after the new figure has been created. The implementation of this class just invokes fireToolDone.


setToolDoneAfterCreation

public void setToolDoneAfterCreation(boolean newValue)
If this is set to false, the CreationTool does not fire toolDone after a new Figure has been created. This allows to create multiple figures consecutively.


isToolDoneAfterCreation

public boolean isToolDoneAfterCreation()
Returns true, if this tool fires toolDone immediately after a new figure has been created.


updateCursor

public void updateCursor(DrawingView view,
                         java.awt.Point p)
Overrides:
updateCursor in class AbstractTool