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. An edit under way ends, and the undo history is cleared: its steps belong to the model that was shown.
    • 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.
    • getUndoHistory

      public UndoHistory getUndoHistory()
      The history edits are recorded in, or null.
    • setUndoHistory

      public void setUndoHistory(UndoHistory undoHistory)

      Records every edit the user makes in a history, so it can be undone. The application's history, UndoHistory.getGlobal(), by default; null records nothing. An undone or redone step brings the graph back into view (ComponentEdit).

      What is recorded is what the user did through this view: moving, resizing, connecting, removing, renaming, adding, pasting, collapsing a group and arrange. Changes the application makes to the model directly are not.

    • undo

      public boolean undo()

      Takes back the last edit in the history. Prefer this to calling the history yourself: it first lets a running arrangement land.

      Returns

      false when there is no history, nothing to undo, or editing is off

    • redo

      public boolean redo()

      Makes the last undone edit again.

      Returns

      false when there is no history, nothing to redo, or editing is off

    • isResizable

      public boolean isResizable()
      True when a selected node shows handles on its corners to resize it by.
    • setResizable

      public void setResizable(boolean resizable)
      Lets the user resize a node by dragging a corner of it while it is the only node selected. Off by default.
    • laneAt

      public GraphGroup laneAt(double worldX, double worldY)
      The lane under a world point, or null. Later lanes are over earlier ones.
    • getPendingTemplate

      public GraphNodeTemplate getPendingTemplate()
      The template the next tap on the background makes a node from, or null.
    • setPendingTemplate

      public void setPendingTemplate(GraphNodeTemplate template)
      Arms a template: the next tap on the background adds a node of it there. A GraphPalette does this when one of its items is tapped, which is how a node is added without a drag.
    • createNode

      public GraphNode createNode(GraphNodeTemplate template, double worldX, double worldY)

      Adds a node made from a template, centred on a world point and selected. The node is offered to the listeners and then to the model, and joins the lane it lands in.

      Returns

      the node, or null when it was refused

    • duplicateSelection

      public boolean duplicateSelection()

      Adds a copy of every selected node, and of the edges among them, a little down and to the trailing side, and selects the copies.

      Returns

      false when nothing was selected or every copy was refused

    • setUserDataCodec

      public void setUserDataCodec(GraphJson.UserDataCodec codec)
      Writes the user data of copied nodes to the clipboard and reads it back on paste; without one, pasted nodes have no user data.
    • copySelection

      public boolean copySelection()

      Puts the selected nodes, and the edges among them, on the clipboard, as the JSON GraphJson writes.

      Returns

      false when no node is selected

    • cutSelection

      public boolean cutSelection()

      Copies the selection and removes it.

      Returns

      false when no node is selected or editing is off

    • paste

      public boolean paste()

      Adds the nodes on the clipboard, each time a step further from where they were copied, and selects them.

      Returns

      false when the clipboard holds no nodes or every one was refused

    • setPortEditorFactory

      public void setPortEditorFactory(PortEditorFactory factory)
      Makes the editors of unconnected input ports; null for none.
    • getPortEditorFactory

      public PortEditorFactory getPortEditorFactory()
      The factory of port editors, or null.
    • getPortEditor

      public Component getPortEditor(GraphNode node, GraphPort port)
      The editor made for a port, or null when none was made yet.
    • setPortValue

      public boolean setPortValue(GraphPort port, Object value)

      Sets the value an input port uses while nothing is connected to it, through the model, as an edit that can be undone. A port editor calls this when the user enters a value.

      Returns

      false when the model refused

    • 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, and a lane's is its own bounds.
    • 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