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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: TableBinding.java,v 1.2 2005/10/10 17:00:51 rbair Exp $
  3.  *
  4.  * Copyright 2004 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.binding.swingx;
  22. import java.beans.PropertyChangeEvent;
  23. import java.beans.PropertyChangeListener;
  24. import javax.swing.JComponent;
  25. import javax.swing.JTable;
  26. import javax.swing.table.TableModel;
  27. import org.jdesktop.binding.DataConstants;
  28. import org.jdesktop.binding.DataModel;
  29. import org.jdesktop.binding.IndexMapper;
  30. import org.jdesktop.binding.SelectionModel;
  31. import org.jdesktop.binding.TabularDataModel;
  32. import org.jdesktop.binding.metadata.TabularMetaData;
  33. import org.jdesktop.binding.swingx.adapter.DataModelToTableModelAdapter;
  34. import org.jdesktop.binding.swingx.adapter.ListSelectionBinding;
  35. import org.jdesktop.swingx.JXTable;
  36. /**
  37.  * Binding a JTable to a field in a DataModel. The field value must be
  38.  * of type TabularDataModel.
  39.  * 
  40.  * PENDING: no buffering, no validation.
  41.  * 
  42.  * @author Jeanette Winzenburg
  43.  */
  44. public class TableBinding extends AbstractBinding implements Binding {
  45.     private JTable table;
  46.     protected TableBinding(JComponent component, DataModel dataModel, String fieldName) {
  47.         super(component, dataModel, fieldName, AUTO_VALIDATE_NONE);
  48.     }
  49.     public boolean isModified() {
  50.         return false;
  51.     }
  52.     public boolean isValid() {
  53.         return true;
  54.     }
  55.     
  56.     protected void setComponent(JComponent component) {
  57.         table = (JTable) component;
  58.         configureSelection();
  59.     }
  60.     
  61.     private void configureSelection() {
  62.         Object selectionMeta = metaData.getCustomProperty(DataConstants.SELECTION_MODEL);
  63.         if (selectionMeta instanceof SelectionModel) {
  64.             new ListSelectionBinding((SelectionModel) selectionMeta, 
  65.                     table.getSelectionModel(), createIndexMapper(table));
  66.         }
  67.         
  68.     }
  69.     private IndexMapper createIndexMapper(final JTable table) {
  70.         if (!(table instanceof JXTable)) return null;
  71.         final JXTable xTable = (JXTable) table;
  72.         IndexMapper mapper = new IndexMapper() {
  73.             public int viewToModel(int index) {
  74.                  return xTable.convertRowIndexToModel(index);
  75.             }
  76.             public int modelToView(int index) {
  77.                 return xTable.convertRowIndexToView(index);
  78.             }
  79.             
  80.         };
  81.         return mapper;
  82.     }
  83.     
  84.     protected void installMetaDataListener() {
  85.         PropertyChangeListener l = new PropertyChangeListener() {
  86.             public void propertyChange(PropertyChangeEvent evt) {
  87.                 if (DataConstants.SELECTION_MODEL.equals(evt.getPropertyName())) {
  88.                     configureSelection();
  89.                 }
  90.                 
  91.             }
  92.             
  93.         };
  94.         metaData.addPropertyChangeListener(l);
  95.     }
  96.     protected Object getComponentValue() {
  97.         // TODO Auto-generated method stub
  98.         throw new UnsupportedOperationException(
  99.                 "must not call: TableBinding does not support getComponentValue");
  100.     }
  101.     protected void setComponentValue(Object value) {
  102.         // TODO don't recreate on every pull
  103.         if (value == table.getModel()) return;
  104.         if (value instanceof TabularDataModel) {
  105.             TableModel model = createTabularAdapter((TabularDataModel) value);
  106.             table.setModel(model);
  107.         }
  108.     }
  109.     private TableModel createTabularAdapter(TabularDataModel model) {
  110.         String[] fieldNames = null;
  111.         if (metaData instanceof TabularMetaData) {
  112.             fieldNames = ((TabularMetaData) metaData).getFieldNames();
  113.         }
  114.         return new DataModelToTableModelAdapter(model, fieldNames);
  115.     }
  116.     public JComponent getComponent() {
  117.         return table;
  118.     }
  119. }