Classes in this File | Line Coverage | Branch Coverage | Complexity | |||||||
ClientIdBinding |
|
| 1.75;1.75 |
1 | // Copyright 2006 The Apache Software Foundation |
|
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 org.apache.tapestry.binding; |
|
16 | ||
17 | import org.apache.hivemind.ApplicationRuntimeException; |
|
18 | import org.apache.hivemind.Location; |
|
19 | import org.apache.hivemind.util.Defense; |
|
20 | import org.apache.tapestry.IBinding; |
|
21 | import org.apache.tapestry.IComponent; |
|
22 | import org.apache.tapestry.coerce.ValueConverter; |
|
23 | ||
24 | /** |
|
25 | * An implementation of Tapestry {@link IBinding} that simplifies |
|
26 | * access to a component's clientId.<br/> |
|
27 | * You can use this binding in order to safely find out the |
|
28 | * client-side id that was (or will be) generated for a given |
|
29 | * component. |
|
30 | * |
|
31 | * <p/> |
|
32 | * Here's an example of how to get the client id of a Tapestry component: |
|
33 | * <code> |
|
34 | * <span jwcid="@Any" dojoType="myDojoComponent" contentId="clientId:content" /> |
|
35 | * </code> |
|
36 | * <p/> |
|
37 | * The previous code can also be written using the ognl binding: |
|
38 | * <code> |
|
39 | * <span jwcid="@Any" dojoType="myDojoComponent" contentId="ognl:components.content.clientId" /> |
|
40 | * </code> |
|
41 | * but the clientId binding is more compact and performs much faster. |
|
42 | */ |
|
43 | public class ClientIdBinding extends AbstractBinding |
|
44 | { |
|
45 | private final IComponent _component; |
|
46 | ||
47 | private final String _componentId; |
|
48 | ||
49 | public ClientIdBinding(String description, ValueConverter valueConverter, Location location, |
|
50 | IComponent component, String componentId) |
|
51 | { |
|
52 | 2 | super(description, valueConverter, location); |
53 | ||
54 | 2 | Defense.notNull(component, "component"); |
55 | 2 | Defense.notNull(componentId, "componentId"); |
56 | ||
57 | 2 | _component = component; |
58 | 2 | _componentId = componentId; |
59 | 2 | } |
60 | ||
61 | public Object getObject() |
|
62 | { |
|
63 | try |
|
64 | { |
|
65 | 2 | return _component.getComponent(_componentId).getClientId(); |
66 | } |
|
67 | 1 | catch (Exception ex) |
68 | { |
|
69 | 1 | throw new ApplicationRuntimeException(ex.getMessage(), getLocation(), ex); |
70 | } |
|
71 | } |
|
72 | ||
73 | public Object getComponent() |
|
74 | { |
|
75 | 1 | return _component; |
76 | } |
|
77 | ||
78 | public boolean isInvariant() |
|
79 | { |
|
80 | // clientId can change even for the same component, so ... |
|
81 | 0 | return false; |
82 | } |
|
83 | ||
84 | } |