org.apache.wicket
Class AttributeModifier

java.lang.Object
  extended by org.apache.wicket.behavior.AbstractBehavior
      extended by org.apache.wicket.AttributeModifier
All Implemented Interfaces:
Serializable, IBehavior, IComponentConfigurationBehavior, IClusterable, IHeaderContributor
Direct Known Subclasses:
AttributeAppender, BodyTagAttributeModifier

public class AttributeModifier
extends AbstractBehavior
implements IClusterable

This class allows a tag attribute of a component to be modified dynamically with a value obtained from a model object. This concept can be used to programmatically alter the attributes of components, overriding the values specified in the markup. The two primary uses of this class are to allow overriding of markup attributes based on business logic and to support dynamic localization. The replacement occurs as the component tag is rendered to the response.

The attribute whose value is to be modified must be given on construction of the instance of this class along with the model containing the value to replace with. Optionally a pattern can be supplied that is a regular expression that the existing value must match before the replacement can be carried out.

If an attribute is not in the markup, this modifier will add an attribute to the tag only if addAttributeIfNotPresent is true and the replacement value is not null.

Instances of this class should be added to components via the org.apache.wicket.Component#add(AttributeModifier) method after the component has been constructed.

It is possible to create new subclasses of AttributeModifier by overriding the newValue(String, String) method. For example, you could create an AttributeModifier subclass which appends the replacement value like this: new AttributeModifier("myAttribute", model) { protected String newValue(final String currentValue, final String replacementValue) { return currentValue + replacementValue; } };

Author:
Chris Turner, Eelco Hillenius, Jonathan Locke, Martijn Dashorst, Ralf Ebert
See Also:
Serialized Form

Field Summary
static String VALUELESS_ATTRIBUTE_ADD
          Marker value to have an attribute without a value added.
static String VALUELESS_ATTRIBUTE_REMOVE
          Marker value to have an attribute without a value removed.
 
Constructor Summary
AttributeModifier(String attribute, boolean addAttributeIfNotPresent, IModel<?> replaceModel)
          Create a new attribute modifier with the given attribute name and model to replace with.
AttributeModifier(String attribute, IModel<?> replaceModel)
          Create a new attribute modifier with the given attribute name and model to replace with.
AttributeModifier(String attribute, String pattern, boolean addAttributeIfNotPresent, IModel<?> replaceModel)
          Create a new attribute modifier with the given attribute name and expected pattern to match plus the model to replace with.
AttributeModifier(String attribute, String pattern, IModel<?> replaceModel)
          Create a new attribute modifier with the given attribute name and expected pattern to match plus the model to replace with.
 
Method Summary
 void detach(Component component)
          Detach the model if it was a IDetachableModel Internal method.
 boolean getAddAttributeIfNotPresent()
           
 String getAttribute()
           
protected  String getContextRelativeValue(String value)
          Checks if value represents a path and if it does transforms it into a relative path
 String getPattern()
           
protected  IModel<?> getReplaceModel()
          Gets the replacement model.
 boolean isEnabled()
          Deprecated.  
 boolean isEnabled(Component component)
          Made final to support the parameterless variant.
protected  String newValue(String currentValue, String replacementValue)
          Gets the value that should replace the current attribute value.
 void onComponentTag(Component component, ComponentTag tag)
          Called any time a component that has this behavior registered is rendering the component tag.
 void replaceAttibuteValue(Component component, ComponentTag tag)
          Deprecated. see replaceAttributeValue(Component, ComponentTag)
 void replaceAttributeValue(Component component, ComponentTag tag)
          Checks the given component tag for an instance of the attribute to modify and if all criteria are met then replace the value of this attribute with the value of the contained model object.
 void setEnabled(boolean enabled)
          Sets whether this attribute modifier is enabled or not.
 String toString()
           
 
Methods inherited from class org.apache.wicket.behavior.AbstractBehavior
afterRender, beforeRender, bind, cleanup, detachModel, exception, getStatelessHint, isTemporary, onConfigure, onException, onRendered, rendered, renderHead
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

VALUELESS_ATTRIBUTE_ADD

public static final String VALUELESS_ATTRIBUTE_ADD
Marker value to have an attribute without a value added.

See Also:
Constant Field Values

VALUELESS_ATTRIBUTE_REMOVE

public static final String VALUELESS_ATTRIBUTE_REMOVE
Marker value to have an attribute without a value removed.

See Also:
Constant Field Values
Constructor Detail

AttributeModifier

public AttributeModifier(String attribute,
                         boolean addAttributeIfNotPresent,
                         IModel<?> replaceModel)
Create a new attribute modifier with the given attribute name and model to replace with. The additional boolean flag specifies whether to add the attribute if it is not present.

Parameters:
attribute - The attribute name to replace the value for
addAttributeIfNotPresent - Whether to add the attribute if it is not present
replaceModel - The model to replace the value with

AttributeModifier

public AttributeModifier(String attribute,
                         IModel<?> replaceModel)
Create a new attribute modifier with the given attribute name and model to replace with. The attribute will not be added if it is not present.

Parameters:
attribute - The attribute name to replace the value for
replaceModel - The model to replace the value with

AttributeModifier

public AttributeModifier(String attribute,
                         String pattern,
                         boolean addAttributeIfNotPresent,
                         IModel<?> replaceModel)
Create a new attribute modifier with the given attribute name and expected pattern to match plus the model to replace with. A null pattern will match the attribute regardless of its value. The additional boolean flag specifies whether to add the attribute if it is not present.

Parameters:
attribute - The attribute name to replace the value for
pattern - The pattern of the current attribute value to match
addAttributeIfNotPresent - Whether to add the attribute if it is not present and the replacement value is not null
replaceModel - The model to replace the value with

AttributeModifier

public AttributeModifier(String attribute,
                         String pattern,
                         IModel<?> replaceModel)
Create a new attribute modifier with the given attribute name and expected pattern to match plus the model to replace with. A null pattern will match the attribute regardless of its value. The attribute will not be added if it is not present.

Parameters:
attribute - The attribute name to replace the value for
pattern - The pattern of the current attribute value to match
replaceModel - The model to replace the value with
Method Detail

detach

public final void detach(Component component)
Detach the model if it was a IDetachableModel Internal method. shouldn't be called from the outside. If the attribute modifier is shared, the detach method will be called multiple times.

Specified by:
detach in interface IBehavior
Overrides:
detach in class AbstractBehavior
Parameters:
component - the model that initiates the detachment
See Also:
IBehavior.detach(Component)

getAddAttributeIfNotPresent

public final boolean getAddAttributeIfNotPresent()
Returns:
whether to add the attribute if it is not an attribute in the markup

getAttribute

public final String getAttribute()
Returns:
the attribute name to replace the value for

getPattern

public final String getPattern()
Returns:
the pattern of the current attribute value to match

isEnabled

@Deprecated
public final boolean isEnabled()
Deprecated. 

Checks whether this attribute modifier is enabled or not.

Returns:
Whether enabled or not

isEnabled

public boolean isEnabled(Component component)
Made final to support the parameterless variant.

Specified by:
isEnabled in interface IBehavior
Overrides:
isEnabled in class AbstractBehavior
Parameters:
component - the component that has this behavior coupled
Returns:
true if this behavior must be executed/rendered
See Also:
AbstractBehavior.isEnabled(org.apache.wicket.Component)

onComponentTag

public final void onComponentTag(Component component,
                                 ComponentTag tag)
Description copied from interface: IBehavior
Called any time a component that has this behavior registered is rendering the component tag.

Specified by:
onComponentTag in interface IBehavior
Overrides:
onComponentTag in class AbstractBehavior
Parameters:
component - the component that renders this tag currently
tag - the tag that is rendered
See Also:
IBehavior.onComponentTag(org.apache.wicket.Component, org.apache.wicket.markup.ComponentTag)

replaceAttibuteValue

@Deprecated
public void replaceAttibuteValue(Component component,
                                            ComponentTag tag)
Deprecated. see replaceAttributeValue(Component, ComponentTag)

Checks the given component tag for an instance of the attribute to modify and if all criteria are met then replace the value of this attribute with the value of the contained model object.

Parameters:
component - The component
tag - The tag to replace the attribute value for

replaceAttributeValue

public final void replaceAttributeValue(Component component,
                                        ComponentTag tag)
Checks the given component tag for an instance of the attribute to modify and if all criteria are met then replace the value of this attribute with the value of the contained model object.

Parameters:
component - The component
tag - The tag to replace the attribute value for

getContextRelativeValue

protected String getContextRelativeValue(String value)
Checks if value represents a path and if it does transforms it into a relative path

Parameters:
value -
Returns:
value as a relative path or untouched if not a path

setEnabled

public final void setEnabled(boolean enabled)
Sets whether this attribute modifier is enabled or not.

Parameters:
enabled - Whether enabled or not

toString

public String toString()
Overrides:
toString in class Object
See Also:
Object.toString()

getReplaceModel

protected final IModel<?> getReplaceModel()
Gets the replacement model. Allows subclasses access to replace model.

Returns:
the replace model of this attribute modifier

newValue

protected String newValue(String currentValue,
                          String replacementValue)
Gets the value that should replace the current attribute value. This gives users the ultimate means to customize what will be used as the attribute value. For instance, you might decide to append the replacement value to the current instead of just replacing it as is Wicket's default.

Parameters:
currentValue - The current attribute value. This value might be null!
replacementValue - The replacement value. This value might be null!
Returns:
The value that should replace the current attribute value


Copyright © 2004-2011 Apache Software Foundation. All Rights Reserved.