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.List;
018    
019    import net.sf.beanform.prop.BeanProperty;
020    import net.sf.beanform.validator.CachingValidatorFactory;
021    
022    import org.apache.hivemind.ApplicationRuntimeException;
023    import org.apache.tapestry.IComponent;
024    import org.apache.tapestry.form.IPropertySelectionModel;
025    import org.apache.tapestry.form.validator.Validator;
026    
027    /**
028     * A low level BeanForm component that renders fields for all the properties.
029     * This component must be wrapped by a {@link BeanFormRows} component.
030     *
031     * @author Daniel Gredler
032     */
033    public abstract class BeanFormField extends BeanFormRowComponent {
034    
035        public final static String PROPERTYSELECTION_FIELD_ID = "propertySelection";
036        public final static String TEXTFIELD_FIELD_ID = "textField";
037        public final static String TEXTAREA_FIELD_ID = "textArea";
038        public final static String CHECKBOX_FIELD_ID = "checkbox";
039        public final static String DATEPICKER_FIELD_ID = "datePicker";
040        public final static String UPLOAD_FIELD_ID = "upload";
041        public final static String INSERT_FIELD_ID = "insert";
042    
043        final static String PROPERTYSELECTION_BLOCK_ID = "propertySelectionBlock";
044        final static String CUSTOM_BLOCK_ID = "customBlock";
045        final static String TEXTFIELD_BLOCK_ID = "textFieldBlock";
046        final static String TEXTAREA_BLOCK_ID = "textAreaBlock";
047        final static String CHECKBOX_BLOCK_ID = "checkboxBlock";
048        final static String DATEPICKER_BLOCK_ID = "datePickerBlock";
049        final static String UPLOAD_BLOCK_ID = "uploadBlock";
050        final static String INSERT_BLOCK_ID = "insertBlock";
051    
052        public abstract CachingValidatorFactory getValidatorFactory();
053    
054        public IComponent getFieldBlock() {
055            BeanProperty property = this.getProperty();
056            if( this.hasPropertySelectionModel( property, true ) ) return this.getComponent( PROPERTYSELECTION_BLOCK_ID );
057            else if( this.hasCustomField( property ) ) return this.getComponent( CUSTOM_BLOCK_ID );
058            else if( property.usesTextField() ) return this.getComponent( TEXTFIELD_BLOCK_ID );
059            else if( property.usesTextArea() ) return this.getComponent( TEXTAREA_BLOCK_ID );
060            else if( property.usesCheckbox() ) return this.getComponent( CHECKBOX_BLOCK_ID );
061            else if( property.usesDatePicker() ) return this.getComponent( DATEPICKER_BLOCK_ID );
062            else if( property.usesUpload() ) return this.getComponent( UPLOAD_BLOCK_ID );
063            else if( property.usesInsert() ) return this.getComponent( INSERT_BLOCK_ID );
064            else {
065                String msg = BeanFormMessages.cantFindFieldForProperty( property );
066                throw new ApplicationRuntimeException( msg );
067            }
068        }
069    
070        public String getFieldId() {
071            String id = this.getProperty().getName();
072            id = id.replace( '.', '_' );
073            return id;
074        }
075    
076        public Object getValue() {
077            BeanProperty property = this.getProperty();
078            if( property.isReadable() == false ) return null;
079            property.setBean( this.getBean() );
080            return property.getValue();
081        }
082    
083        public void setValue( Object value ) {
084            BeanProperty property = this.getProperty();
085            property.setBean( this.getBean() );
086            property.setValue( value );
087        }
088    
089        public boolean getDisabled() {
090            return ( this.getProperty().isWriteable() == false );
091        }
092    
093        public List<Validator> getValidators() {
094            BeanProperty property = this.getProperty();
095            CachingValidatorFactory factory = this.getValidatorFactory();
096            List<Validator> validators = factory.constructValidatorList( this, property );
097            return validators;
098        }
099    
100        public IPropertySelectionModel getPropertySelectionModel() {
101            BeanProperty property = this.getProperty();
102            return this.getPropertySelectionModel( property, true );
103        }
104    
105    }