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.Iterator;
18  import java.util.Map;
19  
20  import net.sf.beanform.prop.BeanProperty;
21  
22  import org.apache.hivemind.ApplicationRuntimeException;
23  import org.apache.tapestry.IComponent;
24  import org.apache.tapestry.form.IFormComponent;
25  
26  /***
27   * A low level BeanForm component that renders labels for all the properties.
28   * This component must be wrapped by a {@link BeanFormRows} component.
29   *
30   * @author Daniel Gredler
31   */
32  public abstract class BeanFormLabel extends BeanFormRowComponent {
33  
34      final static String PROPERTYSELECTION_BLOCK_ID = "propertySelectionBlock";
35      final static String CUSTOM_BLOCK_ID = "customBlock";
36      final static String TEXTFIELD_BLOCK_ID = "textFieldBlock";
37      final static String TEXTAREA_BLOCK_ID = "textAreaBlock";
38      final static String CHECKBOX_BLOCK_ID = "checkboxBlock";
39      final static String DATEPICKER_BLOCK_ID = "datePickerBlock";
40      final static String UPLOAD_BLOCK_ID = "uploadBlock";
41      final static String INSERT_BLOCK_ID = "insertBlock";
42  
43      public IComponent getLabelBlock() {
44          BeanProperty property = this.getProperty();
45          if( this.hasPropertySelectionModel( property, true ) ) return this.getComponent( PROPERTYSELECTION_BLOCK_ID );
46          else if( this.hasCustomField( property ) ) return this.getComponent( CUSTOM_BLOCK_ID );
47          else if( property.usesTextField() ) return this.getComponent( TEXTFIELD_BLOCK_ID );
48          else if( property.usesTextArea() ) return this.getComponent( TEXTAREA_BLOCK_ID );
49          else if( property.usesCheckbox() ) return this.getComponent( CHECKBOX_BLOCK_ID );
50          else if( property.usesDatePicker() ) return this.getComponent( DATEPICKER_BLOCK_ID );
51          else if( property.usesUpload() ) return this.getComponent(  UPLOAD_BLOCK_ID );
52          else if( property.usesInsert() ) return this.getComponent( INSERT_BLOCK_ID );
53          else {
54              String msg = BeanFormMessages.cantFindFieldForProperty( property );
55              throw new ApplicationRuntimeException( msg );
56          }
57      }
58  
59      public IFormComponent getPropertySelection() {
60          return this.getField( BeanFormField.PROPERTYSELECTION_FIELD_ID );
61      }
62  
63      public IFormComponent getTextField() {
64          return this.getField( BeanFormField.TEXTFIELD_FIELD_ID );
65      }
66  
67      public IFormComponent getTextArea() {
68          return this.getField( BeanFormField.TEXTAREA_FIELD_ID );
69      }
70  
71      public IFormComponent getCheckbox() {
72          return this.getField( BeanFormField.CHECKBOX_FIELD_ID );
73      }
74  
75      public IFormComponent getDatePicker() {
76          return this.getField( BeanFormField.DATEPICKER_FIELD_ID );
77      }
78  
79      public IFormComponent getUpload() {
80          return this.getField( BeanFormField.UPLOAD_FIELD_ID );
81      }
82  
83      /***
84       * Called whenever a label needs its field. This method expects the label's "prerender"
85       * attribute to be set to <tt>true</tt> (the default), as it adds any extra user-specified
86       * informal bindings to the field before returning it, in anticipation of a prerender.
87       */
88      private IFormComponent getField( String id ) {
89          IFormComponent field = null;
90          Map siblings = this.getContainer().getComponents();
91          for( Iterator i = siblings.values().iterator(); i.hasNext(); ) {
92              IComponent sibling = (IComponent) i.next();
93              if( sibling instanceof BeanFormField ) {
94                  field = (IFormComponent) sibling.getComponent( id );
95                  this.addExtraBindings( field );
96                  break;
97              }
98          }
99          return field;
100     }
101 
102 }