Search in sources :

Example 1 with ComputedValue

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

the class AssetInjectionProvider method provideInjection.

public boolean provideInjection(PlasticField field, ObjectLocator locator, MutableComponentModel componentModel) {
    Path path = field.getAnnotation(Path.class);
    if (path == null) {
        return false;
    }
    final String assetPath = path.value();
    final String libraryName = componentModel.getLibraryName();
    ComputedValue<Asset> computedAsset = new ComputedValue<Asset>() {

        public Asset get(InstanceContext context) {
            ComponentResources resources = context.get(ComponentResources.class);
            // a different library name than the subclass).
            return assetSource.getComponentAsset(resources, assetPath, libraryName);
        }
    };
    field.injectComputed(computedAsset);
    return true;
}
Also used : Path(org.apache.tapestry5.annotations.Path) ComputedValue(org.apache.tapestry5.plastic.ComputedValue) InstanceContext(org.apache.tapestry5.plastic.InstanceContext) Asset(org.apache.tapestry5.Asset) ComponentResources(org.apache.tapestry5.ComponentResources)

Example 2 with ComputedValue

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

the class MixinWorker method createMixinFieldProvider.

private ComputedValue<FieldConduit<Object>> createMixinFieldProvider(final String fieldName, final String mixinClassName) {
    return new ComputedValue<FieldConduit<Object>>() {

        public FieldConduit get(InstanceContext context) {
            ComponentResources resources = context.get(ComponentResources.class);
            final InternalComponentResources icr = (InternalComponentResources) resources;
            return new ReadOnlyComponentFieldConduit(resources, fieldName) {

                public Object get(Object instance, InstanceContext context) {
                    return icr.getMixinByClassName(mixinClassName);
                }
            };
        }
    };
}
Also used : InternalComponentResources(org.apache.tapestry5.internal.InternalComponentResources) InternalComponentResources(org.apache.tapestry5.internal.InternalComponentResources) ComponentResources(org.apache.tapestry5.ComponentResources)

Example 3 with ComputedValue

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

the class BindParameterWorker method convertFieldIntoContainerBoundParameter.

private void convertFieldIntoContainerBoundParameter(PlasticField field) {
    BindParameter annotation = field.getAnnotation(BindParameter.class);
    field.claim(annotation);
    final String[] possibleNames = annotation.value();
    final String fieldTypeName = field.getTypeName();
    final String fieldName = field.getName();
    ComputedValue<FieldConduit<Object>> computedConduit = new ComputedValue<FieldConduit<Object>>() {

        public FieldConduit<Object> get(InstanceContext context) {
            ComponentResources resources = context.get(ComponentResources.class);
            try {
                return createConduit(resources, fieldTypeName, fieldName, possibleNames);
            } catch (Exception ex) {
                throw new TapestryException(String.format("Failure binding parameter field '%s' of mixin %s (type %s): %s", fieldName, resources.getCompleteId(), resources.getComponentModel().getComponentClassName(), ExceptionUtils.toMessage(ex)), ex);
            }
        }
    };
    field.setComputedConduit(computedConduit);
}
Also used : BindParameter(org.apache.tapestry5.annotations.BindParameter) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException) UnknownValueException(org.apache.tapestry5.commons.util.UnknownValueException) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException) InternalComponentResources(org.apache.tapestry5.internal.InternalComponentResources) ComponentResources(org.apache.tapestry5.ComponentResources)

Example 4 with ComputedValue

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

the class EnvironmentalWorker method transform.

private void transform(final String componentClassName, PlasticField field) {
    Environmental annotation = field.getAnnotation(Environmental.class);
    field.claim(annotation);
    final String fieldName = field.getName();
    final Class fieldType = classCache.forName(field.getTypeName());
    final boolean required = annotation.value();
    ComputedValue<FieldConduit<Object>> provider = new ComputedValue<FieldConduit<Object>>() {

        public FieldConduit<Object> get(InstanceContext context) {
            return new EnvironmentalConduit(componentClassName, fieldName, fieldType, required);
        }

        public void set(Object instance, InstanceContext context, Object newValue) {
            throw new RuntimeException(String.format("Field %s of component %s is read only.", fieldName, componentClassName));
        }
    };
    field.setComputedConduit(provider);
}
Also used : Environmental(org.apache.tapestry5.annotations.Environmental)

Example 5 with ComputedValue

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

the class ParameterWorker method convertFieldIntoParameter.

private void convertFieldIntoParameter(PlasticClass plasticClass, MutableComponentModel model, PlasticField field) {
    Parameter annotation = field.getAnnotation(Parameter.class);
    String fieldType = field.getTypeName();
    String parameterName = getParameterName(field.getName(), annotation.name());
    field.claim(annotation);
    model.addParameter(parameterName, annotation.required(), annotation.allowNull(), annotation.defaultPrefix(), annotation.cache());
    MethodHandle defaultMethodHandle = findDefaultMethodHandle(plasticClass, parameterName);
    ComputedValue<FieldConduit<Object>> computedParameterConduit = createComputedParameterConduit(parameterName, fieldType, annotation, defaultMethodHandle);
    field.setComputedConduit(computedParameterConduit);
}
Also used : Parameter(org.apache.tapestry5.annotations.Parameter)

Aggregations

ComponentResources (org.apache.tapestry5.ComponentResources)5 InternalComponentResources (org.apache.tapestry5.internal.InternalComponentResources)5 TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)2 ComputedValue (org.apache.tapestry5.plastic.ComputedValue)2 InstanceContext (org.apache.tapestry5.plastic.InstanceContext)2 Asset (org.apache.tapestry5.Asset)1 Binding (org.apache.tapestry5.Binding)1 BindParameter (org.apache.tapestry5.annotations.BindParameter)1 Cached (org.apache.tapestry5.annotations.Cached)1 Environmental (org.apache.tapestry5.annotations.Environmental)1 InjectComponent (org.apache.tapestry5.annotations.InjectComponent)1 InjectContainer (org.apache.tapestry5.annotations.InjectContainer)1 Parameter (org.apache.tapestry5.annotations.Parameter)1 Path (org.apache.tapestry5.annotations.Path)1 Persist (org.apache.tapestry5.annotations.Persist)1 UnknownValueException (org.apache.tapestry5.commons.util.UnknownValueException)1 LiteralBinding (org.apache.tapestry5.internal.bindings.LiteralBinding)1 PerThreadValue (org.apache.tapestry5.ioc.services.PerThreadValue)1 Component (org.apache.tapestry5.runtime.Component)1