Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ComponentAddress |
|
| 2.125;2.125 |
1 | // Copyright 2004, 2005 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.util; | |
16 | ||
17 | import java.io.Serializable; | |
18 | ||
19 | import org.apache.hivemind.util.Defense; | |
20 | import org.apache.tapestry.IComponent; | |
21 | import org.apache.tapestry.INamespace; | |
22 | import org.apache.tapestry.IPage; | |
23 | import org.apache.tapestry.IRequestCycle; | |
24 | ||
25 | /** | |
26 | * The ComponentAddress class contains the path to a component, allowing it to locate an instance of | |
27 | * that component in a different {@link org.apache.tapestry.IRequestCycle}. | |
28 | * <p> | |
29 | * This class needs to be used mostly when working with components accessed via the | |
30 | * {@link org.apache.tapestry.IRender}interface. It allows those components to serialize and pass | |
31 | * as a service parameter information about what component they have to talk to if control returns | |
32 | * back to them. | |
33 | * <p> | |
34 | * This situation often occurs when the component used via IRender contains Direct or Action links. | |
35 | * | |
36 | * @author mindbridge | |
37 | * @since 2.2 | |
38 | */ | |
39 | public class ComponentAddress implements Serializable | |
40 | { | |
41 | private static final long serialVersionUID = 533068199722072804L; | |
42 | ||
43 | private String _pageName; | |
44 | ||
45 | private String _idPath; | |
46 | ||
47 | /** | |
48 | * Creates a new ComponentAddress object that carries the identification information of the | |
49 | * given component (the page name and the ID path). | |
50 | * | |
51 | * @param component | |
52 | * the component to get the address of | |
53 | */ | |
54 | public ComponentAddress(IComponent component) | |
55 | { | |
56 | 0 | this(component.getPage().getPageName(), component.getIdPath()); |
57 | 0 | } |
58 | ||
59 | /** | |
60 | * Creates a new ComponentAddress using the given Page Name and ID Path. | |
61 | * | |
62 | * @param pageName | |
63 | * the name of the page that contains the component | |
64 | * @param idPath | |
65 | * the ID Path of the component (which may be null) | |
66 | */ | |
67 | public ComponentAddress(String pageName, String idPath) | |
68 | 0 | { |
69 | 0 | Defense.notNull(pageName, "pageName"); |
70 | ||
71 | 0 | _pageName = pageName; |
72 | 0 | _idPath = idPath; |
73 | 0 | } |
74 | ||
75 | /** | |
76 | * Creates a new ComponentAddress using the given Page Name and ID Path relative on the provided | |
77 | * Namespace. | |
78 | * | |
79 | * @param namespace | |
80 | * the namespace of the page that contains the component | |
81 | * @param pageName | |
82 | * the name of the page that contains the component | |
83 | * @param idPath | |
84 | * the ID Path of the component | |
85 | */ | |
86 | public ComponentAddress(INamespace namespace, String pageName, String idPath) | |
87 | { | |
88 | 0 | this(namespace.constructQualifiedName(pageName), idPath); |
89 | 0 | } |
90 | ||
91 | /** | |
92 | * Finds a component with the current address using the given RequestCycle. | |
93 | * | |
94 | * @param cycle | |
95 | * the RequestCycle to use to locate the component | |
96 | * @return IComponent a component that has been initialized for the given RequestCycle | |
97 | */ | |
98 | public IComponent findComponent(IRequestCycle cycle) | |
99 | { | |
100 | 0 | IPage objPage = cycle.getPage(_pageName); |
101 | 0 | return objPage.getNestedComponent(_idPath); |
102 | } | |
103 | ||
104 | /** | |
105 | * Returns the idPath of the component. | |
106 | * | |
107 | * @return String the ID path of the component, or null if the address references a page, not a | |
108 | * component within a page. | |
109 | */ | |
110 | public String getIdPath() | |
111 | { | |
112 | 0 | return _idPath; |
113 | } | |
114 | ||
115 | /** | |
116 | * Returns the Page Name of the component. | |
117 | * | |
118 | * @return String the Page Name of the component | |
119 | */ | |
120 | public String getPageName() | |
121 | { | |
122 | 0 | return _pageName; |
123 | } | |
124 | ||
125 | /** | |
126 | * @see java.lang.Object#hashCode() | |
127 | */ | |
128 | public int hashCode() | |
129 | { | |
130 | 0 | int hash = _pageName.hashCode() * 31; |
131 | 0 | if (_idPath != null) |
132 | 0 | hash += _idPath.hashCode(); |
133 | 0 | return hash; |
134 | } | |
135 | ||
136 | /** | |
137 | * @see java.lang.Object#equals(Object) | |
138 | */ | |
139 | public boolean equals(Object obj) | |
140 | { | |
141 | 0 | if (!(obj instanceof ComponentAddress)) |
142 | 0 | return false; |
143 | ||
144 | 0 | if (obj == this) |
145 | 0 | return true; |
146 | ||
147 | 0 | ComponentAddress objAddress = (ComponentAddress) obj; |
148 | 0 | if (!getPageName().equals(objAddress.getPageName())) |
149 | 0 | return false; |
150 | ||
151 | 0 | String idPath1 = getIdPath(); |
152 | 0 | String idPath2 = objAddress.getIdPath(); |
153 | 0 | return (idPath1 == idPath2) || (idPath1 != null && idPath1.equals(idPath2)); |
154 | } | |
155 | ||
156 | } |