Search in sources :

Example 1 with FieldObject

use of com.android.tools.profilers.memory.adapters.FieldObject in project android by JetBrains.

the class MemoryInstanceView method ensureTreeInitialized.

private void ensureTreeInitialized(@NotNull JPanel parentPanel) {
    if (myTree != null) {
        assert myTreeModel != null && myTreeRoot != null;
        return;
    }
    myTreeRoot = new MemoryObjectTreeNode<>(new InstanceObject() {

        @NotNull
        @Override
        public String getName() {
            return "";
        }
    });
    myTreeModel = new DefaultTreeModel(myTreeRoot);
    JTree tree = new Tree(myTreeModel);
    tree.setRootVisible(false);
    tree.setShowsRootHandles(true);
    tree.addTreeSelectionListener(e -> {
        TreePath path = e.getPath();
        if (!e.isAddedPath()) {
            return;
        }
        assert path.getLastPathComponent() instanceof MemoryObjectTreeNode;
        MemoryObjectTreeNode instanceObject = (MemoryObjectTreeNode) path.getLastPathComponent();
        assert instanceObject.getAdapter() instanceof InstanceObject;
        InstanceObject selectedInstanceObject = (InstanceObject) instanceObject.getAdapter();
        myStage.selectInstance(selectedInstanceObject);
    });
    // Not all nodes have been populated during buildTree. Here we capture the TreeExpansionEvent to check whether any children
    // under the expanded node need to be populated.
    tree.addTreeExpansionListener(new TreeExpansionListener() {

        @Override
        public void treeExpanded(TreeExpansionEvent event) {
            TreePath path = event.getPath();
            assert path.getLastPathComponent() instanceof MemoryObjectTreeNode;
            MemoryObjectTreeNode treeNode = (MemoryObjectTreeNode) path.getLastPathComponent();
            for (int i = 0; i < treeNode.getChildCount(); i++) {
                assert treeNode.getChildAt(i) instanceof MemoryObjectTreeNode;
                MemoryObjectTreeNode childNode = (MemoryObjectTreeNode) treeNode.getChildAt(i);
                // Anything below the top level should be FieldObjects
                assert childNode.getAdapter() instanceof FieldObject;
                FieldObject childObject = (FieldObject) childNode.getAdapter();
                if (childObject.getValueType() == ValueType.OBJECT && childNode.getChildCount() == 0) {
                    populateFields(childNode);
                    myTreeModel.nodeStructureChanged(childNode);
                }
            }
        }

        @Override
        public void treeCollapsed(TreeExpansionEvent event) {
        // No-op. TODO remove unseen children?
        }
    });
    assert myClassObject != null;
    List<InstanceAttribute> attributes = myClassObject.getInstanceAttributes();
    ColumnTreeBuilder builder = new ColumnTreeBuilder(tree);
    for (InstanceAttribute attribute : attributes) {
        builder.addColumn(myAttributeColumns.get(attribute).getBuilder());
    }
    builder.setTreeSorter((Comparator<MemoryObjectTreeNode<InstanceObject>> comparator, SortOrder sortOrder) -> {
        myTreeRoot.sort(comparator);
        myTreeModel.nodeStructureChanged(myTreeRoot);
    });
    builder.setBackground(ProfilerColors.MONITOR_BACKGROUND);
    myTree = builder.build();
    parentPanel.add(myTree, BorderLayout.CENTER);
}
Also used : TreeExpansionListener(javax.swing.event.TreeExpansionListener) FieldObject(com.android.tools.profilers.memory.adapters.FieldObject) DefaultTreeModel(javax.swing.tree.DefaultTreeModel) InstanceAttribute(com.android.tools.profilers.memory.adapters.ClassObject.InstanceAttribute) Comparator(java.util.Comparator) InstanceObject(com.android.tools.profilers.memory.adapters.InstanceObject) TreePath(javax.swing.tree.TreePath) ColumnTreeBuilder(com.android.tools.adtui.common.ColumnTreeBuilder) Tree(com.intellij.ui.treeStructure.Tree) TreeExpansionEvent(javax.swing.event.TreeExpansionEvent)

Aggregations

ColumnTreeBuilder (com.android.tools.adtui.common.ColumnTreeBuilder)1 InstanceAttribute (com.android.tools.profilers.memory.adapters.ClassObject.InstanceAttribute)1 FieldObject (com.android.tools.profilers.memory.adapters.FieldObject)1 InstanceObject (com.android.tools.profilers.memory.adapters.InstanceObject)1 Tree (com.intellij.ui.treeStructure.Tree)1 Comparator (java.util.Comparator)1 TreeExpansionEvent (javax.swing.event.TreeExpansionEvent)1 TreeExpansionListener (javax.swing.event.TreeExpansionListener)1 DefaultTreeModel (javax.swing.tree.DefaultTreeModel)1 TreePath (javax.swing.tree.TreePath)1