Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members  

TTreeTableModelToTableModelAdapter.java

00001 /*
00002 *  Name:        TTreeTableModelToTableModelAdapter.java
00003 *  Author:      Philip Milne
00004 *               Scott Violet
00005 *  Maintainer:  Rafael Jesus Alcantara Perez
00006 *  Summary:     Tree-table model to table model adapter.
00007 *  Date:        $Date: 2003/09/28 23:22:05 $
00008 *  Revision:    $Revision: 1.2 $
00009 *
00010 *  Copyright (C) 2003  Rafael Jesus Alcantara Perez <rafa@dedalo-ing.com>
00011 *
00012 *  This program is free software; you can redistribute it and/or modify
00013 *  it under the terms of the GNU General Public License as published by
00014 *  the Free Software Foundation; either version 2 of the License, or
00015 *  (at your option) any later version.
00016 *
00017 *  This program is distributed in the hope that it will be useful,
00018 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 *  GNU General Public License for more details.
00021 *
00022 *  You should have received a copy of the GNU General Public License
00023 *  along with this program; if not, write to the Free Software
00024 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00025 *  MA 02111-1307, USA.
00026 *
00027 *
00028 *  Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
00029 *
00030 *  Redistribution and use in source and binary forms, with or
00031 *  without modification, are permitted provided that the following
00032 *  conditions are met:
00033 *
00034 *  - Redistributions of source code must retain the above copyright
00035 *    notice, this list of conditions and the following disclaimer.
00036 *
00037 *  - Redistribution in binary form must reproduce the above
00038 *    copyright notice, this list of conditions and the following
00039 *    disclaimer in the documentation and/or other materials
00040 *    provided with the distribution.
00041 *
00042 *  Neither the name of Sun Microsystems, Inc. or the names of
00043 *  contributors may be used to endorse or promote products derived
00044 *  from this software without specific prior written permission.
00045 *
00046 *  This software is provided "AS IS," without a warranty of any
00047 *  kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
00048 *  WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
00049 *  FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
00050 *  EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY
00051 *  DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR
00052 *  RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE OR
00053 *  ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE
00054 *  FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT,
00055 *  SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
00056 *  CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF
00057 *  THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS
00058 *  BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
00059 *
00060 *  You acknowledge that this software is not designed, licensed or
00061 *  intended for use in the design, construction, operation or
00062 *  maintenance of any nuclear facility.
00063 */
00064 
00066 package org.mpcl.nui.treetable;
00067 
00068 import javax.swing.JTree;
00069 import javax.swing.SwingUtilities;
00070 import javax.swing.table.AbstractTableModel;
00071 import javax.swing.tree.TreePath;
00072 import javax.swing.event.TreeExpansionEvent;
00073 import javax.swing.event.TreeExpansionListener;
00074 import javax.swing.event.TreeModelEvent;
00075 import javax.swing.event.TreeModelListener;
00076 
00077 
00083 public class TTreeTableModelToTableModelAdapter extends AbstractTableModel
00084 {
00085 
00087   private JTree   tTree;
00088 
00090   private ITreeTableModel   tTreeTableModel;
00091 
00092 
00093   //
00094   //  C O N S T R U C T O R S
00095   //
00096 
00102   public TTreeTableModelToTableModelAdapter (ITreeTableModel tTREE_TABLE_MODEL, JTree tTREE)
00103   {
00104 
00105     tTree           = tTREE;
00106     tTreeTableModel = tTREE_TABLE_MODEL;
00107     tTree.addTreeExpansionListener
00108     (
00109       new TreeExpansionListener()
00110       {
00111         //
00112         //  Do not use fireTableRowsInserted() here;  the selection model would
00113         //  get updated twice.
00114         //
00115         public void treeExpanded (TreeExpansionEvent tTREE_EXPANSION_EVENT)
00116         {  
00117           fireTableDataChanged();
00118         }
00119 
00120         public void treeCollapsed (TreeExpansionEvent tTREE_EXPANSION_EVENT)
00121         {
00122           fireTableDataChanged(); 
00123         }
00124       }
00125     );
00126 
00127     //
00128     //  Installs a  TreeModelListener  that  can update  the  table  when  tree
00129     //  changes. We use delayedFireTableDataChanged as we can not be guaranteed
00130     //  the tree will have finished processing the event before us.
00131     //
00132     tTreeTableModel.addTreeModelListener
00133     (
00134       new TreeModelListener()
00135       {
00136         public void treeNodesChanged (TreeModelEvent tTREE_MODEL_EVENT)
00137         {
00138           delayedFireTableDataChanged();
00139         }
00140 
00141         public void treeNodesInserted (TreeModelEvent tTREE_MODEL_EVENT)
00142         {
00143           delayedFireTableDataChanged();
00144         }
00145 
00146         public void treeNodesRemoved (TreeModelEvent tTREE_MODEL_EVENT)
00147         {
00148           delayedFireTableDataChanged();
00149         }
00150 
00151         public void treeStructureChanged (TreeModelEvent tTREE_MODEL_EVENT)
00152         {
00153           delayedFireTableDataChanged();
00154         }
00155       }
00156     );
00157 
00158   }  // TTreeTableModelToTableModelAdapter()
00159 
00160 
00161   //
00162   //  S E L E C T O R S
00163   //
00164 
00169   protected void delayedFireTableDataChanged()
00170   {
00171     SwingUtilities.invokeLater
00172     (
00173       new Runnable()
00174       {
00175         public void run()
00176         {
00177           fireTableDataChanged();
00178         }
00179       }
00180     );
00181   }
00182 
00183   public Class getColumnClass (int iCOLUMN)
00184   {
00185     return tTreeTableModel.getColumnClass (iCOLUMN);
00186   }
00187 
00188   public int getColumnCount()
00189   {
00190     return tTreeTableModel.getColumnCount();
00191   }
00192 
00193   public String getColumnName (int iCOLUMN)
00194   {
00195     return tTreeTableModel.getColumnName (iCOLUMN);
00196   }
00197 
00198   public int getRowCount()
00199   {
00200     return tTree.getRowCount();
00201   }
00202 
00203   public Object getValueAt(int iROW, int iCOLUMN)
00204   {
00205     return tTreeTableModel.getValueAt (nodeForRow (iROW), iCOLUMN);
00206   }
00207 
00208   public boolean isCellEditable (int iROW, int iCOLUMN)
00209   {
00210     return tTreeTableModel.isCellEditable (nodeForRow (iROW), iCOLUMN); 
00211   }
00212 
00213   protected Object nodeForRow (int iROW)
00214   {
00215     return tTree.getPathForRow (iROW).getLastPathComponent();         
00216   }
00217 
00218 
00219   //
00220   //  C O N S T R U C T O R S
00221   //
00222 
00223   public void setValueAt (Object tVALUE, int iROW, int iCOLUMN)
00224   {
00225     tTreeTableModel.setValueAt (tVALUE, nodeForRow (iROW), iCOLUMN);
00226   }
00227 
00228 }  // class TTreeTableModelToTableModelAdapter

Generated on Mon Oct 13 02:35:24 2003 for MPCL by doxygen1.2.18