Example #1
0
  private Node readUnit(final Element actualUnitElement, URL url) {
    Node node;
    int xPos = Integer.parseInt(actualUnitElement.getChild("XPos").getValue());
    int yPos = Integer.parseInt(actualUnitElement.getChild("YPos").getValue());
    String label = "";
    if (actualUnitElement.getChild("Label") != null)
      label = actualUnitElement.getChild("Label").getValue();

    System.out.println("Read unit: " + label);

    Element unitDescriptionElement = actualUnitElement.getChild("UnitDescription");
    if (actualUnitElement.getChild("UnitID") != null && unitDescriptionElement != null) {

      int unitID = Integer.parseInt(actualUnitElement.getChild("UnitID").getValue());
      UnitDescription unitDescription = new UnitDescription(url, unitDescriptionElement);

      UnitElement unitElement;

      Element unitsElement = unitDescriptionElement.getChild("Units");
      Element internalConnectionsElement = unitDescriptionElement.getChild("InternalConnections");
      Element externalConnectionsElement = unitDescriptionElement.getChild("ExternalConnections");
      Element originalConnectionsElement = unitDescriptionElement.getChild("OriginalConnections");
      if (internalConnectionsElement != null
          && originalConnectionsElement != null
          && unitsElement != null) {

        /*
         * GroupUnit
         */

        System.out.println("Process group..");

        unitElement = new GroupUnitElement(new Point(xPos, yPos), label);
        HashMap<Integer, UnitElement> embeddedNodes = new HashMap<Integer, UnitElement>();

        unitElement.setHelpString(unitDescription.helpString);
        newNodes.put(unitID, unitElement);
        groupUnits.add((GroupUnitElement) unitElement);

        /*for (int i = 0; i < unitDescription.numInputs; i++) {
        	unitElement.addInput(UnitFactory.createInput(unitDescription.input[i+1], unitElement, i+1));
        }

        for (int i = 0; i < unitDescription.numOutputs; i++) {
        	unitElement.addOutput(UnitFactory.createOutput(unitDescription.output[i+1], unitElement, i+1));
        }*/

        if (unitsElement != null) {

          List<Element> unitsList = unitsElement.getChildren();
          Iterator<Element> unitsIterator = unitsList.iterator();

          // loop over all Units
          while (unitsIterator.hasNext()) {
            Element actualEmbeddedUnitElement = unitsIterator.next();
            Node embeddedNode = readUnit(actualEmbeddedUnitElement, url);
            ((GroupUnitElement) unitElement).getNodes().add(embeddedNode);
            if (embeddedNode instanceof UnitElement) {
              System.out.println(
                  "put in embeddedNodes: "
                      + embeddedNode
                      + " unitid: "
                      + ((UnitElement) embeddedNode).getUnitID());
              newNodes.put(((UnitElement) embeddedNode).getUnitID(), (UnitElement) embeddedNode);
              embeddedNodes.put(
                  ((UnitElement) embeddedNode).getUnitID(), (UnitElement) embeddedNode);
            }
          }
        }

        if (internalConnectionsElement != null) {
          List<Element> connectionsList = internalConnectionsElement.getChildren();
          Iterator<Element> connectionsIterator = connectionsList.iterator();

          // internal connections require the hashmap with all embedded units
          while (connectionsIterator.hasNext()) {
            Element actualConnectionElement = connectionsIterator.next();

            /*Connection connection = readConnection(newNodes, actualConnectionElement);
            ((GroupUnitElement)unitElement).getInternalConnections().add(connection);*/
            ConnectionDelegate conDelegate =
                new ConnectionDelegate(
                    actualConnectionElement,
                    ((GroupUnitElement) unitElement).getInternalConnections());
            connectionDelegates.add(conDelegate);
          }
        }

        if (originalConnectionsElement != null) {
          List<Element> connectionsList = internalConnectionsElement.getChildren();
          Iterator<Element> connectionsIterator = connectionsList.iterator();

          while (connectionsIterator.hasNext()) {
            Element actualConnectionElement = connectionsIterator.next();
            ConnectionDelegate conDelegate =
                new ConnectionDelegate(
                    actualConnectionElement,
                    ((GroupUnitElement) unitElement).getOriginalConnections());
            connectionDelegates.add(conDelegate);
          }
        }

        if (externalConnectionsElement != null) {
          List<Element> connectionsList = externalConnectionsElement.getChildren();
          Iterator<Element> connectionsIterator = connectionsList.iterator();

          // external connections require the hashmap with all embedded and existing units
          // we need all units of the workflow :/
          while (connectionsIterator.hasNext()) {
            Element actualConnectionElement = connectionsIterator.next();
            //						Connection connection = readConnection(embeddedNodes, actualConnectionElement);
            //						((GroupUnitElement)unitElement).getInternalConnections().add(connection);
            ConnectionDelegate conDelegate =
                new ConnectionDelegate(
                    actualConnectionElement,
                    ((GroupUnitElement) unitElement).getExternalConnections());
            connectionDelegates.add(conDelegate);
          }
        }

      } else {
        // create UnitElement
        unitElement = UnitFactory.createProcessingUnit(unitDescription, new Point(xPos, yPos));
        unitElement.setDisplay(unitDescription.getIsDisplayUnit());
        unitElement.setHelpString(unitDescription.helpString);
        unitElement.setLabel(label);
        newNodes.put(unitID, unitElement);
      }

      node = unitElement;

    } else {
      CommentNode comment = new CommentNode(new Point(xPos, yPos), label);
      node = comment;
    }

    return node;
  }