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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id $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 javax.swing.JComponent;
  23. import javax.swing.JTable;
  24. import javax.swing.table.TableModel;
  25. import org.jdesktop.binding.DataModel;
  26. import org.jdesktop.binding.IndexMapper;
  27. import org.jdesktop.binding.SelectionModel;
  28. import org.jdesktop.binding.TabularDataModel;
  29. import org.jdesktop.binding.swingx.adapter.DataModelToTableModelAdapter;
  30. import org.jdesktop.binding.swingx.adapter.ListSelectionBinding;
  31. import org.jdesktop.swingx.JXTable;
  32. /**
  33.  * This "Binding" happens to the given DataModel as a whole (as
  34.  * opposed to a single field of the model).
  35.  * 
  36.  * JW: added mapping of row indices. (TBD: unit testing of mapping).
  37.  *  
  38.  * @author Richard Bair
  39.  */
  40. public class DirectTableBinding extends AbstractBinding {
  41. private JTable table;
  42. /**
  43.  * @param component
  44.  * @param dataModel
  45.  */
  46. public DirectTableBinding(JTable component, TabularDataModel dataModel) {
  47.         this(component, dataModel, null);
  48. // super(component, dataModel, "", DirectTableBinding.AUTO_VALIDATE_NONE);
  49. // //construct a TableModel for the table based on the given dataModel.
  50. // TableModel tm = createAdapter(dataModel, null);
  51. // table.setModel(tm);
  52. }
  53. public DirectTableBinding(JTable component, TabularDataModel dataModel, String[] fieldNames) {
  54. // super(component, dataModel, "", DirectTableBinding.AUTO_VALIDATE_NONE);
  55. // //construct a TableModel for the table based on the given dataModel.
  56. // TableModel tm = createAdapter(dataModel, fieldNames);
  57. // table.setModel(tm);
  58.         this(component, dataModel, fieldNames, null);
  59. }
  60.     public DirectTableBinding(JTable component, TabularDataModel dataModel, 
  61.             String[] fieldNames, SelectionModel selectionModel) {
  62.         super(component, dataModel, "", DirectTableBinding.AUTO_VALIDATE_NONE);
  63.         //construct a TableModel for the table based on the given dataModel.
  64.         TableModel tm = createAdapter(dataModel, fieldNames);
  65.         table.setModel(tm);
  66.         if (selectionModel != null) {
  67.            new ListSelectionBinding(selectionModel, 
  68.                    table.getSelectionModel(), createIndexMapper(table));
  69.         }
  70.     }
  71.     private IndexMapper createIndexMapper(final JTable table) {
  72.         if (!(table instanceof JXTable)) return null;
  73.         final JXTable xTable = (JXTable) table;
  74.         IndexMapper mapper = new IndexMapper() {
  75.             public int viewToModel(int index) {
  76.                  return xTable.convertRowIndexToModel(index);
  77.             }
  78.             public int modelToView(int index) {
  79.                 return xTable.convertRowIndexToView(index);
  80.             }
  81.             
  82.         };
  83.         return mapper;
  84.     }
  85.     public boolean push() {
  86.         return true;
  87.     }
  88.     
  89.     public boolean pull() {
  90.         return true;
  91.     }
  92.     
  93.     public boolean isValid() {
  94.         return true;
  95.     }
  96.     
  97.     protected TableModel createAdapter(TabularDataModel tabularDataModel, String[] fieldNames) {
  98.         return new DataModelToTableModelAdapter(tabularDataModel, fieldNames);
  99.     }
  100. /* (non-Javadoc)
  101.  * @see org.jdesktop.swing.binding.AbstractUIBinding#getBoundComponent()
  102.  */
  103. protected JComponent getBoundComponent() {
  104. return table;
  105. }
  106. /* (non-Javadoc)
  107.  * @see org.jdesktop.swing.binding.AbstractUIBinding#setBoundComponent(javax.swing.JComponent)
  108.  */
  109. protected void setBoundComponent(JComponent component) {
  110. if (!(component instanceof JTable)) {
  111. throw new IllegalArgumentException("TableBindings only accept a JTable or one of its child classes");
  112. }
  113. this.table = (JTable)component;
  114. }
  115. /* (non-Javadoc)
  116.  * @see org.jdesktop.swing.binding.AbstractUIBinding#getComponentValue()
  117.  */
  118. protected Object getComponentValue() {
  119. //a table component never updates its parent data model in this way
  120. return null;
  121. }
  122. /* (non-Javadoc)
  123.  * @see org.jdesktop.swing.binding.AbstractUIBinding#setComponentValue(java.lang.Object)
  124.  */
  125. protected void setComponentValue(Object value) {
  126. //means nothing to this binding
  127. }
  128.     /** 
  129.      * override super because we don't have MetaData.
  130.      * 
  131.      */
  132.     protected void installDataModel(DataModel dataModel, String fieldName) {
  133.         this.dataModel = dataModel;
  134.         this.fieldName = fieldName;
  135.         installDataModelListener();
  136.     }
  137. /* (non-Javadoc)
  138.  * @see org.jdesktop.jdnc.incubator.rbair.swing.binding.AbstractBinding#getComponent()
  139.  */
  140. public JComponent getComponent() {
  141. return getBoundComponent();
  142. }
  143. /* (non-Javadoc)
  144.  * @see org.jdesktop.jdnc.incubator.rbair.swing.binding.AbstractBinding#setComponent(javax.swing.JComponent)
  145.  */
  146. public void setComponent(JComponent component) {
  147. setBoundComponent(component);
  148. }
  149. }