Java Code Examples for javax.swing.tree.TreePath#isDescendant()

The following examples show how to use javax.swing.tree.TreePath#isDescendant() . 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: GTreeState.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private List<TreePath> getSelectionPaths(GTreeNode node) {
	TreePath[] allSelectionPaths = tree.getSelectionPaths();
	if (node == tree.getViewRoot()) {
		return CollectionUtils.asList(allSelectionPaths);
	}
	if (allSelectionPaths == null) {
		return Collections.emptyList();
	}
	TreePath nodeTreePath = node.getTreePath();
	List<TreePath> pathList = new ArrayList<>();
	for (TreePath path : allSelectionPaths) {
		if (nodeTreePath.isDescendant(path)) {
			pathList.add(nodeTreePath);
		}
	}
	return pathList;
}
 
Example 2
Source File: Node.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * implementation of JTreeOperator.TreePathChooser
 *
 * @param path TreePath
 * @param indexInParent int
 * @return boolean
 */
@Override
public boolean hasAsParent(TreePath path, int indexInParent) {
    if (path.getPathCount() <= parentPathCount) {
        return path.isDescendant(parentPath);
    }
    if (arr.length + parentPathCount < path.getPathCount()) {
        return (false);
    }
    if (indices.length >= path.getPathCount() - parentPathCount
            && indices[path.getPathCount() - parentPathCount - 1] != indexInParent) {
        return (false);
    }
    Object[] comps = path.getPath();
    for (int i = parentPathCount; i < comps.length; i++) {
        if (!comparator.equals(comps[i].toString(), arr[i - parentPathCount])) {
            return (false);
        }
    }
    return (true);
}
 
Example 3
Source File: ProjectViewTree.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void collapsePath(TreePath path) {
  int row = Registry.is("async.project.view.collapse.tree.path.recursively") ? getRowForPath(path) : -1;
  if (row < 0) {
    super.collapsePath(path);
  }
  else {
    ArrayDeque<TreePath> deque = new ArrayDeque<>();
    deque.addFirst(path);
    while (++row < getRowCount()) {
      TreePath next = getPathForRow(row);
      if (!path.isDescendant(next)) break;
      if (isExpanded(next)) deque.addFirst(next);
    }
    deque.forEach(super::collapsePath);
  }
}
 
Example 4
Source File: CheckTreeSelectionModel.java    From osp with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns true if neither the path nor any descendant is selected.
 *
 * @param path the path to test
 */
public boolean isPathUnselected(TreePath path) {
  if(isSelectionEmpty()) {
    return true; // nothing is selected
  }
  if(isPathOrAncestorSelected(path)) {
    return false; // path is selected
  }
  TreePath[] selectionPaths = getSelectionPaths();
  for(int i = 0; i<selectionPaths.length; i++) {
    if(path.isDescendant(selectionPaths[i])) { // found a selected descendant
      return false;
    }
  }
  return true;
}
 
Example 5
Source File: ProgramTreeActionManager.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Update the view list if the the given path is the an ancestor of any of
 * the paths currently in the view; remove the descendant and add the
 * ancestor path.
 *
 * @param path
 *            path the check against the view list
 *
 */
private void updateViewList(TreePath path) {
	ProgramNode node = (ProgramNode) path.getLastPathComponent();
	if (!tree.hasAncestorsInView(node) && !viewList.contains(path)) {
		for (int i = 0; i < viewList.size(); i++) {
			TreePath viewPath = viewList.get(i);
			if (path.isDescendant(viewPath)) {
				tree.removeFromView(viewPath);
				--i;
			}
		}
		tree.addToView(path);
	}
}
 
Example 6
Source File: TreeView.java    From netbeans with Apache License 2.0 5 votes vote down vote up
void removedNodes(List<VisualizerNode> removed) {
       TreeSelectionModel sm = tree.getSelectionModel();
TreePath[] selPaths = (sm != null) ? sm.getSelectionPaths() : null;
       
       List<TreePath> remSel = null;
       for (VisualizerNode vn : removed) {
           visHolder.removeRecur(vn.getChildren(false));
           if (selPaths != null) {
               TreePath path = new TreePath(vn.getPathToRoot());
               for(TreePath tp : selPaths) {
                   if (path.isDescendant(tp)) {
                       if (remSel == null) {
                           remSel = new ArrayList<TreePath>();
                       }
                       remSel.add(tp);
                   }
               }
           }
       }
       
       removedNodeWasSelected = remSel != null;
       if (remSel != null) {
           try {
               sm.removeSelectionPaths(remSel.toArray(new TreePath[remSel.size()]));
           } catch (NullPointerException e) {
               // if editing of label of removed node was in progress
               // BasicTreeUI will try to cancel editing and repaint node 
               // which fails because node is already removed so it cannot get bounds of it
               // catch and ignore (issue #136123)
           }
       }
   }
 
Example 7
Source File: HistoryPanel.java    From triplea with GNU General Public License v3.0 5 votes vote down vote up
/**
 * collapses parents of last path if it is not in the list of expanded path until the new path is
 * a descendant.
 *
 * @param newPath new path
 */
private void collapseUpFromLastParent(final TreePath newPath) {
  TreePath currentParent = lastParent;
  while (currentParent != null
      && !currentParent.isDescendant(newPath)
      && !stayExpandedContainsDescendantOf(currentParent)) {
    tree.collapsePath(currentParent);
    currentParent = currentParent.getParentPath();
  }
}
 
Example 8
Source File: HistoryPanel.java    From triplea with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Indicates whether the expanded path list contains a descendant of parentPath.
 *
 * @param parentPath tree path for which descendants should be check.
 */
private boolean stayExpandedContainsDescendantOf(final TreePath parentPath) {
  for (final TreePath currentPath : stayExpandedPaths) {
    if (parentPath.isDescendant(currentPath)) {
      return true;
    }
  }
  return false;
}
 
Example 9
Source File: HistoryPanel.java    From triplea with GNU General Public License v3.0 5 votes vote down vote up
/**
 * collapses expanded paths except if new path is a descendant.
 *
 * @param newPath new path
 */
private void collapseExpanded(final TreePath newPath) {
  if (!stayExpandedPaths.isEmpty()) {
    // get enumeration of expanded nodes
    TreePath root = newPath;
    while (root.getPathCount() > 1) {
      root = root.getParentPath();
    }
    final Enumeration<TreePath> expandedDescendants = tree.getExpandedDescendants(root);
    final TreePath selectedPath = tree.getSelectionPath();
    // fill stack with nodes that should be collapsed
    final Deque<TreePath> collapsePaths = new ArrayDeque<>();
    while (expandedDescendants.hasMoreElements()) {
      final TreePath currentDescendant = expandedDescendants.nextElement();
      if (!currentDescendant.isDescendant(newPath)
          && (selectedPath == null || !currentDescendant.isDescendant(selectedPath))) {
        collapsePaths.add(currentDescendant);
      }
    }
    // collapse found paths
    if (!collapsePaths.isEmpty()) {
      for (final TreePath currentPath : collapsePaths) {
        tree.collapsePath(currentPath);
      }
      stayExpandedPaths.removeAll(collapsePaths);
    }
  }
}
 
Example 10
Source File: TreeListModelSelectionAdapter.java    From blog with Apache License 2.0 5 votes vote down vote up
public void update(Observable o, Object arg) {
	Person selection = listModelSelection.getSelection();
	if (selection == null) {
		return;
	}
	TreePath treePath = personTreeModel.getTreePath(selection);
	TreePath selectionPath = getSelectionPath();
	if (treePath != null && treePath.isDescendant(selectionPath)) {
		return;
	}
	setSelectionPath(treePath);
}
 
Example 11
Source File: TreeBuilderUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static boolean isNodeOrChildSelected(JTree tree, DefaultMutableTreeNode node){
  TreePath[] selectionPaths = tree.getSelectionPaths();
  if (selectionPaths == null || selectionPaths.length == 0) return false;

  TreePath path = new TreePath(node.getPath());
  for (TreePath selectionPath : selectionPaths) {
    if (path.isDescendant(selectionPath)) return true;
  }

  return false;
}
 
Example 12
Source File: OptionsTree.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void restoreExpandedState(List<Object> toRestore) {
  TreePath[] selected = myTree.getSelectionPaths();
  if (selected == null) {
    selected = new TreePath[0];
  }

  List<TreePath> toCollapse = new ArrayList<>();

  for (int eachRow = 0; eachRow < myTree.getRowCount(); eachRow++) {
    if (!myTree.isExpanded(eachRow)) continue;

    TreePath eachVisiblePath = myTree.getPathForRow(eachRow);
    if (eachVisiblePath == null) continue;

    Object eachElement = myBuilder.getElementFor(eachVisiblePath.getLastPathComponent());
    if (toRestore.contains(eachElement)) continue;


    for (TreePath eachSelected : selected) {
      if (!eachVisiblePath.isDescendant(eachSelected)) {
        toCollapse.add(eachVisiblePath);
      }
    }
  }

  for (TreePath each : toCollapse) {
    myTree.collapsePath(each);
  }

}
 
Example 13
Source File: Outline.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public int compare(RowMapping rm1, RowMapping rm2) {
    int index1 = rm1.getModelRowIndex();
    int index2 = rm2.getModelRowIndex();
    if (index1 == index2) {
        return 0;
    }
    TreePath tp1 = getLayoutCache().getPathForRow(index1);
    TreePath tp2 = getLayoutCache().getPathForRow(index2);
    if (tp1 == null) {
        if (tp2 == null) {
            return 0;
        } else {
            return -1;
        }
    } else if (tp2 == null) {
        return 1;
    }
    if (tp1.isDescendant(tp2)) {
        return -1;
    }
    if (tp2.isDescendant(tp1)) {
        return 1;
    }
    boolean tp1Changed = false;
    boolean tp2Changed = false;
    TreePath parent1 = tp1.getParentPath();
    TreePath parent2 = tp2.getParentPath();
    if (parent1 != null && parent2 != null && parent1.equals(parent2) &&
            getOutlineModel().isLeaf(tp1.getLastPathComponent()) &&
            getOutlineModel().isLeaf(tp2.getLastPathComponent())) {
        return ascending ? super.compare(rm1, rm2) : - super.compare(rm1, rm2);
    }
    while (tp1.getPathCount() < tp2.getPathCount()) {
        tp2 = tp2.getParentPath();
        tp2Changed = true;
    }
    while (tp1.getPathCount() > tp2.getPathCount()) {
        tp1 = tp1.getParentPath();
        tp1Changed = true;
    }
    parent1 = tp1.getParentPath();
    parent2 = tp2.getParentPath();
    while (parent1 != null && parent2 != null && !parent1.equals(parent2)) {
        tp1 = parent1;
        tp2 = parent2;
        parent1 = tp1.getParentPath();
        parent2 = tp2.getParentPath();
        tp1Changed = true;
        tp2Changed = true;
    }
    if (tp1Changed || tp2Changed) {
        return compare(tempSortMap.get(tp1), tempSortMap.get(tp2));
    } else {
        return ascending ? super.compare(rm1, rm2) : - super.compare(rm1, rm2);
    }
}
 
Example 14
Source File: DBBrowserTree.java    From nextreports-designer with Apache License 2.0 4 votes vote down vote up
/**
 * Convenience method to test whether drop location is valid
 *
 * @param destination
 *            The destination path
 * @param dropper
 *            The path for the node to be dropped
 * @param type
 *            object data type
 * @return null if no problems, otherwise an explanation
 */
private String testDropTarget(TreePath destination, TreePath dropper, byte type) {

	boolean destinationPathIsNull = destination == null;
	if (destinationPathIsNull) {
		return "Invalid drop location.";
	}

	DBBrowserNode node = (DBBrowserNode) destination.getLastPathComponent();
	if (!node.getAllowsChildren()) {
		return "This node does not allow children";
	}

	if (destination.equals(dropper)) {
		return "Destination cannot be same as source";
	}

	if (dropper.isDescendant(destination)) {
		return "Destination node cannot be a descendant.";
	}

	if (dropper.getParentPath().equals(destination)) {
		return "Destination node cannot be a parent.";
	}

	if ((type == DBObject.QUERIES) || (type == DBObject.FOLDER_QUERY)) {
		if ((node.getDBObject().getType() != DBObject.FOLDER_QUERY)
				&& (node.getDBObject().getType() != DBObject.QUERIES_GROUP)) {
			return "No query folder";
		}
	} else if ((type == DBObject.REPORTS) || (type == DBObject.FOLDER_REPORT)) {
		if ((node.getDBObject().getType() != DBObject.FOLDER_REPORT)
				&& (node.getDBObject().getType() != DBObject.REPORTS_GROUP)) {
			return "No report folder";
		}
	} else if ((type == DBObject.CHARTS) || (type == DBObject.FOLDER_CHART)) {
		if ((node.getDBObject().getType() != DBObject.FOLDER_CHART)
				&& (node.getDBObject().getType() != DBObject.CHARTS_GROUP)) {
			return "No chart folder";
		}
	} else {
		return "No folder";
	}

	return null;
}