Search in sources :

Example 16 with TreeView

use of org.apache.pivot.wtk.TreeView in project pivot by apache.

the class TerraTreeViewSkin method scrollSelectionToVisible.

/**
 * Scrolls the last visible (expanded) selected node into viewport
 * visibility. If no such node exists, nothing happens. <p> This should only
 * be called when the tree view is valid.
 */
private void scrollSelectionToVisible() {
    TreeView treeView = (TreeView) getComponent();
    Sequence<Path> selectedPaths = treeView.getSelectedPaths();
    int n = selectedPaths.getLength();
    if (n > 0) {
        Bounds nodeBounds = null;
        for (int i = n - 1; i >= 0 && nodeBounds == null; i--) {
            NodeInfo nodeInfo = getNodeInfoAt(selectedPaths.get(i));
            nodeBounds = getNodeBounds(nodeInfo);
        }
        if (nodeBounds != null) {
            Bounds visibleSelectionBounds = treeView.getVisibleArea(nodeBounds);
            if (visibleSelectionBounds != null && visibleSelectionBounds.height < nodeBounds.height) {
                treeView.scrollAreaToVisible(nodeBounds);
            }
        }
    }
}
Also used : Path(org.apache.pivot.collections.Sequence.Tree.Path) GeneralPath(java.awt.geom.GeneralPath) Bounds(org.apache.pivot.wtk.Bounds) TreeView(org.apache.pivot.wtk.TreeView)

Example 17 with TreeView

use of org.apache.pivot.wtk.TreeView in project pivot by apache.

the class TerraTreeViewSkin method mouseMove.

@Override
public boolean mouseMove(Component component, int x, int y) {
    boolean consumed = super.mouseMove(component, x, y);
    TreeView treeView = (TreeView) getComponent();
    if (showHighlight && treeView.getSelectMode() != TreeView.SelectMode.NONE) {
        NodeInfo previousHighlightedNode = highlightedNode;
        highlightedNode = getNodeInfoAt(y);
        if (highlightedNode != previousHighlightedNode) {
            if (previousHighlightedNode != null) {
                previousHighlightedNode.setHighlighted(false);
                repaintNode(previousHighlightedNode);
            }
            if (highlightedNode != null) {
                highlightedNode.setHighlighted(true);
                repaintNode(highlightedNode);
            }
        }
    }
    return consumed;
}
Also used : TreeView(org.apache.pivot.wtk.TreeView)

Example 18 with TreeView

use of org.apache.pivot.wtk.TreeView in project pivot by apache.

the class TerraTreeViewSkin method paint.

@Override
public void paint(Graphics2D graphics) {
    TreeView treeView = (TreeView) getComponent();
    TreeView.NodeRenderer nodeRenderer = treeView.getNodeRenderer();
    int width = getWidth();
    int height = getHeight();
    int nodeHeight = getNodeHeight();
    // Paint the background
    if (backgroundColor != null) {
        graphics.setPaint(backgroundColor);
        graphics.fillRect(0, 0, width, height);
    }
    // nodeStart and nodeEnd are both inclusive
    int nodeStart = 0;
    int nodeEnd = visibleNodes.getLength() - 1;
    // Ensure that we only paint items that are visible
    Rectangle clipBounds = graphics.getClipBounds();
    if (clipBounds != null) {
        nodeStart = Math.max(nodeStart, (int) (clipBounds.y / (double) (nodeHeight + VERTICAL_SPACING)));
        nodeEnd = Math.min(nodeEnd, (int) ((clipBounds.y + clipBounds.height) / (double) (nodeHeight + VERTICAL_SPACING)));
    }
    int nodeY = nodeStart * (nodeHeight + VERTICAL_SPACING);
    VisibleNodeIterator visibleNodeIterator = new VisibleNodeIterator(nodeStart, nodeEnd);
    while (visibleNodeIterator.hasNext()) {
        NodeInfo nodeInfo = visibleNodeIterator.next();
        boolean expanded = false;
        boolean highlighted = nodeInfo.isHighlighted();
        boolean selected = nodeInfo.isSelected();
        boolean disabled = nodeInfo.isDisabled();
        int nodeX = (nodeInfo.depth - 1) * (indent + spacing);
        if (treeView.isEnabled()) {
            if (selected) {
                // Paint the selection state
                Color selectionBackgroundColorLocal = treeView.isFocused() ? this.selectionBackgroundColor : inactiveSelectionBackgroundColor;
                graphics.setPaint(selectionBackgroundColorLocal);
                graphics.fillRect(0, nodeY, width, nodeHeight);
            } else if (highlighted && !disabled) {
                // Paint the highlight state
                graphics.setPaint(highlightBackgroundColor);
                graphics.fillRect(0, nodeY, width, nodeHeight);
            }
        }
        // Paint the expand/collapse control
        if (showBranchControls) {
            if (nodeInfo instanceof BranchInfo) {
                BranchInfo branchInfo = (BranchInfo) nodeInfo;
                boolean showBranchControl = true;
                if (!showEmptyBranchControls) {
                    branchInfo.loadChildren();
                    showBranchControl = !(branchInfo.children == null || branchInfo.children.isEmpty());
                }
                if (showBranchControl) {
                    expanded = branchInfo.isExpanded();
                    Color branchControlColorLocal;
                    if (selected) {
                        if (treeView.isFocused()) {
                            branchControlColorLocal = branchControlSelectionColor;
                        } else {
                            branchControlColorLocal = branchControlInactiveSelectionColor;
                        }
                    } else {
                        branchControlColorLocal = this.branchControlColor;
                    }
                    GeneralPath shape = new GeneralPath();
                    int imageX = nodeX + (indent - BRANCH_CONTROL_IMAGE_WIDTH) / 2;
                    int imageY = nodeY + (nodeHeight - BRANCH_CONTROL_IMAGE_HEIGHT) / 2;
                    if (expanded) {
                        shape.moveTo(imageX, imageY + 1);
                        shape.lineTo(imageX + 8, imageY + 1);
                        shape.lineTo(imageX + 4, imageY + 7);
                    } else {
                        shape.moveTo(imageX + 1, imageY);
                        shape.lineTo(imageX + 7, imageY + 4);
                        shape.lineTo(imageX + 1, imageY + 8);
                    }
                    shape.closePath();
                    Graphics2D branchControlGraphics = (Graphics2D) graphics.create();
                    GraphicsUtilities.setAntialiasingOn(branchControlGraphics);
                    if (!treeView.isEnabled() || disabled) {
                        branchControlGraphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
                    }
                    branchControlGraphics.setPaint(branchControlColorLocal);
                    branchControlGraphics.fill(shape);
                    branchControlGraphics.dispose();
                }
            }
            nodeX += indent + spacing;
        }
        // Paint the checkbox
        TreeView.NodeCheckState checkState = TreeView.NodeCheckState.UNCHECKED;
        if (treeView.getCheckmarksEnabled()) {
            checkState = nodeInfo.getCheckState();
            int checkboxWidth = CHECKBOX.getWidth();
            int checkboxHeight = CHECKBOX.getHeight();
            int checkboxX = Math.max(indent - checkboxWidth, 0) / 2;
            int checkboxY = (nodeHeight - checkboxHeight) / 2;
            Graphics2D checkboxGraphics = (Graphics2D) graphics.create(nodeX + checkboxX, nodeY + checkboxY, checkboxWidth, checkboxHeight);
            Button.State state;
            switch(checkState) {
                case CHECKED:
                    state = Button.State.SELECTED;
                    break;
                case MIXED:
                    state = Button.State.MIXED;
                    break;
                default:
                    state = Button.State.UNSELECTED;
                    break;
            }
            CHECKBOX.setState(state);
            CHECKBOX.setEnabled(treeView.isEnabled() && !disabled && !nodeInfo.isCheckmarkDisabled());
            CHECKBOX.paint(checkboxGraphics);
            checkboxGraphics.dispose();
            nodeX += Math.max(indent, checkboxWidth) + spacing;
        }
        int nodeWidth = Math.max(width - nodeX, 0);
        // Paint the node data
        Graphics2D rendererGraphics = (Graphics2D) graphics.create(nodeX, nodeY, nodeWidth, nodeHeight);
        nodeRenderer.render(nodeInfo.data, visibleNodeIterator.getPath(), visibleNodeIterator.getRowIndex(), treeView, expanded, selected, checkState, highlighted, disabled);
        nodeRenderer.setSize(nodeWidth, nodeHeight);
        nodeRenderer.paint(rendererGraphics);
        rendererGraphics.dispose();
        // Paint the grid line
        if (showGridLines) {
            graphics.setPaint(gridColor);
            GraphicsUtilities.drawLine(graphics, 0, nodeY + nodeHeight, width, Orientation.HORIZONTAL);
        }
        nodeY += nodeHeight + VERTICAL_SPACING;
    }
}
Also used : GeneralPath(java.awt.geom.GeneralPath) Color(java.awt.Color) Rectangle(java.awt.Rectangle) Graphics2D(java.awt.Graphics2D) Button(org.apache.pivot.wtk.Button) TreeView(org.apache.pivot.wtk.TreeView)

Example 19 with TreeView

use of org.apache.pivot.wtk.TreeView in project pivot by apache.

the class BXMLExplorerDocument method initialize.

@Override
public void initialize(Map<String, Object> namespace, URL location, Resources resources) {
    treeView.setSelectMode(SelectMode.SINGLE);
    treeView.setNodeRenderer(new MyTreeViewNodeRenderer());
    treeView.getTreeViewSelectionListeners().add(new TreeViewSelectionListener() {

        private final Decorator focusDecorator = new ShadeDecorator(0.2f, Color.RED);

        private Component previousSelectedComponent = null;

        @Override
        public void selectedNodeChanged(TreeView treeViewArgument, Object previousSelectedNode) {
            TreeNode node = (TreeNode) treeViewArgument.getSelectedNode();
            if (previousSelectedComponent != null && previousSelectedComponent.getDecorators().indexOf(focusDecorator) > -1) {
                previousSelectedComponent.getDecorators().remove(focusDecorator);
                previousSelectedComponent = null;
            }
            if (node == null || !(node.getUserData() instanceof Component)) {
                // TODO make the inspectors able to deal with things like
                // TablePane.Row
                componentPropertyInspector.setSource(null);
                componentStyleInspector.setSource(null);
                return;
            }
            Component selectedComp = (Component) node.getUserData();
            if (selectedComp != null && selectedComp.getDecorators().indexOf(focusDecorator) == -1) {
                selectedComp.getDecorators().add(focusDecorator);
                previousSelectedComponent = selectedComp;
            }
            if (selectedComp instanceof FakeWindow) {
                selectedComp = ((FakeWindow) selectedComp).window;
            }
            componentPropertyInspector.setSource(selectedComp);
            componentStyleInspector.setSource(selectedComp);
        }

        @Override
        public void selectedPathsChanged(TreeView treeViewArgument, Sequence<Path> previousSelectedPaths) {
            // if the selection becomes empty, remove the decorator
            if (treeViewArgument.getSelectedNode() == null && previousSelectedComponent != null && previousSelectedComponent.getDecorators().indexOf(focusDecorator) > -1) {
                previousSelectedComponent.getDecorators().remove(focusDecorator);
            }
        }
    });
    playgroundCardPane.getComponentMouseButtonListeners().add(new ComponentMouseButtonListener() {

        @Override
        public boolean mouseClick(Component component, Button button, int x, int y, int count) {
            if (count == 1) {
                Component comp = playgroundCardPane.getDescendantAt(x, y);
                if (comp != null) {
                    TreeNode treeNode = componentToTreeNode.get(comp);
                    Path path = getPathForNode(treeView, treeNode);
                    if (path != null) {
                        treeView.setSelectedPath(path);
                        return true;
                    }
                }
            }
            return false;
        }
    });
    reloadButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(org.apache.pivot.wtk.Button button) {
            playgroundCardPane.remove(loadedComponent);
            widgetToID = null;
            componentToTreeNode = null;
            loadedComponent = null;
            try {
                load(file);
            } catch (RuntimeException exception) {
                exception.printStackTrace();
                BXMLExplorer.displayLoadException(exception, BXMLExplorerDocument.this.getWindow());
            } catch (IOException exception) {
                exception.printStackTrace();
                BXMLExplorer.displayLoadException(exception, BXMLExplorerDocument.this.getWindow());
            } catch (SerializationException exception) {
                exception.printStackTrace();
                BXMLExplorer.displayLoadException(exception, BXMLExplorerDocument.this.getWindow());
            } catch (ParserConfigurationException exception) {
                exception.printStackTrace();
                BXMLExplorer.displayLoadException(exception, BXMLExplorerDocument.this.getWindow());
            } catch (SAXException exception) {
                exception.printStackTrace();
                BXMLExplorer.displayLoadException(exception, BXMLExplorerDocument.this.getWindow());
            }
        }
    });
}
Also used : Path(org.apache.pivot.collections.Sequence.Tree.Path) ShadeDecorator(org.apache.pivot.wtk.effects.ShadeDecorator) SerializationException(org.apache.pivot.serialization.SerializationException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) Decorator(org.apache.pivot.wtk.effects.Decorator) ShadeDecorator(org.apache.pivot.wtk.effects.ShadeDecorator) ButtonPressListener(org.apache.pivot.wtk.ButtonPressListener) LinkButton(org.apache.pivot.wtk.LinkButton) Button(org.apache.pivot.wtk.Mouse.Button) RadioButton(org.apache.pivot.wtk.RadioButton) PushButton(org.apache.pivot.wtk.PushButton) TreeNode(org.apache.pivot.wtk.content.TreeNode) TreeViewSelectionListener(org.apache.pivot.wtk.TreeViewSelectionListener) TreeView(org.apache.pivot.wtk.TreeView) ComponentMouseButtonListener(org.apache.pivot.wtk.ComponentMouseButtonListener) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Component(org.apache.pivot.wtk.Component)

Aggregations

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