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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: TabularDataModelAdapter.java,v 1.4 2005/10/10 17:01:14 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.ArrayList;
  23. import java.util.List;
  24. import org.jdesktop.binding.TabularDataModel;
  25. import org.jdesktop.binding.TabularValueChangeEvent;
  26. import org.jdesktop.binding.TabularValueChangeListener;
  27. import org.jdesktop.binding.ValueChangeEvent;
  28. import org.jdesktop.binding.ValueChangeListener;
  29. import org.jdesktop.binding.metadata.MetaData;
  30. import org.jdesktop.binding.metadata.Validator;
  31. import org.jdesktop.dataset.DataRow;
  32. import org.jdesktop.dataset.DataTable;
  33. import org.jdesktop.dataset.event.DataTableListener;
  34. /**
  35.  *
  36.  * @author rbair
  37.  */
  38. public class TabularDataModelAdapter implements TabularDataModel {
  39.     private DataTable table;
  40.     private MetaDataProviderAdapter mdp;
  41.     private List<Validator> validators = new ArrayList<Validator>();
  42.     private List<ValueChangeListener> listeners = new ArrayList<ValueChangeListener>();
  43.     private List<TabularValueChangeListener> tabularListeners = new ArrayList<TabularValueChangeListener>();
  44.  
  45.     
  46.     /** Creates a new instance of TabularDataModelAdapter */
  47.     public TabularDataModelAdapter(DataTable table) {
  48.         assert table != null;
  49.         this.table = table;
  50.         mdp = new MetaDataProviderAdapter(table);
  51.         table.addDataTableListener(new DataTableListener() {
  52.             public void rowChanged(org.jdesktop.dataset.event.RowChangeEvent evt) {
  53.                 fireRowChanged((DataRow) evt.getSource());
  54.             }
  55.             public void tableChanged(org.jdesktop.dataset.event.TableChangeEvent evt) {
  56.                 fireTableChanged();
  57.             }
  58.             
  59.         });
  60.     }
  61.     public int getRecordCount() {
  62.         return table.getRowCount();
  63.     }
  64.     public DataTable getDataTable() {
  65.         return table;
  66.     }
  67.     
  68.     public void addValidator(Validator validator) {
  69.         if (!validators.contains(validator)) {
  70.             validators.add(validator);
  71.         }
  72.     }
  73.     public void removeValidator(Validator validator) {
  74.         validators.remove(validator);
  75.     }
  76.     public Validator[] getValidators() {
  77.         return validators.toArray(new Validator[validators.size()]);
  78.     }
  79.     public void addValueChangeListener(ValueChangeListener valueChangeListener) {
  80.         if (!listeners.contains(valueChangeListener)) {
  81.             listeners.add(valueChangeListener);
  82.         }
  83.     }
  84.     public void removeValueChangeListener(ValueChangeListener valueChangeListener) {
  85.         listeners.remove(valueChangeListener);
  86.     }
  87.     public ValueChangeListener[] getValueChangeListeners() {
  88.         return listeners.toArray(new ValueChangeListener[listeners.size()]);
  89.     }
  90.     
  91.     public void addTabularValueChangeListener(TabularValueChangeListener l) {
  92.         if (!tabularListeners.contains(l)) {
  93.             tabularListeners.add(l);
  94.         }
  95.     }
  96.     public TabularValueChangeListener[] getTabularValueChangeListeners() {
  97.         return tabularListeners.toArray(new TabularValueChangeListener[tabularListeners.size()]);
  98.     }
  99.     public void removeTabularValueChangeListener(TabularValueChangeListener l) {
  100.         tabularListeners.remove(l);
  101.     }
  102.     public int getFieldCount() {
  103.         return mdp.getFieldCount();
  104.     }
  105.     public String[] getFieldNames() {
  106.         return mdp.getFieldNames();
  107.     }
  108.     public MetaData[] getMetaData() {
  109.         return mdp.getMetaData();
  110.     }
  111.     public MetaData getMetaData(String dataID) {
  112.         return mdp.getMetaData(dataID);
  113.     }
  114.     public Object getValueAt(String fieldName, int index) {
  115.         return table.getValue(index, fieldName);
  116.     }
  117.     public void setValueAt(String fieldName, int index, Object value) {
  118.         table.setValue(index, fieldName,  value);
  119.         // JW: as long as the DataTable doesn't fire anything on updates
  120.         // we must do it manually
  121.         fireTabularValueChanged(index, fieldName);
  122.     }
  123.     public Object getValue(String fieldName) {
  124.         //null op
  125.         return null;
  126.     }
  127.     public void setValue(String fieldName, Object value) {
  128.         //null op
  129.     }
  130.     
  131. protected void fireValueChanged(String fieldName) {
  132. ValueChangeEvent e = new ValueChangeEvent(this, fieldName);//getCachedEvent(fieldName);
  133.         for (ValueChangeListener listener : listeners) {
  134. try {
  135.     listener.valueChanged(e);
  136. } catch (Exception ex) {
  137.                 //TODO some real exception handling needs to occur
  138.     ex.printStackTrace();
  139. }
  140. }
  141. }
  142.     protected void fireRowChanged(DataRow row) {
  143.        // fireAllFieldsChanged();
  144.         int rowIndex = table.getRows().indexOf(row);
  145.         fireTabularValueChanged(rowIndex, null);
  146.     }
  147.     protected void fireTableChanged() {
  148.        // fireAllFieldsChanged();
  149.         fireTabularValueChanged(-1, null);
  150.     }
  151.     protected void fireTabularValueChanged(int rowIndex, String fieldName) {
  152.         TabularValueChangeEvent e = new TabularValueChangeEvent(this, rowIndex, fieldName);
  153.         for (TabularValueChangeListener l: tabularListeners) {
  154.             l.tabularValueChanged(e);
  155.         }
  156.         
  157.     }
  158.     protected void fireAllFieldsChanged() {
  159.         for (String fieldName : mdp.getFieldNames()) {
  160.             fireValueChanged(fieldName);
  161.         }
  162.         // JW: why? 
  163.         fireValueChanged("");
  164.     }
  165. }