Search in sources :

Example 1 with UINamingContainer

use of jakarta.faces.component.UINamingContainer in project myfaces by apache.

the class FaceletViewDeclarationLanguage method getComponentMetadata.

/**
 * retargetMethodExpressions(FacesContext, UIComponent) has some clues about the behavior of this method
 *
 * {@inheritDoc}
 */
@Override
public BeanInfo getComponentMetadata(FacesContext context, Resource componentResource) {
    BeanInfo beanInfo = null;
    Assert.notNull(context, "context");
    try {
        Facelet compositeComponentFacelet;
        FaceletFactory.setInstance(faceletFactory);
        try {
            compositeComponentFacelet = faceletFactory.getCompositeComponentMetadataFacelet(componentResource.getURL());
        } finally {
            FaceletFactory.setInstance(null);
        }
        // context.getAttributes().put(BUILDING_COMPOSITE_COMPONENT_METADATA, Boolean.TRUE);
        // Create a temporal tree where all components will be put, but we are only
        // interested in metadata.
        UINamingContainer compositeComponentBase = (UINamingContainer) context.getApplication().createComponent(context, UINamingContainer.COMPONENT_TYPE, null);
        // Fill the component resource key, because this information should be available
        // on metadata to recognize which is the component used as composite component base.
        // Since this method is called from Application.createComponent(FacesContext,Resource),
        // and in that specific method this key is updated, this is the best option we
        // have for recognize it (also this key is used by UIComponent.isCompositeComponent)
        compositeComponentBase.getAttributes().put(Resource.COMPONENT_RESOURCE_KEY, componentResource);
        // According to UserTagHandler, in this point we need to wrap the facelet
        // VariableMapper, so local changes are applied on "page context", but
        // data is retrieved from full context
        FaceletContext faceletContext = (FaceletContext) context.getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY);
        VariableMapper orig = faceletContext.getVariableMapper();
        try {
            faceletContext.setVariableMapper(new VariableMapperWrapper(orig));
            compositeComponentBase.pushComponentToEL(context, compositeComponentBase);
            compositeComponentFacelet.apply(context, compositeComponentBase);
            compositeComponentBase.popComponentFromEL(context);
        } finally {
            faceletContext.setVariableMapper(orig);
        }
        beanInfo = (BeanInfo) compositeComponentBase.getAttributes().get(UIComponent.BEANINFO_KEY);
    } catch (IOException e) {
        throw new FacesException(e);
    }
    return beanInfo;
}
Also used : Facelet(jakarta.faces.view.facelets.Facelet) VariableMapper(jakarta.el.VariableMapper) BeanInfo(java.beans.BeanInfo) VariableMapperWrapper(org.apache.myfaces.view.facelets.el.VariableMapperWrapper) FaceletContext(jakarta.faces.view.facelets.FaceletContext) UINamingContainer(jakarta.faces.component.UINamingContainer) IOException(java.io.IOException) FacesException(jakarta.faces.FacesException)

Example 2 with UINamingContainer

use of jakarta.faces.component.UINamingContainer in project myfaces by apache.

the class UIComponentFindComponentTest method testRelativeExpression.

public void testRelativeExpression() throws Exception {
    String expression = "testimpl";
    UIComponent namingContainer = new UINamingContainer();
    UIComponent parent = new UIPanel();
    namingContainer.setId("namingContainer");
    namingContainer.getChildren().add(parent);
    parent.setId("parent");
    parent.getChildren().add(_testImpl);
    _testImpl.setId("testimpl");
    Assert.assertEquals(_testImpl, _testImpl.findComponent(expression));
}
Also used : UIComponent(jakarta.faces.component.UIComponent) UIPanel(jakarta.faces.component.UIPanel) UINamingContainer(jakarta.faces.component.UINamingContainer)

Example 3 with UINamingContainer

use of jakarta.faces.component.UINamingContainer in project myfaces by apache.

the class UIComponentBaseGetClientIdTest method testWithParentNamingContainerChanging.

public void testWithParentNamingContainerChanging() throws Exception {
    String id = "testId";
    String containerClientId = "containerClientId";
    for (int i = 0; i < 10; i++) {
        final int j = i;
        UIComponent namingContainer = new UINamingContainer() {

            @Override
            public String getContainerClientId(FacesContext ctx) {
                return super.getContainerClientId(ctx) + j;
            }
        };
        UIComponent parent = new UIPanel();
        _testImpl.setId(id);
        String expectedClientId = containerClientId + i + NamingContainer.SEPARATOR_CHAR + id;
        namingContainer.setId(containerClientId);
        namingContainer.getChildren().add(parent);
        parent.setId("parent");
        parent.getChildren().add(_testImpl);
        Assert.assertEquals(expectedClientId, _testImpl.getClientId(facesContext));
        Assert.assertEquals(expectedClientId, _testImpl.getClientId(facesContext));
        parent.getChildren().remove(_testImpl);
    }
}
Also used : FacesContext(jakarta.faces.context.FacesContext) UIComponent(jakarta.faces.component.UIComponent) UIPanel(jakarta.faces.component.UIPanel) UINamingContainer(jakarta.faces.component.UINamingContainer)

Example 4 with UINamingContainer

use of jakarta.faces.component.UINamingContainer in project myfaces by apache.

the class CompositeComponentClientBehaviorTestCase method testSimpleClientBehaviorDefaultAjaxWrap.

@Test
public void testSimpleClientBehaviorDefaultAjaxWrap() throws Exception {
    HelloWorld helloWorld = new HelloWorld();
    facesContext.getExternalContext().getRequestMap().put("helloWorldBean", helloWorld);
    UIViewRoot root = facesContext.getViewRoot();
    vdl.buildView(facesContext, root, "testSimpleClientBehaviorDefaultAjaxWrap.xhtml");
    UIComponent form = root.findComponent("testForm1");
    Assert.assertNotNull(form);
    UINamingContainer compositeComponent = (UINamingContainer) form.getChildren().get(0);
    Assert.assertNotNull(compositeComponent);
    UICommand button = (UICommand) compositeComponent.findComponent("button");
    Assert.assertNotNull(button);
    Assert.assertNotNull(button.getClientBehaviors().get("dblclick"));
    Assert.assertEquals(1, button.getClientBehaviors().get("dblclick").size());
// StringWriter sw = new StringWriter();
// MockResponseWriter mrw = new MockResponseWriter(sw);
// facesContext.setResponseWriter(mrw);
// root.encodeAll(facesContext);
// sw.flush();
// System.out.print(sw.toString());
}
Also used : UIComponent(jakarta.faces.component.UIComponent) UICommand(jakarta.faces.component.UICommand) UINamingContainer(jakarta.faces.component.UINamingContainer) UIViewRoot(jakarta.faces.component.UIViewRoot) HelloWorld(org.apache.myfaces.view.facelets.bean.HelloWorld) Test(org.junit.Test)

Example 5 with UINamingContainer

use of jakarta.faces.component.UINamingContainer in project myfaces by apache.

the class CompositeComponentClientBehaviorTestCase method testCompositeClientBehavior.

@Test
public void testCompositeClientBehavior() throws Exception {
    HelloWorld helloWorld = new HelloWorld();
    facesContext.getExternalContext().getRequestMap().put("helloWorldBean", helloWorld);
    UIViewRoot root = facesContext.getViewRoot();
    vdl.buildView(facesContext, root, "testCompositeClientBehavior.xhtml");
    UIComponent form = root.findComponent("testForm1");
    Assert.assertNotNull(form);
    UINamingContainer compositeComponent = (UINamingContainer) form.getChildren().get(0);
    Assert.assertNotNull(compositeComponent);
    UINamingContainer compositeComponent2 = (UINamingContainer) compositeComponent.findComponent("button3");
    Assert.assertNotNull(compositeComponent2);
    UICommand button = (UICommand) compositeComponent2.findComponent("button");
    Assert.assertNotNull(button);
    // One added in testCompositeActionSource, the other one
    // inside compositeActionSource.xhtml
    Assert.assertNotNull(button.getClientBehaviors().get("action"));
    Assert.assertEquals(1, button.getClientBehaviors().get("action").size());
// StringWriter sw = new StringWriter();
// MockResponseWriter mrw = new MockResponseWriter(sw);
// facesContext.setResponseWriter(mrw);
// root.encodeAll(facesContext);
// sw.flush();
// System.out.print(sw.toString());
}
Also used : UIComponent(jakarta.faces.component.UIComponent) UICommand(jakarta.faces.component.UICommand) UINamingContainer(jakarta.faces.component.UINamingContainer) UIViewRoot(jakarta.faces.component.UIViewRoot) HelloWorld(org.apache.myfaces.view.facelets.bean.HelloWorld) Test(org.junit.Test)

Aggregations

UINamingContainer (jakarta.faces.component.UINamingContainer)77 UIComponent (jakarta.faces.component.UIComponent)74 UIViewRoot (jakarta.faces.component.UIViewRoot)71 Test (org.junit.Test)69 UICommand (jakarta.faces.component.UICommand)31 HelloWorld (org.apache.myfaces.view.facelets.bean.HelloWorld)24 UIInput (jakarta.faces.component.UIInput)16 StringWriter (java.io.StringWriter)13 MockResponseWriter (org.apache.myfaces.test.mock.MockResponseWriter)13 ActionEvent (jakarta.faces.event.ActionEvent)10 ValueChangeEvent (jakarta.faces.event.ValueChangeEvent)8 UIPanel (jakarta.faces.component.UIPanel)6 UIOutput (jakarta.faces.component.UIOutput)4 MethodExpression (jakarta.el.MethodExpression)3 UIForm (jakarta.faces.component.UIForm)3 HtmlCommandLink (jakarta.faces.component.html.HtmlCommandLink)3 HtmlRenderedAttr (org.apache.myfaces.test.utils.HtmlRenderedAttr)3 FacesException (jakarta.faces.FacesException)2 HtmlCommandButton (jakarta.faces.component.html.HtmlCommandButton)2 VisitHint (jakarta.faces.component.visit.VisitHint)2