java.lang.Object
org.apache.wicket.behavior.Behavior
org.apache.wicket.AttributeModifier
- All Implemented Interfaces:
Serializable
,IComponentAwareEventSink
,IComponentAwareHeaderContributor
,IClusterable
- Direct Known Subclasses:
AttributeAppender
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:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic enum
Special attribute value markers. -
Field Summary
Modifier and TypeFieldDescriptionstatic final AttributeModifier.MarkerValue
Marker value to have an attribute without a value added.static final AttributeModifier.MarkerValue
Marker value to have an attribute without a value removed. -
Constructor Summary
ConstructorDescriptionAttributeModifier
(String attribute, Serializable value) 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. -
Method Summary
Modifier and TypeMethodDescriptionstatic AttributeAppender
append
(String attributeName, Serializable value) Creates a attribute modifier that appends the current value with the givenvalue
using a default space character (' ') separator.static AttributeAppender
Creates a attribute modifier that appends the current value with the givenvalue
using a default space character (' ') separator.final void
Detach the value if it was aIDetachable
.final String
protected final IModel<?>
Gets the replacement model.protected Serializable
Gets the value that should replace the current attribute value.final void
onComponentTag
(Component component, ComponentTag tag) Called any time a component that has this behavior registered is rendering the component tag.static AttributeAppender
prepend
(String attributeName, Serializable value) Creates a attribute modifier that prepends the current value with the givenvalue
using a default space character (' ') separator.static AttributeAppender
Creates a attribute modifier that prepends the current value with the givenvalue
using a default space character (' ') separator.static AttributeModifier
Creates a attribute modifier that removes an attribute with the specified namestatic AttributeModifier
replace
(String attributeName, Serializable value) Creates a attribute modifier that replaces the current value with the given value.static AttributeModifier
Creates a attribute modifier that replaces the current value with the given value.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.toString()
Methods inherited from class org.apache.wicket.behavior.Behavior
afterRender, beforeRender, bind, canCallListener, getStatelessHint, isEnabled, isTemporary, onAttribute, onConfigure, onEvent, onException, onRemove, onTag, renderHead, unbind
-
Field Details
-
VALUELESS_ATTRIBUTE_ADD
Marker value to have an attribute without a value added. -
VALUELESS_ATTRIBUTE_REMOVE
Marker value to have an attribute without a value removed.
-
-
Constructor Details
-
AttributeModifier
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 forreplaceModel
- The model to replace the value with
-
AttributeModifier
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 forvalue
- The value for the attribute
-
-
Method Details
-
detach
Detach the value if it was aIDetachable
. Internal method, shouldn't be called from the outside. If the attribute modifier is shared, the detach method will be called multiple times. -
getAttribute
- Returns:
- the attribute name to replace the value for
-
onComponentTag
Description copied from class:Behavior
Called any time a component that has this behavior registered is rendering the component tag.- Overrides:
onComponentTag
in classBehavior
- Parameters:
component
- the component that renders this tag currentlytag
- the tag that is rendered
-
replaceAttributeValue
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 componenttag
- The tag to replace the attribute value for
-
toString
-
getReplaceModel
Gets the replacement model. Allows subclasses access to replace model.- Returns:
- the replace model of this attribute modifier
-
newValue
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
Creates a attribute modifier that replaces the current value with the given value.- Parameters:
attributeName
-value
-- Returns:
- the attribute modifier
- Since:
- 1.5
-
replace
Creates a attribute modifier that replaces the current value with the given value.- Parameters:
attributeName
-value
-- Returns:
- the attribute modifier
- Since:
- 1.5
-
append
Creates a attribute modifier that appends the current value with the givenvalue
using a default space character (' ') separator.- Parameters:
attributeName
-value
-- Returns:
- the attribute modifier
- Since:
- 1.5
- See Also:
-
append
Creates a attribute modifier that appends the current value with the givenvalue
using a default space character (' ') separator.- Parameters:
attributeName
-value
-- Returns:
- the attribute modifier
- Since:
- 1.5
- See Also:
-
prepend
Creates a attribute modifier that prepends the current value with the givenvalue
using a default space character (' ') separator.- Parameters:
attributeName
-value
-- Returns:
- the attribute modifier
- Since:
- 1.5
- See Also:
-
prepend
Creates a attribute modifier that prepends the current value with the givenvalue
using a default space character (' ') separator.- Parameters:
attributeName
-value
-- Returns:
- the attribute modifier
- Since:
- 1.5
- See Also:
-
remove
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
-