While writing some unit tests for a wicket application I was working on, I found that I was writing a lot of component path expressions to get at various components in my pages.
However after some refactoring of the page hierarchy I had to re-write most of the expressions to take make the tests work again.
This lead me to think that maybe there could be an easier way for getting at components, using * to represent any child component at a given level, and ** to represent any child component at any level.
Doing this would mean that you could re-write this:
“panel1:innerpanel1:form1:textfield1”
as
“*:*:form1:textfield1”
or
“**:form:textfield1”
or even
“**:textfield1”
Then I got thinking that I would be pretty simple to place type restrictions on the results, so you could set the following expression:
“**” and type restriction of TextField
to get all TextField component on the page or
“panel1:**” and type restriction of ListView
to get all ListView components which are children of the panel1 component.
To use component expressions I created 4 static methods
-
public static Component findComponent(Component parent,
String expression)
-
public static Component findComponent(Component parent,
String expression,Class<? extends Component>typeRestriction)
-
public static List findAllComponents(Component parent,
String expression)
-
public static List findAllComponents(Component parent,
String expression, Class<? extends Component> typeRestriction)
The code is checked into the london wicket google code repository, and you can include it as a dependency to your project by adding the following to your pom.xml
<repositories>
<repository>
<id>londonwicket</id>
<url>http://londonwicket.googlecode.com/svn/repo</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<dependency>
<groupId>com.googlecode.londonwicket</groupId>
<artifactId>wicket-component-expressions</artifactId>
<version>0.1.0</version>
</dependency>
There are tests to cover all the different cases I could think of. Any comments, bug fixes, or improvements would be more than welcome.
Tags:
Java,
Wicket