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.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  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          IRequestCycle cycle = this.getPage().getRequestCycle();
52          BeanForm beanForm = (BeanForm) cycle.getAttribute( BeanForm.BEAN_FORM_ATTRIBUTE );
53          if( beanForm == null ) throw new ApplicationRuntimeException( BeanFormMessages.noBeanForm( this ) );
54          return beanForm;
55      }
56  
57      protected boolean hasCustomField( BeanProperty property ) {
58          return ( this.getCustomFieldBlock( property ) != null &&
59                   this.getCustomField( property ) != null );
60      }
61  
62      protected Block getCustomFieldBlock( BeanProperty property ) {
63          String name = this.getCustomFieldBlockName( property );
64          return this.getSiblingComponent( name, Block.class );
65      }
66  
67      protected String getCustomFieldBlockName( BeanProperty property ) {
68          return this.getBeanForm().getId() + "_" + property.getName() + CUSTOM_FIELD_BLOCK_SUFFIX;
69      }
70  
71      protected IFormComponent getCustomField( BeanProperty property ) {
72          String name = this.getCustomFieldName( property );
73          return this.getSiblingComponent( name, IFormComponent.class );
74      }
75  
76      protected String getCustomFieldName( BeanProperty property ) {
77          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          IComponent component = null;
83          IComponent container = this.getBeanForm().getContainer();
84          Map components = container.getComponents();
85          if( components.containsKey( id ) ) {
86              IComponent candidate = (IComponent) components.get( id );
87              if( clazz.isAssignableFrom( candidate.getClass() ) ) {
88                  component = candidate;
89              }
90              else {
91                  String msg = BeanFormMessages.componentUnexpectedType( candidate, clazz );
92                  throw new ApplicationRuntimeException( msg );
93              }
94          }
95          return (T) component;
96      }
97  
98      protected boolean hasPropertySelectionModel( BeanProperty prop, boolean useCache ) {
99          return this.getPropertySelectionModel( prop, useCache ) != null;
100     }
101 
102     protected IPropertySelectionModel getPropertySelectionModel( BeanProperty prop, boolean useCache ) {
103 
104         IBinding binding;
105         if( useCache ) binding = this.getBeanForm().getFieldBindingsFor( prop ).get( MODEL );
106         else binding = this.getBeanForm().getBinding( prop.getName() + MODEL_SUFFIX );
107 
108         IPropertySelectionModel model = null;
109         if( binding != null ) {
110             Object value = binding.getObject();
111             if( value instanceof IPropertySelectionModel ) {
112                 model = (IPropertySelectionModel) value;
113             }
114         }
115 
116         return model;
117     }
118 
119 }