Search in sources :

Example 1 with ImmutablePath

use of org.apache.pivot.collections.Sequence.Tree.ImmutablePath in project pivot by apache.

the class TreeView method setNodeChecked.

/**
 * Sets the check state of the node at the specified path. If the node
 * already has the specified check state, nothing happens. <p> Note that it
 * is impossible to set the check state of a node to <tt>MIXED</tt>. This is
 * because the mixed check state is a derived state meaning "the node is not
 * checked, but one or more of its descendants are."
 *
 * @param path The path to the node.
 * @param checked <tt>true</tt> to check the node; <tt>false</tt> to uncheck it.
 * @throws IllegalStateException If checkmarks are not enabled (see
 * {@link #getCheckmarksEnabled()}).
 * @see NodeCheckState#MIXED
 */
public void setNodeChecked(Path path, boolean checked) {
    checkNullOrEmpty(path);
    if (!checkmarksEnabled) {
        throw new IllegalStateException("Checkmarks are not enabled.");
    }
    int index = checkedPaths.indexOf(path);
    if ((index < 0 && checked) || (index >= 0 && !checked)) {
        NodeCheckState previousCheckState = getNodeCheckState(path);
        Sequence<NodeCheckState> ancestorCheckStates = null;
        if (showMixedCheckmarkState) {
            // Record the check states of our ancestors before we change
            // anything so we know which events to fire after we're done
            ancestorCheckStates = new ArrayList<>(path.getLength() - 1);
            Path ancestorPath = new Path(path, path.getLength() - 1);
            for (int i = ancestorPath.getLength() - 1; i >= 0; i--) {
                ancestorCheckStates.insert(getNodeCheckState(ancestorPath), 0);
                ancestorPath.remove(i, 1);
            }
        }
        if (checked) {
            // Monitor the path's parent
            monitorBranch(path, false);
            // Update the checked paths
            checkedPaths.add(new ImmutablePath(path));
        } else {
            // Update the checked paths
            checkedPaths.remove(index, 1);
        }
        // Notify listeners
        treeViewNodeStateListeners.nodeCheckStateChanged(this, path, previousCheckState);
        if (showMixedCheckmarkState && ancestorCheckStates != null) {
            // Notify listeners of any changes to our ancestors' check
            // states
            Path ancestorPath = new Path(path, path.getLength() - 1);
            for (int i = ancestorPath.getLength() - 1; i >= 0; i--) {
                NodeCheckState ancestorPreviousCheckState = ancestorCheckStates.get(i);
                NodeCheckState ancestorCheckState = getNodeCheckState(ancestorPath);
                if (ancestorCheckState != ancestorPreviousCheckState) {
                    treeViewNodeStateListeners.nodeCheckStateChanged(this, ancestorPath, ancestorPreviousCheckState);
                }
                ancestorPath.remove(i, 1);
            }
        }
    }
}
Also used : Path(org.apache.pivot.collections.Sequence.Tree.Path) ImmutablePath(org.apache.pivot.collections.Sequence.Tree.ImmutablePath) ImmutablePath(org.apache.pivot.collections.Sequence.Tree.ImmutablePath)

Example 2 with ImmutablePath

use of org.apache.pivot.collections.Sequence.Tree.ImmutablePath in project pivot by apache.

the class TreeView method setSelectedPaths.

/**
 * Set the new selected nodes in the tree.
 *
 * @param selectedPaths The new set of paths to the selected nodes.
 * @return The new set of selected paths (with duplicates eliminated).
 * @throws IllegalStateException If selection has been disabled (select mode
 * <tt>NONE</tt>).
 */
public Sequence<Path> setSelectedPaths(Sequence<Path> selectedPaths) {
    Utils.checkNull(selectedPaths, "Selected paths");
    if (selectMode == SelectMode.NONE) {
        throw new IllegalStateException("Selection is not enabled.");
    }
    if (selectMode == SelectMode.SINGLE && selectedPaths.getLength() > 1) {
        throw new IllegalArgumentException("Cannot select more than one path in SINGLE select mode.");
    }
    Sequence<Path> previousSelectedPaths = this.selectedPaths;
    Object previousSelectedNode = (selectMode == SelectMode.SINGLE) ? getSelectedNode() : null;
    if (selectedPaths != previousSelectedPaths) {
        this.selectedPaths = new ArrayList<>(PATH_COMPARATOR);
        for (int i = 0, n = selectedPaths.getLength(); i < n; i++) {
            Path path = selectedPaths.get(i);
            // Monitor the branch itself, because if showEmptyBranchControls is false
            // we need repaints as children are added/removed from this branch.
            monitorBranch(path, true);
            // Update the selection
            this.selectedPaths.add(new ImmutablePath(path));
        }
        // Notify listeners
        treeViewSelectionListeners.selectedPathsChanged(this, previousSelectedPaths);
        if (selectMode == SelectMode.SINGLE) {
            treeViewSelectionListeners.selectedNodeChanged(TreeView.this, previousSelectedNode);
        }
    }
    return getSelectedPaths();
}
Also used : Path(org.apache.pivot.collections.Sequence.Tree.Path) ImmutablePath(org.apache.pivot.collections.Sequence.Tree.ImmutablePath) ImmutablePath(org.apache.pivot.collections.Sequence.Tree.ImmutablePath)

Example 3 with ImmutablePath

use of org.apache.pivot.collections.Sequence.Tree.ImmutablePath in project pivot by apache.

the class TreeView method setBranchExpanded.

/**
 * Sets the expansion state of the specified branch. If the branch already
 * has the specified expansion state, nothing happens.
 * <p> The listeners are polled first to give them a chance to veto the operation
 * for any reason. If the vote passes, then the state is changed.
 *
 * @param path The path to the branch node.
 * @param expanded <tt>true</tt> to expand the branch; <tt>false</tt> to
 * collapse it.
 */
public void setBranchExpanded(Path path, boolean expanded) {
    checkNullOrEmpty(path);
    // Give listeners a chance to veto the operation
    Vote vote = treeViewBranchListeners.previewBranchExpandedChange(this, path);
    if (vote != Vote.APPROVE) {
        treeViewBranchListeners.branchExpandedChangeVetoed(this, path, vote);
    } else {
        int index = expandedPaths.indexOf(path);
        if (expanded && index < 0) {
            // Monitor the branch itself
            monitorBranch(path, true);
            // Update the expanded paths
            expandedPaths.add(new ImmutablePath(path));
            // Notify listeners
            treeViewBranchListeners.branchExpanded(this, path);
        } else if (!expanded && index >= 0) {
            // Update the expanded paths
            expandedPaths.remove(index, 1);
            // Notify listeners
            treeViewBranchListeners.branchCollapsed(this, path);
        }
    }
}
Also used : Vote(org.apache.pivot.util.Vote) ImmutablePath(org.apache.pivot.collections.Sequence.Tree.ImmutablePath)

Example 4 with ImmutablePath

use of org.apache.pivot.collections.Sequence.Tree.ImmutablePath in project pivot by apache.

the class TreeView method addSelectedPath.

/**
 * Adds a path to the selection.
 *
 * @param path The path to the node to be added to the selection.
 * @return <tt>true</tt> if the path was added to the selection;
 * <tt>false</tt>, otherwise.
 * @throws IllegalStateException If multi-select is not enabled.
 */
public boolean addSelectedPath(Path path) {
    checkNullOrEmpty(path);
    if (selectMode != SelectMode.MULTI) {
        throw new IllegalStateException("Tree view is not in multi-select mode.");
    }
    int index = selectedPaths.indexOf(path);
    if (index < 0) {
        // Monitor the path's parent
        monitorBranch(path, false);
        // Update the selection
        selectedPaths.add(new ImmutablePath(path));
        // Notify listeners
        treeViewSelectionListeners.selectedPathAdded(this, path);
        treeViewSelectionListeners.selectedPathsChanged(this, null);
    }
    return (index < 0);
}
Also used : ImmutablePath(org.apache.pivot.collections.Sequence.Tree.ImmutablePath)

Aggregations

ImmutablePath (org.apache.pivot.collections.Sequence.Tree.ImmutablePath)4 Path (org.apache.pivot.collections.Sequence.Tree.Path)2 Vote (org.apache.pivot.util.Vote)1