Search in sources :

Example 11 with Path

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

the class TerraTreeViewSkin method keyReleased.

/**
 * {@link KeyCode#SPACE SPACE} toggles check mark selection when select mode
 * is {@link SelectMode#SINGLE}
 */
@Override
public boolean keyReleased(Component component, int keyCode, Keyboard.KeyLocation keyLocation) {
    boolean consumed = false;
    TreeView treeView = (TreeView) getComponent();
    if (keyCode == Keyboard.KeyCode.SPACE) {
        if (treeView.getCheckmarksEnabled() && treeView.getSelectMode() == TreeView.SelectMode.SINGLE) {
            Path selectedPath = treeView.getSelectedPath();
            if (selectedPath != null) {
                NodeInfo nodeInfo = getNodeInfoAt(selectedPath);
                if (!nodeInfo.isCheckmarkDisabled()) {
                    treeView.setNodeChecked(selectedPath, !treeView.isNodeChecked(selectedPath));
                }
            }
        }
    } else {
        consumed = super.keyReleased(component, keyCode, keyLocation);
    }
    return consumed;
}
Also used : Path(org.apache.pivot.collections.Sequence.Tree.Path) GeneralPath(java.awt.geom.GeneralPath) TreeView(org.apache.pivot.wtk.TreeView)

Example 12 with Path

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

the class TerraTreeViewSkin method getNodeAt.

// TreeView.Skin methods
@Override
public Path getNodeAt(int y) {
    Path path = null;
    NodeInfo nodeInfo = getNodeInfoAt(y);
    if (nodeInfo != null) {
        path = nodeInfo.getPath();
    }
    return path;
}
Also used : Path(org.apache.pivot.collections.Sequence.Tree.Path) GeneralPath(java.awt.geom.GeneralPath)

Example 13 with Path

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

the class TerraTreeViewSkin method mouseDown.

@Override
public boolean mouseDown(Component component, Mouse.Button button, int x, int y) {
    boolean consumed = super.mouseDown(component, button, x, y);
    if (!consumed) {
        TreeView treeView = (TreeView) getComponent();
        NodeInfo nodeInfo = getNodeInfoAt(y);
        if (nodeInfo != null && !nodeInfo.isDisabled()) {
            int nodeHeight = getNodeHeight();
            int baseNodeX = (nodeInfo.depth - 1) * (indent + spacing);
            int nodeX = baseNodeX + (showBranchControls ? indent + spacing : 0);
            int nodeY = (y / (nodeHeight + VERTICAL_SPACING)) * (nodeHeight + VERTICAL_SPACING);
            int checkboxWidth = CHECKBOX.getWidth();
            int checkboxHeight = CHECKBOX.getHeight();
            int checkboxX = Math.max(indent - checkboxWidth, 0) / 2;
            int checkboxY = (nodeHeight - checkboxHeight) / 2;
            // Only proceed if the user DIDN'T click on a checkbox
            if (!treeView.getCheckmarksEnabled() || nodeInfo.isCheckmarkDisabled() || x < nodeX + checkboxX || x >= nodeX + checkboxX + checkboxWidth || y < nodeY + checkboxY || y >= nodeY + checkboxY + checkboxHeight) {
                Path path = nodeInfo.getPath();
                // a branch. If so, expand/collapse the branch
                if (showBranchControls && nodeInfo instanceof BranchInfo && x >= baseNodeX && x < baseNodeX + indent) {
                    BranchInfo branchInfo = (BranchInfo) nodeInfo;
                    treeView.setBranchExpanded(path, !branchInfo.isExpanded());
                    consumed = true;
                }
                // the selection state of the node
                if (!consumed) {
                    TreeView.SelectMode selectMode = treeView.getSelectMode();
                    if (button == Mouse.Button.LEFT) {
                        Keyboard.Modifier commandModifier = Platform.getCommandModifier();
                        if (Keyboard.isPressed(commandModifier) && selectMode == TreeView.SelectMode.MULTI) {
                            // Toggle the item's selection state
                            if (nodeInfo.isSelected()) {
                                treeView.removeSelectedPath(path);
                            } else {
                                treeView.addSelectedPath(path);
                            }
                        } else if (Keyboard.isPressed(commandModifier) && selectMode == TreeView.SelectMode.SINGLE) {
                            // Toggle the item's selection state
                            if (nodeInfo.isSelected()) {
                                treeView.clearSelection();
                            } else {
                                treeView.setSelectedPath(path);
                            }
                        } else {
                            if (selectMode != TreeView.SelectMode.NONE) {
                                if (nodeInfo.isSelected()) {
                                    selectPath = path;
                                } else {
                                    treeView.setSelectedPath(path);
                                }
                            }
                        }
                    }
                }
            }
        }
        treeView.requestFocus();
    }
    return consumed;
}
Also used : Path(org.apache.pivot.collections.Sequence.Tree.Path) GeneralPath(java.awt.geom.GeneralPath) Modifier(org.apache.pivot.wtk.Keyboard.Modifier) Keyboard(org.apache.pivot.wtk.Keyboard) TreeView(org.apache.pivot.wtk.TreeView) SelectMode(org.apache.pivot.wtk.TreeView.SelectMode)

Example 14 with Path

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

the class TerraTreeViewSkin method showMixedCheckmarkStateChanged.

@Override
public void showMixedCheckmarkStateChanged(TreeView treeView) {
    if (treeView.getCheckmarksEnabled()) {
        // The check state of all *branch* nodes may have changed, so we
        // need to update the cached check state of all BranchNode
        // instances in our hierarchy
        Sequence<NodeInfo> nodes = new ArrayList<>();
        nodes.add(rootBranchInfo);
        while (nodes.getLength() > 0) {
            NodeInfo nodeInfo = nodes.get(0);
            nodes.remove(0, 1);
            // Only branch nodes can be affected by this event
            if (nodeInfo instanceof BranchInfo) {
                BranchInfo branchInfo = (BranchInfo) nodeInfo;
                // Update the cached entry for this branch
                Path path = branchInfo.getPath();
                branchInfo.setCheckState(treeView.getNodeCheckState(path));
                // Add the branch's children to the queue
                if (branchInfo.children != null) {
                    for (int i = 0, n = branchInfo.children.getLength(); i < n; i++) {
                        nodes.insert(branchInfo.children.get(i), i);
                    }
                }
            }
        }
        repaintComponent();
    }
}
Also used : Path(org.apache.pivot.collections.Sequence.Tree.Path) GeneralPath(java.awt.geom.GeneralPath) ArrayList(org.apache.pivot.collections.ArrayList)

Example 15 with Path

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

the class TerraTreeViewSkin method keyPressed.

/**
 * {@link KeyCode#UP UP} Selects the previous enabled node when select mode
 * is not {@link SelectMode#NONE}<br> {@link KeyCode#DOWN DOWN} Selects the
 * next enabled node when select mode is not {@link SelectMode#NONE}<p>
 * {@link Modifier#SHIFT SHIFT} + {@link KeyCode#UP UP} Increases the
 * selection size by including the previous enabled node when select mode is
 * {@link SelectMode#MULTI}<br> {@link Modifier#SHIFT SHIFT} +
 * {@link KeyCode#DOWN DOWN} Increases the selection size by including the
 * next enabled node when select mode is {@link SelectMode#MULTI}
 */
@Override
public boolean keyPressed(Component component, int keyCode, Keyboard.KeyLocation keyLocation) {
    boolean consumed = false;
    TreeView treeView = (TreeView) getComponent();
    TreeView.SelectMode selectMode = treeView.getSelectMode();
    switch(keyCode) {
        case Keyboard.KeyCode.UP:
            {
                if (selectMode != TreeView.SelectMode.NONE) {
                    Path firstSelectedPath = treeView.getFirstSelectedPath();
                    int index;
                    if (firstSelectedPath != null) {
                        NodeInfo previousSelectedNode = getNodeInfoAt(firstSelectedPath);
                        index = visibleNodes.indexOf(previousSelectedNode);
                    } else {
                        // Select the last visible node
                        index = visibleNodes.getLength();
                    }
                    NodeInfo newSelectedNode = null;
                    do {
                        newSelectedNode = (--index >= 0) ? visibleNodes.get(index) : null;
                    } while (newSelectedNode != null && newSelectedNode.isDisabled());
                    if (newSelectedNode != null) {
                        if (Keyboard.isPressed(Keyboard.Modifier.SHIFT) && treeView.getSelectMode() == TreeView.SelectMode.MULTI) {
                            treeView.addSelectedPath(newSelectedNode.getPath());
                        } else {
                            treeView.setSelectedPath(newSelectedNode.getPath());
                        }
                        treeView.scrollAreaToVisible(getNodeBounds(newSelectedNode));
                    }
                    consumed = true;
                }
                break;
            }
        case Keyboard.KeyCode.DOWN:
            {
                if (selectMode != TreeView.SelectMode.NONE) {
                    Path lastSelectedPath = treeView.getLastSelectedPath();
                    int index;
                    if (lastSelectedPath != null) {
                        NodeInfo previousSelectedNode = getNodeInfoAt(lastSelectedPath);
                        index = visibleNodes.indexOf(previousSelectedNode);
                    } else {
                        // Select the first visible node
                        index = -1;
                    }
                    NodeInfo newSelectedNode = null;
                    int n = visibleNodes.getLength();
                    do {
                        newSelectedNode = (++index <= n - 1) ? visibleNodes.get(index) : null;
                    } while (newSelectedNode != null && newSelectedNode.isDisabled());
                    if (newSelectedNode != null) {
                        if (Keyboard.isPressed(Keyboard.Modifier.SHIFT) && treeView.getSelectMode() == TreeView.SelectMode.MULTI) {
                            treeView.addSelectedPath(newSelectedNode.getPath());
                        } else {
                            treeView.setSelectedPath(newSelectedNode.getPath());
                        }
                        treeView.scrollAreaToVisible(getNodeBounds(newSelectedNode));
                    }
                    consumed = true;
                }
                break;
            }
        case Keyboard.KeyCode.LEFT:
            {
                if (showBranchControls) {
                    Sequence<Path> paths = treeView.getSelectedPaths();
                    if (paths != null && paths.getLength() > 0) {
                        Path path = paths.get(paths.getLength() - 1);
                        NodeInfo nodeInfo = getNodeInfoAt(path);
                        if (nodeInfo instanceof BranchInfo) {
                            BranchInfo branchInfo = (BranchInfo) nodeInfo;
                            if (branchInfo.isExpanded()) {
                                treeView.collapseBranch(branchInfo.getPath());
                            }
                        }
                        consumed = true;
                    }
                }
                break;
            }
        case Keyboard.KeyCode.RIGHT:
            {
                if (showBranchControls) {
                    Sequence<Path> paths = treeView.getSelectedPaths();
                    if (paths != null && paths.getLength() > 0) {
                        Path path = paths.get(paths.getLength() - 1);
                        NodeInfo nodeInfo = getNodeInfoAt(path);
                        if (nodeInfo instanceof BranchInfo) {
                            BranchInfo branchInfo = (BranchInfo) nodeInfo;
                            if (!branchInfo.isExpanded()) {
                                treeView.expandBranch(branchInfo.getPath());
                            }
                        }
                        consumed = true;
                    }
                }
                break;
            }
        default:
            consumed = super.keyPressed(component, keyCode, keyLocation);
            break;
    }
    if (consumed) {
        clearHighlightedNode();
    }
    return consumed;
}
Also used : Path(org.apache.pivot.collections.Sequence.Tree.Path) GeneralPath(java.awt.geom.GeneralPath) TreeView(org.apache.pivot.wtk.TreeView) Sequence(org.apache.pivot.collections.Sequence) SelectMode(org.apache.pivot.wtk.TreeView.SelectMode)

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