Quantcast
Channel: Wicket Game (Entries tagged as guildwars assistant)
Viewing all articles
Browse latest Browse all 6

How to change css attributes and disable form elements on the fly

$
0
0

Wicket makes it easy to manipulate CSS attributes on the fly. The most obvious solution would be to overwrite the onComponentTag method and add the logic there. It's a very powerful solution as you can virtually do anything to the tag. But writing html fragments directly from my Java code is something that smells like JSPs and I don't like that smell.
One of the greatest features of wicket is the separation of logic and data (code and html) and the requirement can be met without ruining that.
Wicket offers 2 powerful classes to handle these. One is the SimpleAttributeModifier, "a lightweight version of the attribute modifier." I never had to use the AttributeModifier as the simple version always was enough. The other one is the AttributeAppender.
The main difference between these two is how they react to existing attributes. The SimpleAttributeModifier replaces existing attributes of the same type (e.g. replaces the value of the class-attribute), while the AttributeAppender adds to it using a constructor-defined separator (like adding another class or appending more JavaScript to the onClick-attribute).
A slightly different approach seems obvious when a button or other form element needs to be disabled, since Component offers a setEnabled(boolean) method. Unfortunately this method only set's wicket internal values. The JavaDoc suggests to overwrite this method for form elements but this isn't part of the wicket release, so this approach doesn't work. It sure would be easy to subclass the form elements and implement this (possibly using the AttributeAppender) but I favour Composition over Inheritance, so my solution would be to use the AttributeAppender directly when needed. Maybe when using wicket-security or some other framework to enable/disable buttons and links based on roles or principals this might not be the best solution but since I'm currently not using these...


Continue reading "How to change css attributes and disable form elements on the fly"

Viewing all articles
Browse latest Browse all 6

Trending Articles