org.apache.wicket
Class AttributeModifier

java.lang.Object
  extended by org.apache.wicket.behavior.Behavior
      extended by org.apache.wicket.AttributeModifier
All Implemented Interfaces:
java.io.Serializable, IClusterable, IComponentAwareEventSink, IComponentAwareHeaderContributor
Direct Known Subclasses:
AttributeAppender

public class AttributeModifier
extends Behavior
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.

If an attribute is not in the markup, this modifier will add an attribute.

Instances of this class should be added to components via the Component.add(Behavior...) 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 java.lang.String VALUELESS_ATTRIBUTE_ADD
          Marker value to have an attribute without a value added.
static java.lang.String VALUELESS_ATTRIBUTE_REMOVE
          Marker value to have an attribute without a value removed.
 
Constructor Summary
AttributeModifier(java.lang.String attribute, boolean addAttributeIfNotPresent, IModel<?> replaceModel)
          Deprecated. AttributeModifier will now always add the attribute if not present, use AttributeModifier(String, IModel) instead
AttributeModifier(java.lang.String attribute, IModel<?> replaceModel)
          Create a new attribute modifier with the given attribute name and model to replace with.
AttributeModifier(java.lang.String attribute, java.io.Serializable value)
          Create a new attribute modifier with the given attribute name and model to replace with.
 
Method Summary
static AttributeAppender append(java.lang.String attributeName, IModel<?> value)
          Creates a attribute modifier that appends the current value with the given value using a default space character (' ') separator.
static AttributeAppender append(java.lang.String attributeName, java.io.Serializable value)
          Creates a attribute modifier that appends the current value with the given value using a default space character (' ') separator.
 void detach(Component component)
          Detach the value if it was a IDetachable.
 java.lang.String getAttribute()
           
protected  IModel<?> getReplaceModel()
          Gets the replacement model.
protected  java.lang.String newValue(java.lang.String currentValue, java.lang.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.
static AttributeAppender prepend(java.lang.String attributeName, IModel<?> value)
          Creates a attribute modifier that prepends the current value with the given value using a default space character (' ') separator.
static AttributeAppender prepend(java.lang.String attributeName, java.io.Serializable value)
          Creates a attribute modifier that prepends the current value with the given value using a default space character (' ') separator.
static AttributeModifier remove(java.lang.String attributeName)
          Creates a attribute modifier that removes an attribute with the specified name
static AttributeModifier replace(java.lang.String attributeName, IModel<?> value)
          Creates a attribute modifier that replaces the current value with the given value.
static AttributeModifier replace(java.lang.String attributeName, java.io.Serializable value)
          Creates a attribute modifier that replaces the current value with the given value.
 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.
 java.lang.String toString()
           
 
Methods inherited from class org.apache.wicket.behavior.Behavior
afterRender, beforeRender, bind, canCallListenerInterface, getStatelessHint, isEnabled, isTemporary, onConfigure, onEvent, onException, renderHead, unbind
 
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 java.lang.String VALUELESS_ATTRIBUTE_ADD
Marker value to have an attribute without a value added.


VALUELESS_ATTRIBUTE_REMOVE

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

Constructor Detail

AttributeModifier

@Deprecated
public AttributeModifier(java.lang.String attribute,
                                    boolean addAttributeIfNotPresent,
                                    IModel<?> replaceModel)
Deprecated. AttributeModifier will now always add the attribute if not present, use AttributeModifier(String, IModel) instead

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(java.lang.String attribute,
                         IModel<?> replaceModel)
Create a new attribute modifier with the given attribute name and model to replace with. The attribute will be added with the model value or the value will be replaced with the model value if the attribute is already present.

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

AttributeModifier

public AttributeModifier(java.lang.String attribute,
                         java.io.Serializable value)
Create a new attribute modifier with the given attribute name and model to replace with. The attribute will be added with the model value or the value will be replaced with the value if the attribute is already present.

Parameters:
attribute - The attribute name to replace the value for
value - The value for the attribute
Method Detail

detach

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

Overrides:
detach in class Behavior
Parameters:
component - the model that initiates the detachment

getAttribute

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

onComponentTag

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

Overrides:
onComponentTag in class Behavior
Parameters:
component - the component that renders this tag currently
tag - the tag that is rendered

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

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.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 java.lang.String newValue(java.lang.String currentValue,
                                    java.lang.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

replace

public static AttributeModifier replace(java.lang.String attributeName,
                                        IModel<?> value)
Creates a attribute modifier that replaces the current value with the given value.

Parameters:
attributeName -
value -
Returns:
the attribute modifier
Since:
1.5

replace

public static AttributeModifier replace(java.lang.String attributeName,
                                        java.io.Serializable value)
Creates a attribute modifier that replaces the current value with the given value.

Parameters:
attributeName -
value -
Returns:
the attribute modifier
Since:
1.5

append

public static AttributeAppender append(java.lang.String attributeName,
                                       IModel<?> value)
Creates a attribute modifier that appends the current value with the given value using a default space character (' ') separator.

Parameters:
attributeName -
value -
Returns:
the attribute modifier
Since:
1.5
See Also:
AttributeAppender

append

public static AttributeAppender append(java.lang.String attributeName,
                                       java.io.Serializable value)
Creates a attribute modifier that appends the current value with the given value using a default space character (' ') separator.

Parameters:
attributeName -
value -
Returns:
the attribute modifier
Since:
1.5
See Also:
AttributeAppender

prepend

public static AttributeAppender prepend(java.lang.String attributeName,
                                        IModel<?> value)
Creates a attribute modifier that prepends the current value with the given value using a default space character (' ') separator.

Parameters:
attributeName -
value -
Returns:
the attribute modifier
Since:
1.5
See Also:
AttributeAppender

prepend

public static AttributeAppender prepend(java.lang.String attributeName,
                                        java.io.Serializable value)
Creates a attribute modifier that prepends the current value with the given value using a default space character (' ') separator.

Parameters:
attributeName -
value -
Returns:
the attribute modifier
Since:
1.5
See Also:
AttributeAppender

remove

public static AttributeModifier remove(java.lang.String attributeName)
Creates a attribute modifier that removes an attribute with the specified name

Parameters:
attributeName - the name of the attribute to be removed
Returns:
the attribute modifier
Since:
1.5


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