KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > util > ComponentAddress


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 JavaDoc;
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 JavaDoc
40 {
41     private String JavaDoc _pageName;
42
43     private String JavaDoc _idPath;
44
45     /**
46      * Creates a new ComponentAddress object that carries the identification information of the
47      * given component (the page name and the ID path).
48      *
49      * @param component
50      * the component to get the address of
51      */

52     public ComponentAddress(IComponent component)
53     {
54         this(component.getPage().getPageName(), component.getIdPath());
55     }
56
57     /**
58      * Creates a new ComponentAddress using the given Page Name and ID Path
59      *
60      * @param pageName
61      * the name of the page that contains the component
62      * @param idPath
63      * the ID Path of the component (which may be null)
64      */

65     public ComponentAddress(String JavaDoc pageName, String JavaDoc idPath)
66     {
67         Defense.notNull(pageName, "pageName");
68
69         _pageName = pageName;
70         _idPath = idPath;
71     }
72
73     /**
74      * Creates a new ComponentAddress using the given Page Name and ID Path relative on the provided
75      * Namespace
76      *
77      * @param namespace
78      * the namespace of the page that contains the component
79      * @param pageName
80      * the name of the page that contains the component
81      * @param idPath
82      * the ID Path of the component
83      */

84     public ComponentAddress(INamespace namespace, String JavaDoc pageName, String JavaDoc idPath)
85     {
86         this(namespace.constructQualifiedName(pageName), idPath);
87     }
88
89     /**
90      * Finds a component with the current address using the given RequestCycle.
91      *
92      * @param cycle
93      * the RequestCycle to use to locate the component
94      * @return IComponent a component that has been initialized for the given RequestCycle
95      */

96     public IComponent findComponent(IRequestCycle cycle)
97     {
98         IPage objPage = cycle.getPage(_pageName);
99         return objPage.getNestedComponent(_idPath);
100     }
101
102     /**
103      * Returns the idPath of the component.
104      *
105      * @return String the ID path of the component, or null if the address references a page, not a
106      * component within a page.
107      */

108     public String JavaDoc getIdPath()
109     {
110         return _idPath;
111     }
112
113     /**
114      * Returns the Page Name of the component.
115      *
116      * @return String the Page Name of the component
117      */

118     public String JavaDoc getPageName()
119     {
120         return _pageName;
121     }
122
123     /**
124      * @see java.lang.Object#hashCode()
125      */

126     public int hashCode()
127     {
128         int hash = _pageName.hashCode() * 31;
129         if (_idPath != null)
130             hash += _idPath.hashCode();
131         return hash;
132     }
133
134     /**
135      * @see java.lang.Object#equals(Object)
136      */

137     public boolean equals(Object JavaDoc obj)
138     {
139         if (!(obj instanceof ComponentAddress))
140             return false;
141
142         if (obj == this)
143             return true;
144
145         ComponentAddress objAddress = (ComponentAddress) obj;
146         if (!getPageName().equals(objAddress.getPageName()))
147             return false;
148
149         String JavaDoc idPath1 = getIdPath();
150         String JavaDoc idPath2 = objAddress.getIdPath();
151         return (idPath1 == idPath2) || (idPath1 != null && idPath1.equals(idPath2));
152     }
153
154 }
Popular Tags