Search in sources :

Example 1 with SelectMode

use of org.apache.pivot.wtk.TreeView.SelectMode 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 2 with SelectMode

use of org.apache.pivot.wtk.TreeView.SelectMode 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

GeneralPath (java.awt.geom.GeneralPath)2 Path (org.apache.pivot.collections.Sequence.Tree.Path)2 TreeView (org.apache.pivot.wtk.TreeView)2 SelectMode (org.apache.pivot.wtk.TreeView.SelectMode)2 Sequence (org.apache.pivot.collections.Sequence)1 Keyboard (org.apache.pivot.wtk.Keyboard)1 Modifier (org.apache.pivot.wtk.Keyboard.Modifier)1