01: package org.apache.cocoon.forms.binding;
02:
03: import org.apache.cocoon.forms.formmodel.Widget;
04: import org.apache.cocoon.forms.formmodel.tree.Tree;
05: import org.apache.cocoon.forms.formmodel.tree.TreeModel;
06: import org.apache.commons.jxpath.JXPathContext;
07:
08: public class TreeModelJXPath extends JXPathBindingBase {
09:
10: /**
11: * The xpath expression to the objectModel property
12: */
13: private final String xpath;
14:
15: /**
16: * The id of the CForms form-widget
17: */
18: private final String fieldId;
19:
20: /**
21: * Constructs FieldJXPathBinding.
22: */
23: public TreeModelJXPath(
24: JXPathBindingBuilderBase.CommonAttributes commonAtts,
25: String widgetId, String xpath) {
26: super (commonAtts);
27: this .fieldId = widgetId;
28: this .xpath = xpath;
29: }
30:
31: public String getId() {
32: return fieldId;
33: }
34:
35: /**
36: * Actively performs the binding from the ObjectModel wrapped in a jxpath
37: * context to the CForms-form-widget specified in this object.
38: */
39: public void doLoad(Widget frmModel, JXPathContext jxpc)
40: throws BindingException {
41: Widget widget = selectWidget(frmModel, this .fieldId);
42: if (widget == null) {
43: throw new BindingException(
44: "The widget with the ID ["
45: + this .fieldId
46: + "] referenced in the binding does not exist in the form definition.");
47: }
48: if (!(widget instanceof Tree))
49: throw new BindingException("Widget " + this .fieldId
50: + " is not a Tree!");
51:
52: Object value = jxpc.getValue(this .xpath);
53: if (value != null) {
54: if (!(value instanceof TreeModel))
55: throw new BindingException("Value found in "
56: + this .xpath
57: + " is not a TreeModel, instead it is a "
58: + value.getClass().getName());
59: ((Tree) widget).setModel((TreeModel) value);
60: }
61: }
62:
63: public void doSave(Widget frmModel, JXPathContext jxpc)
64: throws BindingException {
65: // Does nothing
66: }
67:
68: }
|