Search in sources :

Example 1 with PropertyEditContext

use of org.apache.tapestry5.services.PropertyEditContext 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 2 with PropertyEditContext

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

the class PropertyEditor method beginRender.

/**
 * Returns a Block for rendering the property. The Block will be able to access the {@link PropertyEditContext} via
 * the {@link Environmental} annotation.
 */
Block beginRender() {
    Block override = overrides.getOverrideBlock(propertyModel.getId());
    if (override != null) {
        return override;
    }
    String dataType = propertyModel.getDataType();
    if (dataType == null)
        throw new RuntimeException(String.format("The data type for property '%s' of %s is null.", propertyModel.getPropertyName(), object));
    try {
        return beanBlockSource.getEditBlock(dataType);
    } catch (RuntimeException ex) {
        String message = messages.format("core-block-error", propertyModel.getPropertyName(), dataType, object, ex);
        throw new TapestryException(message, resources.getLocation(), ex);
    }
}
Also used : TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException)

Aggregations

Annotation (java.lang.annotation.Annotation)1 TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)1 BeanValidationContext (org.apache.tapestry5.internal.BeanValidationContext)1 PropertyEditContext (org.apache.tapestry5.services.PropertyEditContext)1