下面列出了javax.swing.tree.TreeModel#getChild ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static void expandPath(JTree tree, TreePath treePath, int currentDepth,
TaskMonitor monitor) throws CancelledException {
if (currentDepth <= 0) {
return;
}
GTreeNode treeNode = (GTreeNode) treePath.getLastPathComponent();
TreeModel treeModel = tree.getModel();
int childCount = treeModel.getChildCount(treeNode);
if (childCount > 0) {
for (int i = 0; i < childCount; i++) {
monitor.checkCanceled();
GTreeNode n = (GTreeNode) treeModel.getChild(treeNode, i);
TreePath path = treePath.pathByAddingChild(n);
expandPath(tree, path, currentDepth - 1, monitor);
}
}
tree.expandPath(treePath);
}
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 );
}
}
}
@Nullable
private static Object findClosestPsiElement(@Nonnull PsiElement element, @Nonnull TreePath adjusted, @Nonnull TreeModel treeModel) {
TextRange range = element.getTextRange();
if (range == null) return null;
Object parent = adjusted.getLastPathComponent();
int minDistance = 0;
Object minChild = null;
for (int i = 0, count = treeModel.getChildCount(parent); i < count; i++) {
Object child = treeModel.getChild(parent, i);
Object value = StructureViewComponent.unwrapValue(child);
if (value instanceof StubBasedPsiElement && ((StubBasedPsiElement)value).getStub() != null) continue;
TextRange r = value instanceof PsiElement ? ((PsiElement)value).getTextRange() : null;
if (r == null) continue;
int distance = TextRangeUtil.getDistance(range, r);
if (minChild == null || distance < minDistance) {
minDistance = distance;
minChild = child;
}
}
return minChild;
}
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;
}
static public void expandPath(JTree tree,
TreePath targetPath,
TreePath tp,
int pos) {
Object[] nodes = tp.getPath();
Object node = targetPath.getLastPathComponent();
Object tpNode = nodes[pos];
if(node.equals(tpNode)) {
tree.expandPath(targetPath);
} else {
return;
}
TreeModel model = tree.getModel();
if(pos < nodes.length - 1) {
int n = model.getChildCount(node);
for(int i = 0; i < n; i++) {
Object child = model.getChild(node, i);
if(child.equals(nodes[pos+1])) {
expandPath(tree, targetPath.pathByAddingChild(child), tp, pos+1);
}
}
}
}
protected void treeModelAsString(TreeModel treeModel, Object parent,
StringBuffer sb, String indentation) {
sb.append(indentation);
int childCount = treeModel.getChildCount(parent);
if (childCount == 0) {
sb.append("- ");
} else {
sb.append("+ ");
indentation = indentation + " ";
}
sb.append(parent);
sb.append("\n");
for (int i = 0; i < childCount; i++) {
Object child = treeModel.getChild(parent, i);
treeModelAsString(treeModel, child, sb, indentation);
}
}
protected void treeModelAsString(TreeModel treeModel, Object parent,
StringBuffer sb, String indentation) {
sb.append(indentation);
int childCount = treeModel.getChildCount(parent);
if (childCount == 0) {
sb.append("- ");
} else {
sb.append("+ ");
indentation = indentation + " ";
}
sb.append(parent);
sb.append("\n");
for (int i = 0; i < childCount; i++) {
Object child = treeModel.getChild(parent, i);
treeModelAsString(treeModel, child, sb, indentation);
}
}
private static void expandTreeNodesRecursive(JTree tree, TreePath parent, boolean expand) {
TreeModel model = tree.getModel();
Object node = parent.getLastPathComponent();
int childCount = model.getChildCount(node);
for (int j = 0; j < childCount; j++) {
Object child = model.getChild(node, j);
TreePath path = parent.pathByAddingChild(child);
expandTreeNodesRecursive(tree, path, expand);
}
if (expand) {
tree.expandPath(parent);
} else {
tree.collapsePath(parent);
}
}
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;
}
void setCurrentSubcategory(String subpath) {
TreeModel mdl = errorTree.getModel();
for (int i = 0; i < mdl.getChildCount(mdl.getRoot()); i++) {
Object child = mdl.getChild(mdl.getRoot(), i);
Object data = ((DefaultMutableTreeNode) child).getUserObject();
if (data instanceof POMErrorFixBase) {
POMErrorFixBase rule = (POMErrorFixBase) data;
if (rule.getConfiguration().getId().equals(subpath)) {
errorTree.setSelectionRow(i);
break;
}
}
}
}
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);
}
}
}
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;
}
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;
}
/**
* Opens all paths in the given node and all nodes below that.
*
* @param path
* the tree path to the node to expand
* @param treeModel
* the tree model
* @see JTree#expandPath(TreePath)
*/
protected void expandAllPaths(TreePath path, TreeModel treeModel) {
expandPath(path);
final Object node = path.getLastPathComponent();
final int n = treeModel.getChildCount(node);
for (int index = 0; index < n; index++) {
final Object child = treeModel.getChild(node, index);
expandAllPaths(path.pathByAddingChild(child));
}
}
public void focusToFirstElement() {
final TreeModel model = this.getModel();
final Object root = model.getRoot();
if (root != null) {
final Object firstChild = model.getChildCount(root) > 0 ? model.getChild(root, 0) : null;
if (firstChild != null) {
this.setSelectionPath(new TreePath(new Object[]{root, firstChild}));
}
}
}
/**
* 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;
}
public static TreePath getTreePathByPathStrings(JTree tree, List<String> pathAsStringList) {
TreeModel model = tree.getModel();
if (model == null) {
return null;
}
Object node = model.getRoot();
if (pathAsStringList.isEmpty()) {
return null;
}
if (!pathAsStringList.get(0).equals(node.toString())) {
return null;
}
List<Object> path = new ArrayList<>();
path.add(node);
for (int i = 1; i < pathAsStringList.size(); i++) {
String name = pathAsStringList.get(i);
int childCount = model.getChildCount(node);
for (int j = 0; j < childCount; j++) {
Object child = model.getChild(node, j);
if (child.toString().equals(name)) {
node = child;
path.add(node);
break;
}
}
}
TreePath tp = new TreePath(path.toArray(new Object[path.size()]));
return tp;
}
private MutableTreeNode getNode(TreeModel model, Object node, OpendapLeaf leaf) {
for (int i = 0; i < model.getChildCount(node); i++) {
final DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) (model.getChild(node, i));
if (childNode.getUserObject() == leaf) {
return childNode;
} else {
final MutableTreeNode temp = getNode(model, model.getChild(node, i), leaf);
if (temp != null) {
return temp;
}
}
}
return null;
}
@NotNull
@Override
protected N getChild ( @NotNull final TreeModel model, @NotNull final N parent, final int index )
{
return ( N ) model.getChild ( parent, index );
}
/**
* Returns the node with the given text below the given node in the specified TreeModel
* @param model
* the TreeModel to search
* @param node
* the node to search below
* @param text
* the text associated with the required node
* @return
* the required node, if found; otherwise null
*/
static Object findChildNode(TreeModel model, Object node, String text) {
for (int i = 0; i < model.getChildCount(node); i++) {
Object currNode = model.getChild(node, i);
if (currNode.toString() != null && currNode.toString().equals(text)) return currNode;
}
return null;
}