org.jgraph.graph
Class DefaultGraphModel

java.lang.Object
  extended by javax.swing.undo.UndoableEditSupport
      extended by org.jgraph.graph.DefaultGraphModel
All Implemented Interfaces:
Serializable, GraphModel

public class DefaultGraphModel
extends UndoableEditSupport
implements Serializable, GraphModel

A simple implementation of a graph model.

Version:
1.0 1/1/02
Author:
Gaudenz Alder
See Also:
Serialized Form

Nested Class Summary
static class DefaultGraphModel.EmptyIterator
           
 class DefaultGraphModel.GraphModelEdit
          An implementation of GraphModelChange that can be added to the model event.
 class DefaultGraphModel.GraphModelLayerEdit
          An implementation of GraphViewChange.
 
Field Summary
protected  boolean asksAllowsChildren
          Indicates whether isLeaf is based on a node's allowsChildren value.
protected  Iterator emptyIterator
          Default instance of an empty iterator.
protected  EventListenerList listenerList
          The list of listeners that listen to the model.
protected  List roots
          Set that contains all root cells of this model.
 
Fields inherited from class javax.swing.undo.UndoableEditSupport
compoundEdit, listeners, realSource, updateLevel
 
Constructor Summary
DefaultGraphModel()
          Constructs a model that is not an attribute store.
 
Method Summary
 boolean acceptsSource(Object edge, Object port)
          Returns true if port is a valid source for edge.
 boolean acceptsTarget(Object edge, Object port)
          Returns true if port is a valid target for edge.
 void addGraphModelListener(GraphModelListener l)
          Adds a listener for the GraphModelEvent posted after the graph changes.
 Map cloneCells(Object[] cells)
          Returns a map of (cell, clone)-pairs for all cells and their children.
protected  void connect(Object edge, Object port, boolean isSource, boolean remove)
          Connects or disconnects the edge and port in this model based on remove.
 boolean contains(Object node)
          Returns true if node or one of its ancestors is in the model.
protected  DefaultGraphModel.GraphModelEdit createCellEdit(Map attributes, ConnectionSet cs, ParentMap pm, UndoableEdit[] edits)
          Returns an edit that represents a change.
protected  DefaultGraphModel.GraphModelEdit createEdit(Object[] inserted, Object[] removed, Map attributes, ConnectionSet cs, ParentMap pm)
           
protected  DefaultGraphModel.GraphModelEdit createInsertEdit(Object[] cells, Map attributeMap, ConnectionSet cs, ParentMap pm, UndoableEdit[] edits)
          Returns an edit that represents an insert.
protected  DefaultGraphModel.GraphModelLayerEdit createLayerEdit(Object[] cells, int layer)
           
protected  DefaultGraphModel.GraphModelEdit createRemoveEdit(Object[] cells)
          Returns an edit that represents a remove.
 Iterator edges(Object port)
          Returns an iterator of the edges connected to port.
 void edit(Map attributes, ConnectionSet cs, ParentMap pm, UndoableEdit[] edits)
          Applies attributes and the connection changes to the model.
protected  void fireGraphChanged(Object source, GraphModelEvent.GraphModelChange edit)
           
 Map getAttributes(Object node)
          Returns a Map that represents the attributes for the specified cell.
 Object getChild(Object parent, int index)
          Returns the child of parent at index index in the parent's child array.
 int getChildCount(Object parent)
          Returns the number of children of parent.
static List getDescendantList(GraphModel model, Object[] cells)
           
static Set getDescendants(GraphModel model, Object[] cells)
          Flattens the given array of root cells by adding the roots and their descandants.
static Set getEdges(GraphModel model, Object[] cells)
          Return the set of edges that are connected to the specified cells.
 GraphModelListener[] getGraphModelListeners()
          Return an array of all GraphModelListeners that were added to this model.
 int getIndexOfChild(Object parent, Object child)
          Returns the index of child in parent.
 int getIndexOfRoot(Object root)
          Returns the index of root in the model.
 Object getParent(Object child)
          Returns the parent of child in the model.
 Object getRootAt(int index)
          Returns the root at index index in the model.
 int getRootCount()
          Returns the number of roots in the model.
static Object[] getRoots(GraphModel model)
          Returns the roots of the specified model as an array.
 Object getSource(Object edge)
          Returns the source of edge.
static Object getSourceVertex(GraphModel model, Object edge)
          Returns the source vertex of the edge by calling getParent on getSource on the specified model.
 Object getTarget(Object edge)
          Returns the target of edge.
static Object getTargetVertex(GraphModel model, Object edge)
          Returns the target vertex of the edge by calling getParent on getTarget on the specified model.
protected  Map handleAttributes(Map attributes)
          Applies attributes to the cells specified as keys.
protected  void handleConnection(ConnectionSet.Connection c)
          Inserts the specified connection into the model.
protected  ConnectionSet handleConnectionSet(ConnectionSet cs)
          Applies connectionSet to the model.
protected  Object[] handleInsert(Object[] cells)
          Inserts cells into the model.
protected  ParentMap handleParentMap(ParentMap parentMap)
          Applies cells to the model.
protected  Object[] handleRemove(Object[] cells)
          Removes cells from the model.
 void insert(Object[] roots, Map attributes, ConnectionSet cs, ParentMap pm, UndoableEdit[] edits)
          Inserts the roots and connections into the model.
 boolean isEdge(Object edge)
          Returns true if edge is a valid edge.
 boolean isLeaf(Object node)
          Returns whether the specified node is a leaf node.
 boolean isPort(Object port)
          Returns true if port is a valid port, possibly supporting edge connection.
 void remove(Object[] roots)
          Removes cells from the model.
 void removeGraphModelListener(GraphModelListener l)
          Removes a listener previously added with addGraphModelListener().
 void toBack(Object[] cells)
          Sends cells to back.
 void toFront(Object[] cells)
          Brings cells to front.
 
Methods inherited from class javax.swing.undo.UndoableEditSupport
_postEdit, addUndoableEditListener, beginUpdate, createCompoundEdit, endUpdate, getUndoableEditListeners, getUpdateLevel, postEdit, removeUndoableEditListener, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.jgraph.graph.GraphModel
addUndoableEditListener, removeUndoableEditListener
 

Field Detail

listenerList

protected transient EventListenerList listenerList
The list of listeners that listen to the model.


emptyIterator

protected transient Iterator emptyIterator
Default instance of an empty iterator.


roots

protected List roots
Set that contains all root cells of this model.


asksAllowsChildren

protected boolean asksAllowsChildren
Indicates whether isLeaf is based on a node's allowsChildren value.

Constructor Detail

DefaultGraphModel

public DefaultGraphModel()
Constructs a model that is not an attribute store.

Method Detail

getRootCount

public int getRootCount()
Returns the number of roots in the model. Returns 0 if the model is empty.

Specified by:
getRootCount in interface GraphModel
Returns:
the number of roots in the model

getRootAt

public Object getRootAt(int index)
Returns the root at index index in the model. This should not return null if index is a valid index for the model (that is index >= 0 && index < getRootCount()).

Specified by:
getRootAt in interface GraphModel
Returns:
the root of at index index

getIndexOfRoot

public int getIndexOfRoot(Object root)
Returns the index of root in the model. If root is null, returns -1.

Specified by:
getIndexOfRoot in interface GraphModel
Parameters:
root - is a root in the model, obtained from this data source
Returns:
the index of the root in the model, or -1 if the parent is null

contains

public boolean contains(Object node)
Returns true if node or one of its ancestors is in the model.

Specified by:
contains in interface GraphModel
Returns:
true if node is in the model

getAttributes

public Map getAttributes(Object node)
Returns a Map that represents the attributes for the specified cell. This attributes have precedence over each view's attributes, regardless of isAttributeStore.

Specified by:
getAttributes in interface GraphModel
Returns:
attributes of node as a Map

getSource

public Object getSource(Object edge)
Returns the source of edge. edge must be an object previously obtained from this data source.

Specified by:
getSource in interface GraphModel
Returns:
Object that represents the source of edge

getTarget

public Object getTarget(Object edge)
Returns the target of edge. edge must be an object previously obtained from this data source.

Specified by:
getTarget in interface GraphModel
Returns:
Object that represents the target of edge

acceptsSource

public boolean acceptsSource(Object edge,
                             Object port)
Returns true if port is a valid source for edge. edge and port must be objects previously obtained from this data source.

Specified by:
acceptsSource in interface GraphModel
Returns:
true if port is a valid source for edge.

acceptsTarget

public boolean acceptsTarget(Object edge,
                             Object port)
Returns true if port is a valid target for edge. edge and port must be objects previously obtained from this data source.

Specified by:
acceptsTarget in interface GraphModel
Returns:
true if port is a valid target for edge.

edges

public Iterator edges(Object port)
Returns an iterator of the edges connected to port. port must be a object previously obtained from this data source. This method never returns null.

Specified by:
edges in interface GraphModel
Parameters:
port - a port in the graph, obtained from this data source
Returns:
Iterator that represents the connected edges

isEdge

public boolean isEdge(Object edge)
Returns true if edge is a valid edge.

Specified by:
isEdge in interface GraphModel
Returns:
true if edge is a valid edge.

isPort

public boolean isPort(Object port)
Returns true if port is a valid port, possibly supporting edge connection.

Specified by:
isPort in interface GraphModel
Returns:
true if port is a valid port.

cloneCells

public Map cloneCells(Object[] cells)
Returns a map of (cell, clone)-pairs for all cells and their children. Special care is taken to replace the anchor references between ports. (Iterative implementation.)

Specified by:
cloneCells in interface GraphModel

getParent

public Object getParent(Object child)
Returns the parent of child in the model. child must be a node previously obtained from this data source. This returns null if child is a root in the model.

Specified by:
getParent in interface GraphModel
Parameters:
child - a node in the graph, obtained from this data source
Returns:
the parent of child

getIndexOfChild

public int getIndexOfChild(Object parent,
                           Object child)
Returns the index of child in parent. If either the parent or child is null, returns -1.

Specified by:
getIndexOfChild in interface GraphModel
Parameters:
parent - a note in the tree, obtained from this data source
child - the node we are interested in
Returns:
the index of the child in the parent, or -1 if either the parent or the child is null

getChild

public Object getChild(Object parent,
                       int index)
Returns the child of parent at index index in the parent's child array. parent must be a node previously obtained from this data source. This should not return null if index is a valid index for parent (that is index >= 0 && index < getChildCount(parent)).

Specified by:
getChild in interface GraphModel
Parameters:
parent - a node in the tree, obtained from this data source
Returns:
the child of parent at index index

getChildCount

public int getChildCount(Object parent)
Returns the number of children of parent. Returns 0 if the node is a leaf or if it has no children. parent must be a node previously obtained from this data source.

Specified by:
getChildCount in interface GraphModel
Parameters:
parent - a node in the tree, obtained from this data source
Returns:
the number of children of the node parent

isLeaf

public boolean isLeaf(Object node)
Returns whether the specified node is a leaf node. The way the test is performed depends on the.

Specified by:
isLeaf in interface GraphModel
Parameters:
node - the node to check
Returns:
true if the node is a leaf node

insert

public void insert(Object[] roots,
                   Map attributes,
                   ConnectionSet cs,
                   ParentMap pm,
                   UndoableEdit[] edits)
Inserts the roots and connections into the model. Notifies the model- and undo listeners of the change. The passed-in edits are executed if they implement the GraphModelEvent.ExecutableGraphChange interface in ascending array-order, after execution of the model change. (Note: The external order is important in a special case: After insertion on a partial view, ie. one that does not display all cells of the model, the cell is made visible after it is inserted into the model. This requires the inserting view to be able to add the cell to the visible set before it is inserted into the model.) Note: The passed-in propertyMap may contains PortViews which must be turned into Points when stored in the model.

Specified by:
insert in interface GraphModel

remove

public void remove(Object[] roots)
Removes cells from the model. Notifies the model- and undo listeners of the change.

Specified by:
remove in interface GraphModel

edit

public void edit(Map attributes,
                 ConnectionSet cs,
                 ParentMap pm,
                 UndoableEdit[] edits)
Applies attributes and the connection changes to the model. The initial edits that triggered the call are considered to be part of this transaction. The passed-in edits are executed if they implement the GraphModelEvent.ExecutableGraphChange interface in ascending array-order, after execution of the model change. Notifies the model- and undo listeners of the change. Note: If only edits is non-null, the edits are directly passed to the UndoableEditListeners. Note: The passed-in propertyMap may contains PortViews which must be turned into Points when stored in the model.

Specified by:
edit in interface GraphModel

toBack

public void toBack(Object[] cells)
Sends cells to back.

Specified by:
toBack in interface GraphModel

toFront

public void toFront(Object[] cells)
Brings cells to front.

Specified by:
toFront in interface GraphModel

createLayerEdit

protected DefaultGraphModel.GraphModelLayerEdit createLayerEdit(Object[] cells,
                                                                int layer)

createInsertEdit

protected DefaultGraphModel.GraphModelEdit createInsertEdit(Object[] cells,
                                                            Map attributeMap,
                                                            ConnectionSet cs,
                                                            ParentMap pm,
                                                            UndoableEdit[] edits)
Returns an edit that represents an insert.


createRemoveEdit

protected DefaultGraphModel.GraphModelEdit createRemoveEdit(Object[] cells)
Returns an edit that represents a remove.


createCellEdit

protected DefaultGraphModel.GraphModelEdit createCellEdit(Map attributes,
                                                          ConnectionSet cs,
                                                          ParentMap pm,
                                                          UndoableEdit[] edits)
Returns an edit that represents a change.


createEdit

protected DefaultGraphModel.GraphModelEdit createEdit(Object[] inserted,
                                                      Object[] removed,
                                                      Map attributes,
                                                      ConnectionSet cs,
                                                      ParentMap pm)

handleInsert

protected Object[] handleInsert(Object[] cells)
Inserts cells into the model. Returns the cells that were inserted (including descendants).


handleRemove

protected Object[] handleRemove(Object[] cells)
Removes cells from the model. Returns the cells that were removed as roots.


handleParentMap

protected ParentMap handleParentMap(ParentMap parentMap)
Applies cells to the model. Returns a parent map that may be used to undo this change.


handleAttributes

protected Map handleAttributes(Map attributes)
Applies attributes to the cells specified as keys. Returns the attributes to undo the change.


handleConnectionSet

protected ConnectionSet handleConnectionSet(ConnectionSet cs)
Applies connectionSet to the model. Returns a connection set that may be used to undo this change.


handleConnection

protected void handleConnection(ConnectionSet.Connection c)
Inserts the specified connection into the model.


connect

protected void connect(Object edge,
                       Object port,
                       boolean isSource,
                       boolean remove)
Connects or disconnects the edge and port in this model based on remove. Subclassers should override this to update connectivity datastructures.


addGraphModelListener

public void addGraphModelListener(GraphModelListener l)
Adds a listener for the GraphModelEvent posted after the graph changes.

Specified by:
addGraphModelListener in interface GraphModel
Parameters:
l - the listener to add
See Also:
removeGraphModelListener(org.jgraph.event.GraphModelListener)

removeGraphModelListener

public void removeGraphModelListener(GraphModelListener l)
Removes a listener previously added with addGraphModelListener().

Specified by:
removeGraphModelListener in interface GraphModel
Parameters:
l - the listener to remove
See Also:
addGraphModelListener(org.jgraph.event.GraphModelListener)

fireGraphChanged

protected void fireGraphChanged(Object source,
                                GraphModelEvent.GraphModelChange edit)

getGraphModelListeners

public GraphModelListener[] getGraphModelListeners()
Return an array of all GraphModelListeners that were added to this model.


getSourceVertex

public static Object getSourceVertex(GraphModel model,
                                     Object edge)
Returns the source vertex of the edge by calling getParent on getSource on the specified model.


getTargetVertex

public static Object getTargetVertex(GraphModel model,
                                     Object edge)
Returns the target vertex of the edge by calling getParent on getTarget on the specified model.


getRoots

public static Object[] getRoots(GraphModel model)
Returns the roots of the specified model as an array. This implementation only uses the GraphModel interface.


getEdges

public static Set getEdges(GraphModel model,
                           Object[] cells)
Return the set of edges that are connected to the specified cells. The array is flattened and then all attached edges that are not part of the cells array are returned.


getDescendants

public static Set getDescendants(GraphModel model,
                                 Object[] cells)
Flattens the given array of root cells by adding the roots and their descandants. The resulting set contains all cells, which means it contains branches and leafs. Note: This is an iterative implementation. No recursion used. DEPRECATED: Use getDescendantList


getDescendantList

public static List getDescendantList(GraphModel model,
                                     Object[] cells)