1 ////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code for adherence to a set of rules.
3 // Copyright (C) 2001-2002 Oliver Burn
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 /*
21 * %W% %E%
22 *
23 * Copyright 1997, 1998 Sun Microsystems, Inc. All Rights Reserved.
24 *
25 * Redistribution and use in source and binary forms, with or
26 * without modification, are permitted provided that the following
27 * conditions are met:
28 *
29 * - Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 *
32 * - Redistribution in binary form must reproduce the above
33 * copyright notice, this list of conditions and the following
34 * disclaimer in the documentation and/or other materials
35 * provided with the distribution.
36 *
37 * Neither the name of Sun Microsystems, Inc. or the names of
38 * contributors may be used to endorse or promote products derived
39 * from this software without specific prior written permission.
40 *
41 * This software is provided "AS IS," without a warranty of any
42 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
43 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
44 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
45 * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY
46 * DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR
47 * RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE OR
48 * ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE
49 * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT,
50 * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
51 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF
52 * THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS
53 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
54 *
55 * You acknowledge that this software is not designed, licensed or
56 * intended for use in the design, construction, operation or
57 * maintenance of any nuclear facility.
58 */
59
60 package com.puppycrawl.tools.checkstyle.gui;
61
62 import javax.swing.JTree;
63 import javax.swing.SwingUtilities;
64 import javax.swing.table.AbstractTableModel;
65 import javax.swing.tree.TreePath;
66 import javax.swing.event.TreeExpansionEvent;
67 import javax.swing.event.TreeExpansionListener;
68 import javax.swing.event.TreeModelEvent;
69 import javax.swing.event.TreeModelListener;
70
71 /**
72 * This is a wrapper class takes a TreeTableModel and implements
73 * the table model interface. The implementation is trivial, with
74 * all of the event dispatching support provided by the superclass:
75 * the AbstractTableModel.
76 *
77 * <a href="http://java.sun.com/products/jfc/tsc/articles/treetable1/index.html">Original Source Location</a>
78 *
79 * @author Philip Milne
80 * @author Scott Violet
81 */
82 public class TreeTableModelAdapter extends AbstractTableModel
83 {
84 /** For Serialisation that will never happen. */
85 private static final long serialVersionUID = 8269213416115369275L;
86 private final JTree tree;
87 private final TreeTableModel treeTableModel;
88
89 public TreeTableModelAdapter(TreeTableModel treeTableModel, JTree tree)
90 {
91 this.tree = tree;
92 this.treeTableModel = treeTableModel;
93
94 tree.addTreeExpansionListener(new TreeExpansionListener()
95 {
96 // Don't use fireTableRowsInserted() here; the selection model
97 // would get updated twice.
98 @Override
99 public void treeExpanded(TreeExpansionEvent event)
100 {
101 fireTableDataChanged();
102 }
103
104 @Override
105 public void treeCollapsed(TreeExpansionEvent event)
106 {
107 fireTableDataChanged();
108 }
109 });
110
111 // Install a TreeModelListener that can update the table when
112 // mTree changes. We use delayedFireTableDataChanged as we can
113 // not be guaranteed the mTree will have finished processing
114 // the event before us.
115 treeTableModel.addTreeModelListener(new TreeModelListener()
116 {
117 @Override
118 public void treeNodesChanged(TreeModelEvent e)
119 {
120 delayedFireTableDataChanged();
121 }
122
123 @Override
124 public void treeNodesInserted(TreeModelEvent e)
125 {
126 delayedFireTableDataChanged();
127 }
128
129 @Override
130 public void treeNodesRemoved(TreeModelEvent e)
131 {
132 delayedFireTableDataChanged();
133 }
134
135 @Override
136 public void treeStructureChanged(TreeModelEvent e)
137 {
138 delayedFireTableDataChanged();
139 }
140 });
141 }
142
143 // Wrappers, implementing TableModel interface.
144
145 @Override
146 public int getColumnCount()
147 {
148 return treeTableModel.getColumnCount();
149 }
150
151 /**
152 * {@inheritDoc}
153 */
154 @Override
155 public String getColumnName(int column)
156 {
157 return treeTableModel.getColumnName(column);
158 }
159
160 /**
161 * {@inheritDoc}
162 */
163 @Override
164 public Class<?> getColumnClass(int column)
165 {
166 return treeTableModel.getColumnClass(column);
167 }
168
169 @Override
170 public int getRowCount()
171 {
172 return tree.getRowCount();
173 }
174
175 protected Object nodeForRow(int row)
176 {
177 final TreePath treePath = tree.getPathForRow(row);
178 return treePath.getLastPathComponent();
179 }
180
181 @Override
182 public Object getValueAt(int row, int column)
183 {
184 return treeTableModel.getValueAt(nodeForRow(row), column);
185 }
186
187 /**
188 * {@inheritDoc}
189 */
190 @Override
191 public boolean isCellEditable(int row, int column)
192 {
193 return treeTableModel.isCellEditable(nodeForRow(row), column);
194 }
195
196 /**
197 * {@inheritDoc}
198 */
199 @Override
200 public void setValueAt(Object value, int row, int column)
201 {
202 treeTableModel.setValueAt(value, nodeForRow(row), column);
203 }
204
205 /**
206 * Invokes fireTableDataChanged after all the pending events have been
207 * processed. SwingUtilities.invokeLater is used to handle this.
208 */
209 protected void delayedFireTableDataChanged()
210 {
211 SwingUtilities.invokeLater(new Runnable()
212 {
213 @Override
214 public void run()
215 {
216 fireTableDataChanged();
217 }
218 });
219 }
220 }
221