Main Page   Packages   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

TreeTableModelAdapter.java

00001 package edu.ksu.cis.bandera.abstraction.gui;
00002 
00003 /*
00004  * @(#)TreeTableModelAdapter.java   1.2 98/10/27
00005  *
00006  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
00007  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
00008  * All rights reserved.
00009  *
00010  * This software is the confidential and proprietary information
00011  * of Sun Microsystems, Inc. ("Confidential Information").  You
00012  * shall not disclose such Confidential Information and shall use
00013  * it only in accordance with the terms of the license agreement
00014  * you entered into with Sun.
00015  */
00016 
00017 import javax.swing.JTree;
00018 import javax.swing.SwingUtilities;
00019 import javax.swing.table.AbstractTableModel;
00020 import javax.swing.tree.TreePath;
00021 import javax.swing.event.TreeExpansionEvent;
00022 import javax.swing.event.TreeExpansionListener;
00023 import javax.swing.event.TreeModelEvent;
00024 import javax.swing.event.TreeModelListener;
00025 
00026 /**
00027  * This is a wrapper class takes a TreeTableModel and implements 
00028  * the table model interface. The implementation is trivial, with 
00029  * all of the event dispatching support provided by the superclass: 
00030  * the AbstractTableModel. 
00031  *
00032  * @version 1.2 10/27/98
00033  *
00034  * @author Philip Milne
00035  * @author Scott Violet
00036  */
00037 public class TreeTableModelAdapter extends AbstractTableModel
00038 {
00039     JTree tree;
00040     TreeTableModel treeTableModel;
00041 
00042     public TreeTableModelAdapter(TreeTableModel treeTableModel, JTree tree) {
00043         this.tree = tree;
00044         this.treeTableModel = treeTableModel;
00045 
00046     tree.addTreeExpansionListener(new TreeExpansionListener() {
00047         // Don't use fireTableRowsInserted() here; the selection model
00048         // would get updated twice. 
00049         public void treeExpanded(TreeExpansionEvent event) {  
00050           fireTableDataChanged(); 
00051         }
00052             public void treeCollapsed(TreeExpansionEvent event) {  
00053           fireTableDataChanged(); 
00054         }
00055     });
00056 
00057     // Install a TreeModelListener that can update the table when
00058     // tree changes. We use delayedFireTableDataChanged as we can
00059     // not be guaranteed the tree will have finished processing
00060     // the event before us.
00061     treeTableModel.addTreeModelListener(new TreeModelListener() {
00062         public void treeNodesChanged(TreeModelEvent e) {
00063         delayedFireTableDataChanged();
00064         }
00065 
00066         public void treeNodesInserted(TreeModelEvent e) {
00067         delayedFireTableDataChanged();
00068         }
00069 
00070         public void treeNodesRemoved(TreeModelEvent e) {
00071         delayedFireTableDataChanged();
00072         }
00073 
00074         public void treeStructureChanged(TreeModelEvent e) {
00075         delayedFireTableDataChanged();
00076         }
00077     });
00078     }
00079     /**
00080      * Invokes fireTableDataChanged after all the pending events have been
00081      * processed. SwingUtilities.invokeLater is used to handle this.
00082      */
00083     protected void delayedFireTableDataChanged() {
00084     SwingUtilities.invokeLater(new Runnable() {
00085         public void run() {
00086         fireTableDataChanged();
00087         }
00088     });
00089     }
00090     public Class getColumnClass(int column) {
00091     return treeTableModel.getColumnClass(column);
00092     }
00093 // Wrappers, implementing TableModel interface. 
00094 
00095 public int getColumnCount() {
00096     return treeTableModel.getColumnCount();
00097 }
00098 public String getColumnName(int column) {
00099     return treeTableModel.getColumnName(column);
00100 }
00101 public int getRowCount() {
00102     return tree.getRowCount();
00103 }
00104 public Object getValueAt(int row, int column) {
00105     return treeTableModel.getValueAt(nodeForRow(row), column);
00106 }
00107 public boolean isCellEditable(int row, int column) {
00108     return treeTableModel.isCellEditable(nodeForRow(row), column);
00109 }
00110 public Object nodeForRow(int row) {
00111     TreePath treePath = tree.getPathForRow(row);
00112     return treePath.getLastPathComponent();
00113 }
00114     public void setValueAt(Object value, int row, int column) {
00115     treeTableModel.setValueAt(value, nodeForRow(row), column);
00116     }
00117 }

Generated at Thu Feb 7 06:58:07 2002 for Bandera by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001