Designer 10.5 | webMethods Mobile Development | Code Snippets | Configuring a Content Adapter to Use Multiple Templates
 
Configuring a Content Adapter to Use Multiple Templates
In a Content Adapter, you can declare to use multiple templates. This is useful if the selected data source contains different types of elements. Each type can then use a dedicated template to display its data.
*To add multiple templates to a content adapter
1. Ensure the mobile project is open in the Outline Editor. For instructions, see Displaying a Mobile Project in the Outline Editor.
2. In the Model section of the Outline Editor, expand the project so that you view the Content Adapter where you want to add multiple templates.
3. Right-click and select New Child > TemplateAssignment. Select the template you want to use from the drop-down list. If you need to create a new template, refer to Creating a Template for a Custom Object.
4. Enter the filter expression for this type of element. This String is required later to identify the different types of elements from the data source. The String should be unique across all TemplatesAssignments within that Content Adapter.
5. Repeat steps 3 and 4 for all different types of elements that you want to display. See below for an example of a model using 3 different templates.
6. Open the Java Source file for the data source that you defined in the Content Adapter.
7. Override the method public boolean matchesExpression(String filterExpression, int elementIndex). This method is called for each element of the data source to identify which template should be used for displaying it.
a. The parameter filterExpression is one of the filter expression that you just defined in the template assignment.
b. The parameter elementIndex defines the index of the element in the data source. You can use the methods getEntityForIndex(int) (in an EntityDatasource) or getElementAtIndex(int) (in a ListDatasource) to get the element at this position.
The following example shows how to implement this method.
public boolean matchesExpression(final String filterExpr, final int elementIndex) {
final IEntity entity = getEntityForIndex(elementIndex);
if ((entity instanceof TaskTypeImpl) && "task".equalsIgnoreCase(filterExpr)) {
return true;
} else if ((entity instanceof ProcessTypeImpl)
&& "process".equalsIgnoreCase(filterExpr)) {
return true;
} else if ((entity instanceof SectionTitleImpl)
&& "section".equalsIgnoreCase(filterExpr)) {
return true;
}
return false;
}
Related Topics