View Javadoc

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.List;
18  
19  import net.sf.beanform.prop.BeanProperty;
20  import net.sf.beanform.validator.CachingValidatorFactory;
21  
22  import org.apache.hivemind.ApplicationRuntimeException;
23  import org.apache.tapestry.IComponent;
24  import org.apache.tapestry.form.IPropertySelectionModel;
25  import org.apache.tapestry.form.validator.Validator;
26  
27  /***
28   * A low level BeanForm component that renders fields for all the properties.
29   * This component must be wrapped by a {@link BeanFormRows} component.
30   *
31   * @author Daniel Gredler
32   */
33  public abstract class BeanFormField extends BeanFormRowComponent {
34  
35      public final static String PROPERTYSELECTION_FIELD_ID = "propertySelection";
36      public final static String TEXTFIELD_FIELD_ID = "textField";
37      public final static String TEXTAREA_FIELD_ID = "textArea";
38      public final static String CHECKBOX_FIELD_ID = "checkbox";
39      public final static String DATEPICKER_FIELD_ID = "datePicker";
40      public final static String UPLOAD_FIELD_ID = "upload";
41      public final static String INSERT_FIELD_ID = "insert";
42  
43      final static String PROPERTYSELECTION_BLOCK_ID = "propertySelectionBlock";
44      final static String CUSTOM_BLOCK_ID = "customBlock";
45      final static String TEXTFIELD_BLOCK_ID = "textFieldBlock";
46      final static String TEXTAREA_BLOCK_ID = "textAreaBlock";
47      final static String CHECKBOX_BLOCK_ID = "checkboxBlock";
48      final static String DATEPICKER_BLOCK_ID = "datePickerBlock";
49      final static String UPLOAD_BLOCK_ID = "uploadBlock";
50      final static String INSERT_BLOCK_ID = "insertBlock";
51  
52      public abstract CachingValidatorFactory getValidatorFactory();
53  
54      public IComponent getFieldBlock() {
55          BeanProperty property = this.getProperty();
56          if( this.hasPropertySelectionModel( property, true ) ) return this.getComponent( PROPERTYSELECTION_BLOCK_ID );
57          else if( this.hasCustomField( property ) ) return this.getComponent( CUSTOM_BLOCK_ID );
58          else if( property.usesTextField() ) return this.getComponent( TEXTFIELD_BLOCK_ID );
59          else if( property.usesTextArea() ) return this.getComponent( TEXTAREA_BLOCK_ID );
60          else if( property.usesCheckbox() ) return this.getComponent( CHECKBOX_BLOCK_ID );
61          else if( property.usesDatePicker() ) return this.getComponent( DATEPICKER_BLOCK_ID );
62          else if( property.usesUpload() ) return this.getComponent( UPLOAD_BLOCK_ID );
63          else if( property.usesInsert() ) return this.getComponent( INSERT_BLOCK_ID );
64          else {
65              String msg = BeanFormMessages.cantFindFieldForProperty( property );
66              throw new ApplicationRuntimeException( msg );
67          }
68      }
69  
70      public String getFieldId() {
71          String id = this.getProperty().getName();
72          id = id.replace( '.', '_' );
73          return id;
74      }
75  
76      public Object getValue() {
77          BeanProperty property = this.getProperty();
78          if( property.isReadable() == false ) return null;
79          property.setBean( this.getBean() );
80          return property.getValue();
81      }
82  
83      public void setValue( Object value ) {
84          BeanProperty property = this.getProperty();
85          property.setBean( this.getBean() );
86          property.setValue( value );
87      }
88  
89      public boolean getDisabled() {
90          return ( this.getProperty().isWriteable() == false );
91      }
92  
93      public List<Validator> getValidators() {
94          BeanProperty property = this.getProperty();
95          CachingValidatorFactory factory = this.getValidatorFactory();
96          List<Validator> validators = factory.constructValidatorList( this, property );
97          return validators;
98      }
99  
100     public IPropertySelectionModel getPropertySelectionModel() {
101         BeanProperty property = this.getProperty();
102         return this.getPropertySelectionModel( property, true );
103     }
104 
105 }