Esempio n. 1
0
  protected TreePath getNextPath(TreePath path) {
    TreeModel model = tree.getModel();

    // return the first node
    if (path == null) {
      return new TreePath(model.getRoot());
    }

    Object lastNode = path.getLastPathComponent();

    // if the NODE has children

    if (model.getChildCount(lastNode) > 0) {
      return path.pathByAddingChild(model.getChild(lastNode, 0));
    }

    // if the NODE has NO children

    int index = 0;
    int pathLength = path.getPathCount();
    Object parentNode = path.getPathComponent(pathLength - 2);

    if (pathLength > 1) {
      index = model.getIndexOfChild(parentNode, lastNode);
    }

    // if there is only root node
    if (pathLength == 1) {
      return path;
    }

    // if there are still some siblings (brothers) after this node
    if (index + 1 < model.getChildCount(parentNode)) {
      // replace the lastPathComponent by its next sibling
      return path.getParentPath().pathByAddingChild(model.getChild(parentNode, index + 1));
    } else {
      while (true) {
        // we need to find next sibling for our father
        path = path.getParentPath();

        // if we get to the end of tree then start if
        if (path.getParentPath() == null) {
          // return the root path
          return path;
        }

        pathLength = path.getPathCount();
        parentNode = path.getPathComponent(pathLength - 2);
        index = model.getIndexOfChild(parentNode, path.getLastPathComponent());

        // if there are still some siblings (brothers) after this node
        if (index + 1 < model.getChildCount(parentNode)) {
          // replace the lastPathComponent by its next sibling
          return path.getParentPath().pathByAddingChild(model.getChild(parentNode, index + 1));
        }
      }
    }
  }
  public int getIndexOfChild(Object parent, Object child) {
    if (visibleNodes == null) {
      return model.getIndexOfChild(parent, child);
    }

    int visibleIndex = 0;
    for (int i = 0, count = model.getChildCount(parent); i < count; i++) {
      Object node = model.getChild(parent, i);
      if (visibleNodes.contains(node)) {
        if (node == child) {
          return visibleIndex;
        }
        visibleIndex++;
      }
    }
    return -1;
  }
Esempio n. 3
0
  protected TreePath getPrevPath(TreePath path) {
    int childCount;
    Object lastNode;
    TreeModel model = tree.getModel();

    // return last component
    if (path == null) {
      path = new TreePath(model.getRoot());

      lastNode = path.getLastPathComponent();

      while ((childCount = model.getChildCount(lastNode)) > 0) {
        // add the last child of the lastPathNode
        path = path.pathByAddingChild(model.getChild(lastNode, childCount - 1));
        lastNode = path.getLastPathComponent();
      }

      return path;
    }

    int index = 0;
    int pathLength = path.getPathCount();
    Object parentNode = null;

    lastNode = path.getLastPathComponent();

    if (pathLength > 1) {
      parentNode = path.getPathComponent(pathLength - 2);
      index = model.getIndexOfChild(parentNode, lastNode);
    }

    // if there are still some siblings (brothers) before this node
    if (index > 0) {
      TreePath siblingPath =
          path.getParentPath().pathByAddingChild(model.getChild(parentNode, index - 1));

      lastNode = siblingPath.getLastPathComponent();

      while ((childCount = model.getChildCount(lastNode)) > 0) {
        siblingPath = siblingPath.pathByAddingChild(model.getChild(lastNode, childCount - 1));
        lastNode = siblingPath.getLastPathComponent();
      }

      // return the siblingPath
      return siblingPath;
    } else {
      TreePath parentPath = path.getParentPath();

      // if there is still some parent
      if (parentPath != null) {
        // return his path
        return parentPath;
      }

      lastNode = path.getLastPathComponent();

      while ((childCount = model.getChildCount(lastNode)) > 0) {
        // add the last child of the lastPathNode
        path = path.pathByAddingChild(model.getChild(lastNode, childCount - 1));
        lastNode = path.getLastPathComponent();
      }

      return path;
    }
  }