Search in sources :

Example 6 with Path

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

the class Pivot734WithWorkaround method controlTree.

private void controlTree(BXMLSerializer bxmlSerializer) {
    treeButtonAdd = (PushButton) bxmlSerializer.getNamespace().get("treeButtonAdd");
    treeButtonRemove = (PushButton) bxmlSerializer.getNamespace().get("treeButtonRemove");
    tree = (TreeView) bxmlSerializer.getNamespace().get("tree");
    boolean treeStyleForShowEmptyBranchControls = tree.getStyles().getBoolean(Style.showEmptyBranchControls);
    System.out.println("tree style for showEmptyBranchControls is " + treeStyleForShowEmptyBranchControls);
    tree.getTreeViewSelectionListeners().add(new TreeViewSelectionListener() {

        @Override
        public void selectedPathAdded(TreeView treeView, Path path) {
            System.out.println("selectedPathAdded");
        }

        @Override
        public void selectedPathRemoved(TreeView treeView, Path path) {
            System.out.println("selectedPathRemoved");
        }

        @Override
        public void selectedPathsChanged(TreeView treeView, Sequence<Path> previousSelectedPaths) {
            System.out.println("selectedPathsChanged");
        }

        @Override
        public void selectedNodeChanged(TreeView treeView, Object previousSelectedNode) {
            System.out.println("selectedNodeChanged");
        }
    });
    treeButtonAdd.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            Object x = tree.getSelectedNode();
            System.out.println("add a 'new branch' element to the selected element :: " + x);
            if (x != null && x instanceof TreeBranch) {
                // workaround for PIVOT-734
                Path path = tree.getSelectedPath();
                tree.setBranchExpanded(path, true);
                // end of workaround for PIVOT-734
                ((TreeBranch) x).add(newBranch);
            }
        }
    });
    treeButtonRemove.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            Object x = tree.getSelectedNode();
            System.out.println("remove a 'new branch' element under the selected element :: " + x);
            if (x != null && x instanceof TreeBranch) {
                ((TreeBranch) x).remove(newBranch);
            }
        }
    });
}
Also used : Path(org.apache.pivot.collections.Sequence.Tree.Path) ButtonPressListener(org.apache.pivot.wtk.ButtonPressListener) TreeBranch(org.apache.pivot.wtk.content.TreeBranch) PushButton(org.apache.pivot.wtk.PushButton) Button(org.apache.pivot.wtk.Button) TreeViewSelectionListener(org.apache.pivot.wtk.TreeViewSelectionListener) TreeView(org.apache.pivot.wtk.TreeView)

Example 7 with Path

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

the class TreeView method expandToBranch.

/**
 * Expand "to" the given branch, which means opening up all the parents so that
 * this branch is exposed.
 *
 * @param path The path to the branch to expose.
 */
public final void expandToBranch(Path path) {
    checkNullOrEmpty(path);
    for (int i = 0; i < path.getLength(); i++) {
        Sequence.Tree.Path incrementalPath = new Sequence.Tree.Path(path, i + 1);
        expandBranch(incrementalPath);
    }
}
Also used : Path(org.apache.pivot.collections.Sequence.Tree.Path) ImmutablePath(org.apache.pivot.collections.Sequence.Tree.ImmutablePath) Path(org.apache.pivot.collections.Sequence.Tree.Path) Sequence(org.apache.pivot.collections.Sequence)

Example 8 with Path

use of org.apache.pivot.collections.Sequence.Tree.Path 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 9 with Path

use of org.apache.pivot.collections.Sequence.Tree.Path 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 10 with Path

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

the class JSONViewer method setValue.

private void setValue(Object value) {
    assert (value instanceof Map<?, ?> || value instanceof List<?>);
    // Remove prompt decorator
    if (promptDecorator != null) {
        treeView.getDecorators().remove(promptDecorator);
        promptDecorator = null;
    }
    TreeBranch treeData = new TreeBranch();
    treeData.add(build(value));
    treeView.setTreeData(treeData);
    treeView.expandBranch(new Path(0));
}
Also used : Path(org.apache.pivot.collections.Sequence.Tree.Path) TreeBranch(org.apache.pivot.wtk.content.TreeBranch) List(org.apache.pivot.collections.List) FileList(org.apache.pivot.io.FileList) Map(org.apache.pivot.collections.Map)

Aggregations

Path (org.apache.pivot.collections.Sequence.Tree.Path)20 TreeView (org.apache.pivot.wtk.TreeView)11 GeneralPath (java.awt.geom.GeneralPath)7 TreeViewSelectionListener (org.apache.pivot.wtk.TreeViewSelectionListener)5 ImmutablePath (org.apache.pivot.collections.Sequence.Tree.ImmutablePath)4 Button (org.apache.pivot.wtk.Button)4 ButtonPressListener (org.apache.pivot.wtk.ButtonPressListener)4 PushButton (org.apache.pivot.wtk.PushButton)4 TreeBranch (org.apache.pivot.wtk.content.TreeBranch)4 IOException (java.io.IOException)3 List (org.apache.pivot.collections.List)3 SerializationException (org.apache.pivot.serialization.SerializationException)3 TreeNode (org.apache.pivot.wtk.content.TreeNode)3 BXMLSerializer (org.apache.pivot.beans.BXMLSerializer)2 ArrayList (org.apache.pivot.collections.ArrayList)2 Sequence (org.apache.pivot.collections.Sequence)2 Component (org.apache.pivot.wtk.Component)2 ComponentMouseButtonListener (org.apache.pivot.wtk.ComponentMouseButtonListener)2 SelectMode (org.apache.pivot.wtk.TreeView.SelectMode)2 Method (java.lang.reflect.Method)1