Package org.jgraph.graph
Class DefaultGraphModel
java.lang.Object
javax.swing.undo.UndoableEditSupport
org.jgraph.graph.DefaultGraphModel
- All Implemented Interfaces:
Serializable,GraphModel
The default implementation of a graph model.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classclassAn implementation of GraphModelChange that can be added to the model event.classAn implementation of GraphViewChange. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanIndicates whether isLeaf is based on a node's allowsChildren value.protected AttributeMapThe model's own attributes as a map.protected IteratorDefault instance of an empty iterator.protected EventListenerListThe list of listeners that listen to the model.protected booleanWhether or not to remove group cells from the model when all of their children are removedprotected ListSet that contains all root cells of this model.protected SetStores nested transaction added cellsprotected MapStores nested transaction transport attribute mapsprotected ConnectionSetStores nested transaction connection setsprotected ParentMapStores nested transaction parent mapsprotected SetStores nested transaction removed cellsprotected intCounter for the depth of nested transactions.Fields inherited from class javax.swing.undo.UndoableEditSupport
compoundEdit, listeners, realSource -
Constructor Summary
ConstructorsConstructorDescriptionConstructs 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
Modifier and TypeMethodDescriptionbooleanacceptsSource(Object edge, Object port) Returnstrueifportis a valid source foredge.booleanacceptsTarget(Object edge, Object port) Returnstrueifportis a valid target foredge.voidAdds a listener for the GraphModelEvent posted after the graph changes.voidIndicates the start of one level of an executable changevoidcellsChanged(Object[] cells) Invoke this method after you've changed how the cells are to be represented in the graph.protected ObjectCreates 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.cloneCells(Object[] cells) Returns a map of (cell, clone)-pairs for allcells.protected ObjectcloneUserObject(Object userObject) Clones the user object.protected voidConnects or disconnects the edge and port in this model based onremove.booleanReturnstrueifnodeor 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.Returns an iterator of the edges connected toport.voidedit(Object[] inserted, Object[] removed, Map attributes, ConnectionSet cs, ParentMap pm, UndoableEdit[] edits) Appliesattributesand the connection changes to the model.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.voidIndicates the end of the current level of an executable changevoidexecute(ExecutableChange change) Executes the specified executable change on this graph modelprotected voidfireGraphChanged(Object source, GraphModelEvent.GraphModelChange edit) static Object[]getAll(GraphModel model) Returns all cells of the model in an array.Returns the graph model's attribute.getAttributes(Object node) Returns aMapthat represents the attributes for the specified cell.Returns the child of parent at index index in the parent's child array.intgetChildCount(Object parent) Returns the number of children of parent .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 tocellsor 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.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 ofrootin the model.static ObjectgetOpposite(GraphModel model, Object edge, Object cell) static Object[]getOutgoingEdges(GraphModel model, Object cell) Returns the outgoing edges for cell.Returns the parent of child in the model.getRootAt(int index) Returns the root at index index in the model.intReturns the number of roots in the model.getRoots()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 incellsby checking if their parent isnull.static CollectiongetRootsAsCollection(GraphModel model) Returns the roots of the specified model as a collection.Returns the source ofedge.static ObjectgetSourceVertex(GraphModel model, Object edge) Returns the source vertex of the edge by calling getParent on getSource on the specified model.Returns the target ofedge.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) intstatic ObjectgetUserObject(Object cell) Deprecated.Returns the user object for the specified cell.protected MaphandleAttributes(Map attributes) Appliesattributesto the cells specified as keys.protected voidhandleConnection(ConnectionSet.Connection c, boolean establish) Inserts the specified connection into the model.protected ConnectionSetAppliesconnectionSetto the model.protected Object[]handleInsert(Object[] cells) Insertscellsinto the model.protected ParentMaphandleParentMap(ParentMap parentMap) Appliescellsto the model.protected Object[]handleRemove(Object[] cells) Removescellsfrom 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 therootsand connections into the model.booleanReturnstrueifedgeis a valid edge.static booleanisGroup(GraphModel model, Object cell) Checks whether the cell has at least one child which is not a port.booleanReturns whether the specified node is a leaf node.booleanReturnstrueifportis a valid port, possibly supporting edge connection.booleanstatic booleanisVertex(GraphModel model, Object vertex) Returnstrueifvertexis a valid vertex.static Object[]order(GraphModel model, Object[] cells) Orders cells so that they reflect the model order.voidRemovescellsfrom the model.voidRemoves a listener previously added with addGraphModelListener() .protected voidSets the parent of the specified cell.voidsetRemoveEmptyGroups(boolean removeEmptyGroups) static voidsetSourcePort(GraphModel model, Object edge, Object port) Helper methods that connects the source ofedgetoportinmodel.static voidsetTargetPort(GraphModel model, Object edge, Object port) Helper methods that connects the source ofedgetoportinmodel.voidSendscellsto back.voidBringscellsto front.protected voidupdateTransaction(Object[] inserted, Object[] removed, Map attributes, ConnectionSet cs, ParentMap pm) Updates the current state of the various transaction datavalueForCellChanged(Object cell, Object newValue) Applies the new value to the specified cell.Methods inherited from class javax.swing.undo.UndoableEditSupport
_postEdit, addUndoableEditListener, createCompoundEdit, getUndoableEditListeners, postEdit, removeUndoableEditListener, toStringMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.jgraph.graph.GraphModel
addUndoableEditListener, removeUndoableEditListener
-
Field Details
-
listenerList
The list of listeners that listen to the model. -
emptyIterator
Default instance of an empty iterator. -
roots
Set that contains all root cells of this model. -
asksAllowsChildren
protected boolean asksAllowsChildrenIndicates whether isLeaf is based on a node's allowsChildren value. -
removeEmptyGroups
protected boolean removeEmptyGroupsWhether or not to remove group cells from the model when all of their children are removed -
attributes
The model's own attributes as a map. Defaults to an empty Hashtable. -
updateLevel
protected transient int updateLevelCounter 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. -
transAddedCells
Stores nested transaction added cells -
transRemovedCells
Stores nested transaction removed cells -
transEditAttrs
Stores nested transaction transport attribute maps -
transEditCS
Stores nested transaction connection sets -
transEditPM
Stores nested transaction parent maps
-
-
Constructor Details
-
DefaultGraphModel
public DefaultGraphModel()Constructs a model that is not an attribute store. -
DefaultGraphModel
Constructs a model that is not an attribute store. -
DefaultGraphModel
Constructs a model using the specified information to construct the cells, attributes and connection data.
-
-
Method Details
-
getRoots
-
getRootCount
public int getRootCount()Returns the number of roots in the model. Returns 0 if the model is empty.- Specified by:
getRootCountin interfaceGraphModel- Returns:
- the number of roots in the model
-
getRootAt
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:
getRootAtin interfaceGraphModel- Returns:
- the root of at index index
-
getIndexOfRoot
Returns the index ofrootin the model. If root isnull, returns -1.- Specified by:
getIndexOfRootin interfaceGraphModel- 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
-
contains
Returnstrueifnodeor one of its ancestors is in the model.- Specified by:
containsin interfaceGraphModel- Returns:
trueifnodeis in the model
-
getAttributes
Returns aMapthat represents the attributes for the specified cell. This attributes have precedence over each view's attributes, regardless of isAttributeStore.- Specified by:
getAttributesin interfaceGraphModel- Returns:
- attributes of
nodeas aMap
-
getValue
Description copied from interface:GraphModelReturns the user object for the specified cell.- Specified by:
getValuein interfaceGraphModel- 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.
-
getAttributes
Returns the graph model's attribute. Shortcut togetAttributes(null).- Returns:
- attributes of
nodeas aMap
-
getSource
Returns the source ofedge. edge must be an object previously obtained from this data source.- Specified by:
getSourcein interfaceGraphModel- Returns:
Objectthat represents the source of edge
-
getTarget
Returns the target ofedge. edge must be an object previously obtained from this data source.- Specified by:
getTargetin interfaceGraphModel- Returns:
Objectthat represents the target of edge
-
acceptsSource
Returnstrueifportis a valid source foredge. edge and port must be objects previously obtained from this data source.- Specified by:
acceptsSourcein interfaceGraphModel- Returns:
trueifportis a valid source foredge.
-
acceptsTarget
Returnstrueifportis a valid target foredge. edge and port must be objects previously obtained from this data source.- Specified by:
acceptsTargetin interfaceGraphModel- Returns:
trueifportis a valid target foredge.
-
edges
Returns an iterator of the edges connected toport. port must be a object previously obtained from this data source. This method never returns null.- Specified by:
edgesin interfaceGraphModel- Parameters:
port- a port in the graph, obtained from this data source- Returns:
Iteratorthat represents the connected edges
-
isEdge
Returnstrueifedgeis a valid edge.- Specified by:
isEdgein interfaceGraphModel- Returns:
trueifedgeis a valid edge.
-
isPort
Returnstrueifportis a valid port, possibly supporting edge connection.- Specified by:
isPortin interfaceGraphModel- Returns:
trueifportis a valid port.
-
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. -
cloneCells
Returns a map of (cell, clone)-pairs for allcells. 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 incells. Use JGraph.getDescendants to get a complete list of all children.- Specified by:
cloneCellsin interfaceGraphModel
-
setParent
Sets the parent of the specified cell. -
cloneCell
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. -
cloneUserObject
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 returnsobject. -
getParent
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:
getParentin interfaceGraphModel- Parameters:
child- a node in the graph, obtained from this data source- Returns:
- the parent of child
-
getIndexOfChild
Returns the index of child in parent. If either the parent or child isnull, returns -1.- Specified by:
getIndexOfChildin interfaceGraphModel- Parameters:
parent- a note in the tree, obtained from this data sourcechild- 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
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:
getChildin interfaceGraphModel- Parameters:
parent- a node in the tree, obtained from this data source- Returns:
- the child of parent at index index
-
getChildCount
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:
getChildCountin interfaceGraphModel- Parameters:
parent- a node in the tree, obtained from this data source- Returns:
- the number of children of the node parent
-
isLeaf
Returns whether the specified node is a leaf node. The way the test is performed depends on the.- Specified by:
isLeafin interfaceGraphModel- 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 therootsand connections into the model. Notifies the model- and undo listeners of the change. The passed-in edits are executed if they implement theGraphModelEvent.ExecutableGraphChangeinterface in ascending array-order, after execution of the model change. Note: The passed-in propertyMap may containPortViews which must be turned intoPoints when stored in the model.- Specified by:
insertin interfaceGraphModel
-
remove
Removescellsfrom the model. Notifies the model- and undo listeners of the change.- Specified by:
removein interfaceGraphModel
-
edit
Shortcut to the new edit method which allows inserts and removes to go along with an edit.- Specified by:
editin interfaceGraphModel
-
edit
public void edit(Object[] inserted, Object[] removed, Map attributes, ConnectionSet cs, ParentMap pm, UndoableEdit[] edits) Appliesattributesand the connection changes to the model. The initialeditsthat triggered the call are considered to be part of this transaction. The passed-in edits are executed if they implement theGraphModelEvent.ExecutableGraphChangeinterface in ascending array-order, after execution of the model change. Notifies the model- and undo listeners of the change. Note: If onlyeditsis 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. -
execute
Description copied from interface:GraphModelExecutes the specified executable change on this graph model- Specified by:
executein interfaceGraphModel- Parameters:
change- the change to be executed
-
getUpdateLevel
public int getUpdateLevel()- Overrides:
getUpdateLevelin classUndoableEditSupport
-
beginUpdate
public void beginUpdate()Description copied from interface:GraphModelIndicates the start of one level of an executable change- Specified by:
beginUpdatein interfaceGraphModel- Overrides:
beginUpdatein classUndoableEditSupport
-
endUpdate
public void endUpdate()Description copied from interface:GraphModelIndicates the end of the current level of an executable change- Specified by:
endUpdatein interfaceGraphModel- Overrides:
endUpdatein classUndoableEditSupport
-
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 transactionremoved- removed cells to be removed from the transactionattributes- nested attribute maps to apply to the transactioncs- connection sets to add to the transactionpm- parent maps to add to the transaction
-
toBack
Sendscellsto back.- Specified by:
toBackin interfaceGraphModel
-
toFront
Bringscellsto front.- Specified by:
toFrontin interfaceGraphModel
-
createLayerEdit
-
createRemoveEdit
Returns an edit that represents a remove. -
createEdit
protected DefaultGraphModel.GraphModelEdit createEdit(Object[] inserted, Object[] removed, Map attributes, ConnectionSet cs, ParentMap pm, UndoableEdit[] edits) -
handleInsert
Insertscellsinto the model. Returns the cells that were inserted (including descendants). -
handleRemove
Removescellsfrom the model. Returns the cells that were removed as roots. -
handleParentMap
Appliescellsto the model. Returns a parent map that may be used to undo this change. -
handleAttributes
Appliesattributesto the cells specified as keys. Returns theattributesto undo the change. -
valueForCellChanged
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.- Specified by:
valueForCellChangedin interfaceGraphModel- Parameters:
cell-newValue-- Returns:
- the old value for the cell, if any
-
handleConnectionSet
AppliesconnectionSetto the model. Returns a connection set that may be used to undo this change. -
handleConnection
Inserts the specified connection into the model. -
connect
Connects or disconnects the edge and port in this model based onremove. Subclassers should override this to update connectivity datastructures. -
addGraphModelListener
Adds a listener for the GraphModelEvent posted after the graph changes.- Specified by:
addGraphModelListenerin interfaceGraphModel- Parameters:
l- the listener to add- See Also:
-
removeGraphModelListener
Removes a listener previously added with addGraphModelListener() .- Specified by:
removeGraphModelListenerin interfaceGraphModel- Parameters:
l- the listener to remove- See Also:
-
cellsChanged
Invoke this method after you've changed how the cells are to be represented in the graph. -
fireGraphChanged
-
getGraphModelListeners
Return an array of all GraphModelListeners that were added to this model. -
cloneCell
Returns a deep clone of the specified cell, including all children. -
cloneCell
Returns a deep clone of the specified cells, including all children. -
setSourcePort
Helper methods that connects the source ofedgetoportinmodel. -
setTargetPort
Helper methods that connects the source ofedgetoportinmodel. -
getSourceVertex
Returns the source vertex of the edge by calling getParent on getSource on the specified model. -
getTargetVertex
Returns the target vertex of the edge by calling getParent on getTarget on the specified model. -
getUserObject
Deprecated.UseGraphModel.getValue(Object)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.
-
isGroup
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
-
getAll
Returns all cells of the model in an array.- Returns:
- Returns all cells in the model including all descandants.
- See Also:
-
getRoots
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 aDefaultGraphModelthe performance can be improved to linear time. -
getRootsAsCollection
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 aDefaultGraphModelthe performance can be improved to linear time. -
getRoots
Returns the roots incellsby checking if their parent isnull. This implementation only uses the GraphModel interface. This method never returns null. -
getTopmostCells
- Returns:
- Returns the roots of cells, eg. an array that contains no cell having an ancestor in cells.
-
hasAncestorIn
Returns true if the specified child has an ancestor in parents. -
getDescendants
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. -
order
Orders cells so that they reflect the model order. -
getEdges
Returns the set of all connected edges tocellsor their descendants. The passed-in cells are never returned as part of the result set. This can be used on vertices, edges and ports. -
getOpposite
- Returns:
- Returns the opposite port or vertex in
edge.
-
containsEdgeBetween
Returns true if the given vertices are conntected by a single edge in this document. -
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 thencell1must 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. -
getOutgoingEdges
Returns the outgoing edges for cell. Cell should be a port or a vertex. -
getIncomingEdges
Returns the incoming edges for cell. Cell should be a port or a vertex. -
getEdges
Returns the incoming or outgoing edges for cell. Cell should be a port or a vertex. -
isVertex
Returnstrueifvertexis a valid vertex.- Returns:
trueifvertexis a valid vertex.
-
isRemoveEmptyGroups
public boolean isRemoveEmptyGroups()- Returns:
- the removeEmptyGroups
-
setRemoveEmptyGroups
public void setRemoveEmptyGroups(boolean removeEmptyGroups) - Parameters:
removeEmptyGroups- the removeEmptyGroups to set
-
GraphModel.getValue(Object)instead.