View Javadoc
1   package net.logAnalyzer.utils.gui.treetable;
2   /*
3    * %W% %E%
4    *
5    * Copyright 1997, 1998 Sun Microsystems, Inc. All Rights Reserved.
6    * 
7    * Redistribution and use in source and binary forms, with or
8    * without modification, are permitted provided that the following
9    * conditions are met:
10   * 
11   * - Redistributions of source code must retain the above copyright
12   *   notice, this list of conditions and the following disclaimer. 
13   *   
14   * - Redistribution in binary form must reproduce the above
15   *   copyright notice, this list of conditions and the following
16   *   disclaimer in the documentation and/or other materials
17   *   provided with the distribution. 
18   *   
19   * Neither the name of Sun Microsystems, Inc. or the names of
20   * contributors may be used to endorse or promote products derived
21   * from this software without specific prior written permission.  
22   * 
23   * This software is provided "AS IS," without a warranty of any
24   * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
25   * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
26   * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
27   * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY
28   * DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR
29   * RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE OR
30   * ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE 
31   * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT,   
32   * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER  
33   * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF 
34   * THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS 
35   * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
36   * 
37   * You acknowledge that this software is not designed, licensed or
38   * intended for use in the design, construction, operation or
39   * maintenance of any nuclear facility.
40   */
41  
42  import javax.swing.table.AbstractTableModel;
43  import javax.swing.JTree;
44  import javax.swing.tree.TreePath;
45  import javax.swing.event.TreeExpansionEvent;
46  import javax.swing.event.TreeExpansionListener;
47  
48  /***
49   * This is a wrapper class takes a TreeTableModel and implements 
50   * the table model interface. The implementation is trivial, with 
51   * all of the event dispatching support provided by the superclass: 
52   * the AbstractTableModel. 
53   *
54   * @version %I% %G%
55   *
56   * @author Philip Milne
57   * @author Scott Violet
58   */
59  
60  
61  public class TreeTableModelAdapter extends AbstractTableModel
62  {
63      JTree tree;
64      TreeTableModel treeTableModel;
65  
66      public TreeTableModelAdapter(TreeTableModel treeTableModel, JTree tree) {
67          this.tree = tree;
68          this.treeTableModel = treeTableModel;
69  
70  	tree.addTreeExpansionListener(new TreeExpansionListener() {
71  	    // Don't use fireTableRowsInserted() here; 
72  	    // the selection model would get  updated twice. 
73  	    public void treeExpanded(TreeExpansionEvent event) {  
74  	      fireTableDataChanged(); 
75  	    }
76              public void treeCollapsed(TreeExpansionEvent event) {  
77  	      fireTableDataChanged(); 
78  	    }
79  	});
80      }
81  
82    // Wrappers, implementing TableModel interface. 
83  
84      public int getColumnCount() {
85  	return treeTableModel.getColumnCount();
86      }
87  
88      public String getColumnName(int column) {
89  	return treeTableModel.getColumnName(column);
90      }
91  
92      public Class getColumnClass(int column) {
93  	return treeTableModel.getColumnClass(column);
94      }
95  
96      public int getRowCount() {
97  	return tree.getRowCount();
98      }
99  
100     protected Object nodeForRow(int row) {
101 	TreePath treePath = tree.getPathForRow(row);
102 	return treePath.getLastPathComponent();         
103     }
104 
105     public Object getValueAt(int row, int column) {
106 	return treeTableModel.getValueAt(nodeForRow(row), column);
107     }
108 
109     public boolean isCellEditable(int row, int column) {
110          return treeTableModel.isCellEditable(nodeForRow(row), column); 
111     }
112 
113     public void setValueAt(Object value, int row, int column) {
114 	treeTableModel.setValueAt(value, nodeForRow(row), column);
115     }
116 }
117 
118