Example #1
0
  /**
   * Adds layout options to the elements of the given parent node.
   *
   * @param parentNode parent node representing a graph
   */
  private static void addLayoutOptions(final KNode parentNode) {
    // add options for the parent node
    KShapeLayout parentLayout = parentNode.getData(KShapeLayout.class);
    // set layout direction to horizontal
    parentLayout.setProperty(LayoutOptions.DIRECTION, Direction.RIGHT);

    // add options for the child nodes
    for (KNode childNode : parentNode.getChildren()) {
      KShapeLayout childLayout = childNode.getData(KShapeLayout.class);
      // set some width and height for the child
      childLayout.setWidth(30.0f);
      childLayout.setHeight(30.0f);
      // set fixed size for the child
      // childLayout.setProperty(LayoutOptions.FIXED_SIZE, Boolean.TRUE);
      // set port constraints to fixed port positions
      childLayout.setProperty(LayoutOptions.PORT_CONSTRAINTS, PortConstraints.FIXED_POS);

      // add options for the ports
      int i = 0;
      for (KPort port : childNode.getPorts()) {
        i++;
        KShapeLayout portLayout = port.getData(KShapeLayout.class);
        // set position and side
        portLayout.setYpos(i * 30.0f / (childNode.getPorts().size() + 1));
        if (childNode.getLabels().get(0).getText().equals("node1")) {
          portLayout.setXpos(30.0f);
          portLayout.setProperty(LayoutOptions.PORT_SIDE, PortSide.EAST);
        } else {
          portLayout.setXpos(0.0f);
          portLayout.setProperty(LayoutOptions.PORT_SIDE, PortSide.WEST);
        }
      }
    }
  }
Example #2
0
  /**
   * Creates a KGraph, represented by a parent node that contains the actual nodes and edges of the
   * graph.
   *
   * @return a parent node that contains graph elements
   */
  private static KNode createGraph() {
    // create parent node
    KNode parentNode = KimlUtil.createInitializedNode();

    // create child nodes
    KNode childNode1 = KimlUtil.createInitializedNode();
    // This automatically adds the child to the list of its parent's children.
    childNode1.setParent(parentNode);
    KLabel nodeLabel1 = KimlUtil.createInitializedLabel(childNode1);
    nodeLabel1.setText("node1");
    KNode childNode2 = KimlUtil.createInitializedNode();
    childNode2.setParent(parentNode);
    KLabel nodeLabel2 = KimlUtil.createInitializedLabel(childNode2);
    nodeLabel2.setText("node2");

    // create ports (optional)
    KPort port1 = KimlUtil.createInitializedPort();
    // This automatically adds the port to the node's list of ports.
    port1.setNode(childNode1);
    KPort port2 = KimlUtil.createInitializedPort();
    port2.setNode(childNode2);

    // create edges
    KEdge edge1 = KimlUtil.createInitializedEdge();
    // This automatically adds the edge to the node's list of outgoing edges.
    edge1.setSource(childNode1);
    // This automatically adds the edge to the node's list of incoming edges.
    edge1.setTarget(childNode2);
    // As our ports do not distinguish between incoming and outgoing edges,
    // the edges must be added manually to their list of edges.
    edge1.setSourcePort(port1);
    port1.getEdges().add(edge1);
    edge1.setTargetPort(port2);
    port2.getEdges().add(edge1);

    return parentNode;
  }
  /** {@inheritDoc} */
  @Override
  public void doLayout(final KNode layoutNode, final IKielerProgressMonitor progressMonitor) {
    progressMonitor.begin("Fixed Layout", 1);
    KShapeLayout parentLayout = layoutNode.getData(KShapeLayout.class);
    EdgeRouting edgeRouting = parentLayout.getProperty(LayoutOptions.EDGE_ROUTING);
    float maxx = 0, maxy = 0;

    for (KNode node : layoutNode.getChildren()) {
      KShapeLayout nodeLayout = node.getData(KShapeLayout.class);
      // set the fixed position of the node, or leave it as it is
      KVector pos = nodeLayout.getProperty(LayoutOptions.POSITION);
      if (pos != null) {
        nodeLayout.applyVector(pos);
        // set the fixed size of the node
        // TODO Think about whether this makes sense with the new size constraint options.
        if (nodeLayout
            .getProperty(LayoutOptions.SIZE_CONSTRAINT)
            .contains(SizeConstraint.MINIMUM_SIZE)) {

          float width = nodeLayout.getProperty(LayoutOptions.MIN_WIDTH);
          float height = nodeLayout.getProperty(LayoutOptions.MIN_HEIGHT);
          if (width > 0 && height > 0) {
            KimlUtil.resizeNode(node, width, height, true, true);
          }
        }
      }
      maxx = Math.max(maxx, nodeLayout.getXpos() + nodeLayout.getWidth());
      maxy = Math.max(maxy, nodeLayout.getYpos() + nodeLayout.getHeight());

      // set the fixed position of the node labels, or leave them as they are
      for (KLabel label : node.getLabels()) {
        KShapeLayout labelLayout = label.getData(KShapeLayout.class);
        pos = labelLayout.getProperty(LayoutOptions.POSITION);
        if (pos != null) {
          labelLayout.applyVector(pos);
        }
        maxx =
            Math.max(maxx, nodeLayout.getXpos() + labelLayout.getXpos() + labelLayout.getWidth());
        maxy =
            Math.max(maxy, nodeLayout.getYpos() + labelLayout.getYpos() + labelLayout.getHeight());
      }

      // set the fixed position of the ports, or leave them as they are
      for (KPort port : node.getPorts()) {
        KShapeLayout portLayout = port.getData(KShapeLayout.class);
        pos = portLayout.getProperty(LayoutOptions.POSITION);
        if (pos != null) {
          portLayout.applyVector(pos);
        }
        float portx = nodeLayout.getXpos() + portLayout.getXpos();
        float porty = nodeLayout.getYpos() + portLayout.getYpos();
        maxx = Math.max(maxx, portx + portLayout.getWidth());
        maxy = Math.max(maxy, porty + portLayout.getHeight());

        // set the fixed position of the port labels, or leave them as they are
        for (KLabel label : port.getLabels()) {
          KShapeLayout labelLayout = label.getData(KShapeLayout.class);
          pos = labelLayout.getProperty(LayoutOptions.POSITION);
          if (pos != null) {
            labelLayout.applyVector(pos);
          }
          maxx = Math.max(maxx, portx + labelLayout.getXpos() + labelLayout.getWidth());
          maxy = Math.max(maxy, porty + labelLayout.getYpos() + labelLayout.getHeight());
        }
      }

      // set fixed routing for the connected edges, or leave them as they are
      for (KEdge edge : node.getOutgoingEdges()) {
        KVector maxv = processEdge(edge, edgeRouting);
        maxx = Math.max(maxx, (float) maxv.x);
        maxy = Math.max(maxy, (float) maxv.y);
      }
      for (KEdge edge : node.getIncomingEdges()) {
        if (edge.getSource().getParent() != layoutNode) {
          KVector maxv = processEdge(edge, edgeRouting);
          maxx = Math.max(maxx, (float) maxv.x);
          maxy = Math.max(maxy, (float) maxv.y);
        }
      }
    }

    // if orthogonal routing is selected, determine the junction points
    if (edgeRouting == EdgeRouting.ORTHOGONAL) {
      for (KNode node : layoutNode.getChildren()) {
        for (KEdge edge : node.getOutgoingEdges()) {
          generateJunctionPoints(edge);
        }
      }
    }

    // set size of the parent node
    float borderSpacing = parentLayout.getProperty(LayoutOptions.BORDER_SPACING);
    if (borderSpacing < 0) {
      borderSpacing = DEF_BORDER_SPACING;
    }
    KInsets insets = parentLayout.getInsets();
    float newWidth = maxx + borderSpacing + insets.getLeft() + insets.getRight();
    float newHeight = maxy + borderSpacing + insets.getTop() + insets.getBottom();
    KimlUtil.resizeNode(layoutNode, newWidth, newHeight, true, true);
    progressMonitor.done();
  }