package com.vci.client.ui.tree; import java.util.ArrayList; import java.util.Stack; import javax.swing.tree.DefaultTreeSelectionModel; import javax.swing.tree.TreeModel; import javax.swing.tree.TreePath; import javax.swing.tree.TreeSelectionModel; public class CheckBoxTreeSelectionModel extends DefaultTreeSelectionModel { /** * */ private static final long serialVersionUID = -3647943660639834124L; private TreeModel model; public CheckBoxTreeSelectionModel(TreeModel model) { this.model = model; setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION); } /** * tests whether there is any unselected node in the subtree of given path * * @param path * @return */ public boolean isPartiallySelected(TreePath path){ if(isPathSelected(path, true)) { return false; } TreePath[] selectionPaths = getSelectionPaths(); if(selectionPaths == null) { return false; } for(int j = 0; j < selectionPaths.length; j++){ if(isDescendant(selectionPaths[j], path)) { return true; } } return false; } /** * 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. * @param path * @param dig * @return */ 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; } /** * is path1 descendant of path2 * * @param path1 * @param path2 * @return */ private boolean isDescendant(TreePath path1, TreePath path2){ Object obj1[] = path1.getPath(); Object obj2[] = path2.getPath(); for(int i = 0; i