Class NodeGraph

All Implemented Interfaces:
Animation, Editable, StyleListener, Iterable<Component>

public class NodeGraph extends Container

A pannable, zoomable view of a GraphModel where nodes are dragged, connected and grouped: a system diagram, a mind map, or a node editor.

DefaultGraphModel model = new DefaultGraphModel();
model.addNode(new GraphNode("api", "API gateway"));
model.addNode(new GraphNode("orders", "Orders"));
model.addEdge("api", "orders");
NodeGraph graph = new NodeGraph(model);
graph.arrange(new LayeredArrangement(), false);
Form form = new Form("Services", new BorderLayout());
form.add(BorderLayout.CENTER, graph);
form.show();

What a user can do:

  • drag the background to pan, with a fling; pinch, or Ctrl or Cmd with the wheel, to zoom about the pointer;
  • tap a node or an edge to select it, and Shift-tap to add to the selection; drag a node to move it, with everything selected;
  • drag from an output port to an input port to connect them. A node without ports shows a handle on its trailing edge while it is selected, and a long press on it starts a connection too;
  • drag the end of an edge off an input port to reconnect or drop it;
  • long press the background, or Shift-drag it, to select with a box;
  • tap a group's title to collapse or expand it, double tap a node to open it (and edit its label when setLabelEditing is on);
  • drag in the minimap to move the view.

Every edit is offered to the GraphListeners, then to the model; either may refuse it. With setEditingEnabled(false) the graph can only be panned, zoomed and selected.

The nodes and edges are painted, so the component costs what is on screen: below getDetailZoom they are drawn without text, which keeps a graph of thousands of nodes moving at frame rate. A node can still carry a real component from a NodeComponentFactory.

  • Field Details

    • EDGE_AUTO

      public static final int EDGE_AUTO
      Edges with ports curve between them; other edges are straight, or follow the bends an arrangement gave them.
      See Also:
    • EDGE_STRAIGHT

      public static final int EDGE_STRAIGHT
      Straight segments through the bends.
      See Also:
    • EDGE_ORTHOGONAL

      public static final int EDGE_ORTHOGONAL
      Horizontal and vertical segments only.
      See Also:
    • EDGE_CURVED

      public static final int EDGE_CURVED
      Smooth curves through the bends.
      See Also:
  • Constructor Details

    • NodeGraph

      public NodeGraph()
      An empty graph backed by a new DefaultGraphModel.
    • NodeGraph

      public NodeGraph(GraphModel model)
      A graph showing model.
  • Method Details

    • getModel

      public GraphModel getModel()
      The model shown.
    • setModel

      public void setModel(GraphModel model)
      Shows another model, and clears the selection.
    • getNodeRenderer

      public GraphNodeRenderer getNodeRenderer()
      Paints nodes. Never null.
    • setNodeRenderer

      public void setNodeRenderer(GraphNodeRenderer r)
      Replaces the node painter; null restores the default.
    • getEdgeRenderer

      public GraphEdgeRenderer getEdgeRenderer()
      Paints edges. Never null.
    • setEdgeRenderer

      public void setEdgeRenderer(GraphEdgeRenderer r)
      Replaces the edge painter; null restores the default.
    • addGraphListener

      public void addGraphListener(GraphListener l)
      Adds a listener.
    • removeGraphListener

      public void removeGraphListener(GraphListener l)
      Removes a listener.
    • isEditingEnabled

      public boolean isEditingEnabled()
      True, the default, when the user may move, connect and remove.
    • setEditingEnabled

      public void setEditingEnabled(boolean editable)
      Allows or forbids edits; panning, zooming and selecting stay.
    • getEdgeStyle

      public int getEdgeStyle()
      One of EDGE_AUTO, the default, EDGE_STRAIGHT, EDGE_ORTHOGONAL or EDGE_CURVED.
    • setEdgeStyle

      public void setEdgeStyle(int edgeStyle)
      Sets how edges are drawn.
    • isGridVisible

      public boolean isGridVisible()
      True, the default, when a dot grid is painted behind the graph.
    • setGridVisible

      public void setGridVisible(boolean gridVisible)
      Shows or hides the dot grid.
    • getSnap

      public double getSnap()
      The step dragged nodes snap to in world units, or 0 for none.
    • setSnap

      public void setSnap(double snap)
      Snaps dragged nodes to a step; 0 turns it off.
    • isMinimapVisible

      public boolean isMinimapVisible()
      True when a minimap of the whole graph is painted in a corner.
    • setMinimapVisible

      public void setMinimapVisible(boolean minimapVisible)
      Shows or hides the minimap.
    • isLabelEditing

      public boolean isLabelEditing()
      True when a double tap edits the node's label in place.
    • setLabelEditing

      public void setLabelEditing(boolean labelEditing)
      Lets a double tap edit a node's label in place.
    • getDetailZoom

      public float getDetailZoom()
      The zoom below which nodes and edges are drawn without text.
    • setDetailZoom

      public void setDetailZoom(float detailZoom)
      Sets the zoom below which text is not drawn.
    • getComponentZoom

      public float getComponentZoom()
      The zoom below which nodes show no component from the factory.
    • setComponentZoom

      public void setComponentZoom(float componentZoom)
      Sets the zoom below which components are hidden.
    • setNodeComponentFactory

      public void setNodeComponentFactory(NodeComponentFactory f)
      Makes components for node bodies; null for none.
    • getNodeComponentFactory

      public NodeComponentFactory getNodeComponentFactory()
      The factory, or null.
    • getNodeComponent

      public Component getNodeComponent(GraphNode node)
      The component made for a node, or null when none was made yet.
    • setZoomRange

      public void setZoomRange(float min, float max)
      Limits the zoom.
    • getZoom

      public float getZoom()
      The current zoom. At zoom 1 a node is drawn at its world size scaled to the theme font, so labels fit on dense screens as they do on a desktop; fitToView never goes past it.
    • setZoom

      public void setZoom(float z)
      Zooms about the centre of the view.
    • getViewX

      public double getViewX()
      The world x at the left edge of the view.
    • getViewY

      public double getViewY()
      The world y at the top edge of the view.
    • setView

      public void setView(double worldX, double worldY)
      Scrolls so the world point is at the top left corner of the view.
    • centerOn

      public void centerOn(GraphNode node)
      Scrolls so the node is in the middle of the view.
    • fitToView

      public void fitToView()
      Zooms and scrolls so the whole graph fits the view, never past zoom 1.
    • zoomAt

      public boolean zoomAt(float factor, int screenX, int screenY)

      Multiplies the zoom by factor keeping the world point under the screen point where it is.

      Returns

      false when the zoom was already at its limit

    • worldToScreenX

      public int worldToScreenX(double wx)
      A world x as a pixel from the left of the view.
    • worldToScreenY

      public int worldToScreenY(double wy)
      A world y as a pixel from the top of the view.
    • worldToScreenLength

      public int worldToScreenLength(double len)
      A world length in pixels.
    • screenToWorldX

      public double screenToWorldX(int sx)
      A pixel from the left of the view as a world x.
    • screenToWorldY

      public double screenToWorldY(int sy)
      A pixel from the top of the view as a world y.
    • getSelectedNodes

      public List<GraphNode> getSelectedNodes()
      The selected nodes, in the order they were selected. A copy.
    • getSelectedEdge

      public GraphEdge getSelectedEdge()
      The selected edge, or null.
    • isSelected

      public boolean isSelected(GraphNode node)
      True when the node is selected.
    • select

      public void select(GraphNode node)
      Selects one node alone, or clears the selection with null.
    • selectEdge

      public void selectEdge(GraphEdge edge)
      Selects one edge alone.
    • toggleSelection

      public void toggleSelection(GraphNode node)
      Adds a node to the selection, or takes it out when it is in.
    • deleteSelection

      public void deleteSelection()
      Removes the selected nodes and edge, each offered to the listeners and then to the model.
    • arrange

      public void arrange(GraphArrangement arrangement, boolean animate)
      Places the nodes with an arrangement, gliding them there when animate is true. Arrangements are not user edits: a model that refuses moves is placed anyway, the nodes moved directly.
    • setLiveArrangement

      public void setLiveArrangement(ForceArrangement arrangement)
      Runs a force arrangement live: the graph settles a step a frame, and follows a node while it is dragged. Null stops it.
    • startLabelEdit

      public void startLabelEdit(GraphNode node)
      Edits a node's label in place, in a text field over the node; the new label goes to GraphModel.renameNode when editing ends.
    • portX

      public double portX(GraphNode node, GraphPort port)
      The world x of a port's anchor: inputs on the leading edge, outputs on the trailing one.
    • portY

      public double portY(GraphNode node, GraphPort port)
      The world y of a port's anchor: the ports of one side share the body under the title evenly.
    • getGroupRect

      public double[] getGroupRect(GraphGroup group)
      A group's frame in world units, {x, y, width, height}, or null for a group with no members. A collapsed group's frame is its node.
    • isNodeVisible

      public boolean isNodeVisible(GraphNode n)
      True when a node is drawn: it is not inside a collapsed group.
    • toImage

      public Image toImage(int width, int height)
      Paints the whole graph into a new image of the given size, fitted with a margin, at no more than zoom 1, without the minimap or the selection. For a thumbnail, a share sheet or a printout.
    • nodeAt

      public GraphNode nodeAt(int sx, int sy)
      The topmost visible node at a point of the view, or null.
    • edgeAt

      public GraphEdge edgeAt(int sx, int sy)
      The edge within a finger's reach of a point of the view, or null.
    • initComponent

      protected void initComponent()
      Description copied from class: Component
      Allows subclasses to bind functionality that relies on fully initialized and "ready for action" component state
      Overrides:
      initComponent in class Component
    • layoutContainer

      public void layoutContainer()
      Description copied from class: Container
      Performs the layout of the container if a layout is necessary
      Overrides:
      layoutContainer in class Container
    • calcPreferredSize

      protected Dimension calcPreferredSize()
      Description copied from class: Container

      Calculates the preferred size based on component content. This method is invoked lazily by getPreferred size.

      Returns

      the calculated preferred size based on component content

      Overrides:
      calcPreferredSize in class Container