View Javadoc

1   /*
2    * @(#)TreeTableModelAdapter.java	1.2 98/10/27
3    *
4    * Copyright 1997, 1998 by Sun Microsystems, Inc.,
5    * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
6    * All rights reserved.
7    *
8    * This software is the confidential and proprietary information
9    * of Sun Microsystems, Inc. ("Confidential Information").  You
10   * shall not disclose such Confidential Information and shall use
11   * it only in accordance with the terms of the license agreement
12   * you entered into with Sun.
13   */
14  package org.xnap.gui.table;
15  
16  import javax.swing.JTree;
17  import javax.swing.SwingUtilities;
18  import javax.swing.event.TreeExpansionEvent;
19  import javax.swing.event.TreeExpansionListener;
20  import javax.swing.event.TreeModelEvent;
21  import javax.swing.event.TreeModelListener;
22  import javax.swing.table.AbstractTableModel;
23  import javax.swing.tree.TreePath;
24  
25  /***
26   * This is a wrapper class takes a TreeTableModel and implements 
27   * the table model interface. The implementation is trivial, with 
28   * all of the event dispatching support provided by the superclass: 
29   * the AbstractTableModel. 
30   *
31   * @version 1.2 10/27/98
32   *
33   * @author Philip Milne
34   * @author Scott Violet
35   */
36  public class TreeTableModelAdapter extends AbstractTableModel
37  {
38      JTree tree;
39      TreeTableModel treeTableModel;
40  	
41      public TreeTableModelAdapter(TreeTableModel treeTableModel, JTree tree) {
42          this.tree = tree;
43          this.treeTableModel = treeTableModel;
44  
45  	tree.addTreeExpansionListener(new TreeExpansionListener() {
46  	    // Don't use fireTableRowsInserted() here; the selection model
47  	    // would get updated twice. 
48  	    public void treeExpanded(TreeExpansionEvent event) {  
49  	      fireTableDataChanged(); 
50  	    }
51          public void treeCollapsed(TreeExpansionEvent event) {  
52  	      fireTableDataChanged(); 
53  	    }
54  	});
55  
56  	// Install a TreeModelListener that can update the table when
57  	// tree changes. We use delayedFireTableDataChanged as we can
58  	// not be guaranteed the tree will have finished processing
59  	// the event before us.
60  	treeTableModel.addTreeModelListener(new TreeModelListener() {
61  	    public void treeNodesChanged(TreeModelEvent e) {
62  		delayedFireTableRowsUpdated();
63  	    }
64  
65  	    public void treeNodesInserted(TreeModelEvent e) {
66  			delayedFireTableRowsInserted();
67  	    }
68  
69  	    public void treeNodesRemoved(TreeModelEvent e) {
70  			delayedFireTableRowsDeleted();
71  	    }
72  
73  	    public void treeStructureChanged(TreeModelEvent e) {
74  			delayedFireTableDataChanged();
75  	    }
76  	});
77      }
78  
79      // Wrappers, implementing TableModel interface. 
80  
81      public int getColumnCount() {
82  		return treeTableModel.getColumnCount();
83      }
84  
85      public String getColumnName(int column) {
86  		return treeTableModel.getColumnName(column);
87      }
88  
89      public Class getColumnClass(int column) {
90  		return treeTableModel.getColumnClass(column);
91      }
92  
93      public int getRowCount() {
94  		return tree.getRowCount();
95      }
96  	
97  	protected Object nodeForRow(int row) {
98  		TreePath treePath = tree.getPathForRow(row);
99  		return treePath.getLastPathComponent();         
100     }
101 
102     public Object getValueAt(int row, int column) {
103 		return treeTableModel.getValueAt(nodeForRow(row), column);
104     }
105 
106     public boolean isCellEditable(int row, int column) {
107          return treeTableModel.isCellEditable(nodeForRow(row), column); 
108     }
109 
110     public void setValueAt(Object value, int row, int column) {
111 		treeTableModel.setValueAt(value, nodeForRow(row), column);
112     }
113 
114     /***
115      * Invokes fireTableDataChanged after all the pending events have been
116      * processed. SwingUtilities.invokeLater is used to handle this.
117      */
118     protected void delayedFireTableDataChanged() {
119 		SwingUtilities.invokeLater(new Runnable() {
120 		    public void run() {
121 				fireTableDataChanged();
122 		    }
123 		});
124     }
125 	
126 	protected void delayedFireTableRowsInserted() {
127 		SwingUtilities.invokeLater(new Runnable() {
128 		    public void run() {
129 				fireTableDataChanged();
130 		    }
131 		});
132     }
133 	
134 	protected void delayedFireTableRowsDeleted() {
135 		SwingUtilities.invokeLater(new Runnable() {
136 		    public void run() {
137 				fireTableDataChanged();
138 		    }
139 		});
140     }
141 	
142 	protected void delayedFireTableRowsUpdated() {
143 		SwingUtilities.invokeLater(new Runnable() {
144 		    public void run() {
145 				fireTableRowsUpdated(0, getRowCount());
146 		    }
147 		});
148     }
149 	
150 	
151 }
152