View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.gui;
21  
22  import java.io.Serial;
23  
24  import javax.swing.JTree;
25  import javax.swing.SwingUtilities;
26  import javax.swing.event.TreeExpansionEvent;
27  import javax.swing.event.TreeExpansionListener;
28  import javax.swing.event.TreeModelEvent;
29  import javax.swing.event.TreeModelListener;
30  import javax.swing.table.AbstractTableModel;
31  import javax.swing.tree.TreePath;
32  
33  /**
34   * This is a wrapper class takes a TreeTableModel and implements
35   * the table model interface. The implementation is trivial, with
36   * all the event dispatching support provided by the superclass:
37   * the AbstractTableModel.
38   * <a href=
39   * "https://docs.oracle.com/cd/E48246_01/apirefs.1111/e13403/oracle/ide/controls/TreeTableModel.html">
40   * Original&nbsp;Source&nbsp;Location</a>
41   *
42   */
43  public class TreeTableModelAdapter extends AbstractTableModel {
44  
45      /** A unique serial version identifier. */
46      @Serial
47      private static final long serialVersionUID = 8269213416115369275L;
48  
49      /** JTree component. */
50      private final JTree tree;
51      /** Tree table model. */
52      private final transient ParseTreeTableModel treeTableModel;
53  
54      /**
55       * Initialise tree and treeTableModel class attributes.
56       *
57       * @param treeTableModel Tree table model.
58       * @param tree JTree component.
59       */
60      public TreeTableModelAdapter(ParseTreeTableModel treeTableModel, JTree tree) {
61          this.tree = tree;
62          this.treeTableModel = treeTableModel;
63  
64          tree.addTreeExpansionListener(new UpdatingTreeExpansionListener());
65  
66          // Install a TreeModelListener that can update the table when
67          // mTree changes. We use delayedFireTableDataChanged as we can
68          // not be guaranteed the mTree will have finished processing
69          // the event before us.
70          treeTableModel.addTreeModelListener(new UpdatingTreeModelListener());
71      }
72  
73      // Wrappers, implementing TableModel interface.
74  
75      @Override
76      public int getColumnCount() {
77          return treeTableModel.getColumnCount();
78      }
79  
80      @Override
81      public String getColumnName(int column) {
82          return treeTableModel.getColumnName(column);
83      }
84  
85      @Override
86      public Class<?> getColumnClass(int column) {
87          return treeTableModel.getColumnClass(column);
88      }
89  
90      @Override
91      public int getRowCount() {
92          return tree.getRowCount();
93      }
94  
95      @Override
96      public Object getValueAt(int row, int column) {
97          return treeTableModel.getValueAt(nodeForRow(row), column);
98      }
99  
100     @Override
101     public boolean isCellEditable(int row, int column) {
102         return treeTableModel.isCellEditable(column);
103     }
104 
105     /**
106      * Finds node for a given row.
107      *
108      * @param row Row for which to find a related node.
109      * @return Node for a given row.
110      */
111     private Object nodeForRow(int row) {
112         final TreePath treePath = tree.getPathForRow(row);
113         return treePath.getLastPathComponent();
114     }
115 
116     /**
117      * TreeExpansionListener that can update the table when tree changes.
118      */
119     private final class UpdatingTreeExpansionListener implements TreeExpansionListener {
120 
121         // Don't use fireTableRowsInserted() here; the selection model
122         // would get updated twice.
123         @Override
124         public void treeExpanded(TreeExpansionEvent event) {
125             fireTableDataChanged();
126         }
127 
128         @Override
129         public void treeCollapsed(TreeExpansionEvent event) {
130             fireTableDataChanged();
131         }
132 
133     }
134 
135     /**
136      * TreeModelListener that can update the table when tree changes.
137      */
138     private final class UpdatingTreeModelListener implements TreeModelListener {
139 
140         @Override
141         public void treeNodesChanged(TreeModelEvent event) {
142             delayedFireTableDataChanged();
143         }
144 
145         @Override
146         public void treeNodesInserted(TreeModelEvent event) {
147             delayedFireTableDataChanged();
148         }
149 
150         @Override
151         public void treeNodesRemoved(TreeModelEvent event) {
152             delayedFireTableDataChanged();
153         }
154 
155         @Override
156         public void treeStructureChanged(TreeModelEvent event) {
157             delayedFireTableDataChanged();
158         }
159 
160         /**
161          * Invokes fireTableDataChanged after all the pending events have been
162          * processed. SwingUtilities.invokeLater is used to handle this.
163          */
164         private void delayedFireTableDataChanged() {
165             SwingUtilities.invokeLater(TreeTableModelAdapter.this::fireTableDataChanged);
166         }
167 
168     }
169 
170 }