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