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

The following examples show how to use javax.swing.tree.TreePath#getParentPath() . 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: CheckTreeSelectionModel.java    From osp with GNU General Public License v3.0 18 votes vote down vote up
/**
 * Unselects the ancestor of a path and selects ancestor's descendants
 * other than those for which the path is a descendant.
 *
 * @param path the path
 */
private void unselectAncestor(TreePath path) {
  // find selected ancestor 
  Stack<TreePath> stack = new Stack<TreePath>();
  stack.push(path);
  TreePath ancestor = path.getParentPath();
  while((ancestor!=null)&&!isPathSelected(ancestor)) {
    stack.push(ancestor);
    ancestor = ancestor.getParentPath();
  }
  if(ancestor==null) { // no selected ancestors!
    return;
  }
  stack.push(ancestor);
  // for each path in the stack, unselect it and select all child nodes
  while(!stack.isEmpty()) {
    TreePath next = stack.pop();
    super.removeSelectionPaths(new TreePath[] {next});
    if(stack.isEmpty()) {
      return;
    }
    Object node = next.getLastPathComponent();
    int childCount = model.getChildCount(node);
    for(int i = 0; i<childCount; i++) {
      Object child = model.getChild(node, i);
      super.addSelectionPaths(new TreePath[] {next.pathByAddingChild(child)});
    }
  }
}
 
Example 2
Source File: OutlineOperator.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the rowspan of siblings which are above irTreePath in the tree. Only
 * expanded paths are taken into account.
 *
 * @param irTreePath
 * @return
 */
protected int getPrecedingSiblingsRowSpan(TreePath irTreePath) {
    OutlineModel lrModel = getOutline().getOutlineModel();

    if (irTreePath.getParentPath() == null) {
        return 0 + getVisibleRootModifier();
    }

    Object lrLast = irTreePath.getLastPathComponent();
    TreePath lrParent = irTreePath.getParentPath();
    int lnRowSpan = 0;

    int lnIndex = lrModel.getIndexOfChild(lrParent.getLastPathComponent(), lrLast);

    for (int i = lnIndex - 1; i >= 0; i--) {
        Object lrSibling = lrModel.getChild(lrParent.getLastPathComponent(), i);
        lnRowSpan += getRowSpanOfLastElement(lrParent.pathByAddingChild(lrSibling));
    }

    return lnRowSpan;
}
 
Example 3
Source File: CheckTreeSelectionModel.java    From osp with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Determines whether all siblings of given path are selected
 *
 * @param path the path to test
 * @return true if all siblings selected
 */
private boolean isSiblingsSelected(TreePath path) {
  TreePath parent = path.getParentPath();
  if(parent==null) {
    return true;
  }
  Object node = path.getLastPathComponent();
  Object parentNode = parent.getLastPathComponent();
  int childCount = model.getChildCount(parentNode);
  for(int i = 0; i<childCount; i++) {
    Object childNode = model.getChild(parentNode, i);
    if(childNode.equals(node)) {
      continue;
    }
    if(!isPathSelected(parent.pathByAddingChild(childNode))) {
      return false;
    }
  }
  return true;
}
 
Example 4
Source File: ProfilerTreeTable.java    From netbeans with Apache License 2.0 6 votes vote down vote up
TreePath getNextPath(TreePath path, boolean down) {
    TreeModel _model = model.treeModel;
    TreeNode node = (TreeNode)path.getLastPathComponent();
    if (down && _model.getChildCount(node) > 0)
        return path.pathByAddingChild(_model.getChild(node, 0));

    TreePath parentPath = path.getParentPath();
    if (!down && parentPath == null)
        return path.pathByAddingChild(_model.getChild(node, 0));
    
    TreeNode parent = (TreeNode)parentPath.getLastPathComponent();
    int idx = _model.getIndexOfChild(parent, node) + 1;

    if (_model.getChildCount(parent) > idx)
        return parentPath.pathByAddingChild(_model.getChild(parent, idx));

    if (!down && parentPath.getParentPath() == null) {
        return parentPath.pathByAddingChild(_model.getChild(parent, 0));
    } else {
        return getNextPath(parentPath, false);
    }
}
 
Example 5
Source File: CheckTreeSelectionModel.java    From GsonFormat with Apache License 2.0 6 votes vote down vote up
private boolean areSiblingsSelected(TreePath path) {
    TreePath parent = path.getParentPath();
    if (parent == null) {
        return true;
    }
    Object node = path.getLastPathComponent();
    Object parentNode = parent.getLastPathComponent();

    int childCount = model.getChildCount(parentNode);
    for (int i = 0; i < childCount; i++) {
        Object childNode = model.getChild(parentNode, i);
        if (childNode == node) {
            continue;
        }
        if (!isPathSelected(parent.pathByAddingChild(childNode))) {
            return false;
        }
    }
    return true;
}
 
Example 6
Source File: XDebuggerTreeRestorer.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static boolean pathsEqual(@Nonnull TreePath path1, @Nonnull TreePath path2) {
  if (path1.getPathCount() != path2.getPathCount()) {
    return false;
  }
  do {
    Object component1 = path1.getLastPathComponent();
    Object component2 = path2.getLastPathComponent();
    if (component1 instanceof XNamedTreeNode && component2 instanceof XNamedTreeNode) {
      if (!Comparing.equal(((XNamedTreeNode)component1).getName(), ((XNamedTreeNode)component2).getName())) {
        return false;
      }
    }
    path1 = path1.getParentPath();
    path2 = path2.getParentPath();
  } while (path1 != null && path2 != null);
  return true;
}
 
Example 7
Source File: PToDoTree.java    From PolyGlot with MIT License 6 votes vote down vote up
private void updatePredecessorsWithCheckMode(TreePath tp, boolean check) {
    TreePath parentPath = tp.getParentPath();
    // If it is the root, stop the recursive calls and return
    if (parentPath == null) {
        return;
    }       
    ToDoNode parentCheckedNode = nodesCheckingState.get(parentPath);  
    parentCheckedNode.setDone(false);
    if (parentCheckedNode.getChildren().length != 0) {
        if (parentCheckedNode.allChildrenDone()) {
            parentCheckedNode.setDone(true);
        } else {
            parentCheckedNode.setDone(false);
        }
    }
    if (parentCheckedNode.isDone()) {
        checkedPaths.add(parentPath);
    } else {
        checkedPaths.remove(parentPath);
    }
    // Go to upper predecessor
    updatePredecessorsWithCheckMode(parentPath, check);
}
 
Example 8
Source File: PropagateUpWhiteTreeCheckingMode.java    From importer-exporter with Apache License 2.0 5 votes vote down vote up
@Override
   public void uncheckPath(TreePath path) {
// uncheck is propagated to children
this.model.uncheckSubTree(path);
TreePath parentPath = path;
// uncheck is propagated to parents, too
while ((parentPath = parentPath.getParentPath()) != null) {
    this.model.removeFromCheckedPathsSet(parentPath);
    this.model.updatePathGreyness(parentPath);
}
   }
 
Example 9
Source File: PropagateUpWhiteTreeCheckingMode.java    From importer-exporter with Apache License 2.0 5 votes vote down vote up
@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;
TreePath parentPath = path;
// uncheck is propagated to parents, too
while ((parentPath = parentPath.getParentPath()) != null) {
    this.model.updatePathGreyness(parentPath);
}
   }
 
Example 10
Source File: MainWindow.java    From jadx with Apache License 2.0 5 votes vote down vote up
private static String[] getPathExpansion(TreePath path) {
	List<String> pathList = new ArrayList<>();
	while (path != null) {
		Object node = path.getLastPathComponent();
		String name;
		if (node instanceof JClass) {
			name = ((JClass) node).getCls().getClassNode().getClassInfo().getFullName();
		} else {
			name = node.toString();
		}
		pathList.add(name);
		path = path.getParentPath();
	}
	return pathList.toArray(new String[pathList.size()]);
}
 
Example 11
Source File: SpellBooksTab.java    From pcgen with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Identify the current spell book, being the spell book that spells should
 * be added to. If no books exist then return an empty string.
 *
 * @return The name of the 'current' spell book, or empty string if none
 *         exist.
 */
String getCurrentSpellBookName()
{
	String spellList = "";
	Object selectedObject = selectedTable.getSelectedObject();
	if (selectedObject != null)
	{
		if (selectedObject instanceof SpellNode)
		{
			spellList = ((SpellNode) selectedObject).getRootNode().getName();
		}
		else if (selectedObject instanceof RootNode)
		{
			spellList = ((RootNode) selectedObject).getName();
		}
		else
		{
			JTree tree = selectedTable.getTree();
			TreePath path = tree.getSelectionPath();
			while (path.getParentPath() != null && (path.getParentPath().getParentPath() != null))
			{
				path = path.getParentPath();
			}
			spellList = path.getLastPathComponent().toString();
		}
	}
	if (StringUtils.isEmpty(spellList))
	{
		ListFacade<?> data = selectedTable.getTreeViewModel().getDataModel();
		if (!data.isEmpty())
		{
			Object firstElem = data.getElementAt(0);
			if (firstElem instanceof SpellNode)
			{
				spellList = ((SpellNode) firstElem).getRootNode().getName();
			}
		}
	}
	return spellList;
}
 
Example 12
Source File: ColopediaPanel.java    From freecol with GNU General Public License v2.0 5 votes vote down vote up
private void select(String id) {
    DefaultMutableTreeNode node = nodeMap.get(id);
    if (node == null) {
        logger.warning("Unable to find node with id '" + id + "'.");
    } else {
        TreePath oldPath = tree.getSelectionPath();
        if (oldPath != null && oldPath.getParentPath() != null) {
            tree.collapsePath(oldPath.getParentPath());
        }
        TreePath newPath = new TreePath(node.getPath());
        tree.scrollPathToVisible(newPath);
        tree.expandPath(newPath);
        showDetails((ColopediaTreeItem) node.getUserObject());
    }
}
 
Example 13
Source File: ExtendedCheckTreeSelectionModel.java    From rapidminer-studio with GNU Affero General Public License v3.0 5 votes vote down vote up
private void toggleRemoveSelection(TreePath path) {
	java.util.Stack<TreePath> stack = new Stack<TreePath>();
	TreePath parent = path.getParentPath();
	while (parent != null && !isPathSelected(parent)) {
		stack.push(parent);
		parent = parent.getParentPath();
	}
	if (parent != null) {
		stack.push(parent);
	} else {
		super.removeSelectionPaths(new TreePath[] { path });
		return;
	}

	while (!stack.isEmpty()) {
		TreePath temp = stack.pop();
		TreePath peekPath = stack.isEmpty() ? path : (TreePath) stack.peek();
		Object node = temp.getLastPathComponent();
		Object peekNode = peekPath.getLastPathComponent();
		int childCount = model.getChildCount(node);
		for (int i = 0; i < childCount; i++) {
			Object childNode = model.getChild(node, i);
			if (childNode != peekNode) {
				super.addSelectionPaths(new TreePath[] { temp.pathByAddingChild(childNode) });
			}
		}
	}
	super.removeSelectionPaths(new TreePath[] { parent });
}
 
Example 14
Source File: CheckTreeSelectionModel.java    From osp with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns true if the path or any ancestor is selected.
 *
 * @param path the path to test
 */
public boolean isPathOrAncestorSelected(TreePath path) {
  while((path!=null)&&!isPathSelected(path)) {
    path = path.getParentPath();
  }
  return path!=null;
}
 
Example 15
Source File: LayoutTreeComponent.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
private DefaultMutableTreeNode findParentCompositeElementNode(Point point) {
  TreePath path = myTree.getPathForLocation(point.x, point.y);
  while (path != null) {
    final PackagingElement<?> element = myTree.getElementByPath(path);
    if (element instanceof CompositePackagingElement) {
      return (DefaultMutableTreeNode)path.getLastPathComponent();
    }
    path = path.getParentPath();
  }
  return null;
}
 
Example 16
Source File: CheckTreeSelectionModel.java    From GsonFormat with Apache License 2.0 5 votes vote down vote up
private void toggleRemoveSelection(TreePath path) {
    Stack stack = new Stack();
    TreePath parent = path.getParentPath();
    while (parent != null && !isPathSelected(parent)) {
        stack.push(parent);
        parent = parent.getParentPath();
    }
    if (parent != null) {
        stack.push(parent);
    } else {
        super.removeSelectionPaths(new TreePath[]{path});
        return;
    }

    while (!stack.isEmpty()) {
        TreePath temp = (TreePath) stack.pop();
        TreePath peekPath = stack.isEmpty() ? path : (TreePath) stack.peek();
        Object node = temp.getLastPathComponent();
        Object peekNode = peekPath.getLastPathComponent();
        int childCount = model.getChildCount(node);
        for (int i = 0; i < childCount; i++) {
            Object childNode = model.getChild(node, i);
            if (childNode != peekNode) {
                super.addSelectionPaths(new TreePath[]{temp.pathByAddingChild(childNode)});
            }
        }
    }
    super.removeSelectionPaths(new TreePath[]{parent});
}
 
Example 17
Source File: ExtendedCheckTreeSelectionModel.java    From rapidminer-studio with GNU Affero General Public License v3.0 5 votes vote down vote up
public boolean isPathSelected(TreePath path, boolean dig) {
	if (!dig) {
		return super.isPathSelected(path);
	}
	while (path != null && !super.isPathSelected(path)) {
		path = path.getParentPath();
	}
	return path != null;
}
 
Example 18
Source File: TreeVisitor.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
@Override
public Action visit(@Nonnull TreePath path) {
  return ignoreRoot && null == path.getParentPath() ? Action.CONTINUE : visit(path, converter.fun(path));
}
 
Example 19
Source File: HistoryPanel.java    From triplea with GNU General Public License v3.0 4 votes vote down vote up
/** 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;
}
 
Example 20
Source File: FileTreeModel.java    From consulo with Apache License 2.0 4 votes vote down vote up
private Object getUniqueID(TreePath path, Node node, ArrayDeque<? super String> deque) {
  deque.addFirst(node.getName());
  Object object = path.getLastPathComponent();
  TreePath parent = path.getParentPath();
  return parent != null && object instanceof Node ? getUniqueID(parent, (Node)object, deque) : parent != null || object != state ? null : deque.toArray();
}