private void toggleRemoveSelection(TreePath path){
  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=(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) {
        TreePath pathByAddingChild=temp.pathByAddingChild(childNode);
        super.addSelectionPaths(new TreePath[]{pathByAddingChild});
      }
    }
  }
  super.removeSelectionPaths(new TreePath[]{parent});
}

private void toggleRemoveSelection ( TreePath path ) { 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 = ( 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 ) { TreePath pathByAddingChild = temp . pathByAddingChild ( childNode ) ; super . addSelectionPaths ( new TreePath [ ] { pathByAddingChild } ) ; } } } super . removeSelectionPaths ( new TreePath [ ] { parent } ) ; }
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
