Java Code Examples for javax.swing.tree.TreeNode#getIndex()

The following examples show how to use javax.swing.tree.TreeNode#getIndex() . 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: TreeSearch.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the child in this node's child array that immediately follows
 * <code>aChild</code>, which must be a child of this node. If
 * <code>aChild</code> is the last child, returns null. This method performs
 * a linear search of this node's children for <code>aChild</code> and is
 * O(n) where n is the number of children; to traverse the entire array of
 * children, use an enumeration instead.
 *
 * @param parent
 * @param aChild
 * @see #children
 * @exception IllegalArgumentException if <code>aChild</code> is null or is
 * not a child of this node
 * @return the child of this node that immediately follows
 * <code>aChild</code>
 */
public TreeNode getChildAfter(TreeNode parent, TreeNode aChild) {
    if (aChild == null) {
        throw new IllegalArgumentException("argument is null");
    }

    int index = parent.getIndex(aChild);           // linear search

    if (index == -1) {
        throw new IllegalArgumentException("node is not a child");
    }

    if (index < parent.getChildCount() - 1) {
        return parent.getChildAt(index + 1);
    } else {
        return null;
    }
}
 
Example 2
Source File: TreeSearch.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the child in this node's child array that immediately precedes
 * <code>aChild</code>, which must be a child of this node. If
 * <code>aChild</code> is the first child, returns null. This method
 * performs a linear search of this node's children for <code>aChild</code>
 * and is O(n) where n is the number of children.
 *
 * @param parent
 * @param aChild
 * @exception IllegalArgumentException if <code>aChild</code> is null or is
 * not a child of this node
 * @return the child of this node that immediately precedes
 * <code>aChild</code>
 */
public TreeNode getChildBefore(TreeNode parent, TreeNode aChild) {
    if (aChild == null) {
        throw new IllegalArgumentException("argument is null");
    }

    int index = parent.getIndex(aChild);           // linear search

    if (index == -1) {
        throw new IllegalArgumentException("argument is not a child");
    }

    if (index > 0) {
        return parent.getChildAt(index - 1);
    } else {
        return null;
    }
}
 
Example 3
Source File: TreeUtil.java    From ganttproject with GNU General Public License v3.0 5 votes vote down vote up
static int getPrevSibling(TreeNode node, TreeNode child) {
  if (node == null) {
    return -1;
  }
  int childIndex = node.getIndex(child);
  return childIndex - 1;
}
 
Example 4
Source File: TreeUtil.java    From ganttproject with GNU General Public License v3.0 5 votes vote down vote up
static int getNextSibling(TreeNode node, TreeNode child) {
  if (node == null) {
    return -1;
  }
  int childIndex = node.getIndex(child);
  return childIndex == node.getChildCount() - 1 ? -1 : childIndex + 1;
}
 
Example 5
Source File: GroupedTreeRowPainter.java    From weblaf with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void addNumerationStates ( final List<String> states, final TreePath path )
{
    // Ensure it is not root and that we are working with nodes
    final Object value = path.getLastPathComponent ();
    final Object root = component.getModel ().getRoot ();
    if ( value instanceof TreeNode && root instanceof TreeNode && value != root )
    {
        // Finding out first level node in path
        final TreeNode rootNode = ( TreeNode ) root;
        TreeNode firstLevel = ( TreeNode ) value;
        TreeNode parent = firstLevel.getParent ();
        while ( parent != rootNode && parent != null )
        {
            firstLevel = parent;
            parent = firstLevel.getParent ();
        }

        // Calculating general even/odd state
        final int indexOnFirstLevel = rootNode.getIndex ( firstLevel );
        final boolean odd = indexOnFirstLevel % 2 == 0;
        states.add ( odd ? DecorationState.odd : DecorationState.even );

        // Calculating extra even/odd state
        final int innerIndex = row - component.getRowForPath ( new TreePath ( TreeUtils.getPath ( firstLevel ) ) );
        final boolean innerOdd = innerIndex % 2 == 0;
        states.add ( innerOdd ? DecorationState.innerOdd : DecorationState.innerEven );
    }
    else
    {
        // Using default implementation
        super.addNumerationStates ( states, path );
    }
}
 
Example 6
Source File: IndexTreePathState.java    From consulo with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 7
Source File: TemplateListPanel.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void removeNodeFromParent(DefaultMutableTreeNode node) {
  TreeNode parent = node.getParent();
  int idx = parent.getIndex(node);
  node.removeFromParent();

  ((DefaultTreeModel)myTree.getModel()).nodesWereRemoved(parent, new int[]{idx}, new TreeNode[]{node});
}
 
Example 8
Source File: VimDasaView.java    From Astrosoft with GNU General Public License v2.0 4 votes vote down vote up
private void setRowSelected(TreeNode parent, TreeNode node){
	int index = parent.getIndex(node);
	dasaTable.getSelectionModel().setLeadSelectionIndex(index);
}
 
Example 9
Source File: TodoPanel.java    From consulo with Apache License 2.0 4 votes vote down vote up
private boolean isFirst(final TreeNode node) {
  final TreeNode parent = node.getParent();
  return parent == null || parent.getIndex(node) == 0 && isFirst(parent);
}
 
Example 10
Source File: PrideXMLDisplay.java    From PDV with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Tree valueChanged
 * @param evt TreeSelectionEvent
 */
private void valueChanged(TreeSelectionEvent evt) {

    JTree tree = (JTree)evt.getSource();
    DefaultMutableTreeNode selectionNode = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();

    TreeNode parentNode = selectionNode.getParent();

    String nodeName = selectionNode.toString().split("\\(")[0];

    if(titleToPrideXmlReader.containsKey(nodeName)){
        currentPrideXmlReader = titleToPrideXmlReader.get(nodeName);
        currentProtein = selectionNode.getFirstChild().toString().split("\\(")[0];

        currentFileCount = parentNode.getIndex(selectionNode) + 1;

        currentProteinCount = 1;

        updatePSMJTable();

        updateDetails();

        updateSpectrumTable();

    } else if(!nodeName.equals("Files")){
        if(currentPrideXmlReader != titleToPrideXmlReader.get(parentNode.toString().split("\\(")[0])){
            currentPrideXmlReader = titleToPrideXmlReader.get(parentNode.toString().split("\\(")[0]);
            updateDetails();

            updateSpectrumTable();
        }

        currentProtein = nodeName;

        TreeNode grandNode = parentNode.getParent();

        currentFileCount = grandNode.getIndex(parentNode) + 1;

        currentProteinCount = parentNode.getIndex(selectionNode) + 1;

        updatePSMJTable();
    }

    tree.requestFocus();
}
 
Example 11
Source File: ConfigTreeModel.java    From pentaho-reporting with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Returns the index of child in parent.  If <code>parent</code> is <code>null</code> or <code>child</code> is
 * <code>null</code>, returns -1.
 *
 * @param parent a note in the tree, obtained from this data source
 * @param child  the node we are interested in
 * @return the index of the child in the parent, or -1 if either <code>child</code> or <code>parent</code> are
 * <code>null</code>
 */
public int getIndexOfChild( final Object parent, final Object child ) {
  final TreeNode node = (TreeNode) parent;
  final TreeNode childNode = (TreeNode) child;
  return node.getIndex( childNode );
}