下面列出了javax.swing.tree.TreePath#getPathCount ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
*
*
* @param path1
* @param path2
*
* @return
*/
public static boolean isDescendant(TreePath path1, TreePath path2) {
int count1 = path1.getPathCount();
int count2 = path2.getPathCount();
if (count1 <= count2) {
return false;
}
while (count1 != count2) {
path1 = path1.getParentPath();
count1--;
}
return path1.equals(path2);
}
private Set<ShelvedChangeList> getSelectedLists(final boolean recycled) {
final TreePath[] selections = getSelectionPaths();
final Set<ShelvedChangeList> changeLists = new HashSet<ShelvedChangeList>();
if (selections != null) {
for(TreePath path: selections) {
if (path.getPathCount() >= 2) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getPathComponent(1);
if (node.getUserObject() instanceof ShelvedChangeList) {
final ShelvedChangeList list = (ShelvedChangeList)node.getUserObject();
if (((! recycled) && (! list.isRecycled())) ||
(recycled && list.isRecycled())) {
changeLists.add(list);
}
}
}
}
}
return changeLists;
}
private void expandTwoLevels(TreePath treePath) {
if (treePath.getPathCount() > 2) {
return;
}
TreeNode node = (TreeNode) treePath.getLastPathComponent();
if (node.getChildCount() >= 0) {
for (Enumeration<?> enumChildren = node.children(); enumChildren.hasMoreElements();) {
TreeNode subNode = (TreeNode) enumChildren.nextElement();
TreePath path = treePath.pathByAddingChild(subNode);
expandTwoLevels(path);
}
}
jtrProperties.expandPath(treePath);
}
@Override
protected boolean isLocationInExpandControl(TreePath path, int mouseX,
int mouseY) {
if (path.getPathCount() == 2)
return mouseX < BASE_OFFSET;
return super.isLocationInExpandControl(path, mouseX, mouseY);
}
@Override
public void checkPath(TreePath path) {
// check is propagated to children
this.model.checkSubTree(path);
// check all the ancestors with subtrees checked
TreePath[] parents = new TreePath[path.getPathCount()];
parents[0] = path;
// boolean uncheckAll = false;
boolean greyAll = false;
for (int i = 1; i < parents.length; i++) {
parents[i] = parents[i - 1].getParentPath();
this.model.addToCheckedPathsSet(parents[i]);
if (greyAll) {
this.model.addToGreyedPathsSet(parents[i]);
} else {
switch (this.model.getChildrenChecking(parents[i])) {
case HALF_CHECKED:
this.model.addToGreyedPathsSet(parents[i]);
greyAll = true;
break;
case ALL_UNCHECKED:
System.err.println("This should not happen (PropagatePreservingUncheckTreeCheckingMode)");
break;
case ALL_CHECKED:
this.model.removeFromGreyedPathsSet(parents[i]);
break;
default:
case NO_CHILDREN:
System.err.println("This should not happen (PropagatePreservingCheckTreeCheckingMode)");
break;
}
}
}
}
/**
* @param path a tree path to convert
* @param converter a function to convert path components
* @param type a type of components of the new array
* @return an array of the specified type with the converted path components or {@code null}
* if the specified path is wrong
* or a path component is {@code null}
* or a path component is converted to {@code null}
*/
private static <T> T[] convertTreePathToArray(@Nonnull TreePath path, @Nonnull Function<Object, ? extends T> converter, @Nonnull Class<T> type) {
int count = path.getPathCount();
if (count <= 0) return null;
T[] array = ArrayUtil.newArray(type, count);
while (path != null && count > 0) {
Object component = path.getLastPathComponent();
if (component == null) return null;
T object = convert(component, converter);
if (object == null) return null;
array[--count] = object;
path = path.getParentPath();
}
return path != null || count > 0 ? null : array;
}
private TreePath getCurrentPath(TreePath oldPath) {
if (oldPath == null || oldPath.getPathCount() < 1) return null;
if (!treeTableModel.getRoot().equals(oldPath.getPathComponent(0))) return null;
TreePath p = getRootPath();
Object[] op = oldPath.getPath();
CCTNode n = (CCTNode)treeTableModel.getRoot();
for (int i = 1; i < op.length; i++) {
// #241115
CCTNode[] children = n.getChildren();
if (children == null) return null;
CCTNode nn = null;
for (CCTNode c : children)
if (c.equals(op[i])) {
nn = c;
break;
}
if (nn == null) return null;
n = nn;
p = p.pathByAddingChild(n);
}
return p;
}
private String[] getNames(TreePath path) {
int count = path.getPathCount();
String[] names = new String[count];
for (int i = 0; i < count; i++) {
ProgramNode node = (ProgramNode) path.getPathComponent(i);
names[i] = node.getName();
}
return names;
}
/**
* Tries to expand nodes selected in the explorer manager.
*/
private void expandSelection() {
Node[] arr = manager.getSelectedNodes ();
for (int i = 0; i < arr.length; i++) {
if ( (arr[i].getParentNode() == null) && (! outline.isRootVisible())) {
// don't try to show root if it is invisible
continue;
}
TreeNode tn = Visualizer.findVisualizer(arr[i]);
if (tn != null) {
ArrayList<TreeNode> al = new ArrayList<TreeNode> ();
while (tn != null) {
al.add(tn);
tn = tn.getParent();
}
Collections.reverse(al);
TreePath tp = new TreePath(al.toArray());
Deque<TreePath> pathsStack = new ArrayDeque<TreePath>(al.size());
while ((tp != null) && (tp.getPathCount() > 0)) {
tp = tp.getParentPath();
if (tp != null) {
pathsStack.addFirst(tp);
}
}
for (TreePath etp : pathsStack) {
outline.expandPath(etp);
}
}
}
}
public void setSelectionPaths(TreePath[] path) {
if(path == null || path.length == 0) return;
TreePath maxPath = path[0];
for(int i = 0; i < path.length; i++)
if(path[i].getPathCount() > maxPath.getPathCount())
maxPath = path[i];
maxPath = maxPath.getParentPath();
makeVisible(maxPath);
minSelected = Integer.MAX_VALUE;
for(int i = 0; i < path.length; i++)
if(path[i].getParentPath().equals(maxPath))
select(path[i].getLastPathComponent());
}
private boolean isEditEvent(int row, int column, MouseEvent me) {
if (me.getClickCount() > 1) {
return true;
}
boolean noModifiers = me.getModifiersEx() == MouseEvent.BUTTON1_DOWN_MASK;
if (lastEditPosition != null && selectedRow == row && noModifiers &&
lastEditPosition[0] == row && lastEditPosition[1] == column) {
int handleWidth = DefaultOutlineCellRenderer.getExpansionHandleWidth();
Insets ins = getInsets();
TreePath path = getLayoutCache().getPathForRow(convertRowIndexToModel(row));
int nd = path.getPathCount() - (isRootVisible() ? 1 : 2);
if (nd < 0) {
nd = 0;
}
int handleStart = ins.left + (nd * DefaultOutlineCellRenderer.getNestingWidth());
int handleEnd = ins.left + handleStart + handleWidth;
// Translate 'x' to position of column if non-0:
int columnStart = getCellRect(row, column, false).x;
handleStart += columnStart;
handleEnd += columnStart;
if (me.getX() >= handleEnd) {
lastEditPosition = null;
return true;
}
}
lastEditPosition = new int[] { row, column };
return false;
}
private static int[] pathToChildIndecies(TreePath path) {
int[] result = new int[path.getPathCount()];
for (int i = 0; i < path.getPathCount(); i++) {
TreeNode node = (TreeNode) path.getPathComponent(i);
TreeNode parent = node.getParent();
result[i] = parent != null ? parent.getIndex(node) : 0;
}
return result;
}
public void setSelectionPaths(TreePath[] path) {
if(path == null || path.length == 0) return;
clearSelection();
TreePath maxPath = path[0];
for(int i = 0; i < path.length; i++)
if(path[i].getPathCount() > maxPath.getPathCount())
maxPath = path[i];
makeVisible(maxPath);
for(int i = 0; i < path.length; i++)
select(path[i]);
}
private boolean shouldHidePopup(TreePath path) {
if (getParent() == null) return false;
if (path == null) return false;
if (!myWizardTree.isCollapsed(path)) return false;
if (myWizardTree.isRootVisible()) {
return path.getPathCount() == 1;
}
return path.getPathCount() == 2;
}
static FavoritesListNode getListNodeFromPath(TreePath path) {
if (path != null && path.getPathCount() > 1) {
final Object o = path.getPath()[1];
if (o instanceof DefaultMutableTreeNode) {
final Object obj = ((DefaultMutableTreeNode)o).getUserObject();
if (obj instanceof FavoritesTreeNodeDescriptor) {
final AbstractTreeNode node = ((FavoritesTreeNodeDescriptor)obj).getElement();
if (node instanceof FavoritesListNode) {
return (FavoritesListNode)node;
}
}
}
}
return null;
}
private boolean isAnyRowSelected() {
TreePath treeSelectionPath = tree.getSelectionPath();
return ((treeSelectionPath != null) && (treeSelectionPath.getPathCount() > 0));
}
@Override
protected void paintVerticalPartOfLeg(final Graphics g, final Rectangle clipBounds, final Insets insets, final TreePath path) {
final DefaultMutableTreeNode node = (DefaultMutableTreeNode)path.getLastPathComponent();
if (node.getChildCount() < 2) {
// We could draw lines for nodes with a single child but we omit them
// to more of an emphasis of lines for nodes with multiple children.
return;
}
final DiagnosticsNode diagnostic = maybeGetDiagnostic(node);
if (diagnostic != null && !diagnostic.hasChildren()) {
// This avoids drawing lines for nodes with only property children.
return;
}
final int depth = path.getPathCount() - 1;
if (depth == 0 && !getShowsRootHandles() && !isRootVisible()) {
return;
}
int lineX = getRowX(-1, depth);
if (leftToRight) {
lineX = lineX - getRightChildIndent() + insets.left;
}
else {
lineX = tree.getWidth() - lineX - insets.right +
getRightChildIndent() - 1;
}
final int clipLeft = clipBounds.x;
final int clipRight = clipBounds.x + (clipBounds.width - 1);
if (lineX >= clipLeft && lineX <= clipRight) {
final int clipTop = clipBounds.y;
final int clipBottom = clipBounds.y + clipBounds.height;
Rectangle parentBounds = getPathBounds(tree, path);
boolean previousDashed = false;
int top;
if (parentBounds == null) {
top = Math.max(insets.top + getVerticalLegBuffer(),
clipTop);
}
else {
top = Math.max(parentBounds.y + parentBounds.height +
getVerticalLegBuffer(), clipTop);
}
if (depth == 0 && !isRootVisible()) {
final TreeModel model = getModel();
if (model != null) {
final Object root = model.getRoot();
if (model.getChildCount(root) > 0) {
parentBounds = getPathBounds(tree, path.
pathByAddingChild(model.getChild(root, 0)));
if (parentBounds != null) {
top = Math.max(insets.top + getVerticalLegBuffer(),
parentBounds.y +
parentBounds.height / 2);
}
}
}
}
for (int i = 0; i < node.getChildCount(); ++i) {
final DefaultMutableTreeNode child = (DefaultMutableTreeNode)node.getChildAt(i);
final DiagnosticsNode childDiagnostic = maybeGetDiagnostic(child);
boolean dashed = false;
if (childDiagnostic != null) {
dashed = childDiagnostic.getStyle() == DiagnosticsTreeStyle.offstage;
}
final Rectangle childBounds = getPathBounds(tree, path.pathByAddingChild(child));
if (childBounds == null)
// This shouldn't happen, but if the model is modified
// in another thread it is possible for this to happen.
// Swing isn't multithreaded, but I'll add this check in
// anyway.
{
continue;
}
final int bottom = Math.min(childBounds.y +
(childBounds.height / 2), clipBottom);
if (top <= bottom && bottom >= clipTop && top <= clipBottom) {
g.setColor(JBColor.GRAY);
paintVerticalLine(g, tree, lineX, top, bottom, dashed);
}
top = bottom;
previousDashed = dashed;
}
}
}
@Override
public void valueForPathChanged(TreePath treePath, Object newValue)
{
Object[] path;
int[] childIndices = null;
Object[] children = null;
if (treePath.getPathCount() > 1)
{
Object parent = treePath.getParentPath().getLastPathComponent();
if (parent instanceof FieldType)
{
((FieldType) parent).setValue((String) newValue);
childIndices = new int[] { 0 };
}
else if (parent instanceof SessionType)
{
/*
* TODO Find a better way to know whether the new value is a
* session name or an application version
*/
String newString = (String) newValue;
if ((newString.contains("FIX.") || (newString.contains("FIXT.")))
&& newString.contains(">"))
{
((SessionType) parent).setName(newString);
childIndices = new int[] { 0 };
} else if (newString.contains("FIX."))
{
((SessionType) parent).setAppVersionId(newString);
childIndices = new int[] { 1 };
}
}
treePath = treePath.getParentPath().pathByAddingChild(newValue);
path = treePath.getParentPath().getPath();
children = new Object[] { newValue };
} else
{
path = treePath.getPath();
}
fireTreeNodesChanged(this, path, childIndices, children);
}
private boolean isPathValid(TreePath path) {
if (path == null || path.getPathCount() > 3)
return false;
Object last = path.getLastPathComponent();
return last instanceof Tool;
}
/** Selects the most recent history node, expanding the tree if necessary. */
public void goToEnd() {
final HistoryNode last;
try {
data.acquireWriteLock();
last = data.getHistory().getLastNode();
} finally {
data.releaseWriteLock();
}
final TreePath path = new TreePath(last.getPath());
final TreePath parent = path.getParentPath();
if (!mouseOverPanel) {
// make sure we undo our change of the lock property
gotoNode(last);
if (lastParent == null) {
lastParent = tree.getSelectionPath();
}
tree.setSelectionPath(path);
collapseExpanded(path);
collapseUpFromLastParent(parent);
final Rectangle rect = tree.getPathBounds(path);
rect.setRect(0, rect.getY(), rect.getWidth(), rect.getHeight());
tree.scrollRectToVisible(rect);
} else {
if (!mouseWasOverPanel) {
// save the lock property so that we can undo it
TreePath root = parent;
while (root.getPathCount() > 1) {
root = root.getParentPath();
}
final Enumeration<TreePath> expandedDescendants = tree.getExpandedDescendants(root);
addToStayExpanded(expandedDescendants);
} else {
collapseUpFromLastParent(parent);
}
tree.expandPath(parent);
}
mouseWasOverPanel = mouseOverPanel;
lastParent = parent;
}