Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

Port constraint



It is possible to add Port constraints on an edge. These constraints allow to specify the position of the edge relative to target or the source GraphMLNode of the edge. For example:
  • Setting the position of the port for the source of an edge relative to the source node sides
  • Setting the position of the port for the target of an edge relative to the target node sides

Note that these constaints will only be applied by yEd after an automatic layout. yEd will try to satisfy all the constraints in the diagram if it is possible.


For example in the following diagram you may have a port constraint on the edge between node1 to node2 specifying that the side of the port on the source node is South and the side of the port on the target port is North:
      GraphmlFactory factory = GraphmlFactory.getInstance();
      GraphmlDiagram diagram = factory.newDiagram();

      GraphMLNode node1 = diagram.addNodeWithLabel("node1");
      GraphMLNode node2 = diagram.addNodeWithLabel("node2");

      GraphMLEdge edge = diagram.addEdge(node1, node2);
      edge.setPortConstraint(PortConstraints.SOURCE, PortConstraints.SOUTH); // the edge comes from the South side of node1 (bottom)
      edge.setPortConstraint(PortConstraints.TARGET, PortConstraints.NORTH);  // the edge goes to the North side of node2 (top)


portconstraint

Access the constraints on an edge

The constraints of an edge are defined in the PortConstraints class. The constraints on an edge are retrieved through the GraphMLEdge.getPortConstraints() method.

Setting a constraint on a GraphMLEdge

Setting a constraint on the PortConstraints associated with the edge

To set a constraint on an edge, you can use the PortConstraints.setPortConstraint(short, short) method:
  • The first argument specifies if the constraint is applied on the source or the target of the edge
  • The second argument specifies on which side the edge must be put

The PortConstraints.setPortConstraint(String, String) method does the same thing, but allows to use String values rather than enumerates for the arguments.


For example, here we specify that the edge origin comes from the SOUTH side of the node origin to the north side of the node destination:
      GraphmlFactory factory = GraphmlFactory.getInstance();
      GraphmlDiagram diagram = factory.newDiagram();

      GraphMLNode node1 = diagram.addNode();
      GraphMLNode node2 = diagram.addNode();

      GraphMLEdge edge = diagram.addEdge(node1, node2);
      PortConstraints constraints = edge.getPortConstraints();
      constraints.setPortConstraint(PortConstraints.SOURCE, PortConstraints.SOUTH); // the edge comes from the South side of node1 (bottom)
      constraints.setPortConstraint(PortConstraints.TARGET, PortConstraints.NORTH);  // the edge goes to the North side of node2 (top)
Or using Strings:
      GraphmlFactory factory = GraphmlFactory.getInstance();
      GraphmlDiagram diagram = factory.newDiagram();

      GraphMLNode node1 = diagram.addNode();
      GraphMLNode node2 = diagram.addNode();

      GraphMLEdge edge = diagram.addEdge(node1, node2);
      PortConstraints constraints = edge.getPortConstraints();
      constraints.setPortConstraint("source", "south"); // the edge comes from the South side of node1 (bottom)
      constraints.setPortConstraint("target", "north");  // the edge goes to the North side of node2 (top)

Setting a constraint directly on the edge

The methods which apply on the PortConstraints class can also be applied directly on the GraphMLEdge class. You can use the following methods:

For example, here we specify that the edge origin comes from the SOUTH side of the node origin to the north side of the node destination:
      GraphmlFactory factory = GraphmlFactory.getInstance();
      GraphmlDiagram diagram = factory.newDiagram();

      GraphMLNode node1 = diagram.addNode();
      GraphMLNode node2 = diagram.addNode();

      GraphMLEdge edge = diagram.addEdge(node1, node2);
      edge.setPortConstraint(PortConstraints.SOURCE, PortConstraints.SOUTH); // the edge comes from the South side of node1 (bottom)
      edge.setPortConstraint(PortConstraints.TARGET, PortConstraints.NORTH);  // the edge goes to the North side of node2 (top)
Or using Strings:
      GraphmlFactory factory = GraphmlFactory.getInstance();
      GraphmlDiagram diagram = factory.newDiagram();

      GraphMLNode node1 = diagram.addNode();
      GraphMLNode node2 = diagram.addNode();

      GraphMLEdge edge = diagram.addEdge(node1, node2);
      edge.setPortConstraint("source", "south"); // the edge comes from the South side of node1 (bottom)
      edge.setPortConstraint("target", "north");  // the edge goes to the North side of node2 (top)

Setting the constraints on all the edges on a GraphMLNode

It is possible to set the constraints on all the edges of a node through the following methods:

For example:
      GraphmlFactory factory = GraphmlFactory.getInstance();
      GraphmlDiagram diagram = factory.newDiagram();

      GraphMLNode node1 = diagram.addNode();
      GraphMLNode node2 = diagram.addNode();

      GraphMLEdge edge = diagram.addEdge(node1, node2);
      node1.setFromPortConstraint(PortConstraints.SOUTH); // set all edges originating from this node as coming from the South side of node (bottom)
      node2.setToPortConstraint(PortConstraints.NORTH); // set all edges originating from another node as going to the North side of node (top)

See also


Categories: edges

Copyright 2021 Herve Girod. All Rights Reserved. Documentation and source under the BSD 3-Clause licence