1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
package net.sf.beanform.binding; |
16 |
|
|
17 |
|
import java.util.Map; |
18 |
|
import java.util.Map.Entry; |
19 |
|
|
20 |
|
import org.apache.hivemind.Location; |
21 |
|
import org.apache.tapestry.IBinding; |
22 |
|
import org.apache.tapestry.IComponent; |
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
public class DualBinding implements IBinding { |
33 |
|
|
34 |
|
private IBinding custom; |
35 |
|
private IBinding standard; |
36 |
|
|
37 |
20 |
public DualBinding( IBinding custom, IBinding standard ) { |
38 |
20 |
this.custom = custom; |
39 |
20 |
this.standard = standard; |
40 |
20 |
} |
41 |
|
|
42 |
|
public void setCustom( IBinding custom ) { |
43 |
16 |
this.custom = custom; |
44 |
16 |
} |
45 |
|
|
46 |
|
public Object getObject() { |
47 |
12 |
if( this.custom != null ) return this.custom.getObject(); |
48 |
6 |
else if( this.standard != null ) return this.standard.getObject(); |
49 |
3 |
else return null; |
50 |
|
} |
51 |
|
|
52 |
|
public Object getObject( Class type ) { |
53 |
4 |
if( this.custom != null ) return this.custom.getObject( type ); |
54 |
2 |
else if( this.standard != null ) return this.standard.getObject( type ); |
55 |
1 |
else return null; |
56 |
|
} |
57 |
|
|
58 |
|
public boolean isInvariant() { |
59 |
4 |
return false; |
60 |
|
} |
61 |
|
|
62 |
|
public void setObject( Object value ) { |
63 |
4 |
if( this.custom != null ) this.custom.setObject( value ); |
64 |
2 |
else if( this.standard != null ) this.standard.setObject( value ); |
65 |
4 |
} |
66 |
|
|
67 |
|
public String getDescription() { |
68 |
4 |
if( this.custom != null ) return this.custom.getDescription(); |
69 |
2 |
else if( this.standard != null ) return this.standard.getDescription(); |
70 |
1 |
else return null; |
71 |
|
} |
72 |
|
|
73 |
|
public Location getLocation() { |
74 |
4 |
if( this.custom != null ) return this.custom.getLocation(); |
75 |
2 |
else if( this.standard != null ) return this.standard.getLocation(); |
76 |
1 |
else return null; |
77 |
|
} |
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
public static void addCustomBindings( IComponent component, Map<String, IBinding> bindings, boolean clearFirst ) { |
84 |
5 |
if( clearFirst ) { |
85 |
5 |
DualBinding.removeCustomBindings( component ); |
86 |
|
} |
87 |
5 |
for( Entry<String, IBinding> entry : bindings.entrySet() ) { |
88 |
6 |
String bindingName = entry.getKey(); |
89 |
6 |
IBinding custom = entry.getValue(); |
90 |
6 |
IBinding existing = component.getBinding( bindingName ); |
91 |
|
DualBinding dual; |
92 |
6 |
if( existing instanceof DualBinding ) { |
93 |
1 |
dual = (DualBinding) existing; |
94 |
1 |
dual.setCustom( custom ); |
95 |
1 |
} |
96 |
|
else { |
97 |
5 |
dual = new DualBinding( custom, existing ); |
98 |
|
} |
99 |
6 |
component.setBinding( bindingName, dual ); |
100 |
6 |
} |
101 |
5 |
} |
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
@SuppressWarnings( "unchecked" ) |
108 |
|
public static void removeCustomBindings( IComponent component ) { |
109 |
5 |
Map<String, IBinding> bindings = component.getBindings(); |
110 |
5 |
for( IBinding binding : bindings.values() ) { |
111 |
3 |
if( binding instanceof DualBinding ) { |
112 |
3 |
DualBinding dual = (DualBinding) binding; |
113 |
3 |
dual.setCustom( null ); |
114 |
|
} |
115 |
3 |
} |
116 |
5 |
} |
117 |
|
|
118 |
|
} |