easik.sketch.util
Class SketchAdapter

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

public class SketchAdapter
extends org.jgraph.graph.DefaultGraphModel

This class is the adapter which allows JGraphT graphs to be represented in JGraph I stole this from JGraphT, but modified it into something appropriate for JEase.

Version:
2006-08-01 Kevin Green
Author:
Rob Fletcher 2005, Kevin Green 2006
See Also:
Serialized Form

Nested Class Summary
static interface SketchAdapter.CellFactory
          Creates the JGraph cells that reflect the respective JGraphT elements.
 class SketchAdapter.DefaultCellFactory
          A simple default cell factory.
private  class SketchAdapter.JGraphListener
           Inner class listening to the GraphModel.
private  class SketchAdapter.JGraphTListener
          A listener on the underlying JGraphT graph.
 
Nested classes/interfaces inherited from class org.jgraph.graph.DefaultGraphModel
org.jgraph.graph.DefaultGraphModel.EmptyIterator, org.jgraph.graph.DefaultGraphModel.GraphModelEdit, org.jgraph.graph.DefaultGraphModel.GraphModelLayerEdit
 
Field Summary
private  Sketch _ourSketch
          The sketch
private  SketchAdapter.CellFactory m_cellFactory
          The cell factory
private  java.util.Map<org.jgraph.graph.GraphCell,org.jgrapht.graph.DefaultEdge> m_cellToEdge
          Mapping from cells to edges
private  java.util.Map<org.jgraph.graph.GraphCell,SketchVertex> m_cellToVertex
          Mapping from cells to vertices
private  org.jgraph.graph.AttributeMap m_defaultVertexAttributes
          The default attributes for a default vertex
private  java.util.Map<org.jgrapht.graph.DefaultEdge,org.jgraph.graph.GraphCell> m_edgeToCell
          Mapping from edges to cells
private  org.jgraph.graph.AttributeMap m_fletchedEdgeAttributes
          The default attributes for a fletched edge
private  java.util.Set<org.jgraph.graph.Edge> m_jEdgesBeingAdded
          JGraph edges being added
private  java.util.Set<org.jgraph.graph.Edge> m_jEdgesBeingRemoved
          JGraph edges being removed
private  java.util.Set<org.jgrapht.graph.DefaultEdge> m_jtEdgesBeingAdded
          JGraphT edges being added
private  java.util.Set<org.jgrapht.graph.DefaultEdge> m_jtEdgesBeingRemoved
          JGraphT edges being removed
private  org.jgrapht.graph.ListenableDirectedGraph<SketchVertex,org.jgrapht.graph.DefaultEdge> m_jtGraph
          The JGraphT graph
private  org.jgrapht.event.GraphListener m_jtGraphListener
          The JGraphT graph listener
private  java.util.Set<SketchVertex> m_jtVerticesBeingAdded
          JGraphT vertices being removed
private  java.util.Set<SketchVertex> m_jtVerticesBeingRemoved
          JGraphT vertices being removed
private  java.util.Set<org.jgraph.graph.DefaultGraphCell> m_jVerticesBeingAdded
          JGraph vertices being added
private  java.util.Set<org.jgraph.graph.DefaultGraphCell> m_jVerticesBeingRemoved
          JGraph vertices being removed
private  org.jgraph.graph.AttributeMap m_simpleEdgeAttributes
          The default attributes for a simple edge
private  java.util.Map<SketchVertex,org.jgraph.graph.GraphCell> m_vertexToCell
          Mapping from vertices to cells
private  org.jgraph.graph.AttributeMap m_virtualEdgeAttributes
          The default attributes for a virtual edge
private  org.jgraph.graph.AttributeMap m_virtualVertexAttributes
          The default attributes for a virtual vertex
 
Fields inherited from class org.jgraph.graph.DefaultGraphModel
asksAllowsChildren, attributes, emptyIterator, listenerList, roots
 
Fields inherited from class javax.swing.undo.UndoableEditSupport
compoundEdit, listeners, realSource, updateLevel
 
Constructor Summary
SketchAdapter(org.jgrapht.graph.ListenableDirectedGraph<SketchVertex,org.jgrapht.graph.DefaultEdge> jGraphTGraph, Sketch inSketch)
          Constructs a new JGraph model adapter for the specified JGraphT graph.
 
Method Summary
protected  boolean addJGraphEdge(org.jgraph.graph.Edge jEdge)
          Adds an edge corresponding to the specified JGraph edge to the underlying JGraphT graph.
protected  void addJGraphTEdge(org.jgrapht.graph.DefaultEdge jtEdge)
          Adds the specified JGraphT edge to be reflected by this graph model.
protected  void addJGraphTVertex(java.lang.Object jtVertex)
          Adds the specified JGraphT vertex to be reflected by this graph model.
protected  boolean addJGraphVertex(org.jgraph.graph.GraphCell jVertex)
          Add a vertex corresponding to this JGraph vertex to the JGraphT graph.
static org.jgraph.graph.AttributeMap createDefaultVertexAttributes()
          Create a default attribute mapping
static org.jgraph.graph.AttributeMap createFletchedEdgeAttributes()
          Create a default attribute mapping
static org.jgraph.graph.AttributeMap createSimpleEdgeAttributes()
          Create a default attribute mapping
static org.jgraph.graph.AttributeMap createVirtualEdgeAttributes()
          Create a default attribute mapping
static org.jgraph.graph.AttributeMap createVirtualVertexAttributes()
          Create a default attribute mapping
 void edit(java.util.Map attrs)
          Applies the specified attributes to the model, as in GraphModel.edit(java.util.Map, org.jgraph.graph.ConnectionSet, org.jgraph.graph.ParentMap, javax.swing.undo.UndoableEdit[]) method.
 org.jgraph.graph.DefaultEdge getEdgeCell(org.jgrapht.graph.DefaultEdge jGraphTEdge)
          Returns the JGraph edge cell that corresponds to the specified JGraphT edge.
 java.lang.Object getEdgeFromCell(org.jgraph.graph.DefaultEdge inCell)
          For getting the JGraphT edge from a JGraph cell
 org.jgraph.graph.DefaultGraphCell getVertexCell(java.lang.Object jGraphTVertex)
          Returns the JGraph vertex cell that corresponds to the specified JGraphT vertex.
 java.lang.Object getVertexFromCell(org.jgraph.graph.DefaultGraphCell inCell)
          For getting the JGraphT vertex from a JGraph cell
 org.jgraph.graph.DefaultPort getVertexPort(java.lang.Object jGraphTVertex)
          Returns the JGraph port cell that corresponds to the specified JGraphT vertex.
protected  boolean removeJGraphEdge(org.jgraph.graph.Edge jEdge)
          Remove the edge corresponding to this JGraph edge from the JGraphT graph.
protected  void removeJGraphTEdge(org.jgrapht.graph.DefaultEdge jtEdge)
          Removes the specified JGraphT edge from being reflected by this graph model.
protected  void removeJGraphTVertex(java.lang.Object jtVertex)
          Removes the specified JGraphT vertex from being reflected by this graph model.
protected  boolean removeJGraphVertex(org.jgraph.graph.GraphCell jVertex)
          Remove the vertex corresponding to this JGraph vertex from the JGraphT graph.
 void updateVertexPos(org.jgraph.graph.DefaultGraphCell inCell)
          This is method is called whenever a vertex is moved on screen.
 
Methods inherited from class org.jgraph.graph.DefaultGraphModel
acceptsSource, acceptsTarget, addGraphModelListener, cellsChanged, cloneCell, cloneCell, cloneCell, cloneCells, cloneUserObject, connect, contains, containsEdgeBetween, createEdit, createLayerEdit, createRemoveEdit, edges, edit, edit, fireGraphChanged, getAll, getAttributes, getAttributes, getChild, getChildCount, getConnectionSet, getDescendants, getEdges, getEdges, getEdgesBetween, getGraphModelListeners, getIncomingEdges, getIndexOfChild, getIndexOfRoot, getOpposite, getOutgoingEdges, getParent, getRootAt, getRootCount, getRoots, getRoots, getRoots, getSource, getSourceVertex, getTarget, getTargetVertex, getTopmostCells, getUserObject, getValue, handleAttributes, handleConnection, handleConnectionSet, handleInsert, handleParentMap, handleRemove, hasAncestorIn, insert, isEdge, isGroup, isLeaf, isPort, isVertex, order, remove, removeGraphModelListener, setParent, setSourcePort, setTargetPort, toBack, toFront, valueForCellChanged
 
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

m_jEdgesBeingAdded

private final java.util.Set<org.jgraph.graph.Edge> m_jEdgesBeingAdded
JGraph edges being added


m_jEdgesBeingRemoved

private final java.util.Set<org.jgraph.graph.Edge> m_jEdgesBeingRemoved
JGraph edges being removed


m_jVerticesBeingAdded

private final java.util.Set<org.jgraph.graph.DefaultGraphCell> m_jVerticesBeingAdded
JGraph vertices being added


m_jVerticesBeingRemoved

private final java.util.Set<org.jgraph.graph.DefaultGraphCell> m_jVerticesBeingRemoved
JGraph vertices being removed


m_jtEdgesBeingAdded

private final java.util.Set<org.jgrapht.graph.DefaultEdge> m_jtEdgesBeingAdded
JGraphT edges being added


m_jtEdgesBeingRemoved

private final java.util.Set<org.jgrapht.graph.DefaultEdge> m_jtEdgesBeingRemoved
JGraphT edges being removed


m_jtVerticesBeingAdded

private final java.util.Set<SketchVertex> m_jtVerticesBeingAdded
JGraphT vertices being removed


m_jtVerticesBeingRemoved

private final java.util.Set<SketchVertex> m_jtVerticesBeingRemoved
JGraphT vertices being removed


m_simpleEdgeAttributes

private final org.jgraph.graph.AttributeMap m_simpleEdgeAttributes
The default attributes for a simple edge


m_fletchedEdgeAttributes

private final org.jgraph.graph.AttributeMap m_fletchedEdgeAttributes
The default attributes for a fletched edge


m_virtualEdgeAttributes

private final org.jgraph.graph.AttributeMap m_virtualEdgeAttributes
The default attributes for a virtual edge


m_virtualVertexAttributes

private final org.jgraph.graph.AttributeMap m_virtualVertexAttributes
The default attributes for a virtual vertex


m_defaultVertexAttributes

private final org.jgraph.graph.AttributeMap m_defaultVertexAttributes
The default attributes for a default vertex


m_cellFactory

private SketchAdapter.CellFactory m_cellFactory
The cell factory


m_jtGraph

private org.jgrapht.graph.ListenableDirectedGraph<SketchVertex,org.jgrapht.graph.DefaultEdge> m_jtGraph
The JGraphT graph


m_jtGraphListener

private org.jgrapht.event.GraphListener m_jtGraphListener
The JGraphT graph listener


m_cellToEdge

private java.util.Map<org.jgraph.graph.GraphCell,org.jgrapht.graph.DefaultEdge> m_cellToEdge
Mapping from cells to edges


m_cellToVertex

private java.util.Map<org.jgraph.graph.GraphCell,SketchVertex> m_cellToVertex
Mapping from cells to vertices


m_edgeToCell

private java.util.Map<org.jgrapht.graph.DefaultEdge,org.jgraph.graph.GraphCell> m_edgeToCell
Mapping from edges to cells


m_vertexToCell

private java.util.Map<SketchVertex,org.jgraph.graph.GraphCell> m_vertexToCell
Mapping from vertices to cells


_ourSketch

private Sketch _ourSketch
The sketch

Constructor Detail

SketchAdapter

public SketchAdapter(org.jgrapht.graph.ListenableDirectedGraph<SketchVertex,org.jgrapht.graph.DefaultEdge> jGraphTGraph,
                     Sketch inSketch)
Constructs a new JGraph model adapter for the specified JGraphT graph.

Parameters:
jGraphTGraph - the JGraphT graph for which JGraph model adapter to be created.
inSketch - The sketch
Method Detail

getEdgeFromCell

public java.lang.Object getEdgeFromCell(org.jgraph.graph.DefaultEdge inCell)
For getting the JGraphT edge from a JGraph cell

Parameters:
inCell - The JGraph cell
Returns:
The JGraphT edge from a JGraph cell

getVertexFromCell

public java.lang.Object getVertexFromCell(org.jgraph.graph.DefaultGraphCell inCell)
For getting the JGraphT vertex from a JGraph cell

Parameters:
inCell - The JGraph cell
Returns:
The JGraphT vertex from a JGraph cell

updateVertexPos

public void updateVertexPos(org.jgraph.graph.DefaultGraphCell inCell)
This is method is called whenever a vertex is moved on screen. It updates the saved of the cell, keeping the jgrapht objects and jgraph cells synchronised

Parameters:
inCell -

getEdgeCell

public org.jgraph.graph.DefaultEdge getEdgeCell(org.jgrapht.graph.DefaultEdge jGraphTEdge)
Returns the JGraph edge cell that corresponds to the specified JGraphT edge. If no corresponding cell found, returns null.

Parameters:
jGraphTEdge - a JGraphT edge of the JGraphT graph.
Returns:
the JGraph edge cell that corresponds to the specified JGraphT edge, or null if no corresponding cell found.

getVertexCell

public org.jgraph.graph.DefaultGraphCell getVertexCell(java.lang.Object jGraphTVertex)
Returns the JGraph vertex cell that corresponds to the specified JGraphT vertex. If no corresponding cell found, returns null.

Parameters:
jGraphTVertex - a JGraphT vertex of the JGraphT graph.
Returns:
the JGraph vertex cell that corresponds to the specified JGraphT vertex, or null if no corresponding cell found.

getVertexPort

public org.jgraph.graph.DefaultPort getVertexPort(java.lang.Object jGraphTVertex)
Returns the JGraph port cell that corresponds to the specified JGraphT vertex. If no corresponding port found, returns null.

Parameters:
jGraphTVertex - a JGraphT vertex of the JGraphT graph.
Returns:
the JGraph port cell that corresponds to the specified JGraphT vertex, or null if no corresponding cell found.

createSimpleEdgeAttributes

public static org.jgraph.graph.AttributeMap createSimpleEdgeAttributes()
Create a default attribute mapping

Returns:
The attribute mapping

createVirtualEdgeAttributes

public static org.jgraph.graph.AttributeMap createVirtualEdgeAttributes()
Create a default attribute mapping

Returns:
The attribute mapping

createFletchedEdgeAttributes

public static org.jgraph.graph.AttributeMap createFletchedEdgeAttributes()
Create a default attribute mapping

Returns:
The attribute mapping

createDefaultVertexAttributes

public static org.jgraph.graph.AttributeMap createDefaultVertexAttributes()
Create a default attribute mapping

Returns:
The attribute mapping

createVirtualVertexAttributes

public static org.jgraph.graph.AttributeMap createVirtualVertexAttributes()
Create a default attribute mapping

Returns:
The attribute mapping

edit

public void edit(java.util.Map attrs)
Applies the specified attributes to the model, as in GraphModel.edit(java.util.Map, org.jgraph.graph.ConnectionSet, org.jgraph.graph.ParentMap, javax.swing.undo.UndoableEdit[]) method.

Parameters:
attrs - the attributes to be applied to the model.

addJGraphEdge

protected boolean addJGraphEdge(org.jgraph.graph.Edge jEdge)
Adds an edge corresponding to the specified JGraph edge to the underlying JGraphT graph. We try to find out to which vertices the edge is connected; if we find only one or none at all, we remove it again from the JGraph graph, because we cannot add such an edge to the JGraphT graph.

This method is to be called only for edges that have already been added to the JGraph graph.

Parameters:
jEdge - the JGraph Edge to be added.
Returns:
true if the edge was successfully added, false otherwise.

addJGraphTEdge

protected void addJGraphTEdge(org.jgrapht.graph.DefaultEdge jtEdge)
Adds the specified JGraphT edge to be reflected by this graph model. To be called only for edges that already exist in the JGraphT graph.

Parameters:
jtEdge - a JGraphT edge to be reflected by this graph model.

addJGraphTVertex

protected void addJGraphTVertex(java.lang.Object jtVertex)
Adds the specified JGraphT vertex to be reflected by this graph model. To be called only for edges that already exist in the JGraphT graph.

Parameters:
jtVertex - a JGraphT vertex to be reflected by this graph model.

addJGraphVertex

protected boolean addJGraphVertex(org.jgraph.graph.GraphCell jVertex)
Add a vertex corresponding to this JGraph vertex to the JGraphT graph. In JGraph, two vertices with the same user object are in principle allowed; in JGraphT, this would lead to duplicate vertices, which is not allowed. So if the vertex exists already, we remove it. This method is to be called only for vertices that have already been added to the JGraph graph.

Parameters:
jVertex - the JGraph vertex to be added.
Returns:
true if the vertex was successfully added, false otherwise.

removeJGraphEdge

protected boolean removeJGraphEdge(org.jgraph.graph.Edge jEdge)
Remove the edge corresponding to this JGraph edge from the JGraphT graph. To be called only for edges that have already been removed from the JGraph graph.

Parameters:
jEdge - the JGraph Edge to be removed. If it is not in m_cellsToEdges, it is silently ignored.
Returns:
true if the edge could successfully be removed, false otherwise.

removeJGraphTEdge

protected void removeJGraphTEdge(org.jgrapht.graph.DefaultEdge jtEdge)
Removes the specified JGraphT edge from being reflected by this graph model. To be called only for edges that have already been removed from the JGraphT graph.

Parameters:
jtEdge - a JGraphT edge to be removed from being reflected by this graph model.

removeJGraphTVertex

protected void removeJGraphTVertex(java.lang.Object jtVertex)
Removes the specified JGraphT vertex from being reflected by this graph model. To be called only for vertices that have already been removed from the JGraphT graph.

Parameters:
jtVertex - a JGraphT vertex to be removed from being reflected by this graph model.

removeJGraphVertex

protected boolean removeJGraphVertex(org.jgraph.graph.GraphCell jVertex)
Remove the vertex corresponding to this JGraph vertex from the JGraphT graph. If any edges are incident with this vertex, we remove them from both graphs first, because otherwise the JGraph graph would leave them intact and the JGraphT graph would throw them out. This method is to be called only for vertices that have already been removed from the JGraph graph.

Parameters:
jVertex - the JGraph vertex to be removed. If it is not in m_cellsToVertices, it is silently ignored.
Returns:
true if the vertex could successfully be removed, false otherwise.