Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/puppycrawl/tools/checkstyle/gui/TreeTableModelAdapter.java


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;
37  import javax.swing.SwingUtilities;
38  import javax.swing.table.AbstractTableModel;
39  import javax.swing.tree.TreePath;
40  import javax.swing.event.TreeExpansionEvent;
41  import javax.swing.event.TreeExpansionListener;
42  import javax.swing.event.TreeModelEvent;
43  import javax.swing.event.TreeModelListener;
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
57  {
58      private JTree mTree;
59      private TreeTableModel mTreeTableModel;
60  
61      public TreeTableModelAdapter(TreeTableModel aTreeTableModel, JTree aTree)
62      {
63          this.mTree = aTree;
64          this.mTreeTableModel = aTreeTableModel;
65  
66          aTree.addTreeExpansionListener(new TreeExpansionListener()
67          {
68              // Don't use fireTableRowsInserted() here; the selection model
69              // would get updated twice.
70              public void treeExpanded(TreeExpansionEvent event)
71              {
72                  fireTableDataChanged();
73              }
74  
75              public void treeCollapsed(TreeExpansionEvent 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()
86          {
87              public void treeNodesChanged(TreeModelEvent e)
88              {
89                  delayedFireTableDataChanged();
90              }
91  
92              public void treeNodesInserted(TreeModelEvent e)
93              {
94                  delayedFireTableDataChanged();
95              }
96  
97              public void treeNodesRemoved(TreeModelEvent e)
98              {
99                  delayedFireTableDataChanged();
100             }
101 
102             public void treeStructureChanged(TreeModelEvent 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 getColumnName(int column)
117     {
118         return mTreeTableModel.getColumnName(column);
119     }
120 
121     public Class getColumnClass(int column)
122     {
123         return mTreeTableModel.getColumnClass(column);
124     }
125 
126     public int getRowCount()
127     {
128         final int rowCount = mTree.getRowCount();
129         return rowCount;
130     }
131 
132     protected Object nodeForRow(int row)
133     {
134         TreePath treePath = mTree.getPathForRow(row);
135         return treePath.getLastPathComponent();
136     }
137 
138     public Object getValueAt(int row, int column)
139     {
140         return mTreeTableModel.getValueAt(nodeForRow(row), column);
141     }
142 
143     public boolean isCellEditable(int row, int column)
144     {
145         return mTreeTableModel.isCellEditable(nodeForRow(row), column);
146     }
147 
148     public void setValueAt(Object value, int row, int column)
149     {
150         mTreeTableModel.setValueAt(value, nodeForRow(row), column);
151     }
152 
153     /**
154      * Invokes fireTableDataChanged after all the pending events have been
155      * processed. SwingUtilities.invokeLater is used to handle this.
156      */
157     protected void delayedFireTableDataChanged()
158     {
159         SwingUtilities.invokeLater(new Runnable()
160         {
161             public void run()
162             {
163                 fireTableDataChanged();
164             }
165         });
166     }
167 }
168