Package com.jgraph.layout.hierarchical.model

Examples of com.jgraph.layout.hierarchical.model.JGraphAbstractHierarchyCell


          for (int j = 0; j < model.ranks.size(); j++) {
            JGraphHierarchyRank rank = (JGraphHierarchyRank) model.ranks
                .get(new Integer(j));
            Iterator iter = rank.iterator();
            while (iter.hasNext()) {
              JGraphAbstractHierarchyCell cell = (JGraphAbstractHierarchyCell) iter
                  .next();
              cell.setX(j, cell.getGeneralPurposeVariable(j));
            }
          }
          bestXDelta = currentXDelta;
        } else {
          // Restore the best positions
          for (int j = 0; j < model.ranks.size(); j++) {
            JGraphHierarchyRank rank = (JGraphHierarchyRank) model.ranks
                .get(new Integer(j));
            Iterator iter = rank.iterator();
            while (iter.hasNext()) {
              JGraphAbstractHierarchyCell cell = (JGraphAbstractHierarchyCell) iter
                  .next();
              cell.setGeneralPurposeVariable(j, (int) cell
                  .getX(j));
            }
          }
        }
        currentXDelta = 0;
View Full Code Here


          .get(new Integer(i));
      rank[i] = rankSet.toArray();
      for (int j = 0; j < rank[i].length; j++) {
        // Use the weight to store the rank and visited to store whether
        // or not the cell is in the list
        JGraphAbstractHierarchyCell cell = (JGraphAbstractHierarchyCell) rank[i][j];
        WeightedCellSorter cellWrapper = new WeightedCellSorter(cell, i);
        cellWrapper.rankIndex = j;
        cellWrapper.visited = true;
        nodeList.add(cellWrapper);
        map.put(cell, cellWrapper);
      }
    }
    // Set a limit of the maximum number of times we will access the queue
    // in case a loop appears
    int maxTries = nodeList.size() * 10;
    int count = 0;
    // Don't move cell within this value of their median
    int tolerance = 1;
    while (!nodeList.isEmpty() && count <= maxTries) {
      WeightedCellSorter cellWrapper = (WeightedCellSorter) nodeList
          .getFirst();
      JGraphAbstractHierarchyCell cell = cellWrapper.cell;
      int rankValue = cellWrapper.weightedValue;
      int rankIndex = cellWrapper.rankIndex;
      Object[] nextLayerConnectedCells = cell.getNextLayerConnectedCells(
          rankValue).toArray();
      Object[] previousLayerConnectedCells = cell
          .getPreviousLayerConnectedCells(rankValue).toArray();
      int numNextLayerConnected = nextLayerConnectedCells.length;
      int numPreviousLayerConnected = previousLayerConnectedCells.length;

      int medianNextLevel = medianXValue(nextLayerConnectedCells,
          rankValue + 1);
      int medianPreviousLevel = medianXValue(previousLayerConnectedCells,
          rankValue - 1);

      int numConnectedNeighbours = numNextLayerConnected
          + numPreviousLayerConnected;
      int currentPosition = cell.getGeneralPurposeVariable(rankValue);
      double cellMedian = currentPosition;
      if (numConnectedNeighbours > 0) {
        cellMedian = (medianNextLevel * numNextLayerConnected + medianPreviousLevel
            * numPreviousLayerConnected)
            / numConnectedNeighbours;
      }

      // Flag storing whether or not position has changed
      boolean positionChanged = false;
      if (cellMedian < currentPosition - tolerance) {
        if (rankIndex == 0) {
          cell.setGeneralPurposeVariable(rankValue, (int) cellMedian);
          positionChanged = true;
        } else {
          JGraphAbstractHierarchyCell leftCell = (JGraphAbstractHierarchyCell) rank[rankValue][rankIndex - 1];
          int leftLimit = leftCell
              .getGeneralPurposeVariable(rankValue);
          leftLimit = leftLimit + (int) leftCell.width / 2
              + (int) intraCellSpacing + (int) cell.width / 2;
          if (leftLimit < cellMedian) {
            cell.setGeneralPurposeVariable(rankValue,
                (int) cellMedian);
            positionChanged = true;
          } else if (leftLimit < cell
              .getGeneralPurposeVariable(rankValue)
              - tolerance) {
            cell.setGeneralPurposeVariable(rankValue, leftLimit);
            positionChanged = true;
          }
        }
      } else if (cellMedian > currentPosition + tolerance) {
        int rankSize = rank[rankValue].length;
        if (rankIndex == rankSize - 1) {
          cell.setGeneralPurposeVariable(rankValue, (int) cellMedian);
          positionChanged = true;
        } else {
          JGraphAbstractHierarchyCell rightCell = (JGraphAbstractHierarchyCell) rank[rankValue][rankIndex + 1];
          int rightLimit = rightCell
              .getGeneralPurposeVariable(rankValue);
          rightLimit = rightLimit - (int) rightCell.width / 2
              - (int) intraCellSpacing - (int) cell.width / 2;
          if (rightLimit > cellMedian) {
            cell.setGeneralPurposeVariable(rankValue,
                (int) cellMedian);
            positionChanged = true;
          } else if (rightLimit > cell
              .getGeneralPurposeVariable(rankValue)
              + tolerance) {
            cell.setGeneralPurposeVariable(rankValue, rightLimit);
            positionChanged = true;
          }
        }
      }
      if (positionChanged) {
        // Add connected nodes to map and list
        for (int i = 0; i < nextLayerConnectedCells.length; i++) {
          JGraphAbstractHierarchyCell connectedCell = (JGraphAbstractHierarchyCell) nextLayerConnectedCells[i];
          WeightedCellSorter connectedCellWrapper = (WeightedCellSorter) map
              .get(connectedCell);
          if (connectedCellWrapper != null) {
            if (connectedCellWrapper.visited == false) {
              connectedCellWrapper.visited = true;
              nodeList.add(connectedCellWrapper);
            }
          }
        }
        // Add connected nodes to map and list
        for (int i = 0; i < previousLayerConnectedCells.length; i++) {
          JGraphAbstractHierarchyCell connectedCell = (JGraphAbstractHierarchyCell) previousLayerConnectedCells[i];
          WeightedCellSorter connectedCellWrapper = (WeightedCellSorter) map
              .get(connectedCell);
          if (connectedCellWrapper != null) {
            if (connectedCellWrapper.visited == false) {
              connectedCellWrapper.visited = true;
View Full Code Here

    // depending on whether we're travelling up or down the hierarchy.
    WeightedCellSorter[] weightedValues = new WeightedCellSorter[rank.length];
    Map cellMap = new Hashtable(rank.length);

    for (int i = 0; i < rank.length; i++) {
      JGraphAbstractHierarchyCell currentCell = (JGraphAbstractHierarchyCell) rank[i];
      weightedValues[i] = new WeightedCellSorter();
      weightedValues[i].cell = currentCell;
      weightedValues[i].rankIndex = i;
      cellMap.put(currentCell, weightedValues[i]);
      Collection nextLayerConnectedCells = null;
      if (nextRankValue < rankValue) {
        nextLayerConnectedCells = currentCell
            .getPreviousLayerConnectedCells(rankValue);
      } else {
        nextLayerConnectedCells = currentCell
            .getNextLayerConnectedCells(rankValue);
      }

      // Calcuate the weighing based on this node type and those this
      // node is connected to on the next layer
      weightedValues[i].weightedValue = calculatedWeightedValue(
          currentCell, nextLayerConnectedCells);
    }

    Arrays.sort(weightedValues);
    // Set the new position of each node within the rank using
    // its temp variable
    for (int i = 0; i < weightedValues.length; i++) {
      int numConnectionsNextLevel = 0;
      JGraphAbstractHierarchyCell cell = weightedValues[i].cell;
      Object[] nextLayerConnectedCells = null;
      int medianNextLevel = 0;

      if (nextRankValue < rankValue) {
        nextLayerConnectedCells = cell.getPreviousLayerConnectedCells(
            rankValue).toArray();
      } else {
        nextLayerConnectedCells = cell.getNextLayerConnectedCells(
            rankValue).toArray();
      }

      if (nextLayerConnectedCells != null) {
        numConnectionsNextLevel = nextLayerConnectedCells.length;
        if (numConnectionsNextLevel > 0) {
          medianNextLevel = medianXValue(nextLayerConnectedCells,
              nextRankValue);
        } else {
          // For case of no connections on the next level set the
          // median to be the current position and try to be
          // positioned there
          medianNextLevel = cell.getGeneralPurposeVariable(rankValue);
        }
      }

      double leftBuffer = 0.0;
      double leftLimit = -100000000.0;
      for (int j = weightedValues[i].rankIndex - 1; j >= 0;) {
        WeightedCellSorter weightedValue = (WeightedCellSorter) cellMap
            .get(rank[j]);
        if (weightedValue != null) {
          JGraphAbstractHierarchyCell leftCell = weightedValue.cell;
          if (weightedValue.visited) {
            // The left limit is the right hand limit of that
            // cell
            // plus any allowance for unallocated cells
            // in-between
            leftLimit = leftCell
                .getGeneralPurposeVariable(rankValue)
                + leftCell.width
                / 2.0
                + intraCellSpacing
                + leftBuffer + cell.width / 2.0;
            ;
            j = -1;
          } else {
            leftBuffer += leftCell.width + intraCellSpacing;
            j--;
          }
        }
      }
      double rightBuffer = 0.0;
      double rightLimit = 100000000.0;
      for (int j = weightedValues[i].rankIndex + 1; j < weightedValues.length;) {
        WeightedCellSorter weightedValue = (WeightedCellSorter) cellMap
            .get(rank[j]);
        if (weightedValue != null) {
          JGraphAbstractHierarchyCell rightCell = weightedValue.cell;
          if (weightedValue.visited) {
            // The left limit is the right hand limit of that
            // cell
            // plus any allowance for unallocated cells
            // in-between
            rightLimit = rightCell
                .getGeneralPurposeVariable(rankValue)
                - rightCell.width
                / 2.0
                - intraCellSpacing
                - rightBuffer - cell.width / 2.0;
View Full Code Here

  private int calculatedWeightedValue(
      JGraphAbstractHierarchyCell currentCell, Collection collection) {
    int totalWeight = 0;
    Iterator iter = collection.iterator();
    while (iter.hasNext()) {
      JGraphAbstractHierarchyCell cell = (JGraphAbstractHierarchyCell) iter
          .next();
      if (currentCell.isVertex() && cell.isVertex()) {
        totalWeight++;
      } else if (currentCell.isEdge() && cell.isEdge()) {
        totalWeight += 8;
      } else {
        totalWeight += 2;
      }
    }
View Full Code Here

    Iterator iter = rank.iterator();
    // Store whether or not any of the cells' bounds were unavailable so
    // to only issue the warning once for all cells
    boolean boundsWarning = false;
    while (iter.hasNext()) {
      JGraphAbstractHierarchyCell cell = (JGraphAbstractHierarchyCell) iter
          .next();
      if (cell.isVertex()) {
        JGraphHierarchyNode node = (JGraphHierarchyNode) cell;
        Rectangle2D bounds = facade.getBounds(node.cell);
        if (bounds != null) {
          if (orientation == SwingConstants.NORTH
              || orientation == SwingConstants.SOUTH) {
            cell.width = bounds.getWidth();
            cell.height = bounds.getHeight();
          } else {
            cell.width = bounds.getHeight();
            cell.height = bounds.getWidth();
          }
        } else {
          boundsWarning = true;
        }
        maxY = Math.max(maxY, cell.height);
      } else if (cell.isEdge()) {
        JGraphHierarchyEdge edge = (JGraphHierarchyEdge) cell;
        // The width is the number of additional parallel edges
        // time the parallel edge spacing
        int numEdges = 1;
        if (edge.edges != null) {
          numEdges = edge.edges.size();
        } else {
          logger.info("edge.edges is null");
        }
        cell.width = (numEdges - 1) * parallelEdgeSpacing;
      }
      // Set the initial x-value as being the best result so far
      localX += cell.width / 2.0;
      cell.setX(rankValue, localX);
      cell.setGeneralPurposeVariable(rankValue, (int) localX);
      localX += cell.width / 2.0;
      localX += localIntraCellSpacing;
    }
    if (boundsWarning == true) {
      logger.info("At least one cell has no bounds");
View Full Code Here

      Iterator iter = rank.iterator();
      // Store whether or not any of the cells' bounds were unavailable so
      // to only issue the warning once for all cells
      boolean boundsWarning = false;
      while (iter.hasNext()) {
        JGraphAbstractHierarchyCell cell = (JGraphAbstractHierarchyCell) iter
            .next();
        if (cell.isVertex()) {
          JGraphHierarchyNode node = (JGraphHierarchyNode) cell;
          Rectangle2D bounds = facade.getBounds(node.cell);
          if (bounds != null) {
            if (orientation == SwingConstants.NORTH
                || orientation == SwingConstants.SOUTH) {
              cell.width = bounds.getWidth();
              cell.height = bounds.getHeight();
            } else {
              cell.width = bounds.getHeight();
              cell.height = bounds.getWidth();
            }
          } else {
            boundsWarning = true;
          }
          maxCellHeight = Math.max(maxCellHeight, cell.height);
        } else if (cell.isEdge()) {
          JGraphHierarchyEdge edge = (JGraphHierarchyEdge) cell;
          // The width is the number of additional parallel edges
          // time the parallel edge spacing
          int numEdges = 1;
          if (edge.edges != null) {
            numEdges = edge.edges.size();
          } else {
            logger.info("edge.edges is null");
          }
          cell.width = (numEdges - 1) * parallelEdgeSpacing;
        }
        // Set the initial x-value as being the best result so far
        localX += cell.width / 2.0;
        cell.setX(rankValue, localX);
        cell.setGeneralPurposeVariable(rankValue, (int) localX);
        localX += cell.width / 2.0;
        localX += intraCellSpacing;
        if (localX > widestRankValue) {
          widestRankValue = localX;
          widestRank = rankValue;
        }
        rankWidths[rankValue] = localX;
      }
      if (boundsWarning == true) {
        logger.info("At least one cell has no bounds");
      }
      rankY[rankValue] = y;
      double distanceToNextRank = maxCellHeight / 2.0
          + lastRankMaxCellHeight / 2.0 + interRankCellSpacing;
      lastRankMaxCellHeight = maxCellHeight;
      if (orientation == SwingConstants.NORTH
          || orientation == SwingConstants.WEST) {
        y += distanceToNextRank;
      } else {
        y -= distanceToNextRank;
      }
      iter = rank.iterator();
      while (iter.hasNext()) {
        JGraphAbstractHierarchyCell cell = (JGraphAbstractHierarchyCell) iter
            .next();
        cell.setY(rankValue, y);
      }
    }
  }
View Full Code Here

      for (int i = 0; i < model.ranks.size(); i++) {
        JGraphHierarchyRank rank = (JGraphHierarchyRank) model.ranks
            .get(new Integer(i));
        Iterator iter = rank.iterator();
        while (iter.hasNext()) {
          JGraphAbstractHierarchyCell cell = (JGraphAbstractHierarchyCell) iter
              .next();
          if (cellType == 0 && cell.isVertex()) {
            JGraphHierarchyNode node = (JGraphHierarchyNode) cell;
            Object realCell = node.cell;
            vertices.add(realCell);
            double positionX = node.x[0] - node.width / 2;
            double positionY = node.y[0] - node.height / 2;
            if (orientation == SwingConstants.NORTH
                || orientation == SwingConstants.SOUTH) {
              facade.setLocation(realCell, positionX, positionY);
            } else {
              facade.setLocation(realCell, positionY, positionX);
            }
            // Stores the positive X limit of this graph context to
            // know where the next context can start
            limitX = Math.max(limitX, positionX + node.width);
            // It is possible that a rank sticks out further in the
            // -ve x direction than the widest rank. In this case
            // store a shift that is required to apply to all cells
            // after positioning is complete.
            if (positionX + 1 < initialX) {
              contextTranslation = initialX - positionX;
            }
          } else if (cellType == 1 && cell.isEdge()) {
            JGraphHierarchyEdge edge = (JGraphHierarchyEdge) cell;
            // For parallel edges we need to seperate out the points
            // a
            // little
            Iterator parallelEdges = edge.edges.iterator();
View Full Code Here

        for (int j = 0; j < nestedBestRanks.length; j++) {
          JGraphHierarchyRank rank = (JGraphHierarchyRank) model.ranks
              .get(new Integer(j));
          Iterator iter = rank.iterator();
          for (int k = 0; k < rank.size(); k++) {
            JGraphAbstractHierarchyCell cell = (JGraphAbstractHierarchyCell) iter
                .next();
            nestedBestRanks[j][cell.getGeneralPurposeVariable(j)] = cell;
          }
        }
      } else {
        // Increase count of iterations where we haven't improved the
        // layout
        iterationsWithoutImprovement++;
        // Restore the best values to the cells
        for (int j = 0; j < nestedBestRanks.length; j++) {
          JGraphHierarchyRank rank = (JGraphHierarchyRank) model.ranks
              .get(new Integer(j));
          Iterator iter = rank.iterator();
          for (int k = 0; k < rank.size(); k++) {
            JGraphAbstractHierarchyCell cell = (JGraphAbstractHierarchyCell) iter
                .next();
            cell.setGeneralPurposeVariable(j, k);
          }
        }
      }
      if (currentBestCrossings == 0) {
        // Do nothing further
        break;
      }
    }

    // Store the best rankings but in the model
    Map ranks = new LinkedHashMap(model.maxRank + 1);
    final Collection[] rankList = new JGraphHierarchyRank[model.maxRank + 1];
    for (int i = 0; i < model.maxRank + 1; i++) {
      rankList[i] = new JGraphHierarchyRank();
      ranks.put(new Integer(i), rankList[i]);
    }

    for (int i = 0; i < nestedBestRanks.length; i++) {
      for (int j = 0; j < nestedBestRanks[i].length; j++) {
        JGraphAbstractHierarchyCell cell = (JGraphAbstractHierarchyCell) nestedBestRanks[i][j];
        rankList[i].add(cell);
      }
    }
    model.ranks = ranks;
View Full Code Here

    int previousRankSize = previousRank.size();
    int[][] connections = new int[currentRankSize][previousRankSize];
    // Iterate over the top rank and fill in the connection information
    Iterator iter = rank.iterator();
    while (iter.hasNext()) {
      JGraphAbstractHierarchyCell cell = (JGraphAbstractHierarchyCell) iter
          .next();
      int rankPosition = cell.getGeneralPurposeVariable(i);
      Collection connectedCells = cell.getPreviousLayerConnectedCells(i);
      Iterator iter2 = connectedCells.iterator();
      while (iter2.hasNext()) {
        JGraphAbstractHierarchyCell connectedCell = (JGraphAbstractHierarchyCell) iter2
            .next();
        int otherCellRankPosition = connectedCell
            .getGeneralPurposeVariable(i - 1);
        if (rankPosition >= currentRankSize
            || otherCellRankPosition >= previousRankSize) {
        }
        connections[rankPosition][otherCellRankPosition] = 201207;
View Full Code Here

            .get(new Integer(i));
        JGraphAbstractHierarchyCell[] orderedCells = new JGraphAbstractHierarchyCell[rank
            .size()];
        Iterator iter = rank.iterator();
        for (int j = 0; j < orderedCells.length; j++) {
          JGraphAbstractHierarchyCell cell = (JGraphAbstractHierarchyCell) iter
              .next();
          orderedCells[cell.getGeneralPurposeVariable(i)] = cell;
        }
        List leftCellAboveConnections = null;
        List leftCellBelowConnections = null;
        List rightCellAboveConnections = null;
        List rightCellBelowConnections = null;
        int[] leftAbovePositions = null;
        int[] leftBelowPositions = null;
        int[] rightAbovePositions = null;
        int[] rightBelowPositions = null;
        JGraphAbstractHierarchyCell leftCell = null;
        JGraphAbstractHierarchyCell rightCell = null;

        for (int j = 0; j < (rank.size() - 1); j++) {
          // For each intra-rank adjacent pair of cells
          // see if swapping them around would reduce the
          // number of edges crossing they cause in total
          // On every cell pair except the first on each rank, we
          // can save processing using the previous values for the
          // right cell on the new left cell
          if (j == 0) {
            leftCell = orderedCells[j];
            leftCellAboveConnections = leftCell
                .getNextLayerConnectedCells(i);
            leftCellBelowConnections = leftCell
                .getPreviousLayerConnectedCells(i);
            leftAbovePositions = new int[leftCellAboveConnections
                .size()];
            leftBelowPositions = new int[leftCellBelowConnections
                .size()];
            for (int k = 0; k < leftAbovePositions.length; k++) {
              leftAbovePositions[k] = ((JGraphAbstractHierarchyCell) leftCellAboveConnections
                  .get(k)).getGeneralPurposeVariable(i + 1);
            }
            for (int k = 0; k < leftBelowPositions.length; k++) {
              leftBelowPositions[k] = ((JGraphAbstractHierarchyCell) leftCellBelowConnections
                  .get(k)).getGeneralPurposeVariable(i - 1);
            }
          } else {
            leftCellAboveConnections = rightCellAboveConnections;
            leftCellBelowConnections = rightCellBelowConnections;
            leftAbovePositions = rightAbovePositions;
            leftBelowPositions = rightBelowPositions;
            leftCell = rightCell;
          }
          rightCell = orderedCells[j + 1];
          rightCellAboveConnections = rightCell
              .getNextLayerConnectedCells(i);
          rightCellBelowConnections = rightCell
              .getPreviousLayerConnectedCells(i);
          rightAbovePositions = new int[rightCellAboveConnections
              .size()];
          rightBelowPositions = new int[rightCellBelowConnections
              .size()];

          for (int k = 0; k < rightAbovePositions.length; k++) {
            rightAbovePositions[k] = ((JGraphAbstractHierarchyCell) rightCellAboveConnections
                .get(k)).getGeneralPurposeVariable(i + 1);
          }
          for (int k = 0; k < rightBelowPositions.length; k++) {
            rightBelowPositions[k] = ((JGraphAbstractHierarchyCell) rightCellBelowConnections
                .get(k)).getGeneralPurposeVariable(i - 1);
          }

          int totalCurrentCrossings = 0;
          int totalSwitchedCrossings = 0;
          for (int k = 0; k < leftAbovePositions.length; k++) {
            for (int ik = 0; ik < rightAbovePositions.length; ik++) {
              if (leftAbovePositions[k] > rightAbovePositions[ik]) {
                totalCurrentCrossings++;
              }
              if (leftAbovePositions[k] < rightAbovePositions[ik]) {
                totalSwitchedCrossings++;
              }
            }
          }
          for (int k = 0; k < leftBelowPositions.length; k++) {
            for (int ik = 0; ik < rightBelowPositions.length; ik++) {
              if (leftBelowPositions[k] > rightBelowPositions[ik]) {
                totalCurrentCrossings++;
              }
              if (leftBelowPositions[k] < rightBelowPositions[ik]) {
                totalSwitchedCrossings++;
              }
            }
          }
          if ((totalSwitchedCrossings < totalCurrentCrossings)
              || (totalSwitchedCrossings == totalCurrentCrossings && nudge)) {
            int temp = leftCell.getGeneralPurposeVariable(i);
            leftCell.setGeneralPurposeVariable(i, rightCell
                .getGeneralPurposeVariable(i));
            rightCell.setGeneralPurposeVariable(i, temp);
            // With this pair exchanged we have to switch all of
            // values for the left cell to the right cell so the
            // next iteration for this rank uses it as the left
            // cell again
            rightCellAboveConnections = leftCellAboveConnections;
View Full Code Here

TOP

Related Classes of com.jgraph.layout.hierarchical.model.JGraphAbstractHierarchyCell

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.