01: /**
02: *
03: */package nl.hippo.cms.forms.event;
04:
05: import java.util.Iterator;
06:
07: import nl.hippo.cms.wizard.Constants;
08:
09: import org.apache.cocoon.forms.event.ValueChangedEvent;
10: import org.apache.cocoon.forms.event.ValueChangedListener;
11: import org.apache.cocoon.forms.formmodel.Field;
12: import org.apache.cocoon.forms.formmodel.Widget;
13:
14: /**
15: * Reset the previous resource dropdown when tab is changed and an event is invoked.
16: * This is needed to make sure validation doesn't fail because of required fields still visible
17: * in a hidden tab.
18: *
19: * @author a.bogaart@hippo.nl
20: *
21: */
22: public class ResetOldResourceDropdown implements ValueChangedListener {
23:
24: /**
25: * The CForms tabs implementation stores the index of the visible group as an int in the container element.
26: *
27: * @see org.apache.cocoon.forms.event.ValueChangedListener#valueChanged(org.apache.cocoon.forms.event.ValueChangedEvent)
28: */
29: public void valueChanged(ValueChangedEvent valuechangedevent) {
30: String newValue = (String) valuechangedevent.getNewValue();
31: int newWidgetIndex = (newValue != null && !newValue.equals("")) ? new Integer(
32: newValue).intValue()
33: : -1;
34:
35: Iterator it = valuechangedevent.getSourceWidget().getForm()
36: .getChildren();
37: int currentWidgetIndex = 0;
38: while (it.hasNext()) {
39: Widget widget = (Widget) it.next();
40: if (widget.getDefinition().getId().endsWith(
41: Constants.CFORMS_RESOURCE_DROPDOWN_SUFFIX)
42: && widget instanceof Field) {
43: Field field = (Field) widget;
44: if (field.isRequired()) {
45: field.setValue("");
46: field.setRequired(false);
47: } else if (newWidgetIndex == currentWidgetIndex) {
48: field.setRequired(true);
49: }
50: currentWidgetIndex++;
51: }
52: }
53: }
54: }
|