public static List<TreePath> collectExpandedPaths(final JTree tree, TreePath path) { final ArrayList<TreePath> result = new ArrayList<TreePath>(); if (!tree.isExpanded(path)) return result; final Object lastPathComponent = path.getLastPathComponent(); final TreeModel model = tree.getModel(); if (model.isLeaf(lastPathComponent)) { result.add(path); } else { boolean pathWasAdded = false; for (int i = model.getChildCount(lastPathComponent) - 1; i >= 0; i--) { final TreePath childPath = path.pathByAddingChild(model.getChild(lastPathComponent, i)); if (model.isLeaf(lastPathComponent)) { if (!pathWasAdded) { result.add(path); pathWasAdded = true; } } else if (tree.isExpanded(childPath)) { result.addAll(collectExpandedPaths(tree, childPath)); } else { if (!pathWasAdded) { result.add(path); pathWasAdded = true; } } } } return result; }
public void paint(Graphics g, JComponent c) { JTree tree = (JTree) c; TreeModel mod = tree.getModel(); g.translate(10, 10); paintRecursive(g, 0, 0, 0, 0, tree, mod, mod.getRoot()); g.translate(-10, -10); }
/** * Construit une chaîne de caractères qui contiendra le noeud spécifié ainsi que tous les noeuds * enfants. * * @param model Arborescence à écrire. * @param node Noeud de l'arborescence à écrire. * @param buffer Buffer dans lequel écrire le noeud. * @param level Niveau d'indentation (à partir de 0). * @param last Indique si les niveaux précédents sont en train d'écrire leurs derniers items. * @return Le tableau {@code last}, qui peut éventuellement avoir été agrandit. */ private static boolean[] format( final TreeModel model, final Object node, final Appendable buffer, final int level, boolean[] last, final String lineSeparator) throws IOException { for (int i = 0; i < level; i++) { if (i != level - 1) { buffer.append(last[i] ? '\u00A0' : '\u2502').append("\u00A0\u00A0\u00A0"); } else { buffer.append(last[i] ? '\u2514' : '\u251C').append("\u2500\u2500\u2500"); } } buffer.append(String.valueOf(node)).append(lineSeparator); if (level >= last.length) { last = XArray.resize(last, level * 2); } final int count = model.getChildCount(node); for (int i = 0; i < count; i++) { last[level] = (i == count - 1); last = format(model, model.getChild(node, i), buffer, level + 1, last, lineSeparator); } return last; }
// if any ancestor node of given path is selected then unselect it // and selection all its descendants except given path and descendants. // otherwise just unselect the given path 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}); }
public static ActionCallback selectFirstNode(final JTree tree) { final TreeModel model = tree.getModel(); final Object root = model.getRoot(); TreePath selectionPath = new TreePath(root); if (!tree.isRootVisible() && model.getChildCount(root) > 0) selectionPath = selectionPath.pathByAddingChild(model.getChild(root, 0)); return selectPath(tree, selectionPath); }
protected void changeEvent(DebuggerContextImpl newContext, int event) { if (event == DebuggerSession.EVENT_REFRESH || event == DebuggerSession.EVENT_REFRESH_VIEWS_ONLY) { // in order not to spoil the evaluation result do not re-evaluate the tree final TreeModel treeModel = getTree().getModel(); updateTree(treeModel, (DebuggerTreeNodeImpl) treeModel.getRoot()); } }
/** * @param tree JTree to collect expanded paths from. * @param paths output parameter. */ public static void collectExpandedPaths( @NotNull final JTree tree, @NotNull final List<TreePath> paths) { final TreeModel model = tree.getModel(); final Object root = model.getRoot(); LOG.assertTrue(root != null); collectExpandedPathsImpl(tree, paths, new TreePath(root)); }
protected TreePath getNextPath(TreePath path) { TreeModel model = tree.getModel(); // return the first node if (path == null) { return new TreePath(model.getRoot()); } Object lastNode = path.getLastPathComponent(); // if the NODE has children if (model.getChildCount(lastNode) > 0) { return path.pathByAddingChild(model.getChild(lastNode, 0)); } // if the NODE has NO children int index = 0; int pathLength = path.getPathCount(); Object parentNode = path.getPathComponent(pathLength - 2); if (pathLength > 1) { index = model.getIndexOfChild(parentNode, lastNode); } // if there is only root node if (pathLength == 1) { return path; } // if there are still some siblings (brothers) after this node if (index + 1 < model.getChildCount(parentNode)) { // replace the lastPathComponent by its next sibling return path.getParentPath().pathByAddingChild(model.getChild(parentNode, index + 1)); } else { while (true) { // we need to find next sibling for our father path = path.getParentPath(); // if we get to the end of tree then start if if (path.getParentPath() == null) { // return the root path return path; } pathLength = path.getPathCount(); parentNode = path.getPathComponent(pathLength - 2); index = model.getIndexOfChild(parentNode, path.getLastPathComponent()); // if there are still some siblings (brothers) after this node if (index + 1 < model.getChildCount(parentNode)) { // replace the lastPathComponent by its next sibling return path.getParentPath().pathByAddingChild(model.getChild(parentNode, index + 1)); } } } }
@Nullable public static TreeNode findNodeWithObject( final Object object, final TreeModel model, final Object parent) { for (int i = 0; i < model.getChildCount(parent); i++) { final DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) model.getChild(parent, i); if (childNode.getUserObject().equals(object)) return childNode; } return null; }
/** * 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)); } }
@Override public boolean isModified(CodeStyleSettings settings) { TreeModel treeModel = myTreeTable.getTree().getModel(); TreeNode root = (TreeNode) treeModel.getRoot(); if (isModified(root, settings)) { return true; } return false; }
public NoRemoteComponentsTreeModel(TreeModel model) { this.model = model; for (int i = 0; i < model.getChildCount(model.getRoot()); i++) { Object node = model.getChild(model.getRoot(), i); if (isRemoteComponentsNode(node)) { this.remoteComponentsNode = node; this.remoteComponentsNodeIndex = i; } } }
protected boolean toggleNode(CheckedTreeNode node) { boolean checked = !node.isChecked(); checkNode(node, checked); // notify model listeners about model change final TreeModel model = getTree().getModel(); model.valueForPathChanged(new TreePath(node.getPath()), node.getUserObject()); return checked; }
/** * Expands all paths in the tree. * * @see JTree#expandPath(TreePath) */ public void expandAll() { cancelEditing(); final TreeModel tm = getModel(); final Object root = tm.getRoot(); /* nothing to expand, if no root */ if (root != null) { expandAllPaths(new TreePath(root), tm); } }
protected int paintRecursive( Graphics g, int indentation, int descent, int childNumber, int depth, JTree tree, TreeModel mod, Object curr) { Rectangle clip = g.getClipBounds(); if (indentation > clip.x + clip.width + rightChildIndent || descent > clip.y + clip.height + rowHeight) return descent; int halfHeight = rowHeight / 2; int halfWidth = rightChildIndent / 2; int y0 = descent + halfHeight; if (mod.isLeaf(curr)) { paintLeaf(g, indentation, descent, tree, curr); descent += rowHeight; } else { if (depth > 0 || tree.isRootVisible()) { paintNonLeaf(g, indentation, descent, tree, curr); descent += rowHeight; y0 += halfHeight; } int max = mod.getChildCount(curr); for (int i = 0; i < max; ++i) { g.setColor(hashColor); g.drawLine( indentation + halfWidth, descent + halfHeight, indentation + rightChildIndent, descent + halfHeight); descent = paintRecursive( g, indentation + rightChildIndent, descent, i, depth + 1, tree, mod, mod.getChild(curr, i)); } } int y1 = descent - halfHeight; if (y0 != y1) { g.setColor(hashColor); g.drawLine( indentation + halfWidth, y0, indentation + halfWidth, y1); } return descent; }
/** * Set all properties panels to null to mark for refresh. * * @param level for which the model will be reset */ private void resetPropertiesPanels(LEVEL level) { TreeModel model = levelTrees.get(level); int functionalityCount = model.getChildCount(model.getRoot()); ClassPropertiesInfo funcInfo; DefaultMutableTreeNode node; for (int i = 0; i < functionalityCount; i++) { node = (DefaultMutableTreeNode) model.getChild(model.getRoot(), i); funcInfo = (ClassPropertiesInfo) node.getUserObject(); funcInfo.setPropertiesPanel(null); } }
@Nullable private static TreePath findNodePath(MavenArchetype object, TreeModel model, Object parent) { for (int i = 0; i < model.getChildCount(parent); i++) { DefaultMutableTreeNode each = (DefaultMutableTreeNode) model.getChild(parent, i); if (each.getUserObject().equals(object)) return new TreePath(each.getPath()); TreePath result = findNodePath(object, model, each); if (result != null) return result; } return null; }
@Override public int getChildCount(Object parent) { int count = 0; for (int i = 0; i < model.getChildCount(parent); i++) { if (isFiltered(model.getChild(parent, i))) { count++; } } return count; }
public int getChildCount(Object parent) { if (visibleNodes == null) { return model.getChildCount(parent); } int visibleCount = 0; for (int i = 0, count = model.getChildCount(parent); i < count; i++) { if (visibleNodes.contains(model.getChild(parent, i))) { visibleCount++; } } return visibleCount; }
private void updateTree(final TreeModel model, final DebuggerTreeNodeImpl node) { if (node == null) { return; } if (node.getDescriptor().myIsExpanded) { final int count = model.getChildCount(node); for (int idx = 0; idx < count; idx++) { final DebuggerTreeNodeImpl child = (DebuggerTreeNodeImpl) model.getChild(node, idx); updateTree(model, child); } } node.labelChanged(); }
@Override public int getIndexOfChild(Object parent, Object child) { int index = 0; for (int i = 0; i < model.getChildCount(parent); i++) { Object nodeChild = model.getChild(parent, i); if (isFiltered(nodeChild)) { if (nodeChild == child) { return index; } index++; } } return -1; }
public Object getChild(Object parent, int index) { if (visibleNodes == null) { return model.getChild(parent, index); } int visibleIndex = 0; for (int i = 0, count = model.getChildCount(parent); i < count; i++) { Object node = model.getChild(parent, i); if (visibleNodes.contains(node) && index == visibleIndex++) { return node; } } throw new ArrayIndexOutOfBoundsException(); }
private void expandAll(TreePath tp) { if (tp == null) { return; } Object node = tp.getLastPathComponent(); TreeModel model = mainTree.getModel(); if (!model.isLeaf(node)) { mainTree.expandPath(tp); for (int i = 0; i < model.getChildCount(node); i++) { expandAll(tp.pathByAddingChild(model.getChild(node, i))); } } }
// tells whether all siblings of given path are selected. 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; }
@Override public Object getChild(Object parent, int index) { int count = 0; for (int i = 0; i < model.getChildCount(parent); i++) { Object child = model.getChild(parent, i); if (isFiltered(child)) { if (count == index) { return child; } count++; } } return null; }
private void collapseAll(TreePath tp) { if (tp == null) { return; } Object node = tp.getLastPathComponent(); TreeModel model = mainTree.getModel(); if (!model.isLeaf(node)) { mainTree.collapsePath(tp); for (int i = 0; i < model.getChildCount(node); i++) { // for (int i = node.childCount()-4;i>=0;i--){ collapseAll(tp.pathByAddingChild(model.getChild(node, i))); } mainTree.collapsePath(tp); } }
/** * Hides nodes from the tree that do not match <code>text</code>. * * @param text search text */ public void filterByText(String text) { text = normalize(text); if (text == null || text.length() == 0) { visibleNodes = null; } else { visibleNodes = new HashSet<Object>(); String[] keywords = text.split(" "); for (int i = 0; i < keywords.length; i++) { SortedMap<String, List<Object>> nodeListByKey = getMatches(text); if (i == 0) { for (List<Object> nodes : nodeListByKey.values()) { visibleNodes.addAll(nodes); } } else { Set<Object> allNew = new HashSet<Object>(); for (List<Object> nodes : nodeListByKey.values()) { allNew.addAll(nodes); } visibleNodes.retainAll(allNew); } } ensureParentsVisible(); } TreeModelEvent event = new TreeModelEvent(this, new Object[] {model.getRoot()}); for (TreeModelListener listener : listeners) { listener.treeStructureChanged(event); } }
/** * Method findTreePathByObject. * * @param model TreeModel * @param base TreeNode * @param parent TreePath * @param tofind Object * @return TreePath */ private TreePath findTreePathByObject( TreeModel model, TreeNode base, TreePath parent, Object tofind) { int childCount = model.getChildCount(base); for (int i = 0; i < childCount; i++) { DefaultMutableTreeNode child = (DefaultMutableTreeNode) model.getChild(base, i); if (child.getUserObject().equals(tofind)) { return parent.pathByAddingChild(child); } if (!model.isLeaf(child)) { TreePath foundTreePath = findTreePathByObject(model, child, parent.pathByAddingChild(child), tofind); if (null != foundTreePath) return foundTreePath; } } return null; }
public int getIndexOfChild(Object parent, Object child) { if (visibleNodes == null) { return model.getIndexOfChild(parent, child); } int visibleIndex = 0; for (int i = 0, count = model.getChildCount(parent); i < count; i++) { Object node = model.getChild(parent, i); if (visibleNodes.contains(node)) { if (node == child) { return visibleIndex; } visibleIndex++; } } return -1; }
public void saveChanges() { TreeModel model = tree.getModel(); Object root = model.getRoot(); ClassPropertiesInfo classInfo; int classCount = model.getChildCount(root); for (int c = 0; c < classCount; c++) { classInfo = (ClassPropertiesInfo) ((DefaultMutableTreeNode) model.getChild(root, c)).getUserObject(); PropertySheetPanel propertiesPanel = classInfo.getPropertiesPanel(); if (propertiesPanel != null) { // to exit edit mode and retrieve the value propertiesPanel.getTable().commitEditing(); setProperties(classInfo); } } GeneralPreferences.saveProperties(); }