It's nice to be able to use new technologies in a way easy and agile.
What we want to do is to be able to send a form with ajax and at the same time be able to tell the user that the form is being submitted.
Like this (colors are degraded because it's gif):
First we have to put the html needed to handle the form.
This is the easy part.
</p>
<form style="width: 50%;"> <!-- overlay-<form-name> must be added if we want greyed form -->
<div id="overlay-event-form" style="width: 100%;">
<table style="width: 100%;" border="0">
<caption>My simple calendar</caption>
<tbody>
<tr>
<th>Date</th><th>Hour</th><th>Comment</th>
</tr>
<tr>
<td><input type="text" /></td>
<td><input type="text" /></td>
<td><input type="text" /></td>
</tr>
<tr>
<td colspan="3" align="center"><button>Add new event (10 sec delay)</button></td>
</tr>
</tbody>
</table>
</div>
</form>
<div style="width: 50%;">
<table style="width: 100%;" border="0">
<caption>My Events</caption>
<tbody>
<tr>
<th>Date</th><th>Hour</th><th>Comment</th>
</tr>
<tr>
<td><span></span><br /></td>
<td><span></span><br /></td>
<td><span></span><br /></td>
</tr>
</tbody>
</table>
</div>
<p>
We can add the code that will handle the form. With the new wicket-dojo this is easy:
final DojoForm eventForm = new DojoForm("event-form")
{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void onSubmit() {
// TODO Auto-generated method stub
super.onSubmit();
// Insert delay
long currentTime
= System.
currentTimeMillis();
while(System.
currentTimeMillis() <
; currentTime
+ 5000) {
try {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
eventDate = calendar.getConvertedInput();
if(eventDate!=null)
{
SimpleEventModel event = new SimpleEventModel(eventDate);
if(event!=null)
{
event.setTime(time.getModelObject());
event.setComment(comment.getModelObject());
eventList.add(event);
}
}
}
};
this.add(eventForm);
Adding the submit button is piece of cake:
// Add button to the form
DojoButton button = new DojoButton("button-add")
{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void onClick(AjaxRequestTarget target) {
// TODO Auto-generated method stub
}
@Override
protected void onMouseOver(AjaxRequestTarget target) {
// TODO Auto-generated method stub
}
@Override
protected void onSubmit(AjaxRequestTarget target, Form<!--?--> form) {
if(listContainer!=null)
{
target.addComponent(listContainer);
}
}
};
eventForm.add(button);<p> </p>
We can add two custom control for the date and time controls:
// Add a input type text
{
@Override
public IConverter getConverter(Class<!--?--> type) {
IConverter converter = super.getConverter(type);
if(type.getName().equalsIgnoreCase("java.util.Date"))
converter = new DojoDateConverter();
if(type.getName().equalsIgnoreCase("java.lang.String"))
converter = new DojoDateConverter();
return converter;
}
/**
*
*/
private static final long serialVersionUID = 1L;
};
calendar.add(new DojoDateTextBoxBehavior(){
/**
*
*/
private static final long serialVersionUID = 1L;
});
eventForm.add(calendar);
// Add a input type text
{
@Override
public IConverter getConverter(Class<!--?--> type) {
IConverter converter = super.getConverter(type);
if(type.getName().equalsIgnoreCase("java.util.Date"))
converter = new DojoTimeConverter();
if(type.getName().equalsIgnoreCase("java.lang.String"))
converter = new DojoTimeConverter();
return converter;
}
/**
*
*/
private static final long serialVersionUID = 1L;
};
time.add(new DojoTimeTextBoxBehavior(){
/**
*
*/
private static final long serialVersionUID = 1L;
});
eventForm.add(time);
We add also a new control to let the user imput a comment.
// Add a input type text
comment
= new TextField("form-comment",
new Model
("")); comment.add(new DojoTextBoxBehavior(){
/**
*
*/
private static final long serialVersionUID = 1L;
});
eventForm.add(comment);