1
2
3
4
5
6
7
8
9
10
11
12
13
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 }