001    // Copyright 2006-2007 Daniel Gredler
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package net.sf.beanform;
016    
017    import java.util.Iterator;
018    import java.util.Map;
019    
020    import net.sf.beanform.prop.BeanProperty;
021    
022    import org.apache.hivemind.ApplicationRuntimeException;
023    import org.apache.tapestry.IComponent;
024    import org.apache.tapestry.form.IFormComponent;
025    
026    /**
027     * A low level BeanForm component that renders labels for all the properties.
028     * This component must be wrapped by a {@link BeanFormRows} component.
029     *
030     * @author Daniel Gredler
031     */
032    public abstract class BeanFormLabel extends BeanFormRowComponent {
033    
034        final static String PROPERTYSELECTION_BLOCK_ID = "propertySelectionBlock";
035        final static String CUSTOM_BLOCK_ID = "customBlock";
036        final static String TEXTFIELD_BLOCK_ID = "textFieldBlock";
037        final static String TEXTAREA_BLOCK_ID = "textAreaBlock";
038        final static String CHECKBOX_BLOCK_ID = "checkboxBlock";
039        final static String DATEPICKER_BLOCK_ID = "datePickerBlock";
040        final static String UPLOAD_BLOCK_ID = "uploadBlock";
041        final static String INSERT_BLOCK_ID = "insertBlock";
042    
043        public IComponent getLabelBlock() {
044            BeanProperty property = this.getProperty();
045            if( this.hasPropertySelectionModel( property, true ) ) return this.getComponent( PROPERTYSELECTION_BLOCK_ID );
046            else if( this.hasCustomField( property ) ) return this.getComponent( CUSTOM_BLOCK_ID );
047            else if( property.usesTextField() ) return this.getComponent( TEXTFIELD_BLOCK_ID );
048            else if( property.usesTextArea() ) return this.getComponent( TEXTAREA_BLOCK_ID );
049            else if( property.usesCheckbox() ) return this.getComponent( CHECKBOX_BLOCK_ID );
050            else if( property.usesDatePicker() ) return this.getComponent( DATEPICKER_BLOCK_ID );
051            else if( property.usesUpload() ) return this.getComponent(  UPLOAD_BLOCK_ID );
052            else if( property.usesInsert() ) return this.getComponent( INSERT_BLOCK_ID );
053            else {
054                String msg = BeanFormMessages.cantFindFieldForProperty( property );
055                throw new ApplicationRuntimeException( msg );
056            }
057        }
058    
059        public IFormComponent getPropertySelection() {
060            return this.getField( BeanFormField.PROPERTYSELECTION_FIELD_ID );
061        }
062    
063        public IFormComponent getTextField() {
064            return this.getField( BeanFormField.TEXTFIELD_FIELD_ID );
065        }
066    
067        public IFormComponent getTextArea() {
068            return this.getField( BeanFormField.TEXTAREA_FIELD_ID );
069        }
070    
071        public IFormComponent getCheckbox() {
072            return this.getField( BeanFormField.CHECKBOX_FIELD_ID );
073        }
074    
075        public IFormComponent getDatePicker() {
076            return this.getField( BeanFormField.DATEPICKER_FIELD_ID );
077        }
078    
079        public IFormComponent getUpload() {
080            return this.getField( BeanFormField.UPLOAD_FIELD_ID );
081        }
082    
083        /**
084         * Called whenever a label needs its field. This method expects the label's "prerender"
085         * attribute to be set to <tt>true</tt> (the default), as it adds any extra user-specified
086         * informal bindings to the field before returning it, in anticipation of a prerender.
087         */
088        private IFormComponent getField( String id ) {
089            IFormComponent field = null;
090            Map siblings = this.getContainer().getComponents();
091            for( Iterator i = siblings.values().iterator(); i.hasNext(); ) {
092                IComponent sibling = (IComponent) i.next();
093                if( sibling instanceof BeanFormField ) {
094                    field = (IFormComponent) sibling.getComponent( id );
095                    this.addExtraBindings( field );
096                    break;
097                }
098            }
099            return field;
100        }
101    
102    }