I think what Ittay's and your case have in common is that you try to
make an update of multiple fields atomic. Wicket doesn't support
transactional updating of properties, and honestly I would know how to
fold such a thing elegantly in Wicket without making things a lot more
complicated.
Typically, I would do such a thing just in a submit method, where I
would check some temporary properties 'manually' and if I'm happy with
the input, I would direcly update/ construct the objects I'd need.
That looks a lot easier than how you two do things, BUT the great
advantage of your approaches is that it is a better fit with reusable
components.
So, I think that if this is really something we have to fix with
Wicket - mind you that currently I think the case is not common
enough, while there are several approach to acchieve the same thing -
we should do this not by opening up the internal processing of
components even further, but instead come up with transactional model
updating. But that doesn't seem like something trivial.
Eelco
On 5/17/06, Pekka Enberg <[EMAIL PROTECTED]> wrote:
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
-------------------------------------------------------
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&kid0709&bid&3057&dat1642
_______________________________________________
Wicket-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/wicket-user