|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Object
org.apache.myfaces.trinidad.util.ComponentReference<T>
public abstract class ComponentReference<T extends javax.faces.component.UIComponent>
A utility to store a reference to an UIComponent. Application developers should use this tool if they need to have a reference to an instance of the UIComponent class in managed beans that are longer than requested scoped --for example Session and Application Scoped. The reference will return the UIComponent, if any, with the same scoped id as the Component used to create the reference, in the current UIViewRoot. Use newUIComponentReference() to create a ComponentReference and use the getComponent() to look up the referenced UIComponent. For example, a current weather application might have have a session scoped weatehrBean containing the current list of locations to report the weather on and support using a selectMany component to remove the locations:
<tr:selectManyCheckbox label="Locations" id="smc1" valuePassThru="true"
binding="#{weatherBean.locationsSelectManyComponent}"
value="#{weatherBean.locationsToRemove}">
<f:selectItems value="#{weatherBean.locationSelectItems}" id="si1"/>
</tr:selectManyCheckbox>
<tr:commandButton id="deleteCB" text="Remove Locations"
actionListener="#{weatherBean.removeLocationListener}">
</tr:commandButton>
The weatherBean might looks like this:
public class WeatherBean implements Serializable
{
public void setLocationsToRemove(UIXSelectMany locationsToRemove)
{
_locationsToRemove = locationsToRemove;
}
public UIXSelectMany getLocationsToRemove()
{
return _locationsToRemove;
}
public void removeLocationListener(ActionEvent actionEvent)
{
... code calling getLocationsToRemove() to get the UIXSelectMany ...
}
private UIXSelectMany _locationsToRemove
}
This code has several problems:
Rewritten using ComponentReference, the weatherBean might looks like this:
public class WeatherBean implements Serializable
{
public void setLocationsToRemove(UIXSelectMany locationsToRemove)
{
_locationsToRemoveRef = UIComponentReference.newUIComponentReference(locationsToRemove);
}
public UIXSelectMany getLocationsToRemove()
{
return _locationsToRemoveRef.getComponent();
}
public void removeLocationListener(ActionEvent actionEvent)
{
... code calling getLocationsToRemove() to get the UIXSelectMany ...
}
private UIComponentReference<UIXSelectMany> _locationsToRemoveRef
}
The above code saves a reference to the component passed to the managed bean and then retrieves the correct instance given the current UIViewRoot for this request whenever getLocationsToRemove() is called.
Please note:
UIComponent APIs, however the class is safe to use as long as either of the following is true
newUIComponentReference has an id and is in the component hierarchy when newUIComponentReference is called and all subsequent calls to getComponent are made from Threads with a valid FacesContextgetComponent is on the same Thread that newUIComponentReference was called onUIComponent is required to have an IDUIComponent is moved between NamingContainers or if any of the ancestor NamingContainers have their IDs changed.UIComponents are not Serializable and therefore can not be used at any scope longer than request.newUIComponentReference(UIComponent), getComponent(), Serialized Form| Method Summary | ||
|---|---|---|
protected static java.util.List<java.lang.Object> |
calculateComponentPath(javax.faces.component.UIComponent component)Creates the "component path" started by the given UIComponent up the UIViewRoot of the underlying component tree. |
|
protected static java.lang.String |
calculateScopedId(javax.faces.component.UIComponent component, java.lang.String componentId) |
|
abstract void |
ensureInitialization()Called by the framework to ensure that deferred ComponentReferences are completely initialized before the UIComponent that the ComponentReference is associated with is not longer valid. |
|
boolean |
equals(java.lang.Object o)ComponentRefs are required to test for equivalence by the equivalence of their scoped ids |
|
T |
getComponent()This method will use a calculated "component path" to walk down to the UIComponent that is referenced by this class. |
|
protected abstract java.lang.String |
getComponentId()Returns the id of the Component that this ComponentReference points to |
|
protected abstract java.lang.String |
getScopedId()Returns the scoped id for this ComponentReference |
|
protected static javax.faces.component.UIViewRoot |
getUIViewRoot(javax.faces.component.UIComponent component) |
|
int |
hashCode()ComponentRefs must use the hash code of their scoped id as their hash code |
|
static
|
newUIComponentReference(T component)Factory method to create an instance of the ComponentReference class, which returns a Serializable and often thread-safe reference to a UIComponent. |
|
protected void |
setComponentPath(java.util.List<java.lang.Object> componentPath) |
|
java.lang.String |
toString() |
|
protected java.lang.Object |
writeReplace() |
|
| Methods inherited from class java.lang.Object |
|---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
| Method Detail |
|---|
public static <T extends javax.faces.component.UIComponent> ComponentReference<T> newUIComponentReference(T component)
ComponentReference class, which returns a Serializable and often thread-safe reference to a UIComponent.component - the UIComponent to create a reference to.ComponentReference the reference to the componentjava.lang.NullPointerException - if component is nullpublic final T getComponent()
UIComponent that is referenced by this class. If the component can not be found, the getComponent() will return null.UIComponent or null if it can not be found.java.lang.IllegalStateException - if the component used to create the ComponentReference is not in the component tree or does not have an IdnewUIComponentReference(UIComponent)public abstract void ensureInitialization()
java.lang.IllegalStateException - if ComponentReference isn't already initialized and the component used to create the ComponentReference is not in the component tree or does not have an Idpublic final boolean equals(java.lang.Object o)
equals in class java.lang.Objecto -public final int hashCode()
hashCode in class java.lang.Objectpublic final java.lang.String toString()
toString in class java.lang.Objectprotected abstract java.lang.String getScopedId()
protected abstract java.lang.String getComponentId()
protected final void setComponentPath(java.util.List<java.lang.Object> componentPath)
protected static java.util.List<java.lang.Object> calculateComponentPath(javax.faces.component.UIComponent component)
UIComponent up the UIViewRoot of the underlying component tree. The hierarchy is stored in a List of Objects (the componentHierarchyList parameter). If the given UIComponent is nested in a Facet of a UIComponent, we store the name of the actual facet in the list. If it is a regular child, we store its position/index.
To calculate the scopedID we add the ID of every NamingContainer that we hit, while walking up the component tree, to the scopedIdList.
component - The UIComponent for this current iterationnewUIComponentReference(T)
protected static java.lang.String calculateScopedId(javax.faces.component.UIComponent component,
java.lang.String componentId)
protected java.lang.Object writeReplace()
throws java.io.ObjectStreamException
java.io.ObjectStreamExceptionprotected static javax.faces.component.UIViewRoot getUIViewRoot(javax.faces.component.UIComponent component)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
Copyright © 2001-2012 The Apache Software Foundation. All Rights Reserved.