Scout/Concepts/Template
From Eclipsepedia
| Scout |
| Wiki Home |
| Website |
| Download • Git |
| Community |
| Forums • Blog • Twitter |
| Bugzilla |
| Bugzilla |
A template is some portion of code (a class) that is defined to be used many times in the Client.
Contents |
Overview
The templates are visible in the Explorer View under your scout project > Client > Templates
Form Field template
An easy way to reuse form field code is to use templates. Templates are implemented as abstract classes that may be extended by form fields. Creation and usage of templates is supported by the Scout SDK.
Templates may be extracted from existing fields or other templates. The Scout SDK template support allows for quite powerful refactorings and helps keeping your code clean and DRY with little effort.
Example
Consider the following example: A group box for the billing address containing some fields.
@Order(10.0)
public class BillingAddressBox extends AbstractGroupBox {
@Override
protected String getConfiguredLabel() {
return TEXTS.get("BillingAddress");
}
@Order(10.0)
public class StreetField extends AbstractStringField {
@Override
protected String getConfiguredLabel() {
return TEXTS.get("Street");
}
}
@Order(20.0)
public class CityField extends AbstractSmartField<Long> {
@Override
protected Class<? extends ICodeType<?>> getConfiguredCodeType() {
return CityCodeType.class;
}
@Override
protected String getConfiguredLabel() {
return TEXTS.get("City");
}
}
}
Now let's assume you would like to create a similar box for the correspondence address without copying the code. This is possible by selecting "Create template..." on the group box.
A new abstract class is created containing the code of BillingAddressBox. To make the AddressBox template more useful, we move the configured label code to the BillingAddressBox.
...
@FormData(value = AbstractAddressBoxData.class, sdkCommand = SdkCommand.CREATE, defaultSubtypeSdkCommand = DefaultSubtypeSdkCommand.CREATE)
public abstract class AbstractAddressBox extends AbstractGroupBox {
/* @Override
protected String getConfiguredLabel() {
return TEXTS.get("BillingAddress");
} */
public CityField getCityField() {
return getFieldByClass(CityField.class);
}
...
...
@Order(10.0)
public class BillingAddressBox extends AbstractAddressBox {
@Override
protected String getConfiguredLabel() {
return TEXTS.get("BillingAddress");
}
}
...
Now the correspondance address field can be created by choosing the template as type for the new field.
@Order(20.0)
public class CorrespondanceAddressBox extends AbstractAddressBox {
@Override
protected String getConfiguredLabel() {
return TEXTS.get("Correspondance");
}
}


