Search in sources :

Example 1 with EmbeddedComponentModel

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

the class PageLoaderImpl method startComponent.

private EmbeddedComponentAssembler startComponent(AssemblerContext context) {
    StartComponentToken token = context.next(StartComponentToken.class);
    ComponentAssembler assembler = context.assembler;
    String elementName = token.getElementName();
    // Initial guess: the type from the token (but this may be null in many cases).
    String embeddedType = token.getComponentType();
    // This may be null for an anonymous component.
    String embeddedId = token.getId();
    String embeddedComponentClassName = null;
    final EmbeddedComponentModel embeddedModel = embeddedId == null ? null : assembler.getModel().getEmbeddedComponentModel(embeddedId);
    if (embeddedId == null)
        embeddedId = assembler.generateEmbeddedId(embeddedType);
    if (embeddedModel != null) {
        String modelType = embeddedModel.getComponentType();
        if (InternalUtils.isNonBlank(modelType) && embeddedType != null) {
            throw new TapestryException(String.format("Embedded component '%s' provides a type attribute in the template ('%s') " + "as well as in the component class ('%s'). You should not provide a type attribute " + "in the template when defining an embedded component within the component class.", embeddedId, embeddedType, modelType), token, null);
        }
        embeddedType = modelType;
        embeddedComponentClassName = embeddedModel.getComponentClassName();
    }
    String componentClassName = embeddedComponentClassName;
    if (InternalUtils.isNonBlank(embeddedType)) {
        try {
            componentClassName = componentClassResolver.resolveComponentTypeToClassName(embeddedType);
        } catch (RuntimeException ex) {
            throw new TapestryException(ex.getMessage(), token, ex);
        }
    }
    // OK, now we can record an action to get it instantiated.
    EmbeddedComponentAssembler embeddedAssembler = assembler.createEmbeddedAssembler(embeddedId, componentClassName, embeddedModel, token.getMixins(), token.getLocation());
    addActionForEmbeddedComponent(context, embeddedAssembler, embeddedId, elementName, componentClassName);
    addParameterBindingActions(context, embeddedAssembler, embeddedModel);
    if (embeddedModel != null && embeddedModel.getInheritInformalParameters()) {
        // Another two-step: The first "captures" the container and embedded component. The second
        // occurs at the end of the page setup.
        assembler.add(new PageAssemblyAction() {

            public void execute(PageAssembly pageAssembly) {
                final ComponentPageElement container = pageAssembly.activeElement.peek();
                final ComponentPageElement embedded = pageAssembly.createdElement.peek();
                pageAssembly.deferred.add(new PageAssemblyAction() {

                    public void execute(PageAssembly pageAssembly) {
                        copyInformalParameters(container, embedded);
                    }
                });
            }
        });
    }
    return embeddedAssembler;
}
Also used : EmbeddedComponentModel(org.apache.tapestry5.model.EmbeddedComponentModel) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException)

Example 2 with EmbeddedComponentModel

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

the class BindParameterWorker method getEmbeddedComponentResourcesForPublishedParameter.

/**
 * Returns the {@link InternalComponentResources} of an embeddedComponent that contains the published parameter
 * publishedParameterName. This is basically a recursive search for published parameters.
 */
private InternalComponentResources getEmbeddedComponentResourcesForPublishedParameter(InternalComponentResources containerResources, String publishedParameterName) {
    List<InternalComponentResources> embeddedComponentResourcesList = CollectionFactory.newList();
    embeddedComponentResourcesList.add(containerResources);
    while (!embeddedComponentResourcesList.isEmpty()) {
        InternalComponentResources resources = embeddedComponentResourcesList.remove(0);
        ComponentModel containerComponentModel = resources.getComponentModel();
        for (String embeddedComponentId : containerComponentModel.getEmbeddedComponentIds()) {
            EmbeddedComponentModel embeddedComponentModel = containerComponentModel.getEmbeddedComponentModel(embeddedComponentId);
            InternalComponentResources embeddedComponentResources = (InternalComponentResources) resources.getEmbeddedComponent(embeddedComponentId).getComponentResources();
            /**
             * If the parameter is not a formal parameter, then the parameter must be a published parameter
             * of an embeddedComponent of the component we are currently examining.
             */
            if (embeddedComponentModel.getPublishedParameters().contains(publishedParameterName) && embeddedComponentResources.getComponentModel().isFormalParameter(publishedParameterName)) {
                return embeddedComponentResources;
            }
            embeddedComponentResourcesList.add(embeddedComponentResources);
        }
    }
    return null;
}
Also used : EmbeddedComponentModel(org.apache.tapestry5.model.EmbeddedComponentModel) InternalComponentResources(org.apache.tapestry5.internal.InternalComponentResources) ComponentModel(org.apache.tapestry5.model.ComponentModel) MutableComponentModel(org.apache.tapestry5.model.MutableComponentModel) EmbeddedComponentModel(org.apache.tapestry5.model.EmbeddedComponentModel)

Example 3 with EmbeddedComponentModel

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

the class ComponentAssemblerImpl method createEmbeddedAssembler.

public EmbeddedComponentAssembler createEmbeddedAssembler(String embeddedId, String componentClassName, EmbeddedComponentModel embeddedModel, String mixins, Location location) {
    try {
        if (InternalUtils.isBlank(componentClassName)) {
            throw new TapestryException("You must specify the type via t:type, the element, or @Component annotation.", location, null);
        }
        EmbeddedComponentAssemblerImpl embedded = new EmbeddedComponentAssemblerImpl(assemblerSource, instantiatorSource, componentClassResolver, componentClassName, getSelector(), embeddedModel, mixins, location, strictMixinParameters);
        if (embeddedIdToAssembler == null)
            embeddedIdToAssembler = CollectionFactory.newMap();
        embeddedIdToAssembler.put(embeddedId, embedded);
        if (embeddedModel != null) {
            for (String publishedParameterName : embeddedModel.getPublishedParameters()) {
                if (publishedParameterToEmbeddedId == null)
                    publishedParameterToEmbeddedId = CollectionFactory.newCaseInsensitiveMap();
                String existingEmbeddedId = publishedParameterToEmbeddedId.get(publishedParameterName);
                if (existingEmbeddedId != null) {
                    throw new TapestryException(String.format("Parameter '%s' of embedded component '%s' can not be published as a parameter of component %s, as it has previously been published by embedded component '%s'.", publishedParameterName, embeddedId, instantiator.getModel().getComponentClassName(), existingEmbeddedId), location, null);
                }
                publishedParameterToEmbeddedId.put(publishedParameterName, embeddedId);
            }
        }
        return embedded;
    } catch (Exception ex) {
        throw new TapestryException(String.format("Failure creating embedded component '%s' of %s: %s", embeddedId, instantiator.getModel().getComponentClassName(), ExceptionUtils.toMessage(ex)), location, ex);
    }
}
Also used : TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException)

Example 4 with EmbeddedComponentModel

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

the class MutableComponentModelImplTest method add_embedded.

@Test
public void add_embedded() {
    Resource r = mockResource();
    Logger logger = mockLogger();
    Location l = mockLocation();
    replay();
    MutableComponentModel model = new MutableComponentModelImpl(CLASS_NAME, logger, r, null, false, null);
    assertTrue(model.getEmbeddedComponentIds().isEmpty());
    MutableEmbeddedComponentModel fred = model.addEmbeddedComponent("fred", "Fred", COMPONENT_CLASS_NAME, false, l);
    assertEquals(fred.getId(), "fred");
    assertEquals(fred.getComponentType(), "Fred");
    assertFalse(fred.getInheritInformalParameters());
    assertSame(fred.getLocation(), l);
    MutableEmbeddedComponentModel barney = model.addEmbeddedComponent("barney", "Barney", COMPONENT_CLASS_NAME, false, null);
    assertEquals(model.getEmbeddedComponentIds(), Arrays.asList("barney", "fred"));
    assertSame(model.getEmbeddedComponentModel("fred"), fred);
    assertSame(model.getEmbeddedComponentModel("barney"), barney);
    // Access by id is case insensitive
    assertSame(model.getEmbeddedComponentModel("FRED"), fred);
    assertSame(model.getEmbeddedComponentModel("BARNEY"), barney);
    assertEquals(fred.toString(), "EmbeddedComponentModel[id=fred type=Fred class=org.example.components.Fred inheritInformals=false]");
    verify();
}
Also used : Resource(org.apache.tapestry5.commons.Resource) MutableComponentModel(org.apache.tapestry5.model.MutableComponentModel) Logger(org.slf4j.Logger) MutableEmbeddedComponentModel(org.apache.tapestry5.model.MutableEmbeddedComponentModel) Location(org.apache.tapestry5.commons.Location) Test(org.testng.annotations.Test)

Example 5 with EmbeddedComponentModel

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

the class MutableComponentModelImplTest method add_embedded_with_inherit_informal_parameters.

@Test
public void add_embedded_with_inherit_informal_parameters() {
    Resource r = mockResource();
    Logger logger = mockLogger();
    Location l = mockLocation();
    replay();
    MutableComponentModel model = new MutableComponentModelImpl(CLASS_NAME, logger, r, null, false, null);
    assertTrue(model.getEmbeddedComponentIds().isEmpty());
    MutableEmbeddedComponentModel fred = model.addEmbeddedComponent("fred", "Fred", COMPONENT_CLASS_NAME, true, l);
    assertTrue(fred.getInheritInformalParameters());
    assertEquals(fred.toString(), "EmbeddedComponentModel[id=fred type=Fred class=org.example.components.Fred inheritInformals=true]");
    verify();
}
Also used : Resource(org.apache.tapestry5.commons.Resource) MutableComponentModel(org.apache.tapestry5.model.MutableComponentModel) Logger(org.slf4j.Logger) MutableEmbeddedComponentModel(org.apache.tapestry5.model.MutableEmbeddedComponentModel) Location(org.apache.tapestry5.commons.Location) Test(org.testng.annotations.Test)

Aggregations

EmbeddedComponentModel (org.apache.tapestry5.model.EmbeddedComponentModel)3 MutableComponentModel (org.apache.tapestry5.model.MutableComponentModel)3 Location (org.apache.tapestry5.commons.Location)2 Resource (org.apache.tapestry5.commons.Resource)2 TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)2 MutableEmbeddedComponentModel (org.apache.tapestry5.model.MutableEmbeddedComponentModel)2 Logger (org.slf4j.Logger)2 Test (org.testng.annotations.Test)2 InternalComponentResources (org.apache.tapestry5.internal.InternalComponentResources)1 ComponentModel (org.apache.tapestry5.model.ComponentModel)1