ugo 2003/11/03 15:16:12
Modified: src/blocks/woody/java/org/apache/cocoon/woody/formmodel
AbstractDatatypeWidgetDefinition.java Field.java
src/blocks/woody/java/org/apache/cocoon/woody/datatype
FlowJXPathSelectionList.java
DynamicSelectionList.java
Log:
Added a method for programmatically setting a field's selection list from an
in-memory collection at runtime.
Revision Changes Path
1.4 +28 -2
cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/formmodel/AbstractDatatypeWidgetDefinition.java
Index: AbstractDatatypeWidgetDefinition.java
===================================================================
RCS file:
/home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/formmodel/AbstractDatatypeWidgetDefinition.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- AbstractDatatypeWidgetDefinition.java 25 Sep 2003 17:37:30 -0000
1.3
+++ AbstractDatatypeWidgetDefinition.java 3 Nov 2003 23:16:12 -0000
1.4
@@ -50,11 +50,15 @@
*/
package org.apache.cocoon.woody.formmodel;
+import org.apache.avalon.framework.context.Context;
+import org.apache.avalon.framework.context.ContextException;
+import org.apache.avalon.framework.context.Contextualizable;
import org.apache.avalon.framework.service.ServiceException;
import org.apache.avalon.framework.service.ServiceManager;
import org.apache.avalon.framework.service.Serviceable;
import org.apache.cocoon.woody.datatype.Datatype;
import org.apache.cocoon.woody.datatype.DynamicSelectionList;
+import org.apache.cocoon.woody.datatype.FlowJXPathSelectionList;
import org.apache.cocoon.woody.datatype.SelectionList;
import org.apache.cocoon.woody.event.ValueChangedEvent;
import org.apache.cocoon.woody.event.ValueChangedListener;
@@ -63,16 +67,21 @@
/**
* Base class for WidgetDefinitions that use a Datatype and SelectionList.
*/
-public abstract class AbstractDatatypeWidgetDefinition extends
AbstractWidgetDefinition implements Serviceable {
+public abstract class AbstractDatatypeWidgetDefinition extends
AbstractWidgetDefinition implements Serviceable, Contextualizable {
private Datatype datatype;
private SelectionList selectionList;
private ValueChangedListener listener;
private ServiceManager manager;
+ private Context context;
public void service(ServiceManager manager) throws ServiceException {
this.manager = manager;
}
+ public void contextualize(Context context) throws ContextException {
+ this.context = context;
+ }
+
public Datatype getDatatype() {
return datatype;
}
@@ -92,12 +101,29 @@
}
/**
- * Builds a dynamic selection list from a url. This is a helper method
for widget instances whose selection
+ * Builds a dynamic selection list from a source. This is a helper
method for widget instances whose selection
* list source has to be changed dynamically, and it does not modify
this definition's selection list,
* if any.
+ * @param uri The URI of the source.
*/
public SelectionList buildSelectionList(String uri) {
return new DynamicSelectionList(datatype, uri, this.manager);
+ }
+
+ /**
+ * Builds a dynamic selection list from an in-memory collection.
+ * This is a helper method for widget instances whose selection
+ * list has to be changed dynamically, and it does not modify this
definition's selection list,
+ * if any.
+ * @see org.apache.cocoon.woody.formmodel.Field#setSelectionList(Object
model, String valuePath, String labelPath)
+ * @param model The collection used as a model for the selection list.
+ * @param keyPath An XPath expression referring to the attribute used
+ * to populate the values of the list's items.
+ * @param labelPath An XPath expression referring to the attribute used
+ * to populate the labels of the list's items.
+ */
+ public SelectionList buildSelectionListFromModel(Object model, String
valuePath, String labelPath) {
+ return new FlowJXPathSelectionList(model, valuePath, labelPath,
datatype);
}
public void addValueChangedListener(ValueChangedListener listener) {
1.15 +34 -1
cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/formmodel/Field.java
Index: Field.java
===================================================================
RCS file:
/home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/formmodel/Field.java,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- Field.java 24 Oct 2003 22:49:08 -0000 1.14
+++ Field.java 3 Nov 2003 23:16:12 -0000 1.15
@@ -299,6 +299,10 @@
definition.generateLabel(contentHandler);
}
+ /**
+ * Set this field's selection list.
+ * @param selectionList The new selection list.
+ */
public void setSelectionList(SelectionList selectionList) {
if (selectionList != null &&
selectionList.getDatatype() != null &&
@@ -309,8 +313,37 @@
this.selectionList = selectionList;
}
+ /**
+ * Read this field's selection list from an external source.
+ * All Cocoon-supported protocols can be used.
+ * The format of the XML produced by the source should be the
+ * same as in case of inline specification of the selection list,
+ * thus the root element should be a <code>wd:selection-list</code>
+ * element.
+ * @param uri The URI of the source.
+ */
public void setSelectionList(String uri) {
setSelectionList(this.definition.buildSelectionList(uri));
+ }
+
+ /**
+ * Set this field's selection list using values from an in-memory
+ * object. The <code>object</code> parameter should point to a collection
+ * (Java collection or array, or Javascript array) of objects. Each
object
+ * belonging to the collection should have a <em>value</em> property and
a
+ * <em>label</em> property, whose values are used to specify the
<code>value</code>
+ * attribute and the contents of the <code>wd:label</code> child element
+ * of every <code>wd:item</code> in the list.
+ * <p>Access to the values of the above mentioned properties is done
+ * via <a
href="http://jakarta.apache.org/commons/jxpath/users-guide.html">XPath</a>
expressions.
+ * @param model The collection used as a model for the selection list.
+ * @param keyPath An XPath expression referring to the attribute used
+ * to populate the values of the list's items.
+ * @param labelPath An XPath expression referring to the attribute used
+ * to populate the labels of the list's items.
+ */
+ public void setSelectionList(Object model, String valuePath, String
labelPath) {
+ setSelectionList(this.definition.buildSelectionListFromModel(model,
valuePath, labelPath));
}
public Datatype getDatatype() {
1.2 +37 -9
cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/FlowJXPathSelectionList.java
Index: FlowJXPathSelectionList.java
===================================================================
RCS file:
/home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/FlowJXPathSelectionList.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- FlowJXPathSelectionList.java 22 Oct 2003 20:22:07 -0000 1.1
+++ FlowJXPathSelectionList.java 3 Nov 2003 23:16:12 -0000 1.2
@@ -77,6 +77,7 @@
private String valuePath;
private String labelPath;
private Datatype datatype;
+ private Object model;
public FlowJXPathSelectionList(Context context, String listPath, String
valuePath, String labelPath, Datatype datatype) {
this.context = context;
@@ -85,27 +86,54 @@
this.labelPath = labelPath;
this.datatype = datatype;
}
+
+ /**
+ * Builds a dynamic selection list from an in-memory collection.
+ * @see org.apache.cocoon.woody.formmodel.Field#setSelectionList(Object
model, String valuePath, String labelPath)
+ * @param model The collection used as a model for the selection list.
+ * @param keyPath An XPath expression referring to the attribute used
+ * to populate the values of the list's items.
+ * @param labelPath An XPath expression referring to the attribute used
+ * to populate the labels of the list's items.
+ */
+ public FlowJXPathSelectionList(Object model, String valuePath, String
labelPath, Datatype datatype) {
+ this.model = model;
+ this.valuePath = valuePath;
+ this.labelPath = labelPath;
+ this.datatype = datatype;
+ }
public Datatype getDatatype() {
return this.datatype;
}
public void generateSaxFragment(ContentHandler contentHandler, Locale
locale) throws SAXException {
+
+ JXPathContext ctx = null;
+ Iterator iter = null;
+ if (model == null) {
+ Object flowData =
FlowHelper.getContextObject(ContextHelper.getObjectModel(this.context));
+
+ if (flowData == null) {
+ throw new SAXException("No flow data to produce selection
list");
+ }
+ // Move to the list location
+ ctx = JXPathContext.newContext(flowData);
- Object flowData =
FlowHelper.getContextObject(ContextHelper.getObjectModel(this.context));
+ // Iterate on all elements of the list
+ iter = ctx.iteratePointers(this.listPath);
+ }
+ else {
+ // Move to the list location
+ ctx = JXPathContext.newContext(model);
- if (flowData == null) {
- throw new SAXException("No flow data to produce selection list");
+ // Iterate on all elements of the list
+ iter = ctx.iteratePointers(".");
}
// Start the selection-list
contentHandler.startElement(Constants.WI_NS, SELECTION_LIST_EL,
Constants.WI_PREFIX_COLON + SELECTION_LIST_EL, Constants.EMPTY_ATTRS);
- // Move to the list location
- JXPathContext ctx = JXPathContext.newContext(flowData);
-
- // Iterate on all elements of the list
- Iterator iter = ctx.iteratePointers(this.listPath);
while(iter.hasNext()) {
// Get a context on the current item
1.5 +1 -1
cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/DynamicSelectionList.java
Index: DynamicSelectionList.java
===================================================================
RCS file:
/home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/DynamicSelectionList.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- DynamicSelectionList.java 11 Oct 2003 15:57:00 -0000 1.4
+++ DynamicSelectionList.java 3 Nov 2003 23:16:12 -0000 1.5
@@ -98,7 +98,7 @@
void generateSaxFragment(ContentHandler contentHandler, Locale locale,
Source source) throws ProcessingException, SAXException, IOException {
SelectionListHandler handler = new SelectionListHandler(locale);
handler.setContentHandler(contentHandler);
- SourceUtil.toSAX(source, handler);
+ SourceUtil.toSAX(serviceManager, source, null, handler);
}
public void generateSaxFragment(ContentHandler contentHandler, Locale
locale) throws SAXException {