下面列出了javax.swing.tree.TreeModel#isLeaf ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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 );
}
}
}
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;
}
/** 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);
}
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;
}
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);
}
}
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;
}
/** 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);
}
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;
}
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);
}
}
}
/**
* 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)));
}
}
/**
* 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);
}
/**
* 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;
}
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)));
}
}
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;
}