KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > gui > TreeTableModelAdapter


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  * @(#)TreeTableModelAdapter.java 1.2 98/10/27
22  *
23  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
24  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
25  * All rights reserved.
26  *
27  * This software is the confidential and proprietary information
28  * of Sun Microsystems, Inc. ("Confidential Information"). You
29  * shall not disclose such Confidential Information and shall use
30  * it only in accordance with the terms of the license agreement
31  * you entered into with Sun.
32  */

33
34 package com.puppycrawl.tools.checkstyle.gui;
35
36 import javax.swing.JTree JavaDoc;
37 import javax.swing.SwingUtilities JavaDoc;
38 import javax.swing.table.AbstractTableModel JavaDoc;
39 import javax.swing.tree.TreePath JavaDoc;
40 import javax.swing.event.TreeExpansionEvent JavaDoc;
41 import javax.swing.event.TreeExpansionListener JavaDoc;
42 import javax.swing.event.TreeModelEvent JavaDoc;
43 import javax.swing.event.TreeModelListener JavaDoc;
44
45 /**
46  * This is a wrapper class takes a TreeTableModel and implements
47  * the table model interface. The implementation is trivial, with
48  * all of the event dispatching support provided by the superclass:
49  * the AbstractTableModel.
50  *
51  * @version 1.2 10/27/98
52  *
53  * @author Philip Milne
54  * @author Scott Violet
55  */

56 public class TreeTableModelAdapter extends AbstractTableModel JavaDoc
57 {
58     private JTree JavaDoc mTree;
59     private TreeTableModel mTreeTableModel;
60
61     public TreeTableModelAdapter(TreeTableModel aTreeTableModel, JTree JavaDoc aTree)
62     {
63         this.mTree = aTree;
64         this.mTreeTableModel = aTreeTableModel;
65
66         aTree.addTreeExpansionListener(new TreeExpansionListener JavaDoc()
67         {
68             // Don't use fireTableRowsInserted() here; the selection model
69
// would get updated twice.
70
public void treeExpanded(TreeExpansionEvent JavaDoc event)
71             {
72                 fireTableDataChanged();
73             }
74
75             public void treeCollapsed(TreeExpansionEvent JavaDoc event)
76             {
77                 fireTableDataChanged();
78             }
79         });
80
81         // Install a TreeModelListener that can update the table when
82
// mTree changes. We use delayedFireTableDataChanged as we can
83
// not be guaranteed the mTree will have finished processing
84
// the event before us.
85
aTreeTableModel.addTreeModelListener(new TreeModelListener JavaDoc()
86         {
87             public void treeNodesChanged(TreeModelEvent JavaDoc e)
88             {
89                 delayedFireTableDataChanged();
90             }
91
92             public void treeNodesInserted(TreeModelEvent JavaDoc e)
93             {
94                 delayedFireTableDataChanged();
95             }
96
97             public void treeNodesRemoved(TreeModelEvent JavaDoc e)
98             {
99                 delayedFireTableDataChanged();
100             }
101
102             public void treeStructureChanged(TreeModelEvent JavaDoc e)
103             {
104                 delayedFireTableDataChanged();
105             }
106         });
107     }
108
109     // Wrappers, implementing TableModel interface.
110

111     public int getColumnCount()
112     {
113         return mTreeTableModel.getColumnCount();
114     }
115
116     public String JavaDoc getColumnName(int column)
117     {
118         return mTreeTableModel.getColumnName(column);
119     }
120
121     public Class JavaDoc getColumnClass(int column)
122     {
123         return mTreeTableModel.getColumnClass(column);
124     }
125
126     public int getRowCount()
127     {
128         return mTree.getRowCount();
129     }
130
131     protected Object JavaDoc nodeForRow(int row)
132     {
133         final TreePath JavaDoc treePath = mTree.getPathForRow(row);
134         return treePath.getLastPathComponent();
135     }
136
137     public Object JavaDoc getValueAt(int row, int column)
138     {
139         return mTreeTableModel.getValueAt(nodeForRow(row), column);
140     }
141
142     public boolean isCellEditable(int row, int column)
143     {
144         return mTreeTableModel.isCellEditable(nodeForRow(row), column);
145     }
146
147     public void setValueAt(Object JavaDoc value, int row, int column)
148     {
149         mTreeTableModel.setValueAt(value, nodeForRow(row), column);
150     }
151
152     /**
153      * Invokes fireTableDataChanged after all the pending events have been
154      * processed. SwingUtilities.invokeLater is used to handle this.
155      */

156     protected void delayedFireTableDataChanged()
157     {
158         SwingUtilities.invokeLater(new Runnable JavaDoc()
159         {
160             public void run()
161             {
162                 fireTableDataChanged();
163             }
164         });
165     }
166 }
167
168
Popular Tags