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});
    }
  }
  // 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});
  }
 /**
  * Overrides the super to prevent selection of place holder accounts
  *
  * @param path new path to select
  */
 @Override
 public void setSelectionPath(final TreePath path) {
   if (path != null) {
     Object o = path.getLastPathComponent();
     if (o != null) {
       Account a = (Account) ((DefaultMutableTreeNode) o).getUserObject();
       if (a != null && isAccountEnabled(a)) {
         super.setSelectionPath(path);
       }
     }
   } else {
     super.setSelectionPath(null);
   }
 }
 public void removeSelectionPaths(TreePath[] paths) {
   for (int i = 0; i < paths.length; i++) {
     TreePath path = paths[i];
     if (path.getPathCount() == 1) super.removeSelectionPaths(new TreePath[] {path});
     else toggleRemoveSelection(path);
   }
 }
 @Override
 public void setSelectionPaths(TreePath[] paths) {
   LinkedList<TreePath> filteredPaths = new LinkedList<TreePath>();
   for (TreePath path : paths) {
     if (path.getLastPathComponent() instanceof HierarchicalData) filteredPaths.add(path);
   }
   super.setSelectionPaths(filteredPaths.toArray(new TreePath[0]));
 }
 @Override
 protected void fireValueChanged(TreeSelectionEvent e) {
   if (e.isAddedPath()) {
     HierarchicalData selectedObject = (HierarchicalData) e.getPath().getLastPathComponent();
     tree.setSelectedNode(selectedObject);
   } else if (getSelectionCount() > 0) {
     HierarchicalData selectedObject =
         (HierarchicalData) getSelectionPaths()[0].getLastPathComponent();
     tree.setSelectedNode(selectedObject);
   } else tree.setSelectedNode(null);
   super.fireValueChanged(e);
 }
Esempio n. 7
0
  public Browser(CavityNestingDB cndb) throws SQLException {
    super(new BorderLayout());
    setPreferredSize(new Dimension(300, 500));
    db = cndb;
    displayer = new NodeDisplay();

    top = new DefaultMutableTreeNode("CavityNestingDB");
    tree = new JTree(top);
    DefaultTreeSelectionModel selModel = new DefaultTreeSelectionModel();
    selModel.setSelectionMode(DefaultTreeSelectionModel.SINGLE_TREE_SELECTION);
    tree.setSelectionModel(selModel);

    tree.addTreeSelectionListener(
        new TreeSelectionListener() {
          public void valueChanged(TreeSelectionEvent e) {
            if (e.isAddedPath()) {
              TreePath path = e.getPath();
              DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
              Object value = node.getUserObject();

              // System.out.println(String.valueOf(value));
              if (value instanceof CavityDBObject) {
                displayer.display((CavityDBObject) value);
              } else {
                // System.out.println(value.getClass().getSimpleName());
              }
            }
          }
        });

    JSplitPane splitter = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    add(splitter, BorderLayout.CENTER);
    splitter.add(new JScrollPane(tree));
    splitter.add(displayer);

    populate();
  }
    /**
     * This is overridden to set <code>updatingListSelectionModel</code> and message super. This is
     * the only place DefaultTreeSelectionModel alters the ListSelectionModel.
     */
    public void resetRowSelection() {
      if (!updatingListSelectionModel) {
        updatingListSelectionModel = true;
        try {
          super.resetRowSelection();
        } finally {
          updatingListSelectionModel = false;
        }
      }

      // Notice how we don't message super if
      // updatingListSelectionModel is true. If
      // updatingListSelectionModel is true, it implies the
      // ListSelectionModel has already been updated and the
      // paths are the only thing that needs to be updated.
    }
 @Override
 public void setSelectionPath(TreePath path) {
   if (path.getLastPathComponent() instanceof HierarchicalData) super.setSelectionPath(path);
 }