beowulf.gui
Class JGraph

java.lang.Object
  extended byjava.awt.Component
      extended byjava.awt.Container
          extended byjavax.swing.JComponent
              extended bybeowulf.gui.DraggableContainer
                  extended bybeowulf.gui.JGraph
All Implemented Interfaces:
DraggableComponentSelectionListener, EventListener, GraphListener, ImageObserver, MenuContainer, MouseListener, Serializable

public class JGraph
extends DraggableContainer
implements GraphListener, DraggableComponentSelectionListener

This class is a swing visualization of a beowulf.model.graph.GraphModel. It is fairly customizable and handles drawing small and medium sized graphs at reasonable speed. Care has been take to abstract out different parts of the functionality and one can even respond to JGraph selection events.
Customization of the JGraph should begin with the development of a new subclass of JNode and its appropriate factory. See JNode and JNodeFactory for more details on how to achieve this. Any JGraph can have any JNodeFactory set in order to manufacture JNodes.
JNodes are the GUI representation of the nodes in a Graph. The standard JNode class simply draws a string onto the graphics context of the JGraph. The string drawn is the result of calling .toString() on the node being represented.
Directed and undirected edges (and hence graphs) are handled fine. Self edges and multiple edges between a pair of nodes will also be handled fine.

Version:
1.0 - Feb 18, 2004
Author:
Andy Scukanec - (ags at cs dot cornell dot edu)
See Also:
Serialized Form

Nested Class Summary
 
Nested classes inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent
 
Nested classes inherited from class java.awt.Container
Container.AccessibleAWTContainer
 
Nested classes inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BltBufferStrategy, Component.FlipBufferStrategy
 
Field Summary
static int DIRECTED_GRAPH
          Flag indicating the model is a directed graph.
protected  JNodeFactory factory
          This is the factory which will produce JNodes, or GUI representations of nodes in the graph.
static float HEAD
          The size of the arrow that is drawn for directed edges.
protected  EventListenerList listenerList
          This list contains references to all interested listeners.
protected  GraphModel model
          Holds the graph model that this JGraph represents.
protected  Map nodeLookup
          This Map will hold node->DraggableComponent translations.
static int UNDIRECTED_GRAPH
          Flag indicating the model is an undirected graph.
 
Fields inherited from class beowulf.gui.DraggableContainer
currentComponent
 
Fields inherited from class javax.swing.JComponent
accessibleContext, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
JGraph()
          The basic constructor for JGraph.
JGraph(GraphModel newModel)
          This constructor takes in an existing GraphModel and uses it right away.
JGraph(GraphModel newModel, JNodeFactory newFactory)
          This constructor takes in both an existing GraphModel and a JNodeFactory, used to create JNodes.
JGraph(int graphType)
          This constructor takes in a single int - one of the flags defined in this class indicating a directed or undirected graph.
JGraph(int graphType, JNodeFactory newFactory)
          This constructor takes in both a graph type and a JNodeFactory used to create JNodes.
JGraph(Vector nodeVector, Vector edgeVector, int graphType)
          This constructor takes in a list of existing nodes and edges between them, as well as a graph type.
JGraph(Vector nodeVector, Vector edgeVector, int graphType, JNodeFactory newFactory)
          This constructor takes in lists of existing nodes and edges, the graph type, and a new JNodeFactory.
 
Method Summary
 void addGraphSelectionListener(GraphSelectionListener l)
          This method adds a GraphSelectionListener as an interested listener of events to this JGraph.
 void edgeAdded(GraphEvent ge)
          This method is called when an edge is added to a Graph.
 void edgeRemoved(GraphEvent ge)
          This method is called when an edge is removed from a Graph.
protected  void fireNodeDeselected(Object source, Object value)
          This method must be called by all subclasses after a node is deselected.
protected  void fireNodeSelected(Object source, Object value)
          This method must be called any time a node is selected.
 JNodeFactory getJNodeFactory()
          Returns the JNodeFactory.
 GraphModel getModel()
          Returns the graph model.
 void nodeAdded(GraphEvent ge)
          This method is called when a node is added to a Graph.
 void nodeRemoved(GraphEvent ge)
          This method is called when a node is removed from a Graph.
 void paintComponent(Graphics g)
           
 void removeGraphSelectionListener(GraphSelectionListener l)
          This method removes the GraphSelectionListener as an interested listener of events to this JGraph.
 void setJNodeFactory(JNodeFactory newFactory)
          Sets the JNodeFactory for this JGraph.
 void setModel(GraphModel newModel)
          This method will replace the current model of this graph with the parameter.
 void valueChanged(DraggableComponentSelectionEvent dcse)
          This method will be called whenever any DraggableContainers this object is listening to fire a DraggableComponent selection event.
 
Methods inherited from class beowulf.gui.DraggableContainer
add, add, add, add, add, addDraggableComponentSelectionListener, fireComponentDeselected, fireComponentSelected, getCurrentComponent, mouseClicked, mouseEntered, mouseExited, mousePressed, mouseReleased, removeDraggableComponentSelectionListener, setCurrentComponent, setLayout
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getAccessibleContext, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getGraphics, getHeight, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getUIClassID, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isPreferredSizeSet, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintImmediately, paintImmediately, paramString, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFont, setForeground, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update, updateUI
 
Methods inherited from class java.awt.Container
addContainerListener, addImpl, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DIRECTED_GRAPH

public static final int DIRECTED_GRAPH
Flag indicating the model is a directed graph.

See Also:
Constant Field Values

UNDIRECTED_GRAPH

public static final int UNDIRECTED_GRAPH
Flag indicating the model is an undirected graph.

See Also:
Constant Field Values

HEAD

public static final float HEAD
The size of the arrow that is drawn for directed edges.

See Also:
Constant Field Values

model

protected GraphModel model
Holds the graph model that this JGraph represents.


nodeLookup

protected Map nodeLookup
This Map will hold node->DraggableComponent translations. Note that DCs wrap JNodes, which in turn wrap nodes (Objects) from the underlying Graph.


factory

protected JNodeFactory factory
This is the factory which will produce JNodes, or GUI representations of nodes in the graph. Subclasses can be used to create more interesting or useful JNodes than the plain vanilla default.


listenerList

protected EventListenerList listenerList
This list contains references to all interested listeners.

Constructor Detail

JGraph

public JGraph()
The basic constructor for JGraph. Makes its own model, uses a directed graph, and the standard JNodeFactory.


JGraph

public JGraph(int graphType)
This constructor takes in a single int - one of the flags defined in this class indicating a directed or undirected graph.

Parameters:
graphType - The type of graph (directed or undirected).

JGraph

public JGraph(int graphType,
              JNodeFactory newFactory)
This constructor takes in both a graph type and a JNodeFactory used to create JNodes.

Parameters:
graphType - The type of graph (directed or undirected).
newFactory - The new JNodeFactory.

JGraph

public JGraph(GraphModel newModel)
This constructor takes in an existing GraphModel and uses it right away.

Parameters:
newModel - The model to be represented by this JGraph.

JGraph

public JGraph(GraphModel newModel,
              JNodeFactory newFactory)
This constructor takes in both an existing GraphModel and a JNodeFactory, used to create JNodes.

Parameters:
newModel - The model to be represented by this JGraph.
newFactory - The new JNodeFactory.

JGraph

public JGraph(Vector nodeVector,
              Vector edgeVector,
              int graphType)
This constructor takes in a list of existing nodes and edges between them, as well as a graph type.

Parameters:
nodeVector - The list of nodes.
edgeVector - The list of edges.
graphType - The graph type (directed or undirected).

JGraph

public JGraph(Vector nodeVector,
              Vector edgeVector,
              int graphType,
              JNodeFactory newFactory)
This constructor takes in lists of existing nodes and edges, the graph type, and a new JNodeFactory.

Parameters:
nodeVector - The list of nodes.
edgeVector - The list of edges.
graphType - The graph type (directed or undirected).
newFactory - The new factory for this JGraph.
Method Detail

addGraphSelectionListener

public void addGraphSelectionListener(GraphSelectionListener l)
This method adds a GraphSelectionListener as an interested listener of events to this JGraph.

Parameters:
l - The graph selection listener to add.

removeGraphSelectionListener

public void removeGraphSelectionListener(GraphSelectionListener l)
This method removes the GraphSelectionListener as an interested listener of events to this JGraph.

Parameters:
l - The graph selection listener to remove.

fireNodeSelected

protected void fireNodeSelected(Object source,
                                Object value)
This method must be called any time a node is selected.

Parameters:
source - The graph on which the even occurred.
value - The value of the node that was selected.

fireNodeDeselected

protected void fireNodeDeselected(Object source,
                                  Object value)
This method must be called by all subclasses after a node is deselected.

Parameters:
source - The graph on which the even occurred.
value - The value of the node that was deselected.

setModel

public void setModel(GraphModel newModel)
This method will replace the current model of this graph with the parameter. The JGraph will unassociate itself as a receiver of graph events from the original model and reassociate itself with the incoming model. All of the nodes in the new model will be added (using the factory to create JNodes), and the GUI will be updated immediately.

Parameters:
newModel - The new graph model.

setJNodeFactory

public void setJNodeFactory(JNodeFactory newFactory)
Sets the JNodeFactory for this JGraph. The JNodeFactory plays an important role in the custimization of the JGraph. See above for more information.

Parameters:
newFactory - The new JNodeFactory.

getModel

public GraphModel getModel()
Returns the graph model.

Returns:
The graph model.

getJNodeFactory

public JNodeFactory getJNodeFactory()
Returns the JNodeFactory.

Returns:
The JNodeFactory.

nodeAdded

public void nodeAdded(GraphEvent ge)
Description copied from interface: GraphListener
This method is called when a node is added to a Graph.

Specified by:
nodeAdded in interface GraphListener
Parameters:
ge - The graph event containing all of the necessary data.

nodeRemoved

public void nodeRemoved(GraphEvent ge)
Description copied from interface: GraphListener
This method is called when a node is removed from a Graph.

Specified by:
nodeRemoved in interface GraphListener
Parameters:
ge - The graph event containing all of the necessary data.

edgeAdded

public void edgeAdded(GraphEvent ge)
Description copied from interface: GraphListener
This method is called when an edge is added to a Graph.

Specified by:
edgeAdded in interface GraphListener
Parameters:
ge - The graph event containing all of the necessary data.

edgeRemoved

public void edgeRemoved(GraphEvent ge)
Description copied from interface: GraphListener
This method is called when an edge is removed from a Graph.

Specified by:
edgeRemoved in interface GraphListener
Parameters:
ge - The graph event containing all of the necessary data.

valueChanged

public void valueChanged(DraggableComponentSelectionEvent dcse)
Description copied from interface: DraggableComponentSelectionListener
This method will be called whenever any DraggableContainers this object is listening to fire a DraggableComponent selection event.

Specified by:
valueChanged in interface DraggableComponentSelectionListener
Parameters:
dcse - The generated event.

paintComponent

public void paintComponent(Graphics g)