/** * Identify the current spell book, being the spell book that spells should be added to. If no * books exist then return an empty string. * * @param character The character we are checking for. * @return The name of the 'current' spell book, or empty string if none exist. */ String getCurrentSpellBookName(CharacterFacade character) { 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; }
// 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 void addSelectionPaths(TreePath[] paths) { // unselect all descendants of paths[] for (int i = 0; i < paths.length; i++) { TreePath path = paths[i]; TreePath[] selectionPaths = getSelectionPaths(); if (selectionPaths == null) break; ArrayList toBeRemoved = new ArrayList(); for (int j = 0; j < selectionPaths.length; j++) { if (isDescendant(selectionPaths[j], path)) toBeRemoved.add(selectionPaths[j]); } super.removeSelectionPaths((TreePath[]) toBeRemoved.toArray(new TreePath[0])); } // if all siblings are selected then unselect them and select parent recursively // otherwize just select that path. for (int i = 0; i < paths.length; i++) { TreePath path = paths[i]; TreePath temp = null; while (areSiblingsSelected(path)) { temp = path; if (path.getParentPath() == null) break; path = path.getParentPath(); } if (temp != null) { if (temp.getParentPath() != null) addSelectionPath(temp.getParentPath()); else { if (!isSelectionEmpty()) removeSelectionPaths(getSelectionPaths()); super.addSelectionPaths(new TreePath[] {temp}); } } else super.addSelectionPaths(new TreePath[] {path}); } }
/** Returns whether the given path is currently checked. */ public boolean isChecked(TreePath path) { if (isExplicitlyChecked(path)) { return true; } else { return path.getParentPath() != null && isChecked(path.getParentPath()); } }
private void removeChildren(TreePath parent) { for (Iterator<TreePath> i = checkedPaths.iterator(); i.hasNext(); ) { TreePath p = i.next(); if (p.getParentPath() != null && parent.equals(p.getParentPath())) { i.remove(); } } }
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)); } } } }
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()); } }
public void finishTask() { myCurrentTaskName = null; final TreePath parentPath = myParentPath.getParentPath(); if (parentPath != null) { myParentPath = parentPath; } }
public int getRowForPath(final TreePath path) { if (!isVisible(path)) { return -1; } VariableHeightStateNode correspondingNode = (VariableHeightStateNode) getStateNodeForPath(path); if (correspondingNode != null) { return correspondingNode.getRow(); } VariableHeightStateNode parent = (VariableHeightStateNode) getStateNodeForPath(path.getParentPath()); int modelIndex = parent.getModelIndexOfChild(path.getLastPathComponent()); int rowIncrement = 0; for (int i = 0; i < modelIndex; i++) { rowIncrement++; Object modelNode = parent.getModelChildNode(i); StateNode childNode = parent.getChild(modelNode); if (childNode != null) { rowIncrement += childNode.getTotalChildrenCount(); } } return parent.getRow() + rowIncrement + 1; }
/** * Compares this {@code TreePath} to the specified object. This returns {@code true} if {@code o} * is a {@code TreePath} with the exact same elements (as determined by using {@code equals} on * each element of the path). * * @param o the object to compare */ public boolean equals(Object o) { if (o == this) return true; if (o instanceof TreePath) { TreePath oTreePath = (TreePath) o; if (getPathCount() != oTreePath.getPathCount()) return false; for (TreePath path = this; path != null; path = path.getParentPath()) { if (!(path.getLastPathComponent().equals(oTreePath.getLastPathComponent()))) { return false; } oTreePath = oTreePath.getParentPath(); } return true; } return false; }
/** * Returns the number of elements in the path. * * @return the number of elements in the path */ public int getPathCount() { int result = 0; for (TreePath path = this; path != null; path = path.getParentPath()) { result++; } return result; }
// {{{ removeSelectedNode() method private void removeSelectedNode() { TreePath path = resultTree.getSelectionPath(); if (path == null) return; MutableTreeNode value = (MutableTreeNode) path.getLastPathComponent(); if (path.getPathCount() > 1) { // Adjust selection so that repeating some removals // behave naturally. TreePath parentPath = path.getParentPath(); MutableTreeNode parent = (MutableTreeNode) parentPath.getLastPathComponent(); int removingIndex = parent.getIndex(value); int nextIndex = removingIndex + 1; if (nextIndex < parent.getChildCount()) { TreeNode next = parent.getChildAt(nextIndex); resultTree.setSelectionPath(parentPath.pathByAddingChild(next)); } else { resultTree.setSelectionPath(parentPath); } resultTreeModel.removeNodeFromParent(value); } HyperSearchOperationNode.removeNodeFromCache(value); if (resultTreeRoot.getChildCount() == 0) { hideDockable(); } } // }}}
/** * Returns an ordered array of the elements of this {@code TreePath}. The first element is the * root. * * @return an array of the elements in this {@code TreePath} */ public Object[] getPath() { int i = getPathCount(); Object[] result = new Object[i--]; for (TreePath path = this; path != null; path = path.getParentPath()) { result[i--] = path.getLastPathComponent(); } return result; }
public void invalidatePathBounds(final TreePath path) { StateNode node = getStateNodeForPath(path); if (node == null) { node = getStateNodeForPath(path.getParentPath()); } if (node != null) { node.invalidateTreePartBelow(); } }
@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); } }
/** * Gets the CurrentNode attribute of the JMeterTreeListener object. * * @return the CurrentNode value */ public ReportTreeNode getCurrentNode() { if (currentPath != null) { if (currentPath.getLastPathComponent() != null) { return (ReportTreeNode) currentPath.getLastPathComponent(); } else { return (ReportTreeNode) currentPath.getParentPath().getLastPathComponent(); } } else { return (ReportTreeNode) model.getRoot(); } }
public Rectangle getBounds(final TreePath path, final Rectangle placeIn) { if (!isRoot(path) && !isVisible(path)) { return null; } if (isFixedRowHeight()) { return getFixedHeightBoundsImpl(path, placeIn); } if (getNodeDimensions() == null) { return new Rectangle(); } if (isRoot(path)) { Rectangle result = getNodeDimensions( getStateRoot().getModelNode(), isRootVisible() ? 0 : -1, 0, getStateRoot().isExpanded(), placeIn); result.y = 0; return result; } VariableHeightStateNode parentNode = (VariableHeightStateNode) getStateNodeForPath(path.getParentPath()); StateNode node = parentNode.getChild(path.getLastPathComponent()); Rectangle result = getNodeDimensions( path.getLastPathComponent(), getRowForPath(path), getPathDepth(path), node != null && node.isExpanded(), placeIn); result.y = 0; Object currentModelNode = path.getLastPathComponent(); while (parentNode != null) { if (!parentNode.isRoot() || isRootVisible()) { result.y += parentNode.getHeight(); } if (parentNode.isExpanded()) { int modelIndex = parentNode.getModelIndexOfChild(currentModelNode); for (int i = 0; i < modelIndex; i++) { result.y += parentNode.getChildrenHeights()[i]; } } currentModelNode = parentNode.getModelNode(); parentNode = (VariableHeightStateNode) parentNode.getParent(); } return result; }
@Override public void valueForPathChanged(final TreePath path, final Object value) { File oldFile = (File) path.getLastPathComponent(); String fileParentPath = oldFile.getParent(); String newFileName = (String) value; File targetFile = new File(fileParentPath, newFileName); oldFile.renameTo(targetFile); File parent = new File(fileParentPath); int[] changedChildrenIndices = {getIndexOfChild(parent, targetFile)}; Object[] changedChildren = {targetFile}; fireTreeNodesChanged(path.getParentPath(), changedChildrenIndices, changedChildren); }
@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; }
@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); } }
/** * Returns the path element at the specified index. * * @param index the index of the element requested * @return the element at the specified index * @throws IllegalArgumentException if the index is outside the range of this path */ public Object getPathComponent(int index) { int pathLength = getPathCount(); if (index < 0 || index >= pathLength) throw new IllegalArgumentException("Index " + index + " is out of the specified range"); TreePath path = this; for (int i = pathLength - 1; i != index; i--) { path = path.getParentPath(); } return path.getLastPathComponent(); }
// 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; }
/** * Returns true if <code>aTreePath</code> is a descendant of this {@code TreePath}. A {@code * TreePath} {@code P1} is a descendant of a {@code TreePath} {@code P2} if {@code P1} contains * all of the elements that make up {@code P2's} path. For example, if this object has the path * {@code [a, b]}, and <code>aTreePath</code> has the path {@code [a, b, c]}, then <code>aTreePath * </code> is a descendant of this object. However, if <code>aTreePath</code> has the path {@code * [a]}, then it is not a descendant of this object. By this definition a {@code TreePath} is * always considered a descendant of itself. That is, <code>aTreePath.isDescendant(aTreePath) * </code> returns {@code true}. * * @param aTreePath the {@code TreePath} to check * @return true if <code>aTreePath</code> is a descendant of this path */ public boolean isDescendant(TreePath aTreePath) { if (aTreePath == this) return true; if (aTreePath != null) { int pathLength = getPathCount(); int oPathLength = aTreePath.getPathCount(); if (oPathLength < pathLength) // Can't be a descendant, has fewer components in the path. return false; while (oPathLength-- > pathLength) aTreePath = aTreePath.getParentPath(); return equals(aTreePath); } return false; }
private void setParent(TreePath path) { TreePath parent = path.getParentPath(); if (parent != null) { if (isFullyChecked(parent)) { removeChildren(parent); checkedPaths.add(parent); } else { if (isChecked(parent)) { checkedPaths.remove(parent); addChildren(parent); checkedPaths.remove(path); } } setParent(parent); } }
public Project getCurrentProject() { TreePath path = mainTree.getSelectionPath(); if (path == null) { return null; } Object node = path.getLastPathComponent(); while (node != null && !(node instanceof ProjectTreeNode)) { path = path.getParentPath(); node = (path == null ? null : path.getLastPathComponent()); } if (node == null) { return null; } return ((ProjectTreeNode) node).getProject(); }
/** * Reload the tree model from the filesystem. Clear any stored data. Used by delete and paste to * reflect those changes. * * @param path expands the tree at this treepath */ public void refresh(TreePath path) throws IOException { if (path == null) { setSelectionRow(0); setModel(new JargonTreeModel(roots, selects)); return; } // if file, Get the parent directory, because // expandPath() ignores leaf nodes if (((GeneralFile) path.getLastPathComponent()).isFile()) { path = path.getParentPath(); } setSelectionRow(0); // TODO perhaps a refresh should be built into JargonTreeModel setModel(new JargonTreeModel(roots, selects)); expandPath(path); }
/* Methods for DragGestureListener */ public final void dragGestureRecognized(DragGestureEvent dge) { TreePath path = tree.getSelectionPath(); if (path != null) { draggedPath = path; draggedNodeParentPath = path.getParentPath(); if (drawImage) { Rectangle pathBounds = tree.getPathBounds(path); // getpathbounds of selectionpath JComponent lbl = (JComponent) tree.getCellRenderer() .getTreeCellRendererComponent( tree, draggedPath.getLastPathComponent(), false, tree.isExpanded(path), false, 0, false); // returning the label lbl.setBounds(pathBounds); // setting bounds to lbl image = new BufferedImage( lbl.getWidth(), lbl.getHeight(), java.awt.image.BufferedImage .TYPE_INT_ARGB_PRE); // buffered image reference passing the label's ht and // width Graphics2D graphics = image.createGraphics(); // creating the graphics for buffered image graphics.setComposite( AlphaComposite.getInstance( AlphaComposite.SRC_OVER, 0.5f)); // Sets the Composite for the Graphics2D context lbl.setOpaque(false); lbl.paint(graphics); // painting the graphics to label graphics.dispose(); } dragSource.startDrag( dge, DragSource.DefaultMoveNoDrop, image, new Point(0, 0), new TransferableNode(draggedPath), this); } }
/** * Add a legend to the tree, in the currently selected RuleWrapper, after the currently selected * Legend (if any in both case). A RuleWrapper will be added in the case there is none. */ private void addLegend() { LegendUIChooser legendPicker = new LegendUIChooser(simpleStyleEditor); if (UIFactory.showDialog(legendPicker)) { // Recover the panel that was selected when the user clicked OK. ILegendPanel ilp = legendPicker.getSelectedPanel(); // Get the currently selected RuleWrapper, or the last one in this // style if none is currently selected. RuleWrapper currentrw = getSelectedRule(); StyleWrapper sw = simpleStyleEditor.getStyleWrapper(); if (currentrw == null) { if (sw.getSize() == 0) { addRule(); } currentrw = sw.getRuleWrapper(sw.getSize() - 1); } // Set the Legend's name. Legend legend = ilp.getLegend(); legend .getSymbolizer() .setName(getUniqueName(legend.getLegendTypeName(), currentrw.getRule(), 0)); // Add the panel to the LegendTree. ((LegendTreeModel) tree.getModel()).addElement(currentrw, ilp, getSelectedLegend()); // Automatically select the newly added legend in the tree. TreePath selectionPath = tree.getSelectionPath(); TreePath parent; if (selectionPath.getLastPathComponent() instanceof RuleWrapper) { parent = selectionPath; } else { parent = selectionPath.getParentPath(); } tree.setSelectionPath(parent.pathByAddingChild(ilp)); // Notify the SimpleStyleEditor that a Legend has been added. simpleStyleEditor.legendAdded(ilp); } }
private void myDoubleClick(int row, TreePath path) { if (path != null) { JFrame infoFrame = new JFrame("Object Info for: " + path); JLabel objectLabel; JLabel parentPathLabel; // Display Selected Object Name; objectLabel = new JLabel("Object: " + path.getLastPathComponent().toString()); objectLabel.setHorizontalAlignment(SwingConstants.CENTER); // Display specific object information parentPathLabel = new JLabel("Parent Path: " + path.getParentPath()); parentPathLabel.setHorizontalAlignment(SwingConstants.CENTER); // Construct infoFrame infoFrame.getContentPane().add(objectLabel, BorderLayout.CENTER); infoFrame.getContentPane().add(parentPathLabel, BorderLayout.SOUTH); infoFrame.pack(); infoFrame.setVisible(true); } }
// tells whether given path is selected. // if dig is true, then a path is assumed to be selected, if // one of its ancestor is selected. 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; }