001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2018 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.gui;
021
022import javax.swing.JTree;
023import javax.swing.SwingUtilities;
024import javax.swing.event.TreeExpansionEvent;
025import javax.swing.event.TreeExpansionListener;
026import javax.swing.event.TreeModelEvent;
027import javax.swing.event.TreeModelListener;
028import javax.swing.table.AbstractTableModel;
029import javax.swing.tree.TreePath;
030
031/**
032 * This is a wrapper class takes a TreeTableModel and implements
033 * the table model interface. The implementation is trivial, with
034 * all of the event dispatching support provided by the superclass:
035 * the AbstractTableModel.
036 *
037 * <a href=
038 * "https://docs.oracle.com/cd/E48246_01/apirefs.1111/e13403/oracle/ide/controls/TreeTableModel.html">
039 * Original&nbsp;Source&nbsp;Location</a>
040 *
041 * @author Philip Milne
042 * @author Scott Violet
043 */
044public class TreeTableModelAdapter extends AbstractTableModel {
045
046    private static final long serialVersionUID = 8269213416115369275L;
047
048    /** JTree component. */
049    private final JTree tree;
050    /** Tree table model. */
051    private final transient ParseTreeTableModel treeTableModel;
052
053    /**
054     * Initialise tree and treeTableModel class attributes.
055     * @param treeTableModel Tree table model.
056     * @param tree JTree component.
057     */
058    public TreeTableModelAdapter(ParseTreeTableModel treeTableModel, JTree tree) {
059        this.tree = tree;
060        this.treeTableModel = treeTableModel;
061
062        tree.addTreeExpansionListener(new UpdatingTreeExpansionListener());
063
064        // Install a TreeModelListener that can update the table when
065        // mTree changes. We use delayedFireTableDataChanged as we can
066        // not be guaranteed the mTree will have finished processing
067        // the event before us.
068        treeTableModel.addTreeModelListener(new UpdatingTreeModelListener());
069    }
070
071    // Wrappers, implementing TableModel interface.
072
073    @Override
074    public int getColumnCount() {
075        return treeTableModel.getColumnCount();
076    }
077
078    @Override
079    public String getColumnName(int column) {
080        return treeTableModel.getColumnName(column);
081    }
082
083    @Override
084    public Class<?> getColumnClass(int column) {
085        return treeTableModel.getColumnClass(column);
086    }
087
088    @Override
089    public int getRowCount() {
090        return tree.getRowCount();
091    }
092
093    @Override
094    public Object getValueAt(int row, int column) {
095        return treeTableModel.getValueAt(nodeForRow(row), column);
096    }
097
098    @Override
099    public boolean isCellEditable(int row, int column) {
100        return treeTableModel.isCellEditable(column);
101    }
102
103    /**
104     * Finds node for a given row.
105     * @param row Row for which to find a related node.
106     * @return Node for a given row.
107     */
108    private Object nodeForRow(int row) {
109        final TreePath treePath = tree.getPathForRow(row);
110        return treePath.getLastPathComponent();
111    }
112
113    /**
114     * Invokes fireTableDataChanged after all the pending events have been
115     * processed. SwingUtilities.invokeLater is used to handle this.
116     */
117    private void delayedFireTableDataChanged() {
118        SwingUtilities.invokeLater(this::fireTableDataChanged);
119    }
120
121    /**
122     * TreeExpansionListener that can update the table when tree changes.
123     */
124    private class UpdatingTreeExpansionListener implements TreeExpansionListener {
125
126        // Don't use fireTableRowsInserted() here; the selection model
127        // would get updated twice.
128        @Override
129        public void treeExpanded(TreeExpansionEvent event) {
130            fireTableDataChanged();
131        }
132
133        @Override
134        public void treeCollapsed(TreeExpansionEvent event) {
135            fireTableDataChanged();
136        }
137
138    }
139
140    /**
141     * TreeModelListener that can update the table when tree changes.
142     */
143    private class UpdatingTreeModelListener implements TreeModelListener {
144
145        @Override
146        public void treeNodesChanged(TreeModelEvent event) {
147            delayedFireTableDataChanged();
148        }
149
150        @Override
151        public void treeNodesInserted(TreeModelEvent event) {
152            delayedFireTableDataChanged();
153        }
154
155        @Override
156        public void treeNodesRemoved(TreeModelEvent event) {
157            delayedFireTableDataChanged();
158        }
159
160        @Override
161        public void treeStructureChanged(TreeModelEvent event) {
162            delayedFireTableDataChanged();
163        }
164
165    }
166
167}