Coverage Report - net.sf.echobinding.controls.CheckBox
 
Classes in this File Line Coverage Branch Coverage Complexity
CheckBox
100%
36/36
N/A
1,647
 
 1  
 /**
 2  
  * Copyright (C) 2006 Philipp Mpalampanis
 3  
  *
 4  
  * License: MPL 1.1/GPL 2.0/LGPL 2.1
 5  
  *
 6  
  * The contents of this file are subject to the Mozilla Public License Version
 7  
  * 1.1 (the "License"); you may not use this file except in compliance with
 8  
  * the License. You may obtain a copy of the License at
 9  
  * http://www.mozilla.org/MPL/
 10  
  *
 11  
  * Software distributed under the License is distributed on an "AS IS" basis,
 12  
  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 13  
  * for the specific language governing rights and limitations under the
 14  
  * License.
 15  
  *
 16  
  * Alternatively, the contents of this file may be used under the terms of
 17  
  * either the GNU General Public License Version 2 or later (the "GPL"), or
 18  
  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 19  
  * in which case the provisions of the GPL or the LGPL are applicable instead
 20  
  * of those above. If you wish to allow use of your version of this file only
 21  
  * under the terms of either the GPL or the LGPL, and not to allow others to
 22  
  * use your version of this file under the terms of the MPL, indicate your
 23  
  * decision by deleting the provisions above and replace them with the notice
 24  
  * and other provisions required by the GPL or the LGPL. If you do not delete
 25  
  * the provisions above, a recipient may use your version of this file under
 26  
  * the terms of any one of the MPL, the GPL or the LGPL.
 27  
  */
 28  
 
 29  
 package net.sf.echobinding.controls;
 30  
 
 31  
 import java.beans.PropertyChangeEvent;
 32  
 
 33  
 import net.sf.echobinding.BoundControl;
 34  
 import net.sf.echobinding.binding.BindingContext;
 35  
 import net.sf.echobinding.validation.DefaultValidationHandler;
 36  
 import net.sf.echobinding.validation.ValidationHandler;
 37  
 import nextapp.echo2.app.ImageReference;
 38  
 import nextapp.echo2.app.event.ActionEvent;
 39  
 import nextapp.echo2.app.event.ActionListener;
 40  
 
 41  
 /**
 42  
  * @author Philipp Mpalampanis 
 43  
  *
 44  
  */
 45  
 public class CheckBox extends nextapp.echo2.app.CheckBox implements
 46  
                 BoundControl, ActionListener {
 47  
 
 48  
         private static final long serialVersionUID = 192196546013312251L;
 49  
 
 50  
         /**
 51  
          * 
 52  
          */
 53  
         public CheckBox() {
 54  
                 super();
 55  
 
 56  
         }
 57  
 
 58  
         /**
 59  
          * @param text
 60  
          */
 61  
         public CheckBox(String text) {
 62  
                 super(text);
 63  
 
 64  
         }
 65  
 
 66  
         /**
 67  
          * @param icon
 68  
          */
 69  
         public CheckBox(ImageReference icon) {
 70  
                 super(icon);
 71  
 
 72  
         }
 73  
 
 74  
         /**
 75  
          * @param text
 76  
          * @param icon
 77  
          */
 78  
         public CheckBox(String text, ImageReference icon) {
 79  
                 super(text, icon);
 80  
 
 81  
         }
 82  
         
 83  
         private String _id;
 84  
         private BindingContext _ctx;
 85  
         private ValidationHandler _validationHandler;
 86  
         
 87  
         /**
 88  
          * Creates a data bound check box.
 89  
          * 
 90  
          * @param adapterId
 91  
          * @param context
 92  
          */
 93  5
         public CheckBox(String adapterId, BindingContext context ) {
 94  5
                 _id = adapterId;
 95  5
                 setBindingConext(context);
 96  5
                 addActionListener(this);
 97  5
                 setActionCommand( adapterId );
 98  5
         }
 99  
 
 100  
         /* (non-Javadoc)
 101  
          * @see echobinding.BoundControl#setBindingConext(echobinding.BindingContext)
 102  
          */
 103  
         public void setBindingConext(BindingContext context) {
 104  5
                 if(context == null)        return;
 105  5
                 if( _ctx != null) _ctx.removeControl(this);
 106  5
                 context.registerControl( _id, this);
 107  5
                 _ctx = context;
 108  5
                 update();
 109  5
         }
 110  
 
 111  
 
 112  
         /* (non-Javadoc)
 113  
          * @see echobinding.BoundControl#setBindingId(java.lang.String)
 114  
          */
 115  
         public void setAdapterId(String bindingId) {
 116  
                 _id = bindingId;
 117  
         }
 118  
 
 119  
         /* (non-Javadoc)
 120  
          * @see echobinding.BoundControl#loadValues()
 121  
          */
 122  
         public void update() {
 123  13
                 Boolean checked = (Boolean) _ctx.getValue(_id);
 124  13
                 setSelected(checked);        
 125  13
         }
 126  
 
 127  
         /* (non-Javadoc)
 128  
          * @see echobinding.BoundControl#saveValues()
 129  
          */
 130  
         public void save() {
 131  2
                 if(!isDirty())
 132  
                         return;
 133  2
                 validateInput();
 134  2
                 if(isValid())
 135  2
                         _ctx.setValue(_id, isSelected());
 136  2
         }
 137  
 
 138  
         /* (non-Javadoc)
 139  
          * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
 140  
          */
 141  
         public void propertyChange(PropertyChangeEvent event) {
 142  6
                 update();        // reload values
 143  6
         }
 144  
         
 145  
         /* (non-Javadoc)
 146  
          * @see echobinding.BoundControl#isValid()
 147  
          */
 148  
         public boolean isValid() {
 149  4
                 return _ctx.validate(_id, isSelected()).isValid();
 150  
         }
 151  
 
 152  
                 
 153  
         /* (non-Javadoc)
 154  
          * @see echobinding.BoundControl#validateInput()
 155  
          */
 156  
         public void validateInput() {
 157  2
                 initValidationHandler();
 158  
                 
 159  2
                 if(isValid())
 160  2
                         _validationHandler.markValid(this);
 161  
                 else
 162  
                         _validationHandler.markInvalid(this);
 163  2
         }
 164  
 
 165  
         private void initValidationHandler() {
 166  2
                 if( _validationHandler == null ) {
 167  1
                         _validationHandler = _ctx.getValidationHandler(_id);
 168  1
                         if( _validationHandler == null )
 169  1
                                 _validationHandler = new DefaultValidationHandler(this);
 170  
                 }
 171  2
         }
 172  
 
 173  
         /* (non-Javadoc)
 174  
          * @see echobinding.BoundControl#isDirty()
 175  
          */
 176  
         public boolean isDirty() {
 177  2
                 return _ctx.isDirty(_id, isSelected() );
 178  
         }
 179  
 
 180  
         /* (non-Javadoc)
 181  
          * @see echobinding.BoundControl#getValue()
 182  
          */
 183  
         public Object getValue() {
 184  
                 return new Boolean(isSelected());
 185  
         }
 186  
 
 187  
         /* (non-Javadoc)
 188  
          * @see nextapp.echo2.app.event.ActionListener#actionPerformed(nextapp.echo2.app.event.ActionEvent)
 189  
          */
 190  
         public void actionPerformed(ActionEvent arg0) {
 191  
                 firePropertyChange( ACTION_LISTENERS_CHANGED_PROPERTY, !isSelected(), isSelected() );
 192  
         }
 193  
 
 194  
         /* (non-Javadoc)
 195  
          * @see echobinding.BoundControl#setValue(java.lang.Object)
 196  
          */
 197  
         public void setValue(Object value) {
 198  3
                 if(value==null) return;
 199  2
                 setSelected( (Boolean) value );
 200  2
         }
 201  
         
 202  
 }