A JGraph object doesn't actually contain your data; it simply provides a view of the data. Like any non-trivial Swing component, the graph gets data by querying its data model.
JGraph displays its data by drawing individual elements. Each element displayed by the graph contains exactly one item of data, which is called a cell. A cell may either be a vertex or an edge. Vertices may have neighbours or not, and edges may have source and target vertices or not, depending on whether they are connected.
Creating a Graph
The following code creates a JGraph object:
JGraph graph = new JGraph();
...
JScrollPane graphLayoutCache = new JScrollPane(graph)
The code creates an instance of JGraph and puts it in a scroll pane. JGraphs constructor is called with no arguments in this example, which causes the constructor to create a sample model.
Editing
JGraph supports in-place editing of text and shapes. These features can be disabled using the setEnabled() method, which blocks all features, or individually, using the following methods:
setEditable() controls in-place editing of cells. Moving, cloning, sizing, and bending, connection and disconnection of edges may also be disabled using the respective methods, namemly setMoveable(), setCloneable(), setSizeable(), setBendable(), setConnectable() and setDisconnectable().
The model offers finer control of connection establishment based on the passed-in edge and port. The individual cells offer yet another level of control in that they may allow/disallow being edited, moved, cloned, resized, and shaped, or connected/disconnected to or from other cells.
Keyboard Bindings
JGraph defines the following set of keyboard bindings:
Customization
There are a number of additional methods that customize JGraph. For example, setMinimumMove() defines the minimum amount of pixels before a move operation is initiated. setSnapSize() defines the maximum distance for a cell to be selected. setFloatEnabled() enables/disables port floating.
With setDisconnectOnMove() you can indicate if the selected subgraph should be disconnected from the unselected rest when a move operation is initiated. setDragEnabled() enables/disables the use of Drag And Drop, and setDropEnabled() sets if the graph accepts Drops from external sources.
Customizing a graphs display
JGraph performs some look-and-feel specific painting. You can customize this painting in a limited way. For example, you can modify the grid using setGridColor() and setGridSize(), and you can change the handle colors using setHandleColor() and setLockedHandleColor().
If you want finer control over the rendering, you can subclass one of the default renderers, and extend its paint()-method. A renderer is a Component-extension that paints a cell based on its attributes. Thus, neither the JGraph nor its look-and-feel-specific implementation actually contain the code that paints the cell. Instead, the graph uses the cell renderers painting code.
Selection
Apart from the single-cell and marquee-selection, JGraphs selection model also allows to "step-into" groups, and select children. This feature can be disabled using the setAllowsChildSelection() method of the selection model instance.
If you are interested in knowing when the selection changes implement the GraphSelectionListener
interface and add the instance using the method addGraphSelectionListener
. valueChanged
will be invoked when the selection changes, that is if the user clicks twice on the same vertex valueChanged
will only be invoked once.
Change Notification
If you are interested in handling modifications, implement the GraphEventHandler
interface and add the instance using the method addGraphEventHandler
.
For detection of double-clicks or when a user clicks on a cell, regardless of whether or not it was selected, I recommend you implement a MouseListener and use getFirstCellForLocation
.
Undo Support
To enable Undo-Support, a GraphUndoManager
must be added using addGraphSelectionListener
. The GraphUndoManager is an extension of Swing's GraphUndoManager
that maintains a command history in the context of multiple views. In this setup, a cell may have a set of attributes in each view attached to the model.
For example, consider a position that is stored separately in each view. If a node is inserted, the change will be visible in all attached views, resulting in a new node that pops-up at the initial position. If the node is subsequently moved, say, in view1, this does not constitute a change in view2. If view2 does an "undo", the move and the insertion must be undone, whereas an "undo" in view1 will only undo the previous move operation.
Like all JComponent
classes, you can use {@link javax.swing.InputMap}and {@link javax.swing.ActionMap}to associate an {@link javax.swing.Action}object with a {@link javax.swing.KeyStroke}and execute the action under specified conditions.
@author Gaudenz Alder
@version 2.1 16/03/03
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|