如何在DebugSession中创建TreeTable

时间:2016-11-03 14:27:56

标签: java swing intellij-idea treeview intellij-plugin

我正在寻找一种在intellij插件中创建TreeTable的方法。 DebugSession中的Tab可以在 com.intellij.xdebugger.XDebugProcess #createTabLayouter 中创建,但我不能在'在JPanel中创建一个TreeTable。

我的观点如下:

<?xml version="1.0" encoding="UTF-8"?>
    <form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="debugger.ui.RegisterViewSessionTab">
      <grid id="27dc6" binding="content" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
        <margin top="0" left="0" bottom="0" right="0"/>
        <constraints>
          <xy x="20" y="20" width="500" height="400"/>
        </constraints>
        <properties/>
        <border type="none"/>
        <children>
          <scrollpane id="86eac">
            <constraints>
              <grid row="0" column="0" row-span="1" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
            </constraints>
            <properties/>
            <border type="none"/>
            <children>
              <component id="8217" class="com.intellij.ui.treeStructure.treetable.TreeTable" binding="tree" custom-create="true">
                <constraints/>
                <properties/>
              </component>
            </children>
          </scrollpane>
        </children>
      </grid>
    </form>

代码behinde(RegisterViewSessionTab):

    pc = new DefaultRegisterNode();
    sp = new DefaultRegisterNode();
    DefaultRegisterNode group = new DefaultRegisterNode();

    group.setName("Core Registers");
    group.setDescription("Core Registers");
    group.setValue("");
    DefaultMutableTreeNode root = new DefaultMutableTreeNode(group);

    pc.setName("PC");
    pc.setDescription("Program Counter [Core]");
    pc.setValue("---");
    DefaultMutableTreeNode nodePc = new DefaultMutableTreeNode(pc);
    root.add(nodePc);

    sp.setName("SP");
    sp.setDescription("Stack Pointer [Core]");
    sp.setValue("---");

    DefaultMutableTreeNode node = new DefaultMutableTreeNode(sp);
    root.add(node);

    ColumnInfo[] columnInfos = new ColumnInfo[3];
    columnInfos[0] = new ColumnInfo<DefaultMutableTreeNode, String>("Name") {
        @Nullable
        @Override
        public String valueOf(DefaultMutableTreeNode defaultRegisterNode) {
            return ((DefaultRegisterNode)defaultRegisterNode.getUserObject()).getName();
        }
    };
    columnInfos[1] = new TableModelEditor.EditableColumnInfo<DefaultMutableTreeNode, String>("Value") {
        @Nullable
        @Override
        public String valueOf(DefaultMutableTreeNode defaultRegisterNode) {
            return ((DefaultRegisterNode)defaultRegisterNode.getUserObject()).getValue();
        }

        @Nullable
        @Override
        public TableCellEditor getEditor(DefaultMutableTreeNode defaultRegisterNode) {
            return new StringTableCellEditor(project);
        }

        @Override
        public void setValue(DefaultMutableTreeNode defaultRegisterNode, String value) {
            ((DefaultRegisterNode)defaultRegisterNode.getUserObject()).setValue(value);
        }
    };
    columnInfos[2] = new ColumnInfo<DefaultMutableTreeNode, String>("Description") {
        @Nullable
        @Override
        public String valueOf(DefaultMutableTreeNode defaultRegisterNode) {
            return ((DefaultRegisterNode)defaultRegisterNode.getUserObject()).getDescription();
        }
    };

    ListTreeTableModelOnColumns registersModel = new ListTreeTableModelOnColumns(root, columnInfos);
    tree = new TreeTable(registersModel);

模特课:

public class DefaultRegisterNode {
    private String name;
    private String description;
    private String value;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return null;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

行动中: http://i.imgur.com/RN2w5mT.jpg

但是没有折叠/展开按钮?如何在TreeTable中启用此功能?

如果您需要更多信息,请与我联系! 感谢

1 个答案:

答案 0 :(得分:0)

使用TreeTableView,根节点也会显示。但错误并非如此。我找到了。

ColumnInfo[] columnInfos = new ColumnInfo[] {
    new TreeColumnInfo("Name"),   // <-- This is important!
    new ColumnInfo<Node, String>("Name S1") {
       ...
    }
};

将TreeColumnInfo添加到上面的ColumnInfos。这将添加展开/折叠箭头。现在,您必须创建一个TreeCellRenderer并设置为Tree。

public static class Tree extends TreeTableView {

    public Tree(ListTreeTableModelOnColumns treeTableModel) {
        super(treeTableModel);

        setTreeCellRenderer(new Renderer());
    }

    class Renderer extends ColoredTreeCellRenderer  {
        @Override
        public void customizeCellRenderer(@NotNull JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
            Node node = (Node)value;
            NodeModel model = node.getUserObject();

            append(model.Name);
        }

    }
}