| Reusing the same formData member on more than one field [message #975956] |
Thu, 08 November 2012 02:51  |
Urs Beeli Messages: 162 Registered: October 2012 Location: Bern, Switzerland |
Senior Member |
|
|
I have a form containing two tabs. One tab shows a rough overview over the data returned (company name, the year for which the data is shown, a total over the data) while the second tab shows the detail data but it also includes some "header" data (the year for which the data is shown, the total).
When I try to create two fields with the same name on the different tabs (i.e. in their group boxes) Scout complains, telling me the name already exists. If I rename the field, an additional member with the new name is added to the formData.
I currently solve this, by returning the data twice in my process service, i.e.
SQL.selectInto("select name, year, year, total, total, n1, n2, ... nN" +
"from company where company_nr = :companyNr" +
"into :companyName, :year, :year2, :total, :total2, :n1, ... :nN",
formData);
This works, but seems to me to be a clumsy way of solving this issue. Is there a workaround for this?
|
|
|
|
|
|
| Re: Reusing the same formData member on more than one field [message #981753 is a reply to message #975956] |
Mon, 12 November 2012 12:13   |
|
Hallo Urs
You can "be informed" when a field is changed:
yearField.addPropertyChangeListener(IValueField.PROP_VALUE, new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent e) {
Date year = (Date) e.getNewValue();
year2Field.setValue(year);
}
}
With this the year2Field gets changed, as soon as the yearField is changed. There is a drawback with this: Normally the form gets loaded with
MyFormData formData = new MyFormData();
exportFormData(formData);
formData = SERVICES.getService(IMyFormProcessService.class).load(formData);
importFormData(formData);
The importFormData call doesn't trigger the property change triggers by default (IMHO: which is totally OK for 99.8% of all cases). So your changeListener on the yearField won't get triggered when you load the form. So we have following second option. We overwrite the importFormFieldData call on our yearField:
public class YearField extends AbstractDateField {
{...} //your code
@Override
public void importFormFieldData(AbstractFormFieldData source, boolean valueChangeTriggersEnabled) {
super.importFormFieldData(source, valueChangeTriggersEnabled);
try {
if (!valueChangeTriggersEnabled) {
year2Field.setValueChangeTriggerEnabled(false);
}
year2Field.setValue(YearField.this.getValue());
}
finally {
if (!valueChangeTriggersEnabled) {
year2Field.setValueChangeTriggerEnabled(true);
}
}
}
}
With these two codesnippets your fields will get updated in both cases: if the form is loaded from "importFormData" and if the YearField gets changed in the client.
Please ask if you have any further questions.
Sincerely
Stephan
|
|
|
|
|
|
| Re: Reusing the same formData member on more than one field [message #990022 is a reply to message #987873] |
Mon, 10 December 2012 09:24   |
Urs Beeli Messages: 162 Registered: October 2012 Location: Bern, Switzerland |
Senior Member |
|
|
Actually, having played around with this some more, it seems that the solution suggested by Stephan doesn't work after all (or at least not for all situations).
It does work for the "year1" and "year2" cases. However, when I tried to copy it for another field "total1" and "total2" it fails to work. I debugged it, and what happens is the following:
- total1 is set as part of the importFormData() call
- this triggers both the propertyChange() call and the importFormFieldData() call which copies the total1Field value to total2Field
However, now importFormData() continues, and as my processService did not fill total2 on the form field, importFormData() now overwrites my (propagated and correct) total2Field value with the total2 member of the formData (which is null), so once my form is shown, the corresponding field is empty again 
I guess it makes more sense to use the method proposed by Lukas for the inital population of the form and use the propertyChange listener to propagate changing values during the life time of the form. As it looks now, I can probably forgo overwrite importFieldData().
I'm not quite sure why the same thing works with year1 and year2 (though things are slightly different as there is also a variable year which comes into this, so that might explain it).
Any comments on this?
|
|
|
|
| Re: Reusing the same formData member on more than one field [message #990380 is a reply to message #990067] |
Wed, 12 December 2012 04:02  |
Urs Beeli Messages: 162 Registered: October 2012 Location: Bern, Switzerland |
Senior Member |
|
|
@Jeremie:
Thanks you were spot on! I added the following code to my ModifyHandler.execLoad() method:
formData = service.load(formData);
System.out.println("total1.isValueSet: " + formData.getTotal1().isValueSet());
System.out.println("total2.isValueSet: " + formData.getTotal2().isValueSet());
formData.getTotal2().setValueSet(false);
importFormData(formData);
And it returns true for both fields. Setting valueSet to false for total2 makes the solution using importFormFieldData works again.
Thanks for helping me out with this!
If I understand your last line correctly, best practice would be to do a setValueSet(false) for any formData member which is not set as part of the SQL.selectInto() in the XxxxProcessService.load() method?
|
|
|
Powered by
FUDForum. Page generated in 0.02314 seconds