private static boolean isMoveSupported(JTree tree, int dir) { final TreePath[] selectionPaths = tree.getSelectionPaths(); if (selectionPaths != null) { DefaultMutableTreeNode parent = null; for (TreePath treePath : selectionPaths) if (treePath.getLastPathComponent() != null) { final DefaultMutableTreeNode node = (DefaultMutableTreeNode) treePath.getLastPathComponent(); if (parent == null) { parent = (DefaultMutableTreeNode) node.getParent(); } if (parent != node.getParent()) { return false; } if (dir > 0) { if (parent.getIndex(node) == parent.getChildCount() - 1) { return false; } } else { if (parent.getIndex(node) == 0) { return false; } } } return true; } return false; }
// {{{ 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(); } } // }}}
// {{{ getTreeCellRendererComponent() method @Override protected void configureTreeCellRendererComponent( JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) { setIcon(null); DefaultMutableTreeNode node = (DefaultMutableTreeNode) value; if (node.getUserObject() instanceof HyperSearchOperationNode) { setFont(boldFont); CountNodes countNodes = new CountNodes(); traverseNodes(node, countNodes); setText( jEdit.getProperty( "hypersearch-results.result-caption", new Object[] { node.toString(), Integer.valueOf(countNodes.resultCount), Integer.valueOf(countNodes.bufferCount) })); } else if (node.getUserObject() instanceof HyperSearchFolderNode) { setFont(plainFont); setText(node.toString() + " (" + node.getChildCount() + " files/folders)"); } else if (node.getUserObject() instanceof HyperSearchFileNode) { // file name setFont(boldFont); HyperSearchFileNode hyperSearchFileNode = (HyperSearchFileNode) node.getUserObject(); setText( jEdit.getProperty( "hypersearch-results.file-caption", new Object[] { hyperSearchFileNode, Integer.valueOf(hyperSearchFileNode.getCount()), Integer.valueOf(node.getChildCount()) })); } else { setFont(plainFont); } } // }}}
public static void sort(final DefaultMutableTreeNode node, final Comparator comparator) { final List<TreeNode> children = childrenToArray(node); Collections.sort(children, comparator); node.removeAllChildren(); addChildrenTo(node, children); for (int i = 0; i < node.getChildCount(); i++) { sort((DefaultMutableTreeNode) node.getChildAt(i), comparator); } }
/** * Adds the nodes necessary to display the property values for the given configuration element * described by the given property definition. */ private void addProperty(ConfigElement elt, PropertyDefinition propDef) { DefaultMutableTreeNode parent = getNodeFor(elt); // This property has only one value, add it in if ((propDef.getPropertyValueDefinitionCount() == 1) && (!propDef.isVariable()) && (propDef.getType() != ConfigElement.class)) { int idx = parent.getChildCount(); // System.out.println("Adding property node for single-valued property: "+ // propDesc.getToken()+"["+idx+"]"); Object value = elt.getProperty(propDef.getToken(), 0); DefaultMutableTreeNode value_node = new DefaultMutableTreeNode(value); insertNodeInto(value_node, parent, idx); } // This property has (or can have) more than one value, add a node that is // the parent for all its values. else { int idx = parent.getChildCount(); // System.out.println("Adding property node for multi-valued property: "+ // propDesc.getToken()+"["+idx+"]"); DefaultMutableTreeNode prop_def_node = new DefaultMutableTreeNode(propDef); insertNodeInto(prop_def_node, parent, idx); parent = prop_def_node; int num_props = elt.getPropertyValueCount(propDef.getToken()); for (int i = 0; i < num_props; ++i) { Object value = elt.getProperty(propDef.getToken(), i); if (propDef.getType() != ConfigElement.class) { // System.out.println("Adding property value: // "+propDesc.getToken()+"["+i+"]"); DefaultMutableTreeNode child = new DefaultMutableTreeNode(value); int prop_idx = parent.getChildCount(); insertNodeInto(child, parent, prop_idx); } else { addEmbeddedElement(parent, (ConfigElement) value, i); } } } }
private static MyTreeNode ensureGroup( @NotNull DefaultMutableTreeNode root, @NotNull List<String> path, int index) { String groupName = path.get(index++); for (int i = 0; i < root.getChildCount(); i++) { TreeNode child = root.getChildAt(i); if (child instanceof MyTreeNode && groupName.equals(child.toString())) { return index < path.size() - 1 ? ensureGroup((MyTreeNode) child, path, index) : (MyTreeNode) child; } } MyTreeNode groupNode = new MyTreeNode(groupName); root.add(groupNode); return index < path.size() - 1 ? ensureGroup(groupNode, path, index) : groupNode; }
private void restoreUsageExpandState(final Collection<UsageState> states) { // always expand the last level group final DefaultMutableTreeNode root = (DefaultMutableTreeNode) myTree.getModel().getRoot(); for (int i = root.getChildCount() - 1; i >= 0; i--) { final DefaultMutableTreeNode child = (DefaultMutableTreeNode) root.getChildAt(i); if (child instanceof GroupNode) { final TreePath treePath = new TreePath(child.getPath()); myTree.expandPath(treePath); } } myTree.getSelectionModel().clearSelection(); for (final UsageState usageState : states) { usageState.restore(); } }
@Nullable public static DefaultMutableTreeNode findNodeWithObject( final DefaultMutableTreeNode aRoot, final Object aObject) { if (Comparing.equal(aRoot.getUserObject(), aObject)) { return aRoot; } else { for (int i = 0; i < aRoot.getChildCount(); i++) { final DefaultMutableTreeNode candidate = findNodeWithObject((DefaultMutableTreeNode) aRoot.getChildAt(i), aObject); if (null != candidate) { return candidate; } } return null; } }
private void captureUsagesExpandState(TreePath pathFrom, final Collection<UsageState> states) { if (!myTree.isExpanded(pathFrom)) { return; } final DefaultMutableTreeNode node = (DefaultMutableTreeNode) pathFrom.getLastPathComponent(); final int childCount = node.getChildCount(); for (int idx = 0; idx < childCount; idx++) { final TreeNode child = node.getChildAt(idx); if (child instanceof UsageNode) { final Usage usage = ((UsageNode) child).getUsage(); states.add( new UsageState( usage, myTree.getSelectionModel().isPathSelected(pathFrom.pathByAddingChild(child)))); } else { captureUsagesExpandState(pathFrom.pathByAddingChild(child), states); } } }
/** * @param searchNode the result node * @param selectNode the node that must be selected, or null * @since jEdit 4.3pre12 */ public void searchDone( final DefaultMutableTreeNode searchNode, final DefaultMutableTreeNode selectNode) { stop.setEnabled(false); final int nodeCount = searchNode.getChildCount(); if (nodeCount < 1) { searchFailed(); return; } caption.setText( jEdit.getProperty("hypersearch-results.done", new String[] {trimSearchString()})); EventQueue.invokeLater( new Runnable() { @Override public void run() { if (!multiStatus) { for (int i = 0; i < resultTreeRoot.getChildCount(); i++) { resultTreeRoot.remove(0); } } resultTreeRoot.add(searchNode); resultTreeModel.reload(resultTreeRoot); for (int i = 0; i < nodeCount; i++) { TreePath lastNode = new TreePath(((DefaultMutableTreeNode) searchNode.getChildAt(i)).getPath()); resultTree.expandPath(lastNode); } TreePath treePath; if (selectNode == null) { treePath = new TreePath(new Object[] {resultTreeRoot, searchNode}); } else { treePath = new TreePath(selectNode.getPath()); } resultTree.setSelectionPath(treePath); resultTree.scrollPathToVisible(treePath); } }); } // }}}
/** Sets the value for the given node at the given column to the given value. */ public void setValueAt(Object value, Object node, int col) { DefaultMutableTreeNode tree_node = (DefaultMutableTreeNode) node; Object node_value = tree_node.getUserObject(); switch (col) { // Name (not supported) case 0: break; // Value case 1: DefaultMutableTreeNode parent_node = (DefaultMutableTreeNode) tree_node.getParent(); // First child of root is always the element name if (parent_node == getRoot() && parent_node.getIndex((TreeNode) node) == 0) { ConfigElement elt = (ConfigElement) parent_node.getUserObject(); elt.setName((String) value); tree_node.setUserObject(value); } else if (node_value instanceof PropertyDefinition) { // Hey, we're editing a property definition. If it's type is not a // configuration element, we're probably editing a summary list of // the valuesof the children. if (((PropertyDefinition) node_value).getType() != ConfigElement.class) { ConfigElement elt = (ConfigElement) parent_node.getUserObject(); PropertyDefinition prop_def = (PropertyDefinition) node_value; StringTokenizer tokenizer = new StringTokenizer((String) value, ", "); int idx = 0; while (tokenizer.hasMoreTokens()) { String token = tokenizer.nextToken(); // Make sure we don't overrun the property values if ((idx >= prop_def.getPropertyValueDefinitionCount()) && (!prop_def.isVariable())) { break; } // Convert the value to the appropriate type Object new_value = null; Class type = prop_def.getType(); if (type == Boolean.class) { new_value = new Boolean(token); } else if (type == Integer.class) { new_value = new Integer(token); } else if (type == Float.class) { new_value = new Float(token); } else if (type == String.class) { new_value = new String(token); } else if (type == ConfigElementPointer.class) { new_value = new ConfigElementPointer(token); } setProperty(new_value, elt, prop_def.getToken(), idx); // Get the node for the current property value and update it if (idx < tree_node.getChildCount()) { DefaultMutableTreeNode child_node = (DefaultMutableTreeNode) tree_node.getChildAt(idx); child_node.setUserObject(new_value); } else { // Insert the new property DefaultMutableTreeNode new_node = new DefaultMutableTreeNode(new_value); tree_node.add(new_node); fireTreeNodesInserted( this, new Object[] {getPathToRoot(tree_node)}, new int[] {tree_node.getIndex(new_node)}, new Object[] {new_node}); } ++idx; } } } else { // Parent is a ConfigElement ... must be a single-valued property if (parent_node.getUserObject() instanceof ConfigElement) { ConfigElement elt = (ConfigElement) parent_node.getUserObject(); int desc_idx = parent_node.getIndex(tree_node); // If the parent is the root, take into account the extra name // and type nodes if (parent_node == getRoot()) { desc_idx -= 2; } PropertyDefinition prop_def = (PropertyDefinition) elt.getDefinition().getPropertyDefinitions().get(desc_idx); setProperty(value, elt, prop_def.getToken(), 0); tree_node.setUserObject(value); } else { // Parent must be a PropertyDefinition PropertyDefinition prop_def = (PropertyDefinition) parent_node.getUserObject(); int value_idx = parent_node.getIndex(tree_node); DefaultMutableTreeNode elt_node = (DefaultMutableTreeNode) parent_node.getParent(); ConfigElement elt = (ConfigElement) elt_node.getUserObject(); setProperty(value, elt, prop_def.getToken(), value_idx); tree_node.setUserObject(value); } } fireTreeNodesChanged( this, new Object[] {getPathToRoot(parent_node)}, new int[] {parent_node.getIndex(tree_node)}, new Object[] {tree_node}); break; default: throw new IllegalArgumentException("Invalid column: " + col); } }