Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.
public class NodeGraph
ImplementsAnimation, Editable, Iterable<Component>, StyleListener
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
setLabelEditingis 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.
Fields
public static final int EDGE_AUTO = 0 | Edges with ports curve between them; other edges are straight, or follow the bends an arrangement gave them. |
public static final int EDGE_STRAIGHT = 1 | Straight segments through the bends. |
public static final int EDGE_ORTHOGONAL = 2 | Horizontal and vertical segments only. |
public static final int EDGE_CURVED = 3 | Smooth curves through the bends. |
Constructors
public NodeGraph() | An empty graph backed by a new DefaultGraphModel. |
public NodeGraph(GraphModel model) | A graph showing model. |
Methods
public GraphModel getModel() | The model shown. |
public void setModel(GraphModel model) | Shows another model, and clears the selection. |
public GraphNodeRenderer getNodeRenderer() | Paints nodes. |
public void setNodeRenderer(GraphNodeRenderer r) | Replaces the node painter; null restores the default. |
public GraphEdgeRenderer getEdgeRenderer() | Paints edges. |
public void setEdgeRenderer(GraphEdgeRenderer r) | Replaces the edge painter; null restores the default. |
public void addGraphListener(GraphListener l) | Adds a listener. |
public void removeGraphListener(GraphListener l) | Removes a listener. |
public boolean isEditingEnabled() | True, the default, when the user may move, connect and remove. |
public void setEditingEnabled(boolean editable) | Allows or forbids edits; panning, zooming and selecting stay. |
public int getEdgeStyle() | One of EDGE_AUTO, the default, EDGE_STRAIGHT, EDGE_ORTHOGONAL or EDGE_CURVED. |
public void setEdgeStyle(int edgeStyle) | Sets how edges are drawn. |
public boolean isGridVisible() | True, the default, when a dot grid is painted behind the graph. |
public void setGridVisible(boolean gridVisible) | Shows or hides the dot grid. |
public double getSnap() | The step dragged nodes snap to in world units, or 0 for none. |
public void setSnap(double snap) | Snaps dragged nodes to a step; 0 turns it off. |
public boolean isMinimapVisible() | True when a minimap of the whole graph is painted in a corner. |
public void setMinimapVisible(boolean minimapVisible) | Shows or hides the minimap. |
public boolean isLabelEditing() | True when a double tap edits the node’s label in place. |
public void setLabelEditing(boolean labelEditing) | Lets a double tap edit a node’s label in place. |
public float getDetailZoom() | The zoom below which nodes and edges are drawn without text. |
public void setDetailZoom(float detailZoom) | Sets the zoom below which text is not drawn. |
public float getComponentZoom() | The zoom below which nodes show no component from the factory. |
public void setComponentZoom(float componentZoom) | Sets the zoom below which components are hidden. |
public void setNodeComponentFactory(NodeComponentFactory f) | Makes components for node bodies; null for none. |
public NodeComponentFactory getNodeComponentFactory() | The factory, or null. |
public Component getNodeComponent(GraphNode node) | The component made for a node, or null when none was made yet. |
public void setZoomRange(float min, float max) | Limits the zoom. |
public float getZoom() | The current zoom. |
public void setZoom(float z) | Zooms about the centre of the view. |
public double getViewX() | The world x at the left edge of the view. |
public double getViewY() | The world y at the top edge of the view. |
public void setView(double worldX, double worldY) | Scrolls so the world point is at the top left corner of the view. |
public void centerOn(GraphNode node) | Scrolls so the node is in the middle of the view. |
public void fitToView() | Zooms and scrolls so the whole graph fits the view, never past zoom 1. |
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. |
public int worldToScreenX(double wx) | A world x as a pixel from the left of the view. |
public int worldToScreenY(double wy) | A world y as a pixel from the top of the view. |
public int worldToScreenLength(double len) | A world length in pixels. |
public double screenToWorldX(int sx) | A pixel from the left of the view as a world x. |
public double screenToWorldY(int sy) | A pixel from the top of the view as a world y. |
public List<GraphNode> getSelectedNodes() | The selected nodes, in the order they were selected. |
public GraphEdge getSelectedEdge() | The selected edge, or null. |
public boolean isSelected(GraphNode node) | True when the node is selected. |
public void select(GraphNode node) | Selects one node alone, or clears the selection with null. |
public void selectEdge(GraphEdge edge) | Selects one edge alone. |
public void toggleSelection(GraphNode node) | Adds a node to the selection, or takes it out when it is in. |
public void deleteSelection() | Removes the selected nodes and edge, each offered to the listeners and then to the model. |
public void arrange(GraphArrangement arrangement, boolean animate) | Places the nodes with an arrangement, gliding them there when animate is true. |
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. |
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. |
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. |
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. |
public double[] getGroupRect(GraphGroup group) | A group’s frame in world units, {x, y, width, height}, or null for a group with no members. |
public boolean isNodeVisible(GraphNode n) | True when a node is drawn: it is not inside a collapsed group. |
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. |
public GraphNode nodeAt(int sx, int sy) | The topmost visible node at a point of the view, or null. |
public GraphEdge edgeAt(int sx, int sy) | The edge within a finger’s reach of a point of the view, or null. |
protected void initComponent() | Allows subclasses to bind functionality that relies on fully initialized and “ready for action” component state |
public void layoutContainer() | Performs the layout of the container if a layout is necessary |
protected Dimension calcPreferredSize() | Calculates the preferred size based on component content. |
Inherited fields
From Component
DEFAULT_CURSOR, CROSSHAIR_CURSOR, TEXT_CURSOR, WAIT_CURSOR, SW_RESIZE_CURSOR, SE_RESIZE_CURSOR, NW_RESIZE_CURSOR, NE_RESIZE_CURSOR, N_RESIZE_CURSOR, S_RESIZE_CURSOR, W_RESIZE_CURSOR, E_RESIZE_CURSOR, HAND_CURSOR, MOVE_CURSOR, DRAG_REGION_NOT_DRAGGABLE, DRAG_REGION_POSSIBLE_DRAG_X, DRAG_REGION_POSSIBLE_DRAG_Y, DRAG_REGION_POSSIBLE_DRAG_XY, DRAG_REGION_LIKELY_DRAG_X, DRAG_REGION_LIKELY_DRAG_Y, DRAG_REGION_LIKELY_DRAG_XY, DRAG_REGION_IMMEDIATELY_DRAG_X, DRAG_REGION_IMMEDIATELY_DRAG_Y, DRAG_REGION_IMMEDIATELY_DRAG_XY, BRB_CONSTANT_ASCENT, BRB_CONSTANT_DESCENT, BRB_CENTER_OFFSET, BRB_OTHER, CENTER, TOP, LEFT, BOTTOM, RIGHT, BASELINE
Inherited methods
From Container
encloseIn, encloseIn, initLaf, getUIManager, setUIManager, isSurface, add, addAll, add, add, add, add, add, getLeadComponent, setLeadComponent, getLeadParent, keyPressed, keyReleased, getLayout, setLayout, invalidate, setShouldLayout, setShouldCalcPreferredSize, getLayoutWidth, getLayoutHeight, applyRTL, constrainWidthWhenScrollable, constrainHeightWhenScrollable, addComponent, addComponent, addComponent, addComponent, replaceAndWait, replaceAndWait, replace, replaceAndWait, replace, createReplaceTransition, isEnabled, setEnabled, removeComponent, cancelRepaints, flushReplace, removeAll, revalidateWithAnimationSafety, revalidate, revalidateLater, forceRevalidate, clearClientProperties, paint, paintGlass, isSafeArea, setSafeArea, isSafeAreaRoot, getSafeAreaRoot, setSafeAreaRoot, getComponentCount, getComponentAt, getComponentIndex, contains, scrollComponentToVisible, getClosestComponentTo, getResponderAt, getComponentAt, findDropTargetAt, pointerPressed, paramString, refreshTheme, isScrollableX, setScrollableX, isScrollableY, setScrollableY, getSideGap, getBottomGap, setScrollable, setCellRenderer, getScrollIncrement, setScrollIncrement, findFirstFocusable, dragInitiated, fireClicked, isSelectableInteraction, getGridPosY, paintComponentBackground, getGridPosX, animateHierarchyAndWait, createAnimateHierarchy, animateHierarchy, animateHierarchyFadeAndWait, createAnimateHierarchyFade, animateHierarchyFade, animateLayoutFadeAndWait, createAnimateLayoutFadeAndWait, animateLayoutFade, createAnimateLayoutFade, animateLayoutAndWait, animateLayout, updateTabIndices, createAnimateLayout, drop, createAnimateMotion, morph, morphAndWait, animateUnlayout, animateUnlayoutAndWait, createAnimateUnlayout, getChildrenAsList, iterator, iterator
From Component
setSameSize, isSetCursorSupported, parsePreferredSize, getDefaultDragTransparency, setDefaultDragTransparency, getEditingDelegate, setEditingDelegate, getCursor, setCursor, showNativeOverlay, hideNativeOverlay, updateNativeOverlay, getNativeOverlay, getAllStyles, getSameWidth, setSameWidth, getSameHeight, setSameHeight, getX, setX, getOuterX, getInnerX, getY, setY, getOuterY, getInnerY, isVisible, setVisible, getClientProperty, stripMarginAndPadding, putClientProperty, getDirtyRegion, setDirtyRegion, isOpaque, setOpaque, getWidth, setWidth, getOuterWidth, getInnerWidth, getHeight, setHeight, getOuterHeight, getInnerHeight, isDragRegion, getDragRegionStatus, getBaseline, getBaselineResizeBehavior, getPreferredSizeStr, setPreferredSizeStr, getPreferredSize, setPreferredSize, getScrollDimension, calcScrollSize, setScrollSize, getPreferredW, setPreferredW, getPreferredH, setPreferredH, getOuterPreferredH, getInnerPreferredH, getOuterPreferredW, getInnerPreferredW, setSize, getUIID, setUIID, setUIIDFinal, setUIID, getInlineAllStyles, setInlineAllStyles, getInlineSelectedStyles, setInlineSelectedStyles, getInlineUnselectedStyles, setInlineUnselectedStyles, getInlineDisabledStyles, setInlineDisabledStyles, getInlinePressedStyles, setInlinePressedStyles, remove, getParent, getOwner, setOwner, isOwnedBy, containsOrOwns, addFocusListener, removeFocusListener, addScrollListener, removeScrollListener, getSelectCommandText, setSelectCommandText, getLabelForComponent, setLabelForComponent, focusGained, focusLost, paintBackgrounds, paintShadows, getAbsoluteX, getAbsoluteY, isInClippingRegion, paintIntersectingComponentsAbove, paintScrollbars, paintScrollbarX, getScrollOpacity, getSelectedRect, paintScrollbarY, paintComponent, paintComponent, getBorder, getScrollable, paintBackground, isScrollable, getScrollX, setScrollX, getScrollY, setScrollY, onScrollX, onScrollY, getDraggedx, getDraggedy, contains, visibleBoundsContains, hasFixedPreferredSize, getBounds, getBounds, getVisibleBounds, getVisibleBounds, isFocusable, setFocusable, onSetFocusable, resetFocusable, getTabIndex, setTabIndex, getPreferredTabIndex, setPreferredTabIndex, isTraversable, setTraversable, handlesInput, setHandlesInput, consumesRawTextInput, hasFocus, setFocus, getComponentForm, getTopLevelContainer, repaint, repaint, longKeyPress, keyRepeated, registerForAnimation, deregisterFromAnimation, getAnimationManager, getScrollAnimationSpeed, setScrollAnimationSpeed, isBlockLead, setBlockLead, isIgnorePointerEvents, setIgnorePointerEvents, isRippleEffect, setRippleEffect, getInlineStylesTheme, setInlineStylesTheme, shouldRenderComponentSelection, isHideInLandscape, setHideInLandscape, createStyleAnimation, isSmoothScrolling, setSmoothScrolling, pointerHover, stopScrollMomentum, pointerHoverReleased, pointerHoverPressed, pinch, pinchReleased, pinch, rotation, isPinchBlocksDragAndDrop, setPinchBlocksDragAndDrop, pointerDragged, getDragImage, getDragTransparency, setDragTransparency, toImage, drawDraggedImage, draggingOver, dragEnter, dragExit, addPullToRefresh, setPullToRefresh, respondsToPointerEvents, pointerDragged, isStickyDrag, pointerPressed, isDragAndDropOperation, pointerReleased, longPointerPress, pointerReleased, setVerticalScrollBounds, setHorizontalScrollBounds, isVScrollThumbGrabbed, isHScrollThumbGrabbed, isVScrollThumbHover, isHScrollThumbHover, isTensileDragEnabled, setTensileDragEnabled, getTextSelectionSupport, addDropListener, removeDropListener, addDragOverListener, removeDragOverListener, isNativeDragSource, setNativeDragSource, getNativeDragOperation, setNativeDragOperation, createNativeDragOperation, isNativeDropTarget, setNativeDropTarget, getAcceptedDropMimeTypes, setAcceptedDropMimeTypes, getAcceptedDropActions, setAcceptedDropActions, canAcceptNativeDrop, nativeDragEnter, nativeDragOver, nativeDragExit, nativeDrop, addNativeDropListener, removeNativeDropListener, addNativeDragOverListener, removeNativeDragOverListener, dragFinished, addDragFinishedListener, addStateChangeListener, removeStateChangeListener, addPointerPressedListener, addLongPressListener, addContextMenuListener, removeContextMenuListener, addMouseWheelListener, removeMouseWheelListener, addStylusListener, removeStylusListener, getContextMenuCommands, setContextMenuCommands, mouseWheel, paintRippleOverlay, removePointerPressedListener, removeLongPressListener, removeDragFinishedListener, addPointerReleasedListener, removePointerReleasedListener, addPointerDraggedListener, removePointerDraggedListener, getDragSpeed, getStyle, getPressedStyle, getHoverStyle, setHoverStyle, isHovered, setHovered, setPressedStyle, initUnselectedStyle, initPressedStyle, initDisabledStyle, initSelectedStyle, getUnselectedStyle, setUnselectedStyle, getSelectedStyle, setSelectedStyle, getDisabledStyle, setDisabledStyle, installDefaultPainter, requestFocus, toString, refreshTheme, refreshTheme, isDragActivated, animate, scrollRectToVisible, scrollRectToVisible, paintBorder, paintBorderBackground, isCellRenderer, isScrollVisible, setScrollVisible, setIsScrollVisible, startEditingAsync, stopEditing, isEditing, isEditable, laidOut, deinitialize, isInitialized, setInitialized, styleChanged, getNextFocusDown, setNextFocusDown, getNextFocusUp, setNextFocusUp, getNextFocusLeft, setNextFocusLeft, getNextFocusRight, setNextFocusRight, getName, setName, initCustomStyle, deinitializeCustomStyle, isRTL, setRTL, isTactileTouch, isTactileTouch, setTactileTouch, getPropertyNames, getPropertyTypes, getPropertyTypeNames, getPropertyValue, setPropertyValue, paintLockRelease, paintLock, isSnapToGrid, setSnapToGrid, shouldBlockSideSwipe, shouldBlockSideSwipeLeft, shouldBlockSideSwipeRight, blocksSideSwipe, isFlatten, setFlatten, getTensileLength, setTensileLength, isGrabsPointerEvents, setGrabsPointerEvents, getScrollOpacityChangeSpeed, setScrollOpacityChangeSpeed, growShrink, isAlwaysTensile, setAlwaysTensile, isDraggable, setDraggable, isDropTarget, setDropTarget, isChildOf, isHideInPortrait, setHideInPortrait, getBindablePropertyNames, getBindablePropertyTypes, bindProperty, unbindProperty, getBoundPropertyValue, setBoundPropertyValue, getCloudBoundProperty, setCloudBoundProperty, getCloudDestinationProperty, setCloudDestinationProperty, getComponentState, setComponentState, setHidden, isHidden, setHidden, isHidden, announceForAccessibility, getAccessibilityText, setAccessibilityText, getSemantics, getAccessibilityNode, accessibilityChanged, accessibilityChanged, getTooltip, setTooltip
From Iterable
Field details
EDGE_AUTO
public static final int EDGE_AUTO = 0EDGE_STRAIGHT
public static final int EDGE_STRAIGHT = 1EDGE_ORTHOGONAL
public static final int EDGE_ORTHOGONAL = 2EDGE_CURVED
public static final int EDGE_CURVED = 3Constructor details
NodeGraph
public NodeGraph()DefaultGraphModel.NodeGraph
public NodeGraph(GraphModel model)model.Method details
getModel
public GraphModel getModel()setModel
public void setModel(GraphModel model)getNodeRenderer
public GraphNodeRenderer getNodeRenderer()setNodeRenderer
public void setNodeRenderer(GraphNodeRenderer r)getEdgeRenderer
public GraphEdgeRenderer getEdgeRenderer()setEdgeRenderer
public void setEdgeRenderer(GraphEdgeRenderer r)addGraphListener
public void addGraphListener(GraphListener l)removeGraphListener
public void removeGraphListener(GraphListener l)isEditingEnabled
public boolean isEditingEnabled()setEditingEnabled
public void setEditingEnabled(boolean editable)getEdgeStyle
public int getEdgeStyle()EDGE_AUTO, the default, EDGE_STRAIGHT, EDGE_ORTHOGONAL
or EDGE_CURVED.setEdgeStyle
public void setEdgeStyle(int edgeStyle)isGridVisible
public boolean isGridVisible()setGridVisible
public void setGridVisible(boolean gridVisible)getSnap
public double getSnap()setSnap
public void setSnap(double snap)isMinimapVisible
public boolean isMinimapVisible()setMinimapVisible
public void setMinimapVisible(boolean minimapVisible)isLabelEditing
public boolean isLabelEditing()setLabelEditing
public void setLabelEditing(boolean labelEditing)getDetailZoom
public float getDetailZoom()setDetailZoom
public void setDetailZoom(float detailZoom)getComponentZoom
public float getComponentZoom()setComponentZoom
public void setComponentZoom(float componentZoom)setNodeComponentFactory
public void setNodeComponentFactory(NodeComponentFactory f)getNodeComponentFactory
public NodeComponentFactory getNodeComponentFactory()getNodeComponent
public Component getNodeComponent(GraphNode node)setZoomRange
public void setZoomRange(float min, float max)getZoom
public float getZoom()fitToView never goes past it.setZoom
public void setZoom(float z)getViewX
public double getViewX()getViewY
public double getViewY()setView
public void setView(double worldX, double worldY)centerOn
public void centerOn(GraphNode node)fitToView
public void fitToView()zoomAt
public boolean zoomAt(float factor, int screenX, int screenY)factor keeping the world point under the
screen point where it is.Returns
worldToScreenX
public int worldToScreenX(double wx)worldToScreenY
public int worldToScreenY(double wy)worldToScreenLength
public int worldToScreenLength(double len)screenToWorldX
public double screenToWorldX(int sx)screenToWorldY
public double screenToWorldY(int sy)getSelectedNodes
public List<GraphNode> getSelectedNodes()getSelectedEdge
public GraphEdge getSelectedEdge()isSelected
public boolean isSelected(GraphNode node)select
public void select(GraphNode node)selectEdge
public void selectEdge(GraphEdge edge)toggleSelection
public void toggleSelection(GraphNode node)deleteSelection
public void deleteSelection()arrange
public void arrange(GraphArrangement arrangement, boolean animate)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)startLabelEdit
public void startLabelEdit(GraphNode node)GraphModel.renameNode when editing ends.portX
public double portX(GraphNode node, GraphPort port)portY
public double portY(GraphNode node, GraphPort port)getGroupRect
public double[] getGroupRect(GraphGroup group)isNodeVisible
public boolean isNodeVisible(GraphNode n)toImage
public Image toImage(int width, int height)nodeAt
public GraphNode nodeAt(int sx, int sy)edgeAt
public GraphEdge edgeAt(int sx, int sy)initComponent
protected void initComponent()layoutContainer
public void layoutContainer()calcPreferredSize
protected Dimension calcPreferredSize()