If you want a reusable solution, you could contribute your own custom
binding prefix, so you can use the following in your .tml files:
${enum:object.enumValue}
And then in your .properties message files:
EnumName.ENUM=Enum message
----
1. Extend the AbstractBinding class: (maybe it can also be done by using
the TapestryInternalUtils method you mentioned instead of our
implementation here)
public class EnumBinding extends AbstractBinding {
private final Messages messages;
private final Binding valueBinding;
private final TypeCoercer coercer;
public EnumBinding(Messages messages, Binding valueBinding,
TypeCoercer coercer) {
this.messages = messages;
this.valueBinding = valueBinding;
this.coercer = coercer;
}
@Override
public Object get() {
Object rawValue = valueBinding.get();
if (rawValue instanceof Enum<?>) {
Enum<?> value = (Enum<?>)rawValue;
String simpleName = value.getDeclaringClass().getSimpleName();
return messages.get(simpleName + "." +
coercer.coerce(value, String.class));
} else {
String simpleName = rawValue.getClass().getSimpleName();
return messages.get(simpleName + "." +
coercer.coerce(rawValue, String.class));
}
}
@Override
public boolean isInvariant() {
return valueBinding.isInvariant();
}
@Override
public Class<?> getBindingType() {
return String.class;
}
}
----
2. Create a BindingFactory implementation:
public class EnumBindingFactory implements BindingFactory {
private final BindingSource bindingSource;
private final TypeCoercer coercer;
public EnumBindingFactory(BindingSource bindingSource, TypeCoercer
typeCoercer) {
this.bindingSource = bindingSource;
this.coercer = typeCoercer;
}
@Override
public Binding newBinding(
String description,
ComponentResources container,
ComponentResources component,
String expression,
Location location) {
Binding valueBinding = bindingSource.newBinding(
description, container, component,
BindingConstants.PROP, expression, location);
return new EnumBinding(container.getMessages(), valueBinding,
coercer);
}
}
----
3. Bind the factory service and contribute the new binding in your
AppModule class like following:
public static void bind(ServiceBinder binder) {
binder.bind(BindingFactory.class,
EnumBindingFactory.class).withId("EnumBindingFactory");
}
public static void contributeBindingSource(
MappedConfiguration<String, BindingFactory> configuration,
@InjectService("EnumBindingFactory") BindingFactory
enumBindingFactory) {
configuration.add("enum", enumBindingFactory);
}
Op 26/08/2021 om 04:30 schreef Paul Stanton:
Using ${object.enumValue} renders the enum in raw form ie ENUM1
How do I leverage "TapestryInternalUtils.getLabelForEnum" without
having to use a BeanDisplay or Grid etc?
Or is there super easy way to just render the Enum value formatted
without rendering any wrapping html etc?
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org