org.grinvin.gred

Class GraphPanel

Implemented Interfaces:
ComponentListener, EventListener, AnnotationListener, EmbeddingListener, EventListener, GraphListener, Observer
Known Direct Subclasses:
GraphPanel3D, MutableGraphPanel

public class GraphPanel
extends JPanel
implements GraphListener, EmbeddingListener, AnnotationListener, Observer, ComponentListener

Panel which displays a given graph embedding and reacts to changes in both the embedding and the graph it represents. The graph embedding which is displayed on this panel must be of type EmbeddingModel and its peer graph must be of type GraphModel. Some methods of this class however require the peer graph to be of type MutableGraphModel. The panel displays a two-dimensional embedding which is assumed to use coordinates in the range (-1.0,-1.0)-(1.0,1.0). These coordinates are scaled by a fixed scale factor which is determined at construction time. The center of the panel is used as the center of the coordinate system.

The actual rendering of the graph elements is delegated to a Renderer which is assigned to this panel. Extra information on how to render specific elements of the graph is retrieved from a GraphContext object that is associated with this panel.

Field Summary

protected Element
anchor
protected AnnotationModel
annotation
protected Color
backgroundColor
Panel background color.
protected GraphContext
context
protected EmbeddingModel
embedding
protected GraphView
graph
protected Guides
guides
Current Guides object for this panel.
protected Renderer
renderer
protected double
scale
protected UndoManager
undoManager

Constructor Summary

GraphPanel(EmbeddingModel embedding, AnnotationModel annotation, Renderer renderer, GraphContext context, double scale)
Create a new graph panel with given peer.

Method Summary

void
addGuidesListener(GuidesListener l)
Collection
addInsideOfRectangle(Rectangle2D bounds, Collection set)
Add all elements to the given set whose embedding coordinates are within the given bounds.
void
annotationChanged()
void
clearSelection()
Clear the selection.
void
close()
Decouples all listeners.
void
componentHidden(ComponentEvent e)
void
componentMoved(ComponentEvent e)
void
componentResized(ComponentEvent e)
Recenters the drawing when the component changes size.
void
componentShown(ComponentEvent e)
void
dimensionChanged(int oldDimension)
Called when the dimension of the embedding was changed, and as a consequence that all coordinates have changed as well.
void
edgeAdded(Edge edge)
Called after an edge was added to the graph.
void
edgeChanged(Edge edge)
Called when the annotation of an edge changes.
void
edgeRemoved(Edge edge)
Called when an edge was removed from the graph.
void
edgeRestored(Edge edge)
Called when an edge that was previously removed was restored to the graph.
void
elementAnnotationChanged(Element element)
Rectangle2D
embeddedRectangle(Rectangle mouseRect)
Convert the given rectangle in mouse coordinates to a rectangle in embedding coordinates.
void
embeddingChanged()
Called after a major change in the embedding.
void
fillGraphBundle(GraphBundle bundle)
Copies the current Graph to the given GraphBundle.
Element
getAnchorElement()
Return the 'anchor' element.
AnnotationView
getAnnotation()
Return the annotation used by this panel.
GraphContext
getContext()
Return the graph context associated with this panel.
Point
getDropLocation()
Return the last drag-and-drop location where this component is a target.
EmbeddingView
getEmbedding()
Return the embedding used by this panel.
GraphView
getGraph()
Return the graph displayed by this panel.
Guides
getGuides()
Return the current guides for this panel.
UndoManager
getUndoManager()
Return the undo manager used by this panel.
void
graphChanged()
Called when the graph was changed in a major way.
boolean
isAttached(Edge edge)
Checks whether the given edge is still attached to its parent.
boolean
isAttached(Vertex vertex)
Checks whether the given vertex is still attached to its parent.
boolean
isDirty()
boolean
isDropInProgress()
Is a drag-and-drop operation in progress where this component is a target?
void
joinAnchorToVertex(Vertex vertex, boolean leader)
Join the anchor to the given vertex.
void
loadGraph(EmbeddingView newEmbedding, AnnotationView newAnnotation)
Change the embedding and graph to be used by this panel to a copy of the given embedding.
void
mouseCoordinates(Vertex v, int[] coord)
Store the mouse coordinates of the given vertex into the given array.
protected void
paintBackground(Graphics2D g2)
Paint a background overlay.
protected void
paintEdges(Graphics2D g2)
Paint all edges of the graph using the current renderer.
protected void
paintVertices(Graphics2D g2)
Paint all vertices of the graph using the current vertex renderer.
void
removeGuidesListener(GuidesListener l)
void
selectAll()
Select all graph elements.
void
setAnchorElement(Element anchor)
Set the 'anchor' element.
void
setDirty(boolean dirty)
void
setDropTarget(DropTarget dt)
void
setGraph(EmbeddingModel embedding, AnnotationModel annotation)
Change the embedding to be used by this panel.
void
setGuides(Guides guides)
Install guides for this panel.
void
setRollOver(int mouseX, int mouseY)
Set the roll over element in the corresponding graph context to an element which lies near to the given mouse coordinates.
void
update(Observable o, Object arg)
void
vertexAdded(Vertex vertex)
Called after a vertex was added to the graph.
void
vertexChanged(Vertex vertex)
Called when the annotation of a vertex changes.
void
vertexCoordinatesChanged(Vertex vertex)
Called after a vertex was given new coordinates.
void
vertexRemoved(Vertex vertex)
Called when a vertex was removed from the graph.
void
vertexRestored(Vertex vertex)
Called when a vertex that was previously removed was restored to the graph.
protected @Override
void paintComponent(Graphics g)
Paints the graph on the panel using the current renderer.

Field Details

anchor

protected Element anchor

annotation

protected AnnotationModel annotation

backgroundColor

protected Color backgroundColor
Panel background color.

context

protected GraphContext context

embedding

protected EmbeddingModel embedding

graph

protected GraphView graph

guides

protected Guides guides
Current Guides object for this panel.

renderer

protected Renderer renderer

scale

protected double scale

undoManager

protected UndoManager undoManager

Constructor Details

GraphPanel

public GraphPanel(EmbeddingModel embedding,
                  AnnotationModel annotation,
                  Renderer renderer,
                  GraphContext context,
                  double scale)
Create a new graph panel with given peer. The panel is given a preferred size large enough to display the coordinate range (-1.1,-1.1)-(1.1,1.1).
Parameters:
embedding - Embedding displayed in this panel. The peer of this embedding should be of type GraphModel.
renderer - Renderer for this panel.
context - Graph context for this panel.
scale - Number of pixels corresponding to a unit length in the embedding.

Method Details

addGuidesListener

public void addGuidesListener(GuidesListener l)

addInsideOfRectangle

public Collection addInsideOfRectangle(Rectangle2D bounds,
                                                Collection set)
Add all elements to the given set whose embedding coordinates are within the given bounds.
Parameters:
set - Set to which the elements should be added
Returns:
The value of the set argument

annotationChanged

public void annotationChanged()
Specified by:
annotationChanged in interface AnnotationListener

clearSelection

public void clearSelection()
Clear the selection.

close

public void close()
Decouples all listeners. Should be called immediately before the panel is removed so it can be garbage collected.

componentHidden

public void componentHidden(ComponentEvent e)

componentMoved

public void componentMoved(ComponentEvent e)

componentResized

public void componentResized(ComponentEvent e)
Recenters the drawing when the component changes size.

componentShown

public void componentShown(ComponentEvent e)

dimensionChanged

public void dimensionChanged(int oldDimension)
Called when the dimension of the embedding was changed, and as a consequence that all coordinates have changed as well.
Specified by:
dimensionChanged in interface EmbeddingListener

edgeAdded

public void edgeAdded(Edge edge)
Called after an edge was added to the graph.
Specified by:
edgeAdded in interface GraphListener
Parameters:
edge - Edge that was added to the graph.

edgeChanged

public void edgeChanged(Edge edge)
Called when the annotation of an edge changes.
Specified by:
edgeChanged in interface GraphListener

edgeRemoved

public void edgeRemoved(Edge edge)
Called when an edge was removed from the graph.
Specified by:
edgeRemoved in interface GraphListener

edgeRestored

public void edgeRestored(Edge edge)
Called when an edge that was previously removed was restored to the graph.
Specified by:
edgeRestored in interface GraphListener

elementAnnotationChanged

public void elementAnnotationChanged(Element element)
Specified by:
elementAnnotationChanged in interface AnnotationListener

embeddedRectangle

public Rectangle2D embeddedRectangle(Rectangle mouseRect)
Convert the given rectangle in mouse coordinates to a rectangle in embedding coordinates.

embeddingChanged

public void embeddingChanged()
Called after a major change in the embedding. For instance when the coordinates of all vertices change at the same time. Note that also the peer of the embedding may have changed.
Specified by:
embeddingChanged in interface EmbeddingListener

fillGraphBundle

public void fillGraphBundle(GraphBundle bundle)
Copies the current Graph to the given GraphBundle.
Parameters:
bundle - the GraphBundle to be filled

getAnchorElement

public Element getAnchorElement()
Return the 'anchor' element. The anchor element is a temporary element used by some mouse handlers.

getAnnotation

public AnnotationView getAnnotation()
Return the annotation used by this panel.

getContext

public GraphContext getContext()
Return the graph context associated with this panel.

getDropLocation

public Point getDropLocation()
Return the last drag-and-drop location where this component is a target.

getEmbedding

public EmbeddingView getEmbedding()
Return the embedding used by this panel.

getGraph

public GraphView getGraph()
Return the graph displayed by this panel.

getGuides

public Guides getGuides()
Return the current guides for this panel.

getUndoManager

public UndoManager getUndoManager()
Return the undo manager used by this panel.

graphChanged

public void graphChanged()
Called when the graph was changed in a major way. For instance, when all vertices and edges were removed in one go or when the graph was reinitialized from a file.
Specified by:
graphChanged in interface GraphListener

isAttached

public boolean isAttached(Edge edge)
Checks whether the given edge is still attached to its parent.

isAttached

public boolean isAttached(Vertex vertex)
Checks whether the given vertex is still attached to its parent.

isDirty

public boolean isDirty()

isDropInProgress

public boolean isDropInProgress()
Is a drag-and-drop operation in progress where this component is a target?

joinAnchorToVertex

public void joinAnchorToVertex(Vertex vertex,
                               boolean leader)
Join the anchor to the given vertex.
Parameters:
vertex - Vertex to which the anchor should be joined
leader - Is this command the first of an undo/redo group?

loadGraph

public void loadGraph(EmbeddingView newEmbedding,
                      AnnotationView newAnnotation)
Change the embedding and graph to be used by this panel to a copy of the given embedding.

mouseCoordinates

public void mouseCoordinates(Vertex v,
                             int[] coord)
Store the mouse coordinates of the given vertex into the given array. The array must have dimension at least 2.

paintBackground

protected void paintBackground(Graphics2D g2)
Paint a background overlay. See GraphPanel for the order in which the various paint methods are executed.

This implementation is delegates to the current guides object.


paintEdges

protected void paintEdges(Graphics2D g2)
Paint all edges of the graph using the current renderer.

See GraphPanel for the order in which the various paint methods are executed.


paintVertices

protected void paintVertices(Graphics2D g2)
Paint all vertices of the graph using the current vertex renderer.

See GraphPanel for the order in which the various paint methods are executed.


removeGuidesListener

public void removeGuidesListener(GuidesListener l)

selectAll

public void selectAll()
Select all graph elements.

setAnchorElement

public void setAnchorElement(Element anchor)
Set the 'anchor' element. The anchor element is a temporary element used by some mouse handlers.

setDirty

public void setDirty(boolean dirty)

setDropTarget

public void setDropTarget(DropTarget dt)

setGraph

public void setGraph(EmbeddingModel embedding,
                     AnnotationModel annotation)
Change the embedding to be used by this panel. Clears the selection.

setGuides

public void setGuides(Guides guides)
Install guides for this panel.
Parameters:
guides - New guide object or null to remove the guides.

setRollOver

public void setRollOver(int mouseX,
                        int mouseY)
Set the roll over element in the corresponding graph context to an element which lies near to the given mouse coordinates.

update

public void update(Observable o,
                   Object arg)

vertexAdded

public void vertexAdded(Vertex vertex)
Called after a vertex was added to the graph.
Specified by:
vertexAdded in interface GraphListener
Parameters:
vertex - Vertex that was added to the graph

vertexChanged

public void vertexChanged(Vertex vertex)
Called when the annotation of a vertex changes.
Specified by:
vertexChanged in interface GraphListener

vertexCoordinatesChanged

public void vertexCoordinatesChanged(Vertex vertex)
Called after a vertex was given new coordinates.
Specified by:
vertexCoordinatesChanged in interface EmbeddingListener

vertexRemoved

public void vertexRemoved(Vertex vertex)
Called when a vertex was removed from the graph. Implies that all edges through this vertex were also removed. These removals will not be notified separately.
Specified by:
vertexRemoved in interface GraphListener
Parameters:
vertex - vertex that was removed

vertexRestored

public void vertexRestored(Vertex vertex)
Called when a vertex that was previously removed was restored to the graph.
Specified by:
vertexRestored in interface GraphListener

void paintComponent

protected @Override void paintComponent(Graphics g)
Paints the graph on the panel using the current renderer. Paints background, edges and then vertices.