Search in sources :

Example 1 with TreeViewNodeStateListener

use of org.apache.pivot.wtk.TreeViewNodeStateListener 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)

Aggregations

IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 BXMLSerializer (org.apache.pivot.beans.BXMLSerializer)1 ArrayList (org.apache.pivot.collections.ArrayList)1 List (org.apache.pivot.collections.List)1 Path (org.apache.pivot.collections.Sequence.Tree.Path)1 SerializationException (org.apache.pivot.serialization.SerializationException)1 TreeView (org.apache.pivot.wtk.TreeView)1 TreeViewNodeStateListener (org.apache.pivot.wtk.TreeViewNodeStateListener)1 TreeNode (org.apache.pivot.wtk.content.TreeNode)1