1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
35
36
37
38
39
40
41
42
43 public class TreeTableModelAdapter extends AbstractTableModel {
44
45
46 @Serial
47 private static final long serialVersionUID = 8269213416115369275L;
48
49
50 private final JTree tree;
51
52 private final transient ParseTreeTableModel treeTableModel;
53
54
55
56
57
58
59
60 public TreeTableModelAdapter(ParseTreeTableModel treeTableModel, JTree tree) {
61 this.tree = tree;
62 this.treeTableModel = treeTableModel;
63
64 tree.addTreeExpansionListener(new UpdatingTreeExpansionListener());
65
66
67
68
69
70 treeTableModel.addTreeModelListener(new UpdatingTreeModelListener());
71 }
72
73
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
107
108
109
110
111 private Object nodeForRow(int row) {
112 final TreePath treePath = tree.getPathForRow(row);
113 return treePath.getLastPathComponent();
114 }
115
116
117
118
119 private final class UpdatingTreeExpansionListener implements TreeExpansionListener {
120
121
122
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
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
162
163
164 private void delayedFireTableDataChanged() {
165 SwingUtilities.invokeLater(TreeTableModelAdapter.this::fireTableDataChanged);
166 }
167
168 }
169
170 }