MetaDataProviderAdapter.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:3k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: MetaDataProviderAdapter.java,v 1.3 2005/10/10 17:01:13 rbair Exp $
  3.  *
  4.  * Copyright 2005 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.dataset.adapter;
  22. import java.util.List;
  23. import org.jdesktop.binding.metadata.MetaData;
  24. import org.jdesktop.binding.metadata.MetaDataProvider;
  25. import org.jdesktop.dataset.DataColumn;
  26. import org.jdesktop.dataset.DataTable;
  27. import org.jdesktop.dataset.event.DataTableListener;
  28. import org.jdesktop.dataset.event.RowChangeEvent;
  29. import org.jdesktop.dataset.event.TableChangeEvent;
  30. /**
  31.  *
  32.  * @author rbair
  33.  */
  34. public class MetaDataProviderAdapter implements MetaDataProvider {
  35.     private DataTable table;
  36.     private MetaData[] cachedMetaData;
  37.     private String[] cachedFieldNames;
  38.     
  39.     /** Creates a new instance of MetaDataProviderAdapter */
  40.     public MetaDataProviderAdapter(DataTable table) {
  41.         assert table != null;
  42.         this.table = table;
  43.         initMetaData();
  44.         //add listener to table for column change events
  45.         table.addDataTableListener(new DataTableListener(){
  46.             public void rowChanged(RowChangeEvent evt) {
  47.                 //null op
  48.             }
  49.             public void tableChanged(TableChangeEvent evt) {
  50.                 //reload the metaData
  51.                 initMetaData();
  52.             }
  53.         });
  54.     }
  55.     public int getFieldCount() {
  56.         return cachedMetaData == null ? 0 : cachedMetaData.length;
  57.     }
  58.     public final String[] getFieldNames() {
  59.         return cachedFieldNames ==  null ? new String[0] : cachedFieldNames;
  60.     }
  61.     public final MetaData[] getMetaData() {
  62.         return cachedMetaData == null ? new MetaData[0] : cachedMetaData;
  63.     }
  64.     public MetaData getMetaData(String dataID) {
  65.         if (cachedMetaData == null) {
  66.             return new MetaData(dataID);
  67.         }
  68.         for (MetaData md : cachedMetaData) {
  69.             if (md.getName().equals(dataID)) {
  70.                 return md;
  71.             }
  72.         }
  73.         return null;
  74.     }
  75.     
  76.     private void initMetaData() {
  77.         List<DataColumn> cols = table.getColumns();
  78.         cachedMetaData = new MetaData[cols.size()];
  79.         cachedFieldNames = new String[cachedMetaData.length];
  80.         for (int i=0; i<cachedMetaData.length; i++) {
  81.             DataColumn col = cols.get(i);
  82.             //TODO if the column name changes, my cache is invalidated!!!
  83.             cachedMetaData[i] = new MetaData(col.getName(), col.getType());
  84.             cachedFieldNames[i] = col.getName();
  85.         }
  86.     }
  87. }