Download: ch08.zip (source code)

Graph Editor Framework

Problem Domain: interactive editing of diagrams

Framework Approach

User Interface

.

Mouse Operations

Division of Responsibility

Adding Nodes and Edges

In our graph editor framework, a concrete graph must give the framework prototype objects. For example, a figure might be created by defining a node class (CircleNode), and edge class (LineEdge), and a SimpleGraph class that defines two node prototypes and an edge prototype. (see SimpleGraph.java)

PROTOTYPE Pattern

Context

  1. A system instantiates objects of classes that are not known when the system is built.
  2. You do not want to require a separate class for each kind of object.
  3. You want to avoid a separate hierarchy of classes whose responsibility it is to create the objects.

Solution

  1. Define a prototype interface type that is common to all created objects.
  2. Supply a prototype object for each kind of object that the system creates.
  3. Clone the prototype object whenever a new object of the given kind is required. 

.

PROTOTYPE Pattern

Name in Design Pattern
Actual name (graph editor)
Prototype
Node
ConcretePrototype1
CircleNode
Creator
The GraphPanel that handles the mouse operation for adding new nodes


Framework Classes

Node Connection Points

The getConnectionPoint method in the Node interface type computes an optimal attachment point on the boundary of a node (see below). Since the node boundary may have an arbitrary shape, this computation must be carried out by each concrete class.

.

Framework Classes: AbstractEdge

Framework Classes: Graph

Framework UI Classes

A Framework Instance

Programmer responsibilities

A Framework Instance

.

A Framework Instance

Generic Framework Code

Add New Node

public void mousePressed(MouseEvent event)
{
Point2D mousePoint = event.getPoint();
Object tool = toolBar.getSelectedTool();
...
if (tool instanceof Node)
{
Node prototype = (Node) tool;
Node newNode = (Node)prototype.clone();
graph.add(newNode, mousePoint);
}
...
repaint();
}

Add New Node

.

Add New Edge

Add New Edge

Add New Edge

.


Maintained by John Loomis, last updated 16 Nov 2011