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 javax.swing.JTree;
23 import javax.swing.SwingUtilities;
24 import javax.swing.event.TreeExpansionEvent;
25 import javax.swing.event.TreeExpansionListener;
26 import javax.swing.event.TreeModelEvent;
27 import javax.swing.event.TreeModelListener;
28 import javax.swing.table.AbstractTableModel;
29 import javax.swing.tree.TreePath;
30
31
32
33
34
35
36
37
38
39
40
41 public class TreeTableModelAdapter extends AbstractTableModel {
42
43
44 private static final long serialVersionUID = 8269213416115369275L;
45
46
47 private final JTree tree;
48
49 private final transient ParseTreeTableModel treeTableModel;
50
51
52
53
54
55
56
57 public TreeTableModelAdapter(ParseTreeTableModel treeTableModel, JTree tree) {
58 this.tree = tree;
59 this.treeTableModel = treeTableModel;
60
61 tree.addTreeExpansionListener(new UpdatingTreeExpansionListener());
62
63
64
65
66
67 treeTableModel.addTreeModelListener(new UpdatingTreeModelListener());
68 }
69
70
71
72 @Override
73 public int getColumnCount() {
74 return treeTableModel.getColumnCount();
75 }
76
77 @Override
78 public String getColumnName(int column) {
79 return treeTableModel.getColumnName(column);
80 }
81
82 @Override
83 public Class<?> getColumnClass(int column) {
84 return treeTableModel.getColumnClass(column);
85 }
86
87 @Override
88 public int getRowCount() {
89 return tree.getRowCount();
90 }
91
92 @Override
93 public Object getValueAt(int row, int column) {
94 return treeTableModel.getValueAt(nodeForRow(row), column);
95 }
96
97 @Override
98 public boolean isCellEditable(int row, int column) {
99 return treeTableModel.isCellEditable(column);
100 }
101
102
103
104
105
106
107
108 private Object nodeForRow(int row) {
109 final TreePath treePath = tree.getPathForRow(row);
110 return treePath.getLastPathComponent();
111 }
112
113
114
115
116 private final class UpdatingTreeExpansionListener implements TreeExpansionListener {
117
118
119
120 @Override
121 public void treeExpanded(TreeExpansionEvent event) {
122 fireTableDataChanged();
123 }
124
125 @Override
126 public void treeCollapsed(TreeExpansionEvent event) {
127 fireTableDataChanged();
128 }
129
130 }
131
132
133
134
135 private final class UpdatingTreeModelListener implements TreeModelListener {
136
137 @Override
138 public void treeNodesChanged(TreeModelEvent event) {
139 delayedFireTableDataChanged();
140 }
141
142 @Override
143 public void treeNodesInserted(TreeModelEvent event) {
144 delayedFireTableDataChanged();
145 }
146
147 @Override
148 public void treeNodesRemoved(TreeModelEvent event) {
149 delayedFireTableDataChanged();
150 }
151
152 @Override
153 public void treeStructureChanged(TreeModelEvent event) {
154 delayedFireTableDataChanged();
155 }
156
157
158
159
160
161 private void delayedFireTableDataChanged() {
162 SwingUtilities.invokeLater(TreeTableModelAdapter.this::fireTableDataChanged);
163 }
164
165 }
166
167 }