org.jgraph.graph

Class DefaultGraphModel

public class DefaultGraphModel extends UndoableEditSupport implements Serializable, GraphModel

The default implementation of a graph model.
Nested Class Summary
static classDefaultGraphModel.EmptyIterator
classDefaultGraphModel.GraphModelEdit
An implementation of GraphModelChange that can be added to the model event.
classDefaultGraphModel.GraphModelLayerEdit
An implementation of GraphViewChange.
Field Summary
protected booleanasksAllowsChildren
Indicates whether isLeaf is based on a node's allowsChildren value.
protected AttributeMapattributes
The model's own attributes as a map.
protected IteratoremptyIterator
Default instance of an empty iterator.
protected EventListenerListlistenerList
The list of listeners that listen to the model.
protected booleanremoveEmptyGroups
Whether or not to remove group cells from the model when all of their children are removed
protected Listroots
Set that contains all root cells of this model.
protected SettransAddedCells
Stores nested transaction added cells
protected MaptransEditAttrs
Stores nested transaction transport attribute maps
protected ConnectionSettransEditCS
Stores nested transaction connection sets
protected ParentMaptransEditPM
Stores nested transaction parent maps
protected SettransRemovedCells
Stores nested transaction removed cells
protected intupdateLevel
Counter for the depth of nested transactions.
Constructor Summary
DefaultGraphModel()
Constructs a model that is not an attribute store.
DefaultGraphModel(List roots, AttributeMap attributes)
Constructs a model that is not an attribute store.
DefaultGraphModel(List roots, AttributeMap attributes, ConnectionSet cs)
Constructs a model using the specified information to construct the cells, attributes and connection data.
Method Summary
booleanacceptsSource(Object edge, Object port)
Returns true if port is a valid source for edge.
booleanacceptsTarget(Object edge, Object port)
Returns true if port is a valid target for edge.
voidaddGraphModelListener(GraphModelListener l)
Adds a listener for the GraphModelEvent posted after the graph changes.
voidbeginUpdate()
voidcellsChanged(Object[] cells)
Invoke this method after you've changed how the cells are to be represented in the graph.
protected ObjectcloneCell(Object cellObj)
Creates a shallow copy of the cell including a copy of the user object.
static ObjectcloneCell(GraphModel model, Object cell)
Returns a deep clone of the specified cell, including all children.
static Object[]cloneCell(GraphModel model, Object[] cells)
Returns a deep clone of the specified cells, including all children.
MapcloneCells(Object[] cells)
Returns a map of (cell, clone)-pairs for all cells.
protected ObjectcloneUserObject(Object userObject)
Clones the user object.
protected voidconnect(Object edge, Object port, boolean isSource, boolean insert)
Connects or disconnects the edge and port in this model based on remove.
booleancontains(Object node)
Returns true if node or one of its ancestors is in the model.
static booleancontainsEdgeBetween(GraphModel model, Object v1, Object v2)
Returns true if the given vertices are conntected by a single edge in this document.
protected DefaultGraphModel.GraphModelEditcreateEdit(Object[] inserted, Object[] removed, Map attributes, ConnectionSet cs, ParentMap pm, UndoableEdit[] edits)
protected DefaultGraphModel.GraphModelLayerEditcreateLayerEdit(Object[] cells, int layer)
protected DefaultGraphModel.GraphModelEditcreateRemoveEdit(Object[] cells)
Returns an edit that represents a remove.
Iteratoredges(Object port)
Returns an iterator of the edges connected to port.
voidedit(Map attributes, ConnectionSet cs, ParentMap pm, UndoableEdit[] edits)
Shortcut to the new edit method which allows inserts and removes to go along with an edit.
voidedit(Object[] inserted, Object[] removed, Map attributes, ConnectionSet cs, ParentMap pm, UndoableEdit[] edits)
Applies attributes and the connection changes to the model.
voidendUpdate()
voidexecute(ExecutableChange change)
protected voidfireGraphChanged(Object source, GraphModelEvent.GraphModelChange edit)
static Object[]getAll(GraphModel model)
Returns all cells of the model in an array.
AttributeMapgetAttributes(Object node)
Returns a Map that represents the attributes for the specified cell.
MapgetAttributes()
Returns the graph model's attribute.
ObjectgetChild(Object parent, int index)
Returns the child of parent at index index in the parent's child array.
intgetChildCount(Object parent)
Returns the number of children of parent .
ConnectionSetgetConnectionSet()
A shortcut method to create a connection set that represents the connections in this model.
static ListgetDescendants(GraphModel model, Object[] cells)
Flattens the given array of root cells by adding the roots and their descandants.
static SetgetEdges(GraphModel model, Object[] cells)
Returns the set of all connected edges to cells or their descendants.
static Object[]getEdges(GraphModel model, Object cell, boolean incoming)
Returns the incoming or outgoing edges for cell.
static Object[]getEdgesBetween(GraphModel model, Object cell1, Object cell2, boolean directed)
Returns the edges between two specified ports or two specified vertices.
GraphModelListener[]getGraphModelListeners()
Return an array of all GraphModelListeners that were added to this model.
static Object[]getIncomingEdges(GraphModel model, Object cell)
Returns the incoming edges for cell.
intgetIndexOfChild(Object parent, Object child)
Returns the index of child in parent.
intgetIndexOfRoot(Object root)
Returns the index of root in the model.
static ObjectgetOpposite(GraphModel model, Object edge, Object cell)
static Object[]getOutgoingEdges(GraphModel model, Object cell)
Returns the outgoing edges for cell.
ObjectgetParent(Object child)
Returns the parent of child in the model.
ObjectgetRootAt(int index)
Returns the root at index index in the model.
intgetRootCount()
Returns the number of roots in the model.
ListgetRoots()
static Object[]getRoots(GraphModel model)
Returns the roots of the specified model as an array.
static Object[]getRoots(GraphModel model, Object[] cells)
Returns the roots in cells by checking if their parent is null.
static CollectiongetRootsAsCollection(GraphModel model)
Returns the roots of the specified model as a collection.
ObjectgetSource(Object edge)
Returns the source of edge.
static ObjectgetSourceVertex(GraphModel model, Object edge)
Returns the source vertex of the edge by calling getParent on getSource on the specified model.
ObjectgetTarget(Object edge)
Returns the target of edge.
static ObjectgetTargetVertex(GraphModel model, Object edge)
Returns the target vertex of the edge by calling getParent on getTarget on the specified model.
static Object[]getTopmostCells(GraphModel model, Object[] cells)
intgetUpdateLevel()
static ObjectgetUserObject(Object cell)
ObjectgetValue(Object cell)
protected MaphandleAttributes(Map attributes)
Applies attributes to the cells specified as keys.
protected voidhandleConnection(ConnectionSet.Connection c, boolean establish)
Inserts the specified connection into the model.
protected ConnectionSethandleConnectionSet(ConnectionSet cs)
Applies connectionSet to the model.
protected Object[]handleInsert(Object[] cells)
Inserts cells into the model.
protected ParentMaphandleParentMap(ParentMap parentMap)
Applies cells to the model.
protected Object[]handleRemove(Object[] cells)
Removes cells from the model.
static booleanhasAncestorIn(GraphModel model, Set parents, Object child)
Returns true if the specified child has an ancestor in parents.
voidinsert(Object[] roots, Map attributes, ConnectionSet cs, ParentMap pm, UndoableEdit[] edits)
Inserts the roots and connections into the model.
booleanisEdge(Object edge)
Returns true if edge is a valid edge.
static booleanisGroup(GraphModel model, Object cell)
Checks whether the cell has at least one child which is not a port.
booleanisLeaf(Object node)
Returns whether the specified node is a leaf node.
booleanisPort(Object port)
Returns true if port is a valid port, possibly supporting edge connection.
booleanisRemoveEmptyGroups()
static booleanisVertex(GraphModel model, Object vertex)
Returns true if vertex is a valid vertex.
static Object[]order(GraphModel model, Object[] cells)
Orders cells so that they reflect the model order.
voidremove(Object[] roots)
Removes cells from the model.
voidremoveGraphModelListener(GraphModelListener l)
Removes a listener previously added with addGraphModelListener() .
protected voidsetParent(Object child, Object parent)
Sets the parent of the specified cell.
voidsetRemoveEmptyGroups(boolean removeEmptyGroups)
static voidsetSourcePort(GraphModel model, Object edge, Object port)
Helper methods that connects the source of edge to port in model.
static voidsetTargetPort(GraphModel model, Object edge, Object port)
Helper methods that connects the source of edge to port in model.
voidtoBack(Object[] cells)
Sends cells to back.
voidtoFront(Object[] cells)
Brings cells to front.
protected voidupdateTransaction(Object[] inserted, Object[] removed, Map attributes, ConnectionSet cs, ParentMap pm)
Updates the current state of the various transaction data
ObjectvalueForCellChanged(Object cell, Object newValue)
Applies the new value to the specified cell.

Field Detail

asksAllowsChildren

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

attributes

protected AttributeMap attributes
The model's own attributes as a map. Defaults to an empty Hashtable.

emptyIterator

protected transient Iterator emptyIterator
Default instance of an empty iterator.

listenerList

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

removeEmptyGroups

protected boolean removeEmptyGroups
Whether or not to remove group cells from the model when all of their children are removed

roots

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

transAddedCells

protected transient Set transAddedCells
Stores nested transaction added cells

transEditAttrs

protected transient Map transEditAttrs
Stores nested transaction transport attribute maps

transEditCS

protected transient ConnectionSet transEditCS
Stores nested transaction connection sets

transEditPM

protected transient ParentMap transEditPM
Stores nested transaction parent maps

transRemovedCells

protected transient Set transRemovedCells
Stores nested transaction removed cells

updateLevel

protected transient int updateLevel
Counter for the depth of nested transactions. Each call to beginUpdate increments this counter and each call to endUpdate decrements it. When the counter reaches 0, the transaction is closed and applied to the model.

Constructor Detail

DefaultGraphModel

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

DefaultGraphModel

public DefaultGraphModel(List roots, AttributeMap attributes)
Constructs a model that is not an attribute store.

DefaultGraphModel

public DefaultGraphModel(List roots, AttributeMap attributes, ConnectionSet cs)
Constructs a model using the specified information to construct the cells, attributes and connection data.

Method Detail

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.

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.

Returns: true if port is a valid target for edge.

addGraphModelListener

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

Parameters: l the listener to add

See Also: DefaultGraphModel

beginUpdate

public void beginUpdate()

cellsChanged

public void cellsChanged(Object[] cells)
Invoke this method after you've changed how the cells are to be represented in the graph.

cloneCell

protected Object cloneCell(Object cellObj)
Creates a shallow copy of the cell including a copy of the user object. Subclassers can override the cloneUserObject to provide a custom user object cloning mechanism.

cloneCell

public static Object cloneCell(GraphModel model, Object cell)
Returns a deep clone of the specified cell, including all children.

cloneCell

public static Object[] cloneCell(GraphModel model, Object[] cells)
Returns a deep clone of the specified cells, including all children.

cloneCells

public Map cloneCells(Object[] cells)
Returns a map of (cell, clone)-pairs for all cells. In the new array, all references are replaced with references to the cloned cells (ie parent or anchor). This method does only include children which are in cells. Use JGraph.getDescendants to get a complete list of all children.

cloneUserObject

protected Object cloneUserObject(Object userObject)
Clones the user object. Helper method that is invoked from cloneCells. You must use cloneCells (or cloneCell for single cells) to get a deep copy of a clone. Subclassers must override this and valueForCellChanged to implement custom user objects. This implementation returns object.

connect

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

contains

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

Returns: true if node is in the model

containsEdgeBetween

public static boolean containsEdgeBetween(GraphModel model, Object v1, Object v2)
Returns true if the given vertices are conntected by a single edge in this document.

createEdit

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

createLayerEdit

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

createRemoveEdit

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

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.

Parameters: port a port in the graph, obtained from this data source

Returns: Iterator that represents the connected edges

edit

public void edit(Map attributes, ConnectionSet cs, ParentMap pm, UndoableEdit[] edits)
Shortcut to the new edit method which allows inserts and removes to go along with an edit.

edit

public void edit(Object[] inserted, Object[] removed, 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.

endUpdate

public void endUpdate()

execute

public void execute(ExecutableChange change)

fireGraphChanged

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

getAll

public static Object[] getAll(GraphModel model)
Returns all cells of the model in an array.

Returns: Returns all cells in the model including all descandants.

See Also: (GraphModel, Object[])

getAttributes

public AttributeMap 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.

Returns: attributes of node as a Map

getAttributes

public Map getAttributes()
Returns the graph model's attribute. Shortcut to getAttributes(null).

Returns: attributes of node as a Map

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 )).

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.

Parameters: parent a node in the tree, obtained from this data source

Returns: the number of children of the node parent

getConnectionSet

public ConnectionSet getConnectionSet()
A shortcut method to create a connection set that represents the connections in this model. Useful for encoding to avoid writing redundant connection data stored in the cells.

getDescendants

public static List 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.
Note: This returns a linked list, for frequent read operations you should turn this into an array, or at least an array list.

getEdges

public static Set getEdges(GraphModel model, Object[] cells)
Returns the set of all connected edges to cells or their descendants. The passed-in cells are never returned as part of the result set. This can be used on vertices, edges and ports.

getEdges

public static Object[] getEdges(GraphModel model, Object cell, boolean incoming)
Returns the incoming or outgoing edges for cell. Cell should be a port or a vertex.

getEdgesBetween

public static Object[] getEdgesBetween(GraphModel model, Object cell1, Object cell2, boolean directed)
Returns the edges between two specified ports or two specified vertices. If directed is true then cell1 must be the source of the returned edges. This method never returns null. If there are no edges between the specified cells, then an array of length 0 is returned.

getGraphModelListeners

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

getIncomingEdges

public static Object[] getIncomingEdges(GraphModel model, Object cell)
Returns the incoming edges for cell. Cell should be a port or a vertex.

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.

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

getIndexOfRoot

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

Parameters: root 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

getOpposite

public static Object getOpposite(GraphModel model, Object edge, Object cell)

Returns: Returns the opposite port or vertex in edge.

getOutgoingEdges

public static Object[] getOutgoingEdges(GraphModel model, Object cell)
Returns the outgoing edges for cell. Cell should be a port or a vertex.

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.

Parameters: child a node in the graph, obtained from this data source

Returns: the parent of child

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()).

Returns: the root of at index index

getRootCount

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

Returns: the number of roots in the model

getRoots

public List getRoots()

getRoots

public static Object[] getRoots(GraphModel model)
Returns the roots of the specified model as an array. This implementation uses the GraphModel interface in the general case, but if the model is a DefaultGraphModel the performance can be improved to linear time.

getRoots

public static Object[] getRoots(GraphModel model, Object[] cells)
Returns the roots in cells by checking if their parent is null. This implementation only uses the GraphModel interface. This method never returns null.

getRootsAsCollection

public static Collection getRootsAsCollection(GraphModel model)
Returns the roots of the specified model as a collection. This implementation uses the GraphModel interface in the general case, but if the model is a DefaultGraphModel the performance can be improved to linear time.

getSource

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

Returns: Object that represents the source of edge

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.

getTarget

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

Returns: Object that represents the target of edge

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.

getTopmostCells

public static Object[] getTopmostCells(GraphModel model, Object[] cells)

Returns: Returns the roots of cells, eg. an array that contains no cell having an ancestor in cells.

getUpdateLevel

public int getUpdateLevel()

getUserObject

public static Object getUserObject(Object cell)

Deprecated: Use getValue instead.

Returns: Returns the user object of the given cell. This implementation checks if the cell is a default mutable tree node and returns it's user object.

getValue

public Object getValue(Object cell)

Returns: Returns the user object of the given cell. This implementation checks if the cell is a default mutable tree node and returns it's user object.

handleAttributes

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

handleConnection

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

handleConnectionSet

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

handleInsert

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

handleParentMap

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

handleRemove

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

hasAncestorIn

public static boolean hasAncestorIn(GraphModel model, Set parents, Object child)
Returns true if the specified child has an ancestor in parents.

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 passed-in propertyMap may contain PortView s which must be turned into Point s when stored in the model.

isEdge

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

Returns: true if edge is a valid edge.

isGroup

public static boolean isGroup(GraphModel model, Object cell)
Checks whether the cell has at least one child which is not a port. This implementation operates on the model, not taking into account visibility of cells. It returns true for groups regardless of their folded state.

Parameters: cell the cell to check for being a group

Returns: Returns true if the cell contains at least one cell which is not a port

isLeaf

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

Parameters: node the node to check

Returns: true if the node is a leaf node

isPort

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

Returns: true if port is a valid port.

isRemoveEmptyGroups

public boolean isRemoveEmptyGroups()

Returns: the removeEmptyGroups

isVertex

public static boolean isVertex(GraphModel model, Object vertex)
Returns true if vertex is a valid vertex.

Returns: true if vertex is a valid vertex.

order

public static Object[] order(GraphModel model, Object[] cells)
Orders cells so that they reflect the model order.

remove

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

removeGraphModelListener

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

Parameters: l the listener to remove

See Also: DefaultGraphModel

setParent

protected void setParent(Object child, Object parent)
Sets the parent of the specified cell.

setRemoveEmptyGroups

public void setRemoveEmptyGroups(boolean removeEmptyGroups)

Parameters: removeEmptyGroups the removeEmptyGroups to set

setSourcePort

public static void setSourcePort(GraphModel model, Object edge, Object port)
Helper methods that connects the source of edge to port in model.

setTargetPort

public static void setTargetPort(GraphModel model, Object edge, Object port)
Helper methods that connects the source of edge to port in model.

toBack

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

toFront

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

updateTransaction

protected void updateTransaction(Object[] inserted, Object[] removed, Map attributes, ConnectionSet cs, ParentMap pm)
Updates the current state of the various transaction data

Parameters: inserted inserted cell to be added to the transaction removed removed cells to be removed from the transaction attributes nested attribute maps to apply to the transaction cs connection sets to add to the transaction pm parent maps to add to the transaction

valueForCellChanged

public Object valueForCellChanged(Object cell, Object newValue)
Applies the new value to the specified cell. Unfortunately for cloning the user object you must still override the attribute map and provide a custom cloneUserObject method. This is because the cloning of a cell is local to the cell, which in turn has a reference to its attribute map.

Parameters: cell newValue

Returns: the old value for the cell, if any

Copyright (C) 2001-2009 JGraph Ltd. All rights reserved.