Java Code Examples for javax.swing.tree.TreeModel#getChildCount()

The following examples show how to use javax.swing.tree.TreeModel#getChildCount() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProfilerTreeTable.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void collapseChildren(TreePath tpath) {
        if (tree != null) try {
            markExpansionTransaction();
            
            if (tpath == null || tree.isCollapsed(tpath)) return;
            
            TreeModel tmodel = tree.getModel();
            Object selected = tpath.getLastPathComponent();
            
            int nchildren = tmodel.getChildCount(selected);
            for (int i = 0; i < nchildren; i++) {
                TreePath tp = tpath.pathByAddingChild(tmodel.getChild(selected, i));
                tree.collapsePath(tp);
//                tree.resetExpandedState(tp);
            }
        
        } finally {
            clearExpansionTransaction();
        }
    }
 
Example 2
Source File: ProfilerTreeTable.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void expandFirstPath(int row) {
    if (tree != null) try {
        markExpansionTransaction();

        TreePath tpath = tree.getPathForRow(row);
        if (tpath == null) return;

        TreeModel tmodel = tree.getModel();    

        while (tmodel.getChildCount(tpath.getLastPathComponent()) > 0)
            tpath = tpath.pathByAddingChild(tmodel.getChild(tpath.getLastPathComponent(), 0));

        tree.putClientProperty(UIUtils.PROP_AUTO_EXPANDING, Boolean.TRUE);
        try { selectPath(tpath, true); }
        finally { tree.putClientProperty(UIUtils.PROP_AUTO_EXPANDING, null); }

    } finally {
        clearExpansionTransaction();
    }
}
 
Example 3
Source File: ProfilerTreeTable.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void expandPlainPath(int row, int maxChildren) {
    if (tree != null) try {
        markExpansionTransaction();
        
        TreePath tpath = tree.getPathForRow(row);
        if (tpath == null) return;
        
        TreeModel tmodel = tree.getModel();            
        int childCount = tmodel.getChildCount(tpath.getLastPathComponent());
    
        while (childCount > 0 && childCount <= maxChildren) {
            tpath = tpath.pathByAddingChild(tmodel.getChild(tpath.getLastPathComponent(), 0));
            childCount = tmodel.getChildCount(tpath.getLastPathComponent());
        }

        tree.putClientProperty(UIUtils.PROP_AUTO_EXPANDING, Boolean.TRUE);
        try { tree.expandPath(tpath); selectPath(tpath, true); }
        finally { tree.putClientProperty(UIUtils.PROP_AUTO_EXPANDING, null); }
        
    } finally {
        clearExpansionTransaction();
    }
}
 
Example 4
Source File: ElementTreePanel.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Updates the tree based on the event type. This will invoke either
 * updateTree with the root element, or handleChange.
 */
protected void updateTree(DocumentEvent event) {
    updatingSelection = true;
    try {
        TreeModel model = getTreeModel();
        Object root = model.getRoot();

        for (int counter = model.getChildCount(root) - 1; counter >= 0;
                counter--) {
            updateTree(event, (Element) model.getChild(root, counter));
        }
    } finally {
        updatingSelection = false;
    }
}
 
Example 5
Source File: ElementTreePanel.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Updates the tree based on the event type. This will invoke either
 * updateTree with the root element, or handleChange.
 */
protected void updateTree(DocumentEvent event) {
    updatingSelection = true;
    try {
        TreeModel model = getTreeModel();
        Object root = model.getRoot();

        for (int counter = model.getChildCount(root) - 1; counter >= 0;
                counter--) {
            updateTree(event, (Element) model.getChild(root, counter));
        }
    } finally {
        updatingSelection = false;
    }
}
 
Example 6
Source File: ProfilerTreeTable.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
private TreePath getSimilarPath(TreePath oldPath) {
    if (oldPath == null || oldPath.getPathCount() < 1) return null;

    TreeModel currentModel = getModel();
    Object currentRoot = currentModel.getRoot();
    if (!currentRoot.equals(oldPath.getPathComponent(0))) return null;

    TreePath p = new TreePath(currentRoot);
    Object[] op = oldPath.getPath();
    Object n = currentRoot;

    for (int i = 1; i < op.length; i++) {
        Object nn = null;

        for (int ii = 0; ii < currentModel.getChildCount(n); ii++) {
            Object c = currentModel.getChild(n, ii);
            if (c.equals(op[i])) {
                nn = c;
                break;
            }
        }

        if (nn == null) return null;

        n = nn;
        p = p.pathByAddingChild(n);
    }

    return p;
}
 
Example 7
Source File: ProjectViewTestUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static DefaultMutableTreeNode getNodeForElement(Object root, TreeModel model, PsiElement element) {
  if (root instanceof DefaultMutableTreeNode) {
    Object userObject = ((DefaultMutableTreeNode)root).getUserObject();
    if (userObject instanceof AbstractTreeNode) {
      AbstractTreeNode treeNode = (AbstractTreeNode)userObject;
      if (element.equals(treeNode.getValue())) return (DefaultMutableTreeNode)root;
      for (int i = 0; i < model.getChildCount(root); i++) {
        DefaultMutableTreeNode nodeForChild = getNodeForElement(model.getChild(root, i), model, element);
        if (nodeForChild != null) return nodeForChild;
      }
    }
  }
  return null;
}
 
Example 8
Source File: JTreeJavaElement.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
private TreePath getPath(JTree tree, String path) {
    String[] tokens = path.substring(1).split("(?<!\\\\)/");
    TreeModel treeModel = tree.getModel();
    if (treeModel == null) {
        throw new RuntimeException("Could not find model for tree");
    }
    Object rootNode = treeModel.getRoot();
    int start = tree.isRootVisible() ? 1 : 0;
    TreePath treePath = new TreePath(rootNode);
    StringBuilder searchedPath = new StringBuilder();
    if (tree.isRootVisible()) {
        String rootNodeText = unescapeSpecialCharacters(tokens[0]);
        searchedPath.append("/" + rootNodeText);
        assertTrue("JTree does not have a root node!", rootNode != null);
        assertTrue("JTree root node does not match: Expected </" + getPathText(tree, treePath) + "> Actual: <"
                + searchedPath.toString() + ">", searchedPath.toString().equals("/" + getPathText(tree, treePath)));
    }
    for (int i = start; i < tokens.length; i++) {
        String childText = unescapeSpecialCharacters(tokens[i]);
        searchedPath.append("/" + childText);
        boolean matched = false;
        tree.expandPath(treePath);
        for (int j = 0; j < treeModel.getChildCount(treePath.getLastPathComponent()); j++) {
            Object child = treeModel.getChild(treePath.getLastPathComponent(), j);
            TreePath childPath = treePath.pathByAddingChild(child);
            if (childText.equals(getPathText(tree, childPath))) {
                treePath = childPath;
                matched = true;
                break;
            }
        }
        if (!matched) {
            return null;
        }
    }
    return treePath;
}
 
Example 9
Source File: ProfilerTreeTable.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
private static TreePath getSimilarPath(TreePath oldPath, TreeModel currentModel) {
        if (oldPath == null || oldPath.getPathCount() < 1) return null;

//        TreeModel currentModel = getModel();
        Object currentRoot = currentModel.getRoot();
        if (!currentRoot.equals(oldPath.getPathComponent(0))) return null;

        TreePath p = new TreePath(currentRoot);
        Object[] op = oldPath.getPath();
        Object n = currentRoot;

        for (int i = 1; i < op.length; i++) {
            Object nn = null;

            for (int ii = 0; ii < currentModel.getChildCount(n); ii++) {
                Object c = currentModel.getChild(n, ii);
                if (c.equals(op[i])) {
                    nn = c;
                    break;
                }
            }

            if (nn == null) return null;

            n = nn;
            p = p.pathByAddingChild(n);
        }
//        System.err.println(">>> Similar path for " + oldPath + " is " + p);
        return p;
    }
 
Example 10
Source File: ElementTreePanel.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Updates the tree based on the event type. This will invoke either
 * updateTree with the root element, or handleChange.
 */
protected void updateTree(DocumentEvent event) {
    updatingSelection = true;
    try {
        TreeModel model = getTreeModel();
        Object root = model.getRoot();

        for (int counter = model.getChildCount(root) - 1; counter >= 0;
                counter--) {
            updateTree(event, (Element) model.getChild(root, counter));
        }
    } finally {
        updatingSelection = false;
    }
}
 
Example 11
Source File: TreeUtil.java    From nextreports-designer with Apache License 2.0 5 votes vote down vote up
/**
 * Collapse a tree node and all its child nodes recursively.
 *
 * @param tree The tree whose nodes to collapse.
 * @param path Path to the node to start at.
 */
public static void collapseAll(JTree tree, TreePath path) {
    Object node = path.getLastPathComponent();
    TreeModel model = tree.getModel();
    if (model.isLeaf(node)) {
        return;
    }
    int num = model.getChildCount(node);
    for (int i = 0; i < num; i++) {
        collapseAll(tree, path.pathByAddingChild(model.getChild(node, i)));
    }
    tree.collapsePath(path);
}
 
Example 12
Source File: GUIBrowser.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private void expandAll(JTree tree, TreePath path) {
    tree.expandPath(path);
    TreeModel model = tree.getModel();
    Object lastComponent = path.getLastPathComponent();
    for (int i = 0; i < model.getChildCount(lastComponent); i++) {
        expandAll(tree,
                path.pathByAddingChild(model.getChild(lastComponent, i)));
    }
}
 
Example 13
Source File: ElementTreePanel.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Messaged when the selection in the editor has changed. Will update
 * the selection in the tree.
 */
public void caretUpdate(CaretEvent e) {
    if (!updatingSelection) {
        int selBegin = Math.min(e.getDot(), e.getMark());
        int end = Math.max(e.getDot(), e.getMark());
        List<TreePath> paths = new ArrayList<TreePath>();
        TreeModel model = getTreeModel();
        Object root = model.getRoot();
        int rootCount = model.getChildCount(root);

        // Build an array of all the paths to all the character elements
        // in the selection.
        for (int counter = 0; counter < rootCount; counter++) {
            int start = selBegin;

            while (start <= end) {
                TreePath path = getPathForIndex(start, root,
                        (Element) model.getChild(root, counter));
                Element charElement = (Element) path.getLastPathComponent();

                paths.add(path);
                if (start >= charElement.getEndOffset()) {
                    start++;
                } else {
                    start = charElement.getEndOffset();
                }
            }
        }

        // If a path was found, select it (them).
        int numPaths = paths.size();

        if (numPaths > 0) {
            TreePath[] pathArray = new TreePath[numPaths];

            paths.toArray(pathArray);
            updatingSelection = true;
            try {
                getTree().setSelectionPaths(pathArray);
                getTree().scrollPathToVisible(pathArray[0]);
            } finally {
                updatingSelection = false;
            }
        }
    }
}
 
Example 14
Source File: ElementTreePanel.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Messaged when the selection in the editor has changed. Will update
 * the selection in the tree.
 */
public void caretUpdate(CaretEvent e) {
    if (!updatingSelection) {
        int selBegin = Math.min(e.getDot(), e.getMark());
        int end = Math.max(e.getDot(), e.getMark());
        List<TreePath> paths = new ArrayList<TreePath>();
        TreeModel model = getTreeModel();
        Object root = model.getRoot();
        int rootCount = model.getChildCount(root);

        // Build an array of all the paths to all the character elements
        // in the selection.
        for (int counter = 0; counter < rootCount; counter++) {
            int start = selBegin;

            while (start <= end) {
                TreePath path = getPathForIndex(start, root,
                        (Element) model.getChild(root, counter));
                Element charElement = (Element) path.getLastPathComponent();

                paths.add(path);
                if (start >= charElement.getEndOffset()) {
                    start++;
                } else {
                    start = charElement.getEndOffset();
                }
            }
        }

        // If a path was found, select it (them).
        int numPaths = paths.size();

        if (numPaths > 0) {
            TreePath[] pathArray = new TreePath[numPaths];

            paths.toArray(pathArray);
            updatingSelection = true;
            try {
                getTree().setSelectionPaths(pathArray);
                getTree().scrollPathToVisible(pathArray[0]);
            } finally {
                updatingSelection = false;
            }
        }
    }
}
 
Example 15
Source File: ElementTreePanel.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Messaged when the selection in the editor has changed. Will update
 * the selection in the tree.
 */
public void caretUpdate(CaretEvent e) {
    if (!updatingSelection) {
        int selBegin = Math.min(e.getDot(), e.getMark());
        int end = Math.max(e.getDot(), e.getMark());
        List<TreePath> paths = new ArrayList<TreePath>();
        TreeModel model = getTreeModel();
        Object root = model.getRoot();
        int rootCount = model.getChildCount(root);

        // Build an array of all the paths to all the character elements
        // in the selection.
        for (int counter = 0; counter < rootCount; counter++) {
            int start = selBegin;

            while (start <= end) {
                TreePath path = getPathForIndex(start, root,
                        (Element) model.getChild(root, counter));
                Element charElement = (Element) path.getLastPathComponent();

                paths.add(path);
                if (start >= charElement.getEndOffset()) {
                    start++;
                } else {
                    start = charElement.getEndOffset();
                }
            }
        }

        // If a path was found, select it (them).
        int numPaths = paths.size();

        if (numPaths > 0) {
            TreePath[] pathArray = new TreePath[numPaths];

            paths.toArray(pathArray);
            updatingSelection = true;
            try {
                getTree().setSelectionPaths(pathArray);
                getTree().scrollPathToVisible(pathArray[0]);
            } finally {
                updatingSelection = false;
            }
        }
    }
}
 
Example 16
Source File: TreeDropLocationPainter.java    From weblaf with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Returns drop BETWEEN view bounds.
 *
 * @param location {@link javax.swing.JTree.DropLocation}
 * @param model    {@link TreeModel}
 * @param path     parent {@link TreePath}
 * @param index    drop index at parent {@link TreePath}
 * @return drop BETWEEN view bounds
 */
@Nullable
protected Rectangle getDropBetweenViewBounds ( @NotNull final JTree.DropLocation location, @NotNull final TreeModel model,
                                               @NotNull final TreePath path, final int index )
{
    final Object parent = path.getLastPathComponent ();
    final int childCount = model.getChildCount ( parent );

    // Retrieving actual child index
    final int actualIndex;
    final boolean atStart;
    if ( index < childCount )
    {
        // Simply using child at [index] as it exists
        actualIndex = index;
        atStart = true;
    }
    else
    {
        // Using child at [childCount-1], it should be the same as [index-1] but safer
        actualIndex = childCount - 1;
        atStart = false;
    }

    // Retrieving child bounds
    final Object child = model.getChild ( parent, actualIndex );
    final TreePath childPath = path.pathByAddingChild ( child );
    final Rectangle bounds = component.getPathBounds ( childPath );

    // Adjusting drop indicator bounds
    if ( bounds != null )
    {
        // Adjusting for RTL
        final Dimension ps = getPreferredSize ();
        if ( !ltr )
        {
            bounds.x = bounds.x + bounds.width - ps.width;
        }

        // Adjusting to start or end of the node bounds
        if ( atStart )
        {
            bounds.y -= ps.height / 2;
        }
        else
        {
            bounds.y += bounds.height - ps.height / 2;
        }

        // Simply using preferred width and height
        // We want preferred sizes to avoid painting extremely long or wide location indicator
        bounds.width = ps.width;
        bounds.height = ps.height;
    }

    return bounds;
}
 
Example 17
Source File: InspectorTreeUI.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected void paintVerticalPartOfLeg(final Graphics g, final Rectangle clipBounds, final Insets insets, final TreePath path) {
  final DefaultMutableTreeNode node = (DefaultMutableTreeNode)path.getLastPathComponent();
  if (node.getChildCount() < 2) {
    // We could draw lines for nodes with a single child but we omit them
    // to more of an emphasis of lines for nodes with multiple children.
    return;
  }
  final DiagnosticsNode diagnostic = maybeGetDiagnostic(node);
  if (diagnostic != null && !diagnostic.hasChildren()) {
    // This avoids drawing lines for nodes with only property children.
    return;
  }

  final int depth = path.getPathCount() - 1;
  if (depth == 0 && !getShowsRootHandles() && !isRootVisible()) {
    return;
  }

  int lineX = getRowX(-1, depth);
  if (leftToRight) {
    lineX = lineX - getRightChildIndent() + insets.left;
  }
  else {
    lineX = tree.getWidth() - lineX - insets.right +
            getRightChildIndent() - 1;
  }
  final int clipLeft = clipBounds.x;
  final int clipRight = clipBounds.x + (clipBounds.width - 1);

  if (lineX >= clipLeft && lineX <= clipRight) {
    final int clipTop = clipBounds.y;
    final int clipBottom = clipBounds.y + clipBounds.height;
    Rectangle parentBounds = getPathBounds(tree, path);
    boolean previousDashed = false;

    int top;
    if (parentBounds == null) {
      top = Math.max(insets.top + getVerticalLegBuffer(),
                     clipTop);
    }
    else {
      top = Math.max(parentBounds.y + parentBounds.height +
                     getVerticalLegBuffer(), clipTop);
    }

    if (depth == 0 && !isRootVisible()) {
      final TreeModel model = getModel();

      if (model != null) {
        final Object root = model.getRoot();

        if (model.getChildCount(root) > 0) {
          parentBounds = getPathBounds(tree, path.
            pathByAddingChild(model.getChild(root, 0)));
          if (parentBounds != null) {
            top = Math.max(insets.top + getVerticalLegBuffer(),
                           parentBounds.y +
                           parentBounds.height / 2);
          }
        }
      }
    }

    for (int i = 0; i < node.getChildCount(); ++i) {
      final DefaultMutableTreeNode child = (DefaultMutableTreeNode)node.getChildAt(i);
      final DiagnosticsNode childDiagnostic = maybeGetDiagnostic(child);
      boolean dashed = false;
      if (childDiagnostic != null) {
        dashed = childDiagnostic.getStyle() == DiagnosticsTreeStyle.offstage;
      }

      final Rectangle childBounds = getPathBounds(tree, path.pathByAddingChild(child));
      if (childBounds == null)
      // This shouldn't happen, but if the model is modified
      // in another thread it is possible for this to happen.
      // Swing isn't multithreaded, but I'll add this check in
      // anyway.
      {
        continue;
      }

      final int bottom = Math.min(childBounds.y +
                                  (childBounds.height / 2), clipBottom);

      if (top <= bottom && bottom >= clipTop && top <= clipBottom) {
        g.setColor(JBColor.GRAY);
        paintVerticalLine(g, tree, lineX, top, bottom, dashed);
      }
      top = bottom;
      previousDashed = dashed;
    }
  }
}
 
Example 18
Source File: ElementTreePanel.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Messaged when the selection in the editor has changed. Will update
 * the selection in the tree.
 */
public void caretUpdate(CaretEvent e) {
    if (!updatingSelection) {
        int selBegin = Math.min(e.getDot(), e.getMark());
        int end = Math.max(e.getDot(), e.getMark());
        List<TreePath> paths = new ArrayList<TreePath>();
        TreeModel model = getTreeModel();
        Object root = model.getRoot();
        int rootCount = model.getChildCount(root);

        // Build an array of all the paths to all the character elements
        // in the selection.
        for (int counter = 0; counter < rootCount; counter++) {
            int start = selBegin;

            while (start <= end) {
                TreePath path = getPathForIndex(start, root,
                        (Element) model.getChild(root, counter));
                Element charElement = (Element) path.getLastPathComponent();

                paths.add(path);
                if (start >= charElement.getEndOffset()) {
                    start++;
                } else {
                    start = charElement.getEndOffset();
                }
            }
        }

        // If a path was found, select it (them).
        int numPaths = paths.size();

        if (numPaths > 0) {
            TreePath[] pathArray = new TreePath[numPaths];

            paths.toArray(pathArray);
            updatingSelection = true;
            try {
                getTree().setSelectionPaths(pathArray);
                getTree().scrollPathToVisible(pathArray[0]);
            } finally {
                updatingSelection = false;
            }
        }
    }
}
 
Example 19
Source File: SimpleTreeWalker.java    From weblaf with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected int getChildCount ( @NotNull final TreeModel model, @NotNull final N parent )
{
    return model.getChildCount ( parent );
}
 
Example 20
Source File: ElementTreePanel.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Messaged when the selection in the editor has changed. Will update
 * the selection in the tree.
 */
public void caretUpdate(CaretEvent e) {
    if (!updatingSelection) {
        int selBegin = Math.min(e.getDot(), e.getMark());
        int end = Math.max(e.getDot(), e.getMark());
        List<TreePath> paths = new ArrayList<TreePath>();
        TreeModel model = getTreeModel();
        Object root = model.getRoot();
        int rootCount = model.getChildCount(root);

        // Build an array of all the paths to all the character elements
        // in the selection.
        for (int counter = 0; counter < rootCount; counter++) {
            int start = selBegin;

            while (start <= end) {
                TreePath path = getPathForIndex(start, root,
                        (Element) model.getChild(root, counter));
                Element charElement = (Element) path.getLastPathComponent();

                paths.add(path);
                if (start >= charElement.getEndOffset()) {
                    start++;
                } else {
                    start = charElement.getEndOffset();
                }
            }
        }

        // If a path was found, select it (them).
        int numPaths = paths.size();

        if (numPaths > 0) {
            TreePath[] pathArray = new TreePath[numPaths];

            paths.toArray(pathArray);
            updatingSelection = true;
            try {
                getTree().setSelectionPaths(pathArray);
                getTree().scrollPathToVisible(pathArray[0]);
            } finally {
                updatingSelection = false;
            }
        }
    }
}