JHotDraw 7.4.1

org.jhotdraw.draw.tool
Class ConnectionTool

java.lang.Object
  extended by org.jhotdraw.beans.AbstractBean
      extended by org.jhotdraw.draw.tool.AbstractTool
          extended by org.jhotdraw.draw.tool.ConnectionTool
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

public class ConnectionTool
extends AbstractTool

A tool to create a connection between two figures. The ConnectionFigure to be created is specified by a prototype. The location of the start and end points are controlled by Connectors.

To create a connection using the ConnectionTool, the user does the following mouse gestures on a DrawingView:

  1. Press the mouse button inside of a Figure. If the ConnectionTool can find a Connector at this location, it uses it as the starting point for the connection.
  2. Drag the mouse while keeping the mouse button pressed, and then release the mouse button. This defines the end point of the connection. If the ConnectionTool finds a Connector at this location, it uses it as the end point of the connection and creates a ConnectionFigure.

Version:
$Id: ConnectionTool.java 604 2010-01-09 12:00:29Z rawcoder $
Author:
Werner Randelshofer
See Also:
Serialized Form

Field Summary
protected  java.util.Collection<Connector> connectors
           
protected  ConnectionFigure createdFigure
          The created figure.
protected  Connector endConnector
          The Connector at the end point of the connection.
protected  ConnectionFigure prototype
          the prototypical figure that is used to create new connections.
protected  Connector startConnector
          The Connector at the start point of the connection.
protected  Figure targetFigure
          The figure for which we enabled drawing of connectors.
 
Fields inherited from class org.jhotdraw.draw.tool.AbstractTool
anchor, editor, isWorking, listenerList
 
Fields inherited from class org.jhotdraw.beans.AbstractBean
propertySupport
 
Constructor Summary
ConnectionTool(ConnectionFigure prototype)
          Creates a new instance.
ConnectionTool(ConnectionFigure prototype, java.util.Map<AttributeKey,java.lang.Object> attributes)
           
ConnectionTool(ConnectionFigure prototype, java.util.Map<AttributeKey,java.lang.Object> attributes, java.lang.String presentationName)
           
ConnectionTool(java.lang.String prototypeClassName)
           
ConnectionTool(java.lang.String prototypeClassName, java.util.Map<AttributeKey,java.lang.Object> attributes, java.lang.String presentationName)
           
 
Method Summary
 void activate(DrawingEditor editor)
          Activates the tool for the given editor.
protected  boolean canConnect(ConnectionFigure f, Connector startConnector)
          This method is called on the Figure, onto which the user wants to start a new connection.
protected  boolean canConnect(ConnectionFigure f, Connector startConnector, Connector endConnector)
          This method is called on the Figure, onto which the user wants to end a new connection.
protected  ConnectionFigure createFigure()
          Creates the ConnectionFigure.
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.
 void draw(java.awt.Graphics2D g)
          Draws the tool.
protected  int getAnchorWidth()
           
 ConnectionFigure getPrototype()
           
 boolean isToolDoneAfterCreation()
          Returns true, if this tool fires toolDone immediately after a new figure has been created.
 void mouseDragged(java.awt.event.MouseEvent e)
          Adjust the created connection.
 void mouseMoved(java.awt.event.MouseEvent evt)
           
 void mousePressed(java.awt.event.MouseEvent evt)
          Manipulates connections in a context dependent way.
 void mouseReleased(java.awt.event.MouseEvent e)
          Connects the figures if the mouse is released over another figure.
 void repaintConnectors(java.awt.event.MouseEvent evt)
          Updates the list of connectors that we draw when the user moves or drags the mouse over a figure to which can connect.
 void setToolDoneAfterCreation(boolean newValue)
          If this is set to false, the CreationTool does not fire toolDone after a new Figure has been created.
 
Methods inherited from class org.jhotdraw.draw.tool.AbstractTool
addToolListener, addUndoableEditListener, constrainPoint, constrainPoint, createActionMap, createInputMap, editCopy, editCut, editDelete, editDuplicate, editPaste, fireAreaInvalidated, fireAreaInvalidated, fireBoundsInvalidated, fireToolDone, fireToolStarted, getActionMap, getDrawing, getEditor, getInputMap, getToolTipText, getView, isActive, keyPressed, keyReleased, keyTyped, maybeFireBoundsInvalidated, mouseClicked, mouseEntered, mouseExited, removeToolListener, removeUndoableEditListener, setActionMap, setInputMap, supportsHandleInteraction, updateCursor, viewToDrawing
 
Methods inherited from class org.jhotdraw.beans.AbstractBean
addPropertyChangeListener, addPropertyChangeListener, clone, firePropertyChange, firePropertyChange, firePropertyChange, getPropertyChangeListeners, removePropertyChangeListener, removePropertyChangeListener
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

startConnector

protected Connector startConnector
The Connector at the start point of the connection.


endConnector

protected Connector endConnector
The Connector at the end point of the connection.


createdFigure

protected ConnectionFigure createdFigure
The created figure.


prototype

protected ConnectionFigure prototype
the prototypical figure that is used to create new connections.


targetFigure

protected Figure targetFigure
The figure for which we enabled drawing of connectors.


connectors

protected java.util.Collection<Connector> connectors
Constructor Detail

ConnectionTool

public ConnectionTool(ConnectionFigure prototype)
Creates a new instance.


ConnectionTool

public ConnectionTool(ConnectionFigure prototype,
                      java.util.Map<AttributeKey,java.lang.Object> attributes)

ConnectionTool

public ConnectionTool(ConnectionFigure prototype,
                      java.util.Map<AttributeKey,java.lang.Object> attributes,
                      java.lang.String presentationName)

ConnectionTool

public ConnectionTool(java.lang.String prototypeClassName)

ConnectionTool

public ConnectionTool(java.lang.String prototypeClassName,
                      java.util.Map<AttributeKey,java.lang.Object> attributes,
                      java.lang.String presentationName)
Method Detail

getPrototype

public ConnectionFigure getPrototype()

getAnchorWidth

protected int getAnchorWidth()

canConnect

protected boolean canConnect(ConnectionFigure f,
                             Connector startConnector)
This method is called on the Figure, onto which the user wants to start a new connection.

Parameters:
f - The ConnectionFigure.
startConnector - The Connector of the start Figure.
Returns:
True, if a connection can be made.

canConnect

protected boolean canConnect(ConnectionFigure f,
                             Connector startConnector,
                             Connector endConnector)
This method is called on the Figure, onto which the user wants to end a new connection.

Parameters:
f - The ConnectionFigure.
startConnector - The Connector of the start Figure.
endConnector - The Connector of the end Figure.
Returns:
True, if a connection can be made.

mouseMoved

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

repaintConnectors

public void repaintConnectors(java.awt.event.MouseEvent evt)
Updates the list of connectors that we draw when the user moves or drags the mouse over a figure to which can connect.


mousePressed

public void mousePressed(java.awt.event.MouseEvent evt)
Manipulates connections in a context dependent way. If the mouse down hits a figure start a new connection. If the mousedown hits a connection split a segment or join two segments.

Specified by:
mousePressed in interface java.awt.event.MouseListener
Overrides:
mousePressed in class AbstractTool

mouseDragged

public void mouseDragged(java.awt.event.MouseEvent e)
Adjust the created connection.


mouseReleased

public void mouseReleased(java.awt.event.MouseEvent e)
Connects the figures if the mouse is released over another figure.

Specified by:
mouseReleased in interface java.awt.event.MouseListener
Overrides:
mouseReleased in class AbstractTool

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

createFigure

protected ConnectionFigure createFigure()
Creates the ConnectionFigure. By default the figure prototype is cloned.


draw

public void draw(java.awt.Graphics2D g)
Description copied from interface: Tool
Draws the tool.

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

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.


Copyright 1996-2010 (c) by the authors and contributors of the JHotDraw project.
Some rights reserved.