An Event Mechanism for the Wicket Framework
Join the DZone community and get the full member experience.
Join For FreeThe event system I will discuss about here is based on the event handling mechanism used in Buoy Swing library (http://buoy.sourceforge.net). It is an excellent library to develop Swing applications. I modified the event handling code of buoy library in order to provide generics support and wicket integration.
Java has no mechanism corresponding to events and delegates in C#. Instead of them, we have to use interfaces and anonymous classes to be informed by component events; however, this leads to the proliferation of classes and makes it hard to read the source code. This novel event system simplifies event handling, which implies clear separation between components and their clients.
I will develop a login page as an example to explain the concept. This page includes a reusable component called “LoginComponent”. The task of this component is to check if the user types the correct username and password. After checking the user input, the reusable component will send events indicating login status. If the user logs in successfully, “success” field of event is set to “true”, otherwise it is set to “false”. The “LoginPage” including the “LoginComponent” receives login events and acts accordingly. If login fails, it displays an error message; otherwise, it redirects the user to another page.
I am listing the code below.
LoginPage.html
<html><head>
<title> Login page </title>
</head>
<body>
<table>
<tr>
<td>
<div wicket:id="feedbackPanel">
</div>
</td>
</tr>
<tr>
<td>
<div wicket:id="loginPanel">
</div>
</td>
</tr>
</table>
</body>
</html>
LoginPage.java
public class LoginPage extends EasyPage {
public LoginPage() {
add(new FeedbackPanel("feedbackPanel"));
LoginComponent loginComponent = new LoginComponent("loginPanel");
add(loginComponent);
loginComponent.addEventLink(LoginEvent.class, this, "onLogin");
}
@SuppressWarnings("unused")
private void onLogin(LoginEvent event) {
if ( event.isSuccess() ) {
// redirect to another page
info("Login successful");
}else {
error("Login failed");
}
}
}
Code listing for “LoginComponent” is below;
LoginComponent.html
<wicket:panel>
<form wicket:id="form">
<table>
<tr>
<td> User name </td>
<td> <input type="text" wicket:id="txtUsername"> </td>
</tr>
<tr>
<td> Password </td>
<td> <input type="password"" wicket:id="txtPassword"> </td>
</tr>
<tr>
<td> </td>
<td> <button type="submit" wicket:id="btnLogin"> Login </button> </td>
</tr>
</table>
</form>
</wicket:panel>
LoginComponent.java
public class LoginComponent extends EasyPanel {
@EasyWicket(id="form")
Form<Void> form;
@EasyWicket(id="form.txtUsername", value="username", required=true)
TextField<String>txtUsername;
@EasyWicket(id="form.txtPassword", value="password", required=true)
PasswordTextField txtPassword;
@EasyWicket(id="form.btnLogin", action="actionLogin")
Button btnLogin;
String username, password;
private static Logger logger = LoggerFactory.getLogger(LoginComponent.class);
public LoginComponent(String id) {
super(id);
}
public void actionLogin() {
if ( logger.isInfoEnabled() ) {
logger.info("username=" + username + " password=" + password);
}
LoginEvent event = new LoginEvent(this);
event.setSuccess(System.currentTimeMillis() % 2 == 0);
dispatchEvent(event);
}
}
Finally, source code of “LoginEvent”
LoginEvent.java
public class LoginEvent extends WicketEvent {
private boolean success;
public LoginEvent(Component source) {
super(source);
}
public void setSuccess(boolean success) {
this.success = success;
}
public boolean isSuccess() {
return success;
}
}
To receive events, event listener must be registered as in “LoginPage.java”
loginComponent.addEventLink(LoginEvent.class, this, "onLogin");
This code snippet says, “this” is registered as “LoginEvent” listener and “onLogin” method must be called when “LoginEvent” is triggered.
To support this type of behavior, “LoginComponent” must use the event dispatcher as in following code:
From JavaComponent.java
LoginEvent event = new LoginEvent(this);
event.setSuccess(System.currentTimeMillis() % 2 == 0);
dispatchEvent(event);
This code creates a login event, sets the “success” field according to the login result, and sends event to the listeners of the event.
As you see from the source code, user interface elements are annotated with “@EasyWicket” annotation. This annotation comes from EasyWicket library I discussed about in my previous article. I also incorporated the event handling code into the EasyWicket library. You can reach the codes here.
Opinions expressed by DZone contributors are their own.
Comments