In this tutorial you will learn how to create your own FormComponentInterceptor
, by integrating xswingx. The xswingx project provides input prompts.
As our new interceptor will intercept text components, we can subclass the TextComponentInterceptor
class, which defines a protected void processComponent(String propertyName, JTextComponent textComponent)
method:
public class PromptTextFieldFormComponentInterceptor extends TextComponentInterceptor {
The following attributes are needed. The MessageSource
to fetch the prompt text, the FormModel
to scope our message key, and the prompt key to construct the message key:
private MessageSource messageSource;
private FormModel formModel;
private String promptKey;
public PromptTextFieldFormComponentInterceptor(FormModel formModel, MessageSource messageSource,
String promptKey) {
this.formModel = formModel;
this.messageSource = messageSource;
this.promptKey = promptKey;
}
The processComponent
method will use the MessageSource
to find the prompt text (using the keys created by the getMessageKeys
method), and will use the xswingx PromptSupport
class to install the prompt on the JTextComponent
:
protected void processComponent(String propertyName, JTextComponent textComponent) {
String prompt = messageSource.getMessage(new DefaultMessageSourceResolvable(getMessageKeys(formModel,
propertyName), ""), Locale.getDefault());
if (StringUtils.hasText(prompt)) {
PromptSupport.setPrompt(prompt, textComponent);
}
}
And finally the getMessageKeys
method, which will create an array of message keys used to load the message. Two keys will be created, formModelId.propertyName.promptKey
and propertyName.promptKey
:
protected String[] getMessageKeys(FormModel formModel, String propertyName) {
return new String[] { formModel.getId() + "." + propertyName + "." + promptKey,
propertyName + "." + promptKey };
}
}
Now define the interceptor in the formComponentInterceptorFactory
bean in your application context:
<bean class="org.springframework.richclient.form.PromptTextFieldFormComponentInterceptorFactory"/>
And add some messages to messages.properties
:
firstName.prompt=<Enter the first name>
lastName.prompt=<Enter the last name>