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

The following examples show how to use javax.swing.tree.TreeNode#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: StandardTreePopupMenu.java    From importer-exporter with Apache License 2.0 6 votes vote down vote up
private boolean showAll(TreePath root, TreePath sub, boolean expand) {
	TreeNode node = (TreeNode)sub.getLastPathComponent();
	
	for (int i = 0; i < node.getChildCount(); ++i) {
		TreeNode child = node.getChildAt(i);
		if (child.isLeaf())
			continue;
		
		if (showAll(root, sub.pathByAddingChild(child), expand))
			return true;
	}
	
	if (root == sub)
		return false;
	
	return expand ? tree.isCollapsed(sub) : tree.isExpanded(sub);
}
 
Example 2
Source File: StandardTreePopupMenu.java    From importer-exporter with Apache License 2.0 6 votes vote down vote up
public void prepare(JTree tree, TreePath path) {
	this.tree = tree;
	this.path = path;

	TreeNode node = (TreeNode)path.getLastPathComponent();
	boolean hasNestedChildren = hasNestedChildren(path);		
	boolean isCollapsed = tree.isCollapsed(path);
	boolean isLeaf = node.isLeaf();

	expand.setEnabled(!isLeaf && isCollapsed);
	expandAll.setEnabled(!isLeaf && hasNestedChildren);
	collapse.setEnabled(!isLeaf && !isCollapsed);
	collapseAll.setEnabled(!isLeaf && !isCollapsed && hasNestedChildren);

	expand.setVisible(isLeaf || isCollapsed);
	expandAll.setVisible(!isLeaf && hasNestedChildren && (isCollapsed || showAll(path, path, true)));
	collapse.setVisible(!isLeaf && !isCollapsed);
	collapseAll.setVisible(!isLeaf && !isCollapsed && hasNestedChildren && showAll(path, path, false));
	separator.setVisible((expand.isVisible() || expandAll.isVisible()) && (collapse.isVisible() || collapseAll.isVisible()));
}
 
Example 3
Source File: Model.java    From Luyten with Apache License 2.0 6 votes vote down vote up
@Override
public void treeExpanded(final TreeExpansionEvent event) {
	final TreePath treePath = event.getPath();

	final Object expandedTreePathObject = treePath.getLastPathComponent();
	if (!(expandedTreePathObject instanceof TreeNode)) {
		return;
	}

	final TreeNode expandedTreeNode = (TreeNode) expandedTreePathObject;
	if (expandedTreeNode.getChildCount() == 1) {
		final TreeNode descendantTreeNode = expandedTreeNode.getChildAt(0);

		if (descendantTreeNode.isLeaf()) {
			return;
		}

		final TreePath nextTreePath = treePath.pathByAddingChild(descendantTreeNode);
		tree.expandPath(nextTreePath);
	}
}
 
Example 4
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
private static void searchTree(JTree tree, TreePath path, String q) {
  Object o = path.getLastPathComponent();
  if (o instanceof TreeNode) {
    TreeNode node = (TreeNode) o;
    if (Objects.toString(node).startsWith(q)) {
      tree.expandPath(path.getParentPath());
    }
    if (!node.isLeaf()) {
      // Java 9: Collections.list(node.children())
      Collections.list((Enumeration<?>) node.children())
          .forEach(n -> searchTree(tree, path.pathByAddingChild(n), q));
    }
  }
}
 
Example 5
Source File: XDebuggerTreeSpeedSearch.java    From consulo with Apache License 2.0 5 votes vote down vote up
private TreePath findInChildren(TreeNode node, String string) {
  if (node.isLeaf() || !(node instanceof XDebuggerTreeNode)) {
    return null;
  }

  LinkedList<XDebuggerTreeNode> queue = new LinkedList<XDebuggerTreeNode>();
  queue.addLast((XDebuggerTreeNode)node);

  int initialLevel = ((XDebuggerTreeNode)node).getPath().getPathCount();

  while (!queue.isEmpty()) {
    XDebuggerTreeNode p = queue.removeFirst();

    if ((p.getPath().getPathCount() - initialLevel) > 3) {
      return null;
    }

    List<? extends TreeNode> children = p.getChildren();
    if (children.isEmpty()) {
      continue;
    }

    for (TreeNode child : children) {
      if (!(child instanceof XDebuggerTreeNode)) {
        continue;
      }

      TreePath result = match(child, string);
      if (result != null) {
        return result;
      }

      if (!child.isLeaf()) {
        queue.addLast((XDebuggerTreeNode)child);
      }
    }
  }

  return null;
}
 
Example 6
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
public static void visitAll(JTree tree, TreePath parent, boolean expand) {
  TreeNode node = (TreeNode) parent.getLastPathComponent();
  if (!node.isLeaf()) {
    // Java 9: Collections.list(node.children())
    Collections.list((Enumeration<?>) node.children())
        .forEach(n -> visitAll(tree, parent.pathByAddingChild(n), expand));
  }
  if (expand) {
    tree.expandPath(parent);
  } else {
    tree.collapsePath(parent);
  }
}
 
Example 7
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
private static void collapseAll(JTree tree, TreePath parent) {
  TreeNode node = (TreeNode) parent.getLastPathComponent();
  if (!node.isLeaf()) {
    // Java 9: Collections.list(node.children())
    Collections.list((Enumeration<?>) node.children())
        .forEach(n -> collapseAll(tree, parent.pathByAddingChild(n)));
  }
  tree.collapsePath(parent);
}
 
Example 8
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
private static void searchTree(JTree tree, TreePath path, String q) {
  TreeNode node = (TreeNode) path.getLastPathComponent();
  if (Objects.isNull(node)) {
    return;
  } else if (node.toString().startsWith(q)) {
    tree.expandPath(path.getParentPath());
  }
  if (!node.isLeaf()) {
    // Java 9: Collections.list(node.children())
    Collections.list((Enumeration<?>) node.children())
        .forEach(n -> searchTree(tree, path.pathByAddingChild(n), q));
  }
}
 
Example 9
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
private static void collapseAll(JTree tree, TreePath parent) {
  TreeNode node = (TreeNode) parent.getLastPathComponent();
  if (!node.isLeaf()) {
    // Java 9: Collections.list(node.children())
    Collections.list((Enumeration<?>) node.children())
        .forEach(parent::pathByAddingChild);
  }
  tree.collapsePath(parent);
}
 
Example 10
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
protected static void visitAll(JTree tree, TreePath parent, boolean expand) {
  TreeNode node = (TreeNode) parent.getLastPathComponent();
  if (!node.isLeaf()) {
    // Java 9: Collections.list(node.children())
    Collections.list((Enumeration<?>) node.children())
        .forEach(n -> visitAll(tree, parent.pathByAddingChild(n), expand));
  }
  if (expand) {
    tree.expandPath(parent);
  } else if (tree.isRootVisible() || Objects.nonNull(parent.getParentPath())) {
    tree.collapsePath(parent);
  }
}
 
Example 11
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
@Override public void setSelectedIndex(int index) {
  TreeNode node = getItemAt(index);
  if (Objects.nonNull(node) && node.isLeaf()) {
    super.setSelectedIndex(index);
  } else {
    isNotSelectableIndex = true;
  }
}
 
Example 12
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
private static void makeComboBoxModel(DefaultComboBoxModel<TreeNode> model, TreeNode node) {
  if (node instanceof DefaultMutableTreeNode && !((DefaultMutableTreeNode) node).isRoot()) {
    model.addElement(node);
  }
  if (!node.isLeaf()) {
    // Java 9: Collections.list(node.children()).stream()
    Collections.list((Enumeration<?>) node.children()).stream()
      .filter(TreeNode.class::isInstance).map(TreeNode.class::cast)
      .forEach(n -> makeComboBoxModel(model, n));
  }
}
 
Example 13
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
private static void searchTree(JTree tree, TreePath path, String q) {
  Object o = path.getLastPathComponent();
  if (o instanceof TreeNode) {
    TreeNode node = (TreeNode) o;
    if (node.toString().equals(q)) {
      tree.addSelectionPath(path);
    }
    if (!node.isLeaf()) {
      // Java 9: Collections.list(node.children())
      Collections.list((Enumeration<?>) node.children())
          .forEach(n -> searchTree(tree, path.pathByAddingChild(n), q));
    }
  }
}
 
Example 14
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
private static void searchTree(JTree tree, TreePath path, String q, List<TreePath> rollOverPathLists) {
  Object o = path.getLastPathComponent();
  if (o instanceof TreeNode) {
    TreeNode node = (TreeNode) o;
    if (node.toString().startsWith(q)) {
      rollOverPathLists.add(path);
      tree.expandPath(path.getParentPath());
    }
    if (!node.isLeaf()) {
      // Java 9: Collections.list(node.children())
      Collections.list((Enumeration<?>) node.children())
          .forEach(n -> searchTree(tree, path.pathByAddingChild(n), q, rollOverPathLists));
    }
  }
}
 
Example 15
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
public static void searchTree(JTree tree, TreePath path, String q, List<TreePath> rollOverPathLists) {
  Object o = path.getLastPathComponent();
  if (o instanceof TreeNode) {
    TreeNode node = (TreeNode) o;
    if (node.toString().startsWith(q)) {
      rollOverPathLists.add(path);
      tree.expandPath(path.getParentPath());
    }
    if (!node.isLeaf()) {
      // Java 9: Collections.list(node.children())
      Collections.list((Enumeration<?>) node.children())
          .forEach(n -> searchTree(tree, path.pathByAddingChild(n), q, rollOverPathLists));
    }
  }
}
 
Example 16
Source File: LeafCellEditor.java    From ET_Redux with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param event
 * @return
 */
@Override
public boolean isCellEditable(EventObject event) {
    boolean returnValue = super.isCellEditable(event);
    if (returnValue) {
        Object node = tree.getLastSelectedPathComponent();
        if ((node != null) && (node instanceof TreeNode)) {
            TreeNode treeNode = (TreeNode) node;
            returnValue = treeNode.isLeaf();
        }
    }
    return returnValue;
}
 
Example 17
Source File: TreeUtil.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static TreePath buildTreePath(JTree tree, TreePath parent, String[] nodes, int startdepth, boolean expandable) {
    TreeNode node = (TreeNode) parent.getLastPathComponent();
    String o = node.toString();
    


    // If equal, go down the branch
    if (o.equals(nodes[startdepth])) {
        // If at end, return match
        if (startdepth == nodes.length - 1) {
            return parent;
        }

        // Traverse children
        if (node.getChildCount() >= 0) {
            for (Enumeration e = node.children(); e.hasMoreElements();) {
                TreeNode n = (TreeNode) e.nextElement();     
                    TreePath path = parent.pathByAddingChild(n);
                    if (n.isLeaf() && expandable) {
                        return parent;
                    }
                    TreePath result = buildTreePath(tree, path, nodes, startdepth + 1,expandable);
                    // Found a match
                    if (result != null) {
                        return result;
                    }
                

            }
        }
    }

    // No match at this branch
    return null;
}
 
Example 18
Source File: SearchUtils.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public static boolean findString(ProfilerTable table, String text, boolean matchCase, boolean next) {
    int rowCount = table.getRowCount();
    
    ProfilerTreeTable treeTable = null;
    
    if (rowCount == 0) {
        ProfilerDialogs.displayWarning(MSG_NODATA, ACTION_FIND, null);
        return false;
    } else if (rowCount == 1) {
        if (!(table instanceof ProfilerTreeTable)) return false;
        
        treeTable = (ProfilerTreeTable)table;
        TreeNode node = treeTable.getValueForRow(0);
        if (node == null || node.isLeaf()) return false;
    }
    
    if (treeTable != null || table instanceof ProfilerTreeTable) {
        if (treeTable == null) treeTable = (ProfilerTreeTable)table;
        return findString(treeTable, text, matchCase, next, null);
    } else {
        table.putClientProperty(LAST_FIND_TEXT, text);
        table.putClientProperty(LAST_FIND_MATCH_CASE, matchCase);
        
        if (!matchCase) text = text.toLowerCase();
        
        int mainColumn = table.convertColumnIndexToView(table.getMainColumn());
    
        int selectedRow = table.getSelectedRow();
        boolean fromSelection = selectedRow != -1;
    
        if (!fromSelection) selectedRow = next ? 0 : rowCount - 1;
        else selectedRow = next ? table.getNextRow(selectedRow) :
                                  table.getPreviousRow(selectedRow);
    
        int searchSteps = fromSelection ? rowCount - 1 : rowCount;
        for (int i = 0; i < searchSteps; i++) {
            String value = table.getStringValue(selectedRow, mainColumn);
            if (!matchCase) value = value.toLowerCase();
            if (value.contains(text)) {
                table.selectRow(selectedRow, true);
                return true;
            }
            selectedRow = next ? table.getNextRow(selectedRow) :
                                 table.getPreviousRow(selectedRow);
        }
        
        ProfilerDialogs.displayInfo(MSG_NOTFOUND, ACTION_FIND, null);
        return false;
    }
}
 
Example 19
Source File: SearchUtils.java    From visualvm with GNU General Public License v2.0 4 votes vote down vote up
public static boolean findString(ProfilerTable table, String text, boolean matchCase, boolean next) {
    int rowCount = table.getRowCount();
    
    ProfilerTreeTable treeTable = null;
    
    if (rowCount == 0) {
        ProfilerDialogs.displayWarning(MSG_NODATA, ACTION_FIND, null);
        return false;
    } else if (rowCount == 1) {
        if (!(table instanceof ProfilerTreeTable)) return false;
        
        treeTable = (ProfilerTreeTable)table;
        TreeNode node = treeTable.getValueForRow(0);
        if (node == null || node.isLeaf()) return false;
    }
    
    if (treeTable != null || table instanceof ProfilerTreeTable) {
        if (treeTable == null) treeTable = (ProfilerTreeTable)table;
        return findString(treeTable, text, matchCase, next, null);
    } else {
        table.putClientProperty(LAST_FIND_TEXT, text);
        table.putClientProperty(LAST_FIND_MATCH_CASE, matchCase);
        
        if (!matchCase) text = text.toLowerCase();
        
        int mainColumn = table.convertColumnIndexToView(table.getMainColumn());
    
        int selectedRow = table.getSelectedRow();
        boolean fromSelection = selectedRow != -1;
    
        if (!fromSelection) selectedRow = next ? 0 : rowCount - 1;
        else selectedRow = next ? table.getNextRow(selectedRow) :
                                  table.getPreviousRow(selectedRow);
    
        int searchSteps = fromSelection ? rowCount - 1 : rowCount;
        for (int i = 0; i < searchSteps; i++) {
            String value = table.getStringValue(selectedRow, mainColumn);
            if (!matchCase) value = value.toLowerCase();
            if (value.contains(text)) {
                table.selectRow(selectedRow, true);
                return true;
            }
            selectedRow = next ? table.getNextRow(selectedRow) :
                                 table.getPreviousRow(selectedRow);
        }
        
        ProfilerDialogs.displayInfo(MSG_NOTFOUND, ACTION_FIND, null);
        return false;
    }
}
 
Example 20
Source File: ConfigTreeModel.java    From pentaho-reporting with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Returns <code>true</code> if <code>node</code> is a leaf. It is possible for this method to return
 * <code>false</code> even if <code>node</code> has no children. A directory in a filesystem, for example, may contain
 * no files; the node representing the directory is not a leaf, but it also has no children.
 *
 * @param node a node in the tree, obtained from this data source
 * @return true if <code>node</code> is a leaf
 */
public boolean isLeaf( final Object node ) {
  final TreeNode tnode = (TreeNode) node;
  return tnode.isLeaf();
}