Search in sources :

Example 1 with FieldTranslator

use of org.apache.tapestry5.FieldTranslator in project tapestry-5 by apache.

the class ComponentDefaultProviderImplTest method default_translator.

@Test
public void default_translator() {
    ComponentResources resources = mockComponentResources();
    FieldTranslator translator = mockFieldTranslator();
    FieldTranslatorSource source = newMock(FieldTranslatorSource.class);
    train_createDefaultTranslator(source, resources, "object", translator);
    replay();
    ComponentDefaultProvider provider = new ComponentDefaultProviderImpl(null, null, null, source, null);
    assertSame(provider.defaultTranslator("object", resources), translator);
    verify();
}
Also used : FieldTranslatorSource(org.apache.tapestry5.services.FieldTranslatorSource) FieldTranslator(org.apache.tapestry5.FieldTranslator) ComponentResources(org.apache.tapestry5.ComponentResources) ComponentDefaultProvider(org.apache.tapestry5.services.ComponentDefaultProvider) Test(org.testng.annotations.Test)

Example 2 with FieldTranslator

use of org.apache.tapestry5.FieldTranslator in project tapestry-5 by apache.

the class FieldTranslatorSourceImpl method createDefaultTranslator.

public FieldTranslator createDefaultTranslator(ComponentResources resources, String parameterName) {
    assert resources != null;
    assert InternalUtils.isNonBlank(parameterName);
    Field field = (Field) resources.getComponent();
    Class propertyType = resources.getBoundType(parameterName);
    return createDefaultTranslator(field, resources.getId(), resources.getContainerMessages(), null, propertyType, resources.getAnnotationProvider(parameterName));
}
Also used : Field(org.apache.tapestry5.Field)

Example 3 with FieldTranslator

use of org.apache.tapestry5.FieldTranslator in project tapestry-5 by apache.

the class FieldTranslatorSourceImpl method createTranslator.

public FieldTranslator createTranslator(ComponentResources resources, String translatorName) {
    assert resources != null;
    assert InternalUtils.isNonBlank(translatorName);
    Field field = (Field) resources.getComponent();
    Translator translator = translatorSource.get(translatorName);
    return createTranslator(field, resources.getId(), resources.getContainerMessages(), null, translator);
}
Also used : Field(org.apache.tapestry5.Field) FieldTranslator(org.apache.tapestry5.FieldTranslator) Translator(org.apache.tapestry5.Translator)

Example 4 with FieldTranslator

use of org.apache.tapestry5.FieldTranslator in project tapestry-5 by apache.

the class PropertyEditor method setupEnvironment.

/**
 * Creates a {@link org.apache.tapestry5.services.PropertyEditContext} and pushes it onto the {@link
 * org.apache.tapestry5.services.Environment} stack.
 */
void setupEnvironment(final String propertyName) {
    propertyModel = model.get(propertyName);
    PropertyEditContext context = new PropertyEditContext() {

        public Messages getContainerMessages() {
            return overrides.getOverrideMessages();
        }

        public String getLabel() {
            return propertyModel.getLabel();
        }

        public String getPropertyId() {
            return propertyModel.getId();
        }

        public Class getPropertyType() {
            return propertyModel.getPropertyType();
        }

        public Object getPropertyValue() {
            return propertyModel.getConduit().get(object);
        }

        public FieldTranslator getTranslator(Field field) {
            return fieldTranslatorSource.createDefaultTranslator(field, propertyName, overrides.getOverrideMessages(), locale, propertyModel.getPropertyType(), propertyModel.getConduit());
        }

        public FieldValidator getValidator(Field field) {
            return fieldValidatorDefaultSource.createDefaultValidator(field, propertyName, overrides.getOverrideMessages(), locale, propertyModel.getPropertyType(), propertyModel.getConduit());
        }

        public void setPropertyValue(Object value) {
            propertyModel.getConduit().set(object, value);
        }

        public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
            return propertyModel.getAnnotation(annotationClass);
        }
    };
    environment.push(PropertyEditContext.class, context);
    BeanValidationContext beanValidationContext = environment.peek(BeanValidationContext.class);
    if (beanValidationContext != null) {
        beanValidationContext.setCurrentProperty(propertyName);
    }
    heartbeat.begin();
}
Also used : PropertyEditContext(org.apache.tapestry5.services.PropertyEditContext) BeanValidationContext(org.apache.tapestry5.internal.BeanValidationContext) Annotation(java.lang.annotation.Annotation)

Example 5 with FieldTranslator

use of org.apache.tapestry5.FieldTranslator in project tapestry-5 by apache.

the class FieldTranslatorSourceImplTest method create_default_translator_with_name_and_null_key.

@Test
public void create_default_translator_with_name_and_null_key() {
    Field field = mockField();
    Messages messages = mockMessages();
    Locale locale = Locale.ENGLISH;
    Class propertyType = Map.class;
    TranslatorSource ts = mockTranslatorSource();
    FormSupport fs = mockFormSupport();
    Translator translator = mockTranslator("maptrans", Map.class);
    Messages globalMessages = mockMessages();
    MessageFormatter formatter = mockMessageFormatter();
    MarkupWriter writer = mockMarkupWriter();
    String label = "Field Label";
    String message = "Woops, did it again.";
    AnnotationProvider ap = mockAnnotationProvider(null);
    train_findByType(ts, propertyType, translator);
    train_getFormValidationId(fs, "myform");
    train_contains(messages, "myform-myfield-maptrans-message", false);
    train_contains(messages, "myfield-maptrans-message", false);
    train_getMessageKey(translator, null);
    train_getMessageFormatter(globalMessages, "maptrans-message", formatter);
    train_getLabel(field, label);
    train_format(formatter, message, label);
    translator.render(field, message, writer, fs);
    replay();
    FieldTranslatorSource source = new FieldTranslatorSourceImpl(ts, globalMessages, fs);
    FieldTranslator ft = source.createDefaultTranslator(field, "myfield", messages, locale, propertyType, ap);
    assertEquals(ft.getType(), Map.class);
    ft.render(writer);
    verify();
}
Also used : Locale(java.util.Locale) Messages(org.apache.tapestry5.commons.Messages) AnnotationProvider(org.apache.tapestry5.commons.AnnotationProvider) FieldTranslator(org.apache.tapestry5.FieldTranslator) MessageFormatter(org.apache.tapestry5.commons.MessageFormatter) MarkupWriter(org.apache.tapestry5.MarkupWriter) FormSupport(org.apache.tapestry5.services.FormSupport) TranslatorSource(org.apache.tapestry5.services.TranslatorSource) FieldTranslatorSource(org.apache.tapestry5.services.FieldTranslatorSource) Field(org.apache.tapestry5.Field) FieldTranslator(org.apache.tapestry5.FieldTranslator) Translator(org.apache.tapestry5.Translator) FieldTranslatorSource(org.apache.tapestry5.services.FieldTranslatorSource) Map(java.util.Map) Test(org.testng.annotations.Test)

Aggregations

FieldTranslator (org.apache.tapestry5.FieldTranslator)8 FieldTranslatorSource (org.apache.tapestry5.services.FieldTranslatorSource)7 Test (org.testng.annotations.Test)7 Field (org.apache.tapestry5.Field)6 Translator (org.apache.tapestry5.Translator)6 MarkupWriter (org.apache.tapestry5.MarkupWriter)5 MessageFormatter (org.apache.tapestry5.commons.MessageFormatter)5 Messages (org.apache.tapestry5.commons.Messages)5 FormSupport (org.apache.tapestry5.services.FormSupport)5 TranslatorSource (org.apache.tapestry5.services.TranslatorSource)5 Locale (java.util.Locale)4 Map (java.util.Map)4 AnnotationProvider (org.apache.tapestry5.commons.AnnotationProvider)4 ComponentResources (org.apache.tapestry5.ComponentResources)3 Annotation (java.lang.annotation.Annotation)1 Binding (org.apache.tapestry5.Binding)1 Location (org.apache.tapestry5.commons.Location)1 StringInternerImpl (org.apache.tapestry5.commons.internal.services.StringInternerImpl)1 BeanValidationContext (org.apache.tapestry5.internal.BeanValidationContext)1 FieldComponent (org.apache.tapestry5.root.FieldComponent)1