View Javadoc

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