public void setModel(TreeTableModel treeTableModel) {
    // Create the tree. It will be used as a renderer and editor.
    tree = new TreeTableCellRenderer(treeTableModel);

    // Install a tableModel representing the visible rows in the tree.
    super.setModel(new TreeTableModelAdapter(treeTableModel, tree));

    // Force the JTable and JTree to share their row selection models.
    ListToTreeSelectionModelWrapper selectionWrapper = new ListToTreeSelectionModelWrapper();
    tree.setSelectionModel(selectionWrapper);
    setSelectionModel(selectionWrapper.getListSelectionModel());

    // Install the tree editor renderer and editor.
    setDefaultRenderer(TreeTableModel.class, tree);

    // No grid.
    setShowGrid(false);

    // No intercell spacing
    setIntercellSpacing(new Dimension(0, 0));

    // And update the height of the trees row to match that of
    // the table.
    if (tree.getRowHeight() < 1) {
      // Metal looks better like this.
      setRowHeight(18);
    }
  }
예제 #2
0
  public JTreeTable(TreeTableModel treeTableModel) {

    // Create the tree. It will be used as a renderer and editor.
    tree = new TreeTableCellRenderer(treeTableModel);

    // Install a tableModel representing the visible rows in the tree.
    setModel(new TreeTableModelAdapter(treeTableModel, tree));

    // Force the JTable and JTree to share their row selection models.
    final ListToTreeSelectionModelWrapper selectionWrapper = new ListToTreeSelectionModelWrapper();
    tree.setSelectionModel(selectionWrapper);
    setSelectionModel(selectionWrapper.getListSelectionModel());

    // Install the tree editor renderer and editor.
    setDefaultRenderer(TreeTableModel.class, tree);
    setDefaultEditor(TreeTableModel.class, new TreeTableCellEditor());

    // No grid.
    setShowGrid(false);

    // No intercell spacing
    setIntercellSpacing(new Dimension(0, 0));

    // And update the height of the trees row to match that of
    // the table.
    if (tree.getRowHeight() < 1) {
      // Metal looks better like this.
      setRowHeight(getRowHeight());
    }

    final Action expand =
        new AbstractAction() {
          private static final long serialVersionUID = -5859674518660156121L;

          @Override
          public void actionPerformed(ActionEvent e) {
            final TreePath selected = tree.getSelectionPath();
            final DetailAST ast = (DetailAST) selected.getLastPathComponent();
            new CodeSelector(ast, editor, lines2position).select();

            if (tree.isExpanded(selected)) {
              tree.collapsePath(selected);
            } else {
              tree.expandPath(selected);
            }
            tree.setSelectionPath(selected);
          }
        };
    final KeyStroke stroke = KeyStroke.getKeyStroke("ENTER");
    final String command = "expand/collapse";
    getInputMap().put(stroke, command);
    getActionMap().put(command, expand);
  }