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

The following examples show how to use javax.swing.tree.TreeModel#isLeaf() . 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: ReportDesignerFrame.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
private void insertReports( final TreeModel model,
                            final Object currentLevel,
                            final XulMenupopup popup ) throws XulException {
  final int childCount = model.getChildCount( currentLevel );
  for ( int i = 0; i < childCount; i += 1 ) {
    final ReportDesignerView frame = context.getView();

    final Object child = model.getChild( currentLevel, i );
    if ( model.isLeaf( child ) ) {
      final DefaultMutableTreeNode node = (DefaultMutableTreeNode) child;
      final File file = new File( String.valueOf( node.getUserObject() ) );
      final OpenSampleReportAction action = new OpenSampleReportAction( file, node.toString() );
      action.setReportDesignerContext( context );
      popup.addChild( frame.createMenuItem( action ) );
    } else {
      final XulMenupopup childPopup = frame.createPopupMenu( String.valueOf( child ), popup );
      insertReports( model, child, childPopup );
    }
  }
}
 
Example 2
Source File: TreeExpandCollapse.java    From consulo with Apache License 2.0 6 votes vote down vote up
public int expand(JTree tree, TreePath path) {
  if (myLevelsLeft == 0) return myExpansionLimit;
  TreeModel model = tree.getModel();
  Object node = path.getLastPathComponent();
  int levelDecrement = 0;
  if (!tree.isExpanded(path) && !model.isLeaf(node)) {
    tree.expandPath(path);
    levelDecrement = 1;
    myExpansionLimit--;
  }
  for (int i = 0; i < model.getChildCount(node); i++) {
    Object child = model.getChild(node, i);
    if (model.isLeaf(child)) continue;
    ExpandContext childContext = new ExpandContext(myExpansionLimit, myLevelsLeft - levelDecrement);
    myExpansionLimit = childContext.expand(tree, path.pathByAddingChild(child));
    if (myExpansionLimit <= 0) return 0;
  }
  return myExpansionLimit;
}
 
Example 3
Source File: UIUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Checks give TreePath for the last node, and if it ends with a node with just one child,
 * it keeps expanding further.
 * Current implementation expands through the first child that is not leaf. To more correctly
 * fulfil expected semantics in case maxChildToExpand is > 1, it should expand all paths through
 * all children.
 *
 * @param tree
 * @param path
 * @param maxChildToExpand
 */
public static void autoExpand(JTree tree, TreePath path, int maxLines, int maxChildToExpand, boolean dontExpandToLeafs) {
    TreeModel model = tree.getModel();
    Object node = path.getLastPathComponent();
    TreePath newPath = path;

    int currentLines = 0;

    while (currentLines++ < maxLines &&
            !model.isLeaf(node) &&
            (model.getChildCount(node) > 0) &&
            (model.getChildCount(node) <= maxChildToExpand)) {
        for (int i = 0; i < model.getChildCount(node); i++) {
            node = tree.getModel().getChild(node, i);

            if (!model.isLeaf(node)) {
                if (dontExpandToLeafs && hasOnlyLeafs(tree, node)) {
                    break;
                }

                newPath = newPath.pathByAddingChild(node); // if the leaf is added the path will not expand

                break; // from for
            }
        }
    }

    tree.expandPath(newPath);
}
 
Example 4
Source File: UIUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static boolean hasOnlyLeafs(JTree tree, Object node) {
    TreeModel model = tree.getModel();

    for (int i = 0; i < model.getChildCount(node); i++) {
        if (!model.isLeaf(model.getChild(node, i))) {
            return false;
        }
    }

    return true;
}
 
Example 5
Source File: WebProjectValidation.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void dumpNode(TreeModel model, Object node, int offset) {
    for (int i = 0; i < offset; i++) {
        getRef().print("\t");
    }
    getRef().println(node.toString());
    if (model.isLeaf(node)) {
        return;
    }
    for (int i = 0; i < model.getChildCount(node); i++) {
        Object object = model.getChild(node, i);
        dumpNode(model, object, offset + 1);
    }
}
 
Example 6
Source File: NewProjectTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private ArrayList<String> getChildren(TreeModel tree, Object root, String spaces) {
    int categoriesCount = tree.getChildCount(root);
    ArrayList<String> returnList = new ArrayList<String>();
    for (int i = 0; i <= categoriesCount - 1; i++) {
        Object actualChild = tree.getChild(root, i);
        returnList.add(spaces + actualChild.toString());

        if (!tree.isLeaf(actualChild)) {
            spaces = "+-" + spaces;
            returnList.addAll(getChildren(tree, actualChild, spaces));
            spaces = spaces.substring(2);
        }
    }
    return returnList;
}
 
Example 7
Source File: UIUtils.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
/** Checks give TreePath for the last node, and if it ends with a node with just one child,
 * it keeps expanding further.
 * Current implementation expands through the first child that is not leaf. To more correctly
 * fulfil expected semantics in case maxChildToExpand is > 1, it should expand all paths through
 * all children.
 *
 * @param tree
 * @param path
 * @param maxChildToExpand
 */
public static void autoExpand(JTree tree, TreePath path, int maxLines, int maxChildToExpand, boolean dontExpandToLeafs) {
    TreeModel model = tree.getModel();
    Object node = path.getLastPathComponent();
    TreePath newPath = path;

    int currentLines = 0;

    while (currentLines++ < maxLines &&
            !model.isLeaf(node) &&
            (model.getChildCount(node) > 0) &&
            (model.getChildCount(node) <= maxChildToExpand)) {
        for (int i = 0; i < model.getChildCount(node); i++) {
            node = tree.getModel().getChild(node, i);

            if (!model.isLeaf(node)) {
                if (dontExpandToLeafs && hasOnlyLeafs(tree, node)) {
                    break;
                }

                newPath = newPath.pathByAddingChild(node); // if the leaf is added the path will not expand

                break; // from for
            }
        }
    }

    tree.expandPath(newPath);
}
 
Example 8
Source File: UIUtils.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
public static boolean hasOnlyLeafs(JTree tree, Object node) {
    TreeModel model = tree.getModel();

    for (int i = 0; i < model.getChildCount(node); i++) {
        if (!model.isLeaf(model.getChild(node, i))) {
            return false;
        }
    }

    return true;
}
 
Example 9
Source File: CatalogTree.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
private void getLeaves(Object node, TreeModel model, Set<OpendapLeaf> result) {
    for (int i = 0; i < model.getChildCount(node); i++) {
        if (model.isLeaf(model.getChild(node, i))) {
            final DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) (model.getChild(node, i));
            if (CatalogTreeUtils.isDapNode(childNode) || CatalogTreeUtils.isFileNode(childNode)) {
                result.add((OpendapLeaf) childNode.getUserObject());
            }
        } else {
            getLeaves(model.getChild(node, i), model, result);
        }
    }
}
 
Example 10
Source File: TreeUtil.java    From nextreports-designer with Apache License 2.0 5 votes vote down vote up
/**
 * Expand a tree node and all its child nodes recursively.
 *
 * @param tree The tree whose nodes to expand.
 * @param path Path to the node to start at.
 */
public static void expandAll(JTree tree, TreePath path) {
    Object node = path.getLastPathComponent();
    TreeModel model = tree.getModel();
    if (model.isLeaf(node)) {
        return;
    }
    tree.expandPath(path);
    int num = model.getChildCount(node);
    for (int i = 0; i < num; i++) {
        expandAll(tree, path.pathByAddingChild(model.getChild(node, i)));
    }
}
 
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: TreeUtil.java    From nextreports-designer with Apache License 2.0 5 votes vote down vote up
/**
 * Search for a path in the specified tree model, whose nodes have
 * the same name (compared using <code>equals()</code>)
 * as the ones specified in the old path.
 *
 * @return a new path for the specified model, or null if no such path
 *         could be found.
 */
public static TreePath searchPath(TreeModel model, TreePath oldPath) {
    Object treenode = model.getRoot();
    Object[] oldPathNodes = oldPath.getPath();
    TreePath newPath = new TreePath(treenode);
    for (int i = 0; i < oldPathNodes.length; ++i) {
        Object oldPathNode = oldPathNodes[i];
        if (treenode.toString().equals(oldPathNode.toString())) {
            if (i == (oldPathNodes.length - 1)) {
                return newPath;
            } else {
                if (model.isLeaf(treenode)) {
                    return null; // not found
                } else {
                    int count = model.getChildCount(treenode);
                    boolean foundChild = false;
                    for (int j = 0; j < count; ++j) {
                        Object child = model.getChild(treenode, j);
                        if (child.toString().equals(oldPathNodes[i + 1].toString())) {
                            newPath = newPath.pathByAddingChild(child);
                            treenode = child;
                            foundChild = true;
                            break;
                        }
                    }
                    if (!foundChild) {
                        return null; // couldn't find child with same name
                    }
                }
            }
        }
    }
    return null;
}
 
Example 13
Source File: DNDTree.java    From TrakEM2 with GNU General Public License v3.0 5 votes vote down vote up
static public void expandAllNodes(final JTree tree, final TreePath path) {
	final DefaultMutableTreeNode node = (DefaultMutableTreeNode)path.getLastPathComponent();
	final TreeModel tree_model = tree.getModel();
	if (tree_model.isLeaf(node)) return;
	tree.expandPath(path);
	final int n_children = tree_model.getChildCount(node);
	for (int i=0; i<n_children; i++) {
		expandAllNodes(tree, path.pathByAddingChild(tree_model.getChild(node, i)));
	}
}
 
Example 14
Source File: DNDTree.java    From TrakEM2 with GNU General Public License v3.0 5 votes vote down vote up
static public boolean expandNode(final DNDTree tree, final DefaultMutableTreeNode node) {
	final TreeModel tree_model = tree.getModel();
	if (tree_model.isLeaf(node)) return false;
	tree.expandPath(new TreePath(node.getPath()));
	tree.updateUILater();
	return true;
}