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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: SpinnerBinding.java,v 1.2 2005/10/10 17:00:53 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.JSpinner;
  26. import javax.swing.SpinnerModel;
  27. import javax.swing.event.ChangeEvent;
  28. import javax.swing.event.ChangeListener;
  29. import org.jdesktop.binding.DataModel;
  30. /**
  31.  * Class which binds a component that supports setting a value within
  32.  * a sequence of values (JSpinner) to a field in a data model.
  33.  * Although this binding is most commonly used for spinners, it may
  34.  * be used with any component that defines a SpinnerModel to represent
  35.  * its current value.
  36.  * @author Amy Fowler
  37.  * @version 1.0
  38.  */
  39. public class SpinnerBinding extends AbstractBinding {
  40.     private JComponent component;
  41.     private SpinnerModel spinnerModel;
  42.     public SpinnerBinding(JSpinner spinner,
  43.                             DataModel dataModel, String fieldName) {
  44.         super(spinner, dataModel, fieldName, AbstractBinding.AUTO_VALIDATE);
  45.         initModel(spinner.getModel());
  46.     }
  47.     public SpinnerBinding(JSpinner spinner,
  48.                             DataModel dataModel, String fieldName,
  49.                            int validationPolicy) {
  50.         super(spinner, dataModel, fieldName, validationPolicy);
  51.         initModel(spinner.getModel());
  52.     }
  53.     public SpinnerBinding(JComponent component, SpinnerModel spinnerModel,
  54.                           DataModel dataModel, String fieldName,
  55.                           int validationPolicy) {
  56.         super(component, dataModel, fieldName, validationPolicy);
  57.         initModel(spinnerModel);
  58.     }
  59.     public JComponent getComponent() {
  60.         return component;
  61.     }
  62.     protected void setComponent(JComponent component) {
  63.         this.component = component;
  64.         configureEditability();
  65.     }
  66.     protected void configureEditability() {
  67.         if (!(component instanceof JSpinner)) return;
  68.         JSpinner spinnerComponent = (JSpinner) component;
  69.         spinnerComponent.setEnabled(!metaData.isReadOnly());
  70.     }
  71.     
  72.     protected void installMetaDataListener() {
  73.         PropertyChangeListener l = new PropertyChangeListener() {
  74.             public void propertyChange(PropertyChangeEvent evt) {
  75.                 if ("readOnly".equals(evt.getPropertyName())) {
  76.                     configureEditability();
  77.                 }
  78.             }
  79.         };
  80.         metaData.addPropertyChangeListener(l); 
  81.     }
  82.     
  83.     protected Object getComponentValue(){
  84.         return spinnerModel.getValue();
  85.     }
  86.     protected void setComponentValue(Object value) {
  87.         if (value != null) {
  88.             spinnerModel.setValue(value);
  89.         }
  90.     }
  91.     private void initModel(SpinnerModel spinnerModel) {
  92.         this.spinnerModel = spinnerModel;
  93.         spinnerModel.addChangeListener(new ChangeListener() {
  94.             public void stateChanged(ChangeEvent e) {
  95.                 if (!pulling) {
  96.                     setModified(true);
  97.                 }
  98.             }
  99.         });
  100.     }
  101. }