Search in sources :

Example 1 with TreeNode

use of org.apache.pivot.wtk.content.TreeNode in project pivot by apache.

the class EventLoggerSkin method install.

@Override
public void install(Component component) {
    super.install(component);
    EventLogger eventLogger = (EventLogger) component;
    eventLogger.getEventLoggerListeners().add(this);
    BXMLSerializer bxmlSerializer = new BXMLSerializer();
    try {
        content = (Component) bxmlSerializer.readObject(EventLoggerSkin.class, "event_logger_skin.bxml", true);
    } catch (IOException exception) {
        throw new RuntimeException(exception);
    } catch (SerializationException exception) {
        throw new RuntimeException(exception);
    }
    eventLogger.add(content);
    declaredEventsTreeView = (TreeView) bxmlSerializer.getNamespace().get("declaredEventsTreeView");
    firedEventsTableView = (TableView) bxmlSerializer.getNamespace().get("firedEventsTableView");
    // Propagate check state upwards or downwards as necessary
    declaredEventsTreeView.getTreeViewNodeStateListeners().add(new TreeViewNodeStateListener() {

        @Override
        public void nodeCheckStateChanged(TreeView treeView, Path path, TreeView.NodeCheckState previousCheckState) {
            TreeView.NodeCheckState checkState = treeView.getNodeCheckState(path);
            if (!updating) {
                // Set the updating flag for the life of this event loop
                updating = true;
                ApplicationContext.queueCallback(new Runnable() {

                    @Override
                    public void run() {
                        updating = false;
                    }
                });
                EventLogger eventLoggerLocal = (EventLogger) getComponent();
                boolean checked = (checkState == TreeView.NodeCheckState.CHECKED);
                List<?> treeData = treeView.getTreeData();
                TreeNode treeNode = (TreeNode) Sequence.Tree.get(treeData, path);
                if (treeNode instanceof List<?>) {
                    if (previousCheckState == TreeView.NodeCheckState.CHECKED || checkState == TreeView.NodeCheckState.CHECKED) {
                        // Propagate downward
                        List<?> treeBranch = (List<?>) treeNode;
                        Path childPath = new Path(path);
                        int lastIndex = childPath.getLength();
                        childPath.add(0);
                        for (int i = 0, n = treeBranch.getLength(); i < n; i++) {
                            childPath.update(lastIndex, i);
                            treeView.setNodeChecked(childPath, checked);
                            EventNode eventNode = (EventNode) treeBranch.get(i);
                            Method event = eventNode.getEvent();
                            if (checked) {
                                eventLoggerLocal.getIncludeEvents().add(event);
                            } else {
                                eventLoggerLocal.getIncludeEvents().remove(event);
                            }
                        }
                    }
                } else {
                    Path parentPath = new Path(path, path.getLength() - 1);
                    EventNode eventNode = (EventNode) treeNode;
                    Method event = eventNode.getEvent();
                    if (checked) {
                        List<?> treeBranch = (List<?>) Sequence.Tree.get(treeData, parentPath);
                        Path childPath = new Path(path);
                        int lastIndex = parentPath.getLength();
                        int i = 0, n = treeBranch.getLength();
                        while (i < n) {
                            childPath.update(lastIndex, i);
                            if (!treeView.isNodeChecked(childPath)) {
                                break;
                            }
                            i++;
                        }
                        if (i == n) {
                            // Propagate upward
                            treeView.setNodeChecked(parentPath, checked);
                        }
                        eventLoggerLocal.getIncludeEvents().add(event);
                    } else {
                        // Propagate upward
                        treeView.setNodeChecked(parentPath, checked);
                        eventLoggerLocal.getIncludeEvents().remove(event);
                    }
                }
            }
        }
    });
    sourceChanged(eventLogger, null);
}
Also used : Path(org.apache.pivot.collections.Sequence.Tree.Path) TreeViewNodeStateListener(org.apache.pivot.wtk.TreeViewNodeStateListener) SerializationException(org.apache.pivot.serialization.SerializationException) IOException(java.io.IOException) Method(java.lang.reflect.Method) TreeNode(org.apache.pivot.wtk.content.TreeNode) TreeView(org.apache.pivot.wtk.TreeView) ArrayList(org.apache.pivot.collections.ArrayList) List(org.apache.pivot.collections.List) BXMLSerializer(org.apache.pivot.beans.BXMLSerializer)

Example 2 with TreeNode

use of org.apache.pivot.wtk.content.TreeNode in project pivot by apache.

the class BXMLExplorerDocument method analyseObjectTree.

@SuppressWarnings("unchecked")
private TreeNode analyseObjectTree(Object container) {
    // doesn't look neat
    if (container instanceof TablePane) {
        TreeBranch branch = new TreeBranch(nameForObject(container));
        TablePane table = (TablePane) container;
        for (TablePane.Row row : table.getRows()) {
            TreeNode childBranch = analyseObjectTree(row);
            branch.add(childBranch);
        }
        setComponentIconOnTreeNode(container, branch);
        return branch;
    }
    // We don't want to analyse the components that are added as part of the
    // skin, so use similar logic to BXMLSerializer
    DefaultProperty defaultProperty = container.getClass().getAnnotation(DefaultProperty.class);
    if (defaultProperty != null) {
        TreeBranch branch = new TreeBranch(nameForObject(container));
        String defaultPropertyName = defaultProperty.value();
        BeanAdapter beanAdapter = new BeanAdapter(container);
        if (!beanAdapter.containsKey(defaultPropertyName)) {
            throw new IllegalStateException("default property " + defaultPropertyName + " not found on " + container);
        }
        Object defaultPropertyValue = beanAdapter.get(defaultPropertyName);
        if (defaultPropertyValue != null) {
            if (defaultPropertyValue instanceof Component) {
                TreeNode childBranch = analyseObjectTree(defaultPropertyValue);
                branch.add(childBranch);
            }
        }
        // so make empty branches into nodes.
        if (branch.isEmpty()) {
            TreeNode node = new TreeNode(branch.getText());
            setComponentIconOnTreeNode(container, node);
            return node;
        }
        setComponentIconOnTreeNode(container, branch);
        return branch;
    }
    if (container instanceof Sequence<?>) {
        TreeBranch branch = new TreeBranch(nameForObject(container));
        Iterable<Object> sequence = (Iterable<Object>) container;
        for (Object child : sequence) {
            TreeNode childBranch = analyseObjectTree(child);
            branch.add(childBranch);
        }
        setComponentIconOnTreeNode(container, branch);
        return branch;
    }
    TreeNode node = new TreeNode(nameForObject(container));
    setComponentIconOnTreeNode(container, node);
    return node;
}
Also used : Sequence(org.apache.pivot.collections.Sequence) DefaultProperty(org.apache.pivot.beans.DefaultProperty) TreeBranch(org.apache.pivot.wtk.content.TreeBranch) TreeNode(org.apache.pivot.wtk.content.TreeNode) BeanAdapter(org.apache.pivot.beans.BeanAdapter) Component(org.apache.pivot.wtk.Component) TablePane(org.apache.pivot.wtk.TablePane)

Example 3 with TreeNode

use of org.apache.pivot.wtk.content.TreeNode in project pivot by apache.

the class Pivot718 method controlTree.

private void controlTree(BXMLSerializer bxmlSerializer) {
    treeDelButton = (PushButton) bxmlSerializer.getNamespace().get("treeDelButton");
    tree = (TreeView) bxmlSerializer.getNamespace().get("tree");
    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");
        }
    });
    treeDelButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            TreeNode selectedNode = (TreeNode) tree.getSelectedNode();
            System.out.println("delete :: " + selectedNode);
            if (selectedNode != null) {
                TreeBranch parent = selectedNode.getParent();
                if (parent != null) {
                    parent.remove(selectedNode);
                }
            }
        }
    });
}
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) TreeNode(org.apache.pivot.wtk.content.TreeNode) TreeViewSelectionListener(org.apache.pivot.wtk.TreeViewSelectionListener) TreeView(org.apache.pivot.wtk.TreeView)

Example 4 with TreeNode

use of org.apache.pivot.wtk.content.TreeNode in project pivot by apache.

the class JSONViewer method build.

@SuppressWarnings("unchecked")
private static TreeNode build(Object value) {
    TreeNode treeNode;
    if (value instanceof Map<?, ?>) {
        TreeBranch treeBranch = new TreeBranch("{}");
        treeBranch.setComparator(new Comparator<TreeNode>() {

            @Override
            public int compare(TreeNode treeNode1, TreeNode treeNode2) {
                return treeNode1.getText().compareTo(treeNode2.getText());
            }
        });
        Map<String, Object> map = (Map<String, Object>) value;
        for (String key : map) {
            TreeNode valueNode = build(map.get(key));
            String text = valueNode.getText();
            if (text == null) {
                valueNode.setText(key);
            } else {
                valueNode.setText(key + " : " + text);
            }
            treeBranch.add(valueNode);
        }
        treeNode = treeBranch;
    } else if (value instanceof List<?>) {
        TreeBranch treeBranch = new TreeBranch("[]");
        List<Object> list = (List<Object>) value;
        for (int i = 0, n = list.getLength(); i < n; i++) {
            TreeNode itemNode = build(list.get(i));
            String text = itemNode.getText();
            if (text == null) {
                itemNode.setText("[" + i + "]");
            } else {
                itemNode.setText("[" + i + "] " + text);
            }
            treeBranch.add(itemNode);
        }
        treeNode = treeBranch;
    } else if (value instanceof String) {
        treeNode = new TreeNode("\"" + value.toString() + "\"");
    } else if (value instanceof Number) {
        treeNode = new TreeNode(value.toString());
    } else if (value instanceof Boolean) {
        treeNode = new TreeNode(value.toString());
    } else {
        treeNode = new TreeNode("null");
    }
    return treeNode;
}
Also used : TreeBranch(org.apache.pivot.wtk.content.TreeBranch) TreeNode(org.apache.pivot.wtk.content.TreeNode) List(org.apache.pivot.collections.List) FileList(org.apache.pivot.io.FileList) Map(org.apache.pivot.collections.Map)

Example 5 with TreeNode

use of org.apache.pivot.wtk.content.TreeNode 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

TreeNode (org.apache.pivot.wtk.content.TreeNode)7 TreeBranch (org.apache.pivot.wtk.content.TreeBranch)4 ArrayList (org.apache.pivot.collections.ArrayList)3 List (org.apache.pivot.collections.List)3 Path (org.apache.pivot.collections.Sequence.Tree.Path)3 TreeView (org.apache.pivot.wtk.TreeView)3 IOException (java.io.IOException)2 Method (java.lang.reflect.Method)2 SerializationException (org.apache.pivot.serialization.SerializationException)2 ButtonPressListener (org.apache.pivot.wtk.ButtonPressListener)2 Component (org.apache.pivot.wtk.Component)2 PushButton (org.apache.pivot.wtk.PushButton)2 TreeViewSelectionListener (org.apache.pivot.wtk.TreeViewSelectionListener)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 BXMLSerializer (org.apache.pivot.beans.BXMLSerializer)1 BeanAdapter (org.apache.pivot.beans.BeanAdapter)1 DefaultProperty (org.apache.pivot.beans.DefaultProperty)1 HashMap (org.apache.pivot.collections.HashMap)1 Map (org.apache.pivot.collections.Map)1 Sequence (org.apache.pivot.collections.Sequence)1