If
not, Tapestry will take the component's unique ID and make a reasonable label from
it. To enable all this clever functionality, all we need to do is to add the following
snippet to the component class:
@Inject
private ComponentResources resources;
@Inject
private ComponentDefaultProvider defaultProvider;
String defaultLabel()
???
Chapter 8
[ 219 ]
{
return defaultProvider.defaultLabel(resources);
}
As for the getClientId method, the value returned by it should be unique within
the page. The simplest way to ensure this is to ask Tapestry to give us this ID,
like this:
public String getClientId()
{
return resources.getId();
}
Finally, we need to provide a way to make the component disabled, which means
creating a boolean property. We can then return the value of this property:
@Parameter
private boolean disabled;
public boolean isDisabled()
{
return disabled;
}
But this is not enough. When the DateInput component is disabled, the Select
components that it is made of should be disabled too, but this is easy. Modify the
DateInput.tml template to look like this:
t:encoder="encoder" t:disabled="disabled"/>
t:disabled="disabled"/>
t:encoder="encoder" t:disabled="disabled"/>
Finally, don't forget to declare that the DateInput component implements the
Field interface:
public class DateInput implements Field
Now, having the DateInput component declared like this:
Date test:
Creating Custom Components
[ 220 ]
you will see that it displays properly, and the value for its label is taken from the
component's ID:
Having the component's label displayed properly is one of the benefits of making
it an implementation of Field.
Pages:
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275