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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: ListBinding.java,v 1.2 2005/10/10 17:00:50 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 java.util.ArrayList;
  25. import java.util.List;
  26. import javax.swing.DefaultListModel;
  27. import javax.swing.JComponent;
  28. import javax.swing.JList;
  29. import javax.swing.ListModel;
  30. import org.jdesktop.binding.DataConstants;
  31. import org.jdesktop.binding.DataModel;
  32. import org.jdesktop.binding.SelectionModel;
  33. import org.jdesktop.binding.TabularDataModel;
  34. import org.jdesktop.binding.metadata.ListMetaData;
  35. import org.jdesktop.binding.swingx.adapter.DataModelToListModelAdapter;
  36. import org.jdesktop.binding.swingx.adapter.ListSelectionBinding;
  37. public class ListBinding extends AbstractBinding {
  38.     private JList list;
  39.     public ListBinding(JList list, DataModel model, String fieldName) {
  40.         super(list, model, fieldName, AbstractBinding.AUTO_VALIDATE_NONE);
  41.     }
  42.     public boolean isModified() {
  43.         return false;
  44.     }
  45.     public boolean isValid() {
  46.         return true;
  47.     }
  48.     public JComponent getComponent() {
  49.         return list;
  50.     }
  51.     protected void setComponent(JComponent component) {
  52.         list = (JList) component;
  53.         configureSelection();
  54.     }
  55.     private void configureSelection() {
  56.         Object selectionMeta = metaData
  57.                 .getCustomProperty(DataConstants.SELECTION_MODEL);
  58.         if (selectionMeta instanceof SelectionModel) {
  59.             new ListSelectionBinding((SelectionModel) selectionMeta, list
  60.                     .getSelectionModel());
  61.         }
  62.     }
  63.     protected void installMetaDataListener() {
  64.         PropertyChangeListener l = new PropertyChangeListener() {
  65.             public void propertyChange(PropertyChangeEvent evt) {
  66.                 if (DataConstants.SELECTION_MODEL.equals(evt.getPropertyName())) {
  67.                     configureSelection();
  68.                 }
  69.             }
  70.         };
  71.         metaData.addPropertyChangeListener(l);
  72.     }
  73.     protected Object getComponentValue() {
  74.         ListModel model = list.getModel();
  75.         Class klazz = metaData.getElementClass();
  76.         if (klazz.equals(List.class)) {
  77.             List lvalue = new ArrayList();
  78.             for (int i = 0, size = model.getSize(); i < size; i++) {
  79.                 lvalue.add(model.getElementAt(i));
  80.             }
  81.             return lvalue;
  82.         } else if (klazz.isArray()) {
  83.             // XXX we lose the array type, use a generic Object[]
  84.             int size = model.getSize();
  85.             Object[] values = new Object[size];
  86.             for (int i = 0; i < size; i++) {
  87.                 values[i] = model.getElementAt(i);
  88.             }
  89.             return values;
  90.         }
  91.         return null;
  92.     }
  93.     protected void setComponentValue(Object value) {
  94.         if (metaData instanceof ListMetaData) {
  95.             setComponentValueFromTabularDataModel((TabularDataModel) value);
  96.             return;
  97.         }
  98.         Class klazz = metaData.getElementClass();
  99.         if (klazz.equals(List.class)) {
  100.             List lvalue = (List) value;
  101.             if (lvalue != null) {
  102.                 list.setListData(lvalue.toArray());
  103.             }
  104.         } else if (klazz.isArray()) {
  105.             Object[] arrayValue = (Object[]) value;
  106.             if (arrayValue != null) {
  107.                 list.setListData(arrayValue);
  108.             } else {
  109.                 // Empty the list.
  110.                 list.setModel(new DefaultListModel());
  111.             }
  112.         }
  113.     }
  114.     private void setComponentValueFromTabularDataModel(TabularDataModel model) {
  115.         //create a custom ListModel bound to the entire TabularDataModel
  116.         ListMetaData listMeta = (ListMetaData) metaData;
  117.         list.setModel(new DataModelToListModelAdapter(model, listMeta.getRendererFieldName()));
  118.         
  119.         
  120.     }
  121. }