At some point in time, Ittay Dror wrote:
> i have a similar use case. below is the code for a component i wrote.
> it is a markup container, so you can add
> components to it, which can be referenced from the html:
[snip]
Thanks. We ended up using a different approach which I have included below.
Pekka
<?xml version="1.0" encoding="utf-8"?>
<wicket:panel>
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td>
<input wicket:id="min" type="text" />
</td>
<td> - </td>
<td>
<input wicket:id="max" type="text" />
</td>
</tr>
</table>
</wicket:panel>
package wicket.sets;
import wicket.Component;
import wicket.markup.html.form.Form;
import wicket.markup.html.form.FormComponent;
import wicket.markup.html.form.TextField;
import wicket.markup.html.form.validation.AbstractFormValidator;
import wicket.markup.html.panel.Panel;
import wicket.model.IModel;
import wicket.model.IModelComparator;
import wicket.model.Model;
import sets.IntegerRange;
/**
* @author Pekka Enberg
*/
public class IntegerRangeField extends Panel {
private final TextField minimum;
private final TextField maximum;
public IntegerRangeField(Form parent, String id) {
this(parent, id, null);
}
public IntegerRangeField(Form parent, String id,
IModel/*<IntegerRange>*/model) {
super(id, model);
minimum = new TextField("min", new Model(defaultMinimumValue()),
Integer.class);
maximum = new MaximumField("max", new Model(defaultMaximumValue()) {
@Override
public void setObject(Component component, Object object) {
super.setObject(component, object);
try {
IntegerRange newRange = null;
if (minimumValue() != null && maximumValue() != null) {
newRange = new IntegerRange(minimumValue(),
maximumValue());
}
IntegerRangeField.this.setModelObject(newRange);
} catch (IllegalArgumentException e) {
/*
* Ignore the exception. We don't want to update the
* actual model when user input is invalid.
*/
}
}
});
maximum.setType(Integer.class);
add(minimum);
add(maximum);
parent.add(new Validator(this));
}
private Integer defaultMinimumValue() {
if (rangeValue() == null) {
return null;
}
return rangeValue().getMinimum();
}
private Integer defaultMaximumValue() {
if (rangeValue() == null) {
return null;
}
return rangeValue().getMaximum();
}
private IntegerRange rangeValue() {
return (IntegerRange) getModelObject();
}
private Integer minimumValue() {
return (Integer) minimum.getModelObject();
}
private Integer maximumValue() {
return (Integer) maximum.getModelObject();
}
private final class MaximumField extends TextField {
private MaximumField(String id, IModel object) {
super(id, object);
}
@Override
protected IModelComparator getModelComparator() {
/*
* We need this to ensure Model.setObject() is called for maximum
* even if only minimum value was changed by user.
*/
return new IModelComparator() {
public boolean compare(Component component, Object newObject) {
return false;
}
};
}
}
public static class Validator extends AbstractFormValidator {
private final IntegerRangeField range;
public Validator(IntegerRangeField range) {
this.range = range;
}
public FormComponent[] getDependentFormComponents() {
return new FormComponent[] { range.minimum, range.maximum };
}
public void validate(Form form) {
String minimumInput = range.minimum.getInput();
String maximumInput = range.maximum.getInput();
if (minimumInput == null && maximumInput == null) {
/* Both are NULL -- the input is ok. */
return;
}
if (minimumInput == null || maximumInput == null) {
form.error("both 'min' and 'max' must be defined");
return;
}
if (minimumInput.compareTo(maximumInput) > 0) {
form.error("'min' is greater than 'max'");
return;
}
}
}
}
-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Wicket-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/wicket-user