Search in sources :

Example 1 with InstanceContext

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

the class BlockInjectionProvider method createConduit.

private FieldConduit<Object> createConduit(PlasticField field, final String blockId) {
    final String className = field.getPlasticClass().getClassName();
    final String fieldName = field.getName();
    return new ReadOnlyComponentFieldConduit(className, fieldName) {

        public Object get(Object instance, InstanceContext context) {
            ComponentResources resources = context.get(ComponentResources.class);
            return resources.getBlock(blockId);
        }
    };
}
Also used : ReadOnlyComponentFieldConduit(org.apache.tapestry5.internal.transform.ReadOnlyComponentFieldConduit) InstanceContext(org.apache.tapestry5.plastic.InstanceContext) ComponentResources(org.apache.tapestry5.ComponentResources)

Example 2 with InstanceContext

use of org.apache.tapestry5.plastic.InstanceContext 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 3 with InstanceContext

use of org.apache.tapestry5.plastic.InstanceContext 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 4 with InstanceContext

use of org.apache.tapestry5.plastic.InstanceContext 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 5 with InstanceContext

use of org.apache.tapestry5.plastic.InstanceContext 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)

Aggregations

ComponentResources (org.apache.tapestry5.ComponentResources)6 InternalComponentResources (org.apache.tapestry5.internal.InternalComponentResources)5 InstanceContext (org.apache.tapestry5.plastic.InstanceContext)5 TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)2 ReadOnlyComponentFieldConduit (org.apache.tapestry5.internal.transform.ReadOnlyComponentFieldConduit)2 ComputedValue (org.apache.tapestry5.plastic.ComputedValue)2 PlasticField (org.apache.tapestry5.plastic.PlasticField)2 PersistenceContext (javax.persistence.PersistenceContext)1 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 Path (org.apache.tapestry5.annotations.Path)1 Persist (org.apache.tapestry5.annotations.Persist)1 UnknownValueException (org.apache.tapestry5.commons.util.UnknownValueException)1 Predicate (org.apache.tapestry5.func.Predicate)1 LiteralBinding (org.apache.tapestry5.internal.bindings.LiteralBinding)1 PerThreadValue (org.apache.tapestry5.ioc.services.PerThreadValue)1