Class DefaultGraphModel

java.lang.Object
com.codename1.graph.AbstractGraphModel
com.codename1.graph.DefaultGraphModel
All Implemented Interfaces:
GraphModel

public class DefaultGraphModel extends AbstractGraphModel

An editable graph kept in memory.

Edits made by a user are checked by the ConnectionValidator before an edge is made. Ids of edges the model makes itself are "e" followed by a counter, skipping any already taken.

  • Constructor Details

    • DefaultGraphModel

      public DefaultGraphModel()
  • Method Details

    • getNodes

      public List<GraphNode> getNodes()
      Description copied from interface: GraphModel
      Every node, in painting order: later nodes are drawn over earlier ones.
    • getEdges

      public List<GraphEdge> getEdges()
      Description copied from interface: GraphModel
      Every edge.
    • getGroups

      public List<GraphGroup> getGroups()
      Description copied from interface: GraphModel
      Every group.
    • getNode

      public GraphNode getNode(String id)
      Description copied from interface: GraphModel
      The node with this id, or null.
      Specified by:
      getNode in interface GraphModel
      Overrides:
      getNode in class AbstractGraphModel
    • getEdge

      public GraphEdge getEdge(String id)
      The edge with this id, or null.
    • getValidator

      public ConnectionValidator getValidator()
      The validator connect asks. Never null.
    • setValidator

      public void setValidator(ConnectionValidator validator)
      Replaces the validator; null restores the default one.
    • addNode

      public GraphNode addNode(GraphNode node)

      Adds a node, replacing one with the same id.

      Returns

      the node

    • addEdge

      public GraphEdge addEdge(GraphEdge edge)

      Adds an edge as it is, without asking the validator.

      Returns

      the edge

    • addEdge

      public GraphEdge addEdge(String sourceId, String targetId)
      Adds an edge between two nodes with a generated id.
    • addGroup

      public GraphGroup addGroup(GraphGroup group)
      Adds a group.
    • clear

      public void clear()
      Removes every node, edge and group.
    • getEdges

      public List<GraphEdge> getEdges(GraphNode node)
      The edges that touch a node.
    • moveNode

      public boolean moveNode(GraphNode node, double x, double y)
      Description copied from interface: GraphModel

      Moves a node to a world position.

      Returns

      false when the model refuses the move

      Specified by:
      moveNode in interface GraphModel
      Overrides:
      moveNode in class AbstractGraphModel
    • connect

      public GraphEdge connect(GraphNode source, GraphPort sourcePort, GraphNode target, GraphPort targetPort)
      Description copied from interface: GraphModel

      Makes an edge from one node or port to another.

      Parameters
      • source: the node the edge leaves
      • sourcePort: its output port, or null
      • target: the node the edge enters
      • targetPort: its input port, or null
      Returns

      the new edge, or null when the model refuses it

      Specified by:
      connect in interface GraphModel
      Overrides:
      connect in class AbstractGraphModel
    • removeEdge

      public boolean removeEdge(GraphEdge edge)
      Description copied from interface: GraphModel

      Removes an edge.

      Returns

      false when the model refuses

      Specified by:
      removeEdge in interface GraphModel
      Overrides:
      removeEdge in class AbstractGraphModel
    • removeNode

      public boolean removeNode(GraphNode node)
      Description copied from interface: GraphModel

      Removes a node and every edge that touches it.

      Returns

      false when the model refuses

      Specified by:
      removeNode in interface GraphModel
      Overrides:
      removeNode in class AbstractGraphModel
    • renameNode

      public boolean renameNode(GraphNode node, String label)
      Description copied from interface: GraphModel

      Gives a node a new label.

      Returns

      false when the model refuses

      Specified by:
      renameNode in interface GraphModel
      Overrides:
      renameNode in class AbstractGraphModel
    • setCollapsed

      public boolean setCollapsed(GraphGroup group, boolean collapsed)
      Description copied from interface: GraphModel

      Collapses or expands a group.

      Returns

      false when the model refuses

      Specified by:
      setCollapsed in interface GraphModel
      Overrides:
      setCollapsed in class AbstractGraphModel
    • insertNode

      public boolean insertNode(GraphNode node, int index)
      Description copied from interface: GraphModel

      Adds a node the user made, from a palette or by pasting.

      Returns

      false when the model refuses, or already has a node with this id

      Parameters
      • node: the node
      • index: where in getNodes it goes, which is its painting order; negative, or past the end, for the end. Undoing a removal puts a node back where it was.

      A view's undo of a delete puts nodes back through this method, so a model that refuses it cannot have its deletes undone.

      Specified by:
      insertNode in interface GraphModel
      Overrides:
      insertNode in class AbstractGraphModel
    • insertEdge

      public boolean insertEdge(GraphEdge edge, int index)
      Description copied from interface: GraphModel

      Adds an edge as it is, without the checks connect makes: pasting and undo put back edges that were valid when they were made.

      Returns

      false when the model refuses, has an edge with this id, or lacks one of the nodes the edge names

      Parameters
      • edge: the edge
      • index: where in getEdges it goes; negative, or past the end, for the end
      Specified by:
      insertEdge in interface GraphModel
      Overrides:
      insertEdge in class AbstractGraphModel
    • resizeNode

      public boolean resizeNode(GraphNode node, double width, double height)
      Description copied from interface: GraphModel

      Gives a node a new size, in world units.

      Returns

      false when the model refuses

      Specified by:
      resizeNode in interface GraphModel
      Overrides:
      resizeNode in class AbstractGraphModel
    • setNodeGroup

      public boolean setNodeGroup(GraphNode node, String groupId)
      Description copied from interface: GraphModel

      Moves a node into a group or a lane, or out of every one with null.

      Returns

      false when the model refuses

      Specified by:
      setNodeGroup in interface GraphModel
      Overrides:
      setNodeGroup in class AbstractGraphModel
    • resizeGroup

      public boolean resizeGroup(GraphGroup group, double x, double y, double width, double height)
      Description copied from interface: GraphModel

      Gives a lane new bounds, in world units.

      Returns

      false when the model refuses

      Specified by:
      resizeGroup in interface GraphModel
      Overrides:
      resizeGroup in class AbstractGraphModel
    • setPortValue

      public boolean setPortValue(GraphPort port, Object value)
      Description copied from interface: GraphModel

      Sets the value an unconnected input port uses.

      Returns

      false when the model refuses

      Specified by:
      setPortValue in interface GraphModel
      Overrides:
      setPortValue in class AbstractGraphModel
    • removeGroup

      public boolean removeGroup(GraphGroup group)

      Removes a group. Its members stay, in no group.

      Returns

      false when the model has no such group