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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: BooleanBinding.java,v 1.2 2005/10/10 17:00:54 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.awt.event.ItemEvent;
  23. import java.awt.event.ItemListener;
  24. import javax.swing.ButtonModel;
  25. import javax.swing.JComponent;
  26. import javax.swing.JToggleButton;
  27. import org.jdesktop.binding.DataModel;
  28. /**
  29.  * Class which binds a component that supports setting a boolean
  30.  * value (JCheckBox) to a data model field which is type Boolean.
  31.  * Although this binding is most commonly used for checkboxes, it may
  32.  * be used with any component that defines a ButtonModel to represent
  33.  * its selected state.
  34.  *
  35.  * @author Amy Fowler
  36.  * @version 1.0
  37.  */
  38. public class BooleanBinding extends AbstractBinding {
  39.     private JComponent component;
  40.     private ButtonModel buttonModel;
  41.     public BooleanBinding(JToggleButton toggleButton,
  42.                            DataModel dataModel, String fieldName) {
  43.         // checkboxes don't need to be validated because they cannot have a null
  44.         // value and type conversion should not be necessary.
  45.         super(toggleButton, dataModel, fieldName, AbstractBinding.AUTO_VALIDATE_NONE);
  46.         initModel(toggleButton.getModel());
  47.     }
  48.     public BooleanBinding(JComponent component, ButtonModel buttonModel,
  49.                            DataModel dataModel, String fieldName) {
  50.         super(component, dataModel, fieldName, AbstractBinding.AUTO_VALIDATE_NONE);
  51.         initModel(buttonModel);
  52.     }
  53.     public JComponent getComponent() {
  54.         return component;
  55.     }
  56.     protected void setComponent(JComponent component) {
  57.         this.component = component;
  58.     }
  59.     protected Object getComponentValue(){
  60.         return Boolean.valueOf(buttonModel.isSelected());
  61.     }
  62.     protected void setComponentValue(Object value) {
  63.         if (value != null) {
  64.             buttonModel.setSelected( ( (Boolean) value).booleanValue());
  65.         } else {
  66.             buttonModel.setSelected(false);
  67.         }
  68.     }
  69.     private void initModel(ButtonModel model) {
  70.         buttonModel = model;
  71.         buttonModel.addItemListener(new ItemListener() {
  72.             public void itemStateChanged(ItemEvent e) {
  73.                 if (!pulling) {
  74.                     setModified(true);
  75.                 }
  76.             }
  77.         });
  78.     }
  79. }