Coverage Report - net.sf.beanform.BeanFormComponent
 
Classes in this File Line Coverage Branch Coverage Complexity
BeanFormComponent
100% 
100% 
0
 
 1  
 // Copyright 2006 Daniel Gredler
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package net.sf.beanform;
 16  
 
 17  
 import java.util.Map;
 18  
 
 19  
 import net.sf.beanform.prop.BeanProperty;
 20  
 
 21  
 import org.apache.hivemind.ApplicationRuntimeException;
 22  
 import org.apache.tapestry.BaseComponent;
 23  
 import org.apache.tapestry.IBinding;
 24  
 import org.apache.tapestry.IComponent;
 25  
 import org.apache.tapestry.IRequestCycle;
 26  
 import org.apache.tapestry.components.Block;
 27  
 import org.apache.tapestry.form.IFormComponent;
 28  
 import org.apache.tapestry.form.IPropertySelectionModel;
 29  
 
 30  
 /**
 31  
  * Superclass for low level BeanForm components which must be wrapped by
 32  
  * a {@link BeanForm} component.
 33  
  *
 34  
  * @see BeanFormRows
 35  
  * @see BeanFormLabel
 36  
  * @see BeanFormField
 37  
  * @see BeanFormButtons
 38  
  *
 39  
  * @author Daniel Gredler
 40  
  */
 41  109
 public abstract class BeanFormComponent extends BaseComponent {
 42  
 
 43  
     final static String CUSTOM_FIELD_BLOCK_SUFFIX = "_BeanFieldBlock";
 44  
     final static String CUSTOM_FIELD_SUFFIX = "_BeanField";
 45  
 
 46  
     final static String BINDING_OVERRIDE_SEPARATOR = "_";
 47  
     final static String MODEL = "model";
 48  
     final static String MODEL_SUFFIX = BINDING_OVERRIDE_SEPARATOR + MODEL;
 49  
 
 50  
     protected BeanForm getBeanForm() {
 51  19
         IRequestCycle cycle = this.getPage().getRequestCycle();
 52  19
         BeanForm beanForm = (BeanForm) cycle.getAttribute( BeanForm.BEAN_FORM_ATTRIBUTE );
 53  19
         if( beanForm == null ) throw new ApplicationRuntimeException( BeanFormMessages.noBeanForm( this ) );
 54  19
         return beanForm;
 55  
     }
 56  
 
 57  
     protected boolean hasCustomField( BeanProperty property ) {
 58  12
         return ( this.getCustomFieldBlock( property ) != null &&
 59  
                  this.getCustomField( property ) != null );
 60  
     }
 61  
 
 62  
     protected Block getCustomFieldBlock( BeanProperty property ) {
 63  14
         String name = this.getCustomFieldBlockName( property );
 64  14
         return this.getSiblingComponent( name, Block.class );
 65  
     }
 66  
 
 67  
     protected String getCustomFieldBlockName( BeanProperty property ) {
 68  15
         return this.getBeanForm().getId() + "_" + property.getName() + CUSTOM_FIELD_BLOCK_SUFFIX;
 69  
     }
 70  
 
 71  
     protected IFormComponent getCustomField( BeanProperty property ) {
 72  4
         String name = this.getCustomFieldName( property );
 73  4
         return this.getSiblingComponent( name, IFormComponent.class );
 74  
     }
 75  
 
 76  
     protected String getCustomFieldName( BeanProperty property ) {
 77  5
         return this.getBeanForm().getId() + "_" + property.getName() + CUSTOM_FIELD_SUFFIX;
 78  
     }
 79  
 
 80  
     @SuppressWarnings( "unchecked" )
 81  
     protected <T extends IComponent> T getSiblingComponent( String id, Class<T> clazz ) {
 82  20
         IComponent component = null;
 83  20
         IComponent container = this.getBeanForm().getContainer();
 84  20
         Map components = container.getComponents();
 85  20
         if( components.containsKey( id ) ) {
 86  9
             IComponent candidate = (IComponent) components.get( id );
 87  9
             if( clazz.isAssignableFrom( candidate.getClass() ) ) {
 88  7
                 component = candidate;
 89  7
             }
 90  
             else {
 91  2
                 String msg = BeanFormMessages.componentUnexpectedType( candidate, clazz );
 92  2
                 throw new ApplicationRuntimeException( msg );
 93  
             }
 94  
         }
 95  18
         return (T) component;
 96  
     }
 97  
 
 98  
     protected boolean hasPropertySelectionModel( BeanProperty prop, boolean useCache ) {
 99  9
         return this.getPropertySelectionModel( prop, useCache ) != null;
 100  
     }
 101  
 
 102  
     protected IPropertySelectionModel getPropertySelectionModel( BeanProperty prop, boolean useCache ) {
 103  
 
 104  
         IBinding binding;
 105  12
         if( useCache ) binding = this.getBeanForm().getFieldBindingsFor( prop ).get( MODEL );
 106  9
         else binding = this.getBeanForm().getBinding( prop.getName() + MODEL_SUFFIX );
 107  
 
 108  12
         IPropertySelectionModel model = null;
 109  12
         if( binding != null ) {
 110  2
             Object value = binding.getObject();
 111  2
             if( value instanceof IPropertySelectionModel ) {
 112  1
                 model = (IPropertySelectionModel) value;
 113  
             }
 114  
         }
 115  
 
 116  12
         return model;
 117  
     }
 118  
 
 119  
 }