-
Aug 19th, 2010, 07:57 AM
#1
Converter from Character to String solution
Finally found out the solution.
Two converters needed, fist one:
public class CharacterToString extends AbstractFormattingConverter {
private final boolean allowEmpty;
protected CharacterToString(FormatterFactory formatterFactory, boolean allowEmpty) {
super(formatterFactory);
this.allowEmpty = allowEmpty;
}
public CharacterToString(){
this(new SimpleFormatterFactory(),true);
}
public Class[] getSourceClasses() {
return new Class[] {Character.class};
}
public Class[] getTargetClasses() {
return new Class[] {String.class};
}
@Override
protected Object doConvert(Object source, Class targetClass, ConversionContext context) throws Exception {
return (!allowEmpty || source!=null) ? source.toString() : null;
}
}
The second one:
public class StringToCharacter extends AbstractFormattingConverter {
private final boolean allowEmpty;
protected StringToCharacter(FormatterFactory formatterFactory, boolean allowEmpty) {
super(formatterFactory);
this.allowEmpty = allowEmpty;
}
public StringToCharacter(){
this(new SimpleFormatterFactory(),true);
}
public Class[] getSourceClasses() {
return new Class[] {String.class};
}
public Class[] getTargetClasses() {
return new Class[] {Character.class};
}
@Override
protected Object doConvert(Object source, Class targetClass, ConversionContext context) throws Exception {
String stringSource = String.valueOf(source);
char[] targetArray;
Character target = null;
if(!allowEmpty || stringSource != null){
targetArray = stringSource.toCharArray();
target = targetArray[0];
}
//return (!allowEmpty || source!=null) ? source.toString() : null;
return target;
}
}
And the registration in the rich-client-context.xml
<bean id="conversionService" class="org.springframework.richclient.application. DefaultConversionServiceFactoryBean" />
<bean class="org.springframework.beans.factory.config.Me thodInvokingFactoryBean" >
<property name="targetObject" ref="conversionService" />
<property name="targetMethod" value="addConverters" />
<property name="arguments">
<list>
<list>
<bean class="converter.CharacterToString" />
<bean class="converter.StringToCharacter" />
</list>
</list>
</property>
</bean>
Hope it helps, marekto2
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules