DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Frameworks
  4. An Event Mechanism for the Wicket Framework

An Event Mechanism for the Wicket Framework

Erdinç Kocaman user avatar by
Erdinç Kocaman
·
Mar. 29, 10 · Interview
Like (0)
Save
Tweet
Share
14.50K Views

Join the DZone community and get the full member experience.

Join For Free

The 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.

Event Framework

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • DevOps for Developers: Continuous Integration, GitHub Actions, and Sonar Cloud
  • Introduction to Spring Cloud Kubernetes
  • Solving the Kubernetes Security Puzzle
  • Full Lifecycle API Management Is Dead

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: