webtier@glassfish.java.net

Re: Programmatic or dynamic construction of composite components

From: <webtier_at_javadesktop.org>
Date: Sun, 28 Feb 2010 13:46:04 PST

Then I though it would be nice to have some kind of CompositeComponentTagHandler where I could implement/override setAttributes or populateComponent methods to assign my values to the component, or to add facets or children programmatically. Here's what I wrote:

[pre]
        public void setComponent5(UIComponent component) {
                Application application = facesContext.getApplication();
                // Create a resource
                Resource resource = application.getResourceHandler().createResource(
                                "test.xhtml", "components/dynamicfaces");

                // Create a "dummy" tag config
                ComponentConfig tagConfig = new FakeComponentConfig(
                                "http://java.sun.com/jsf/composite/components/dynamicfaces",
                                "test", "test", UINamingContainer.COMPONENT_TYPE, null, "tag");

                // Create a tag handler for the resource
                CompositeComponentTagHandler tagHandler = new CompositeComponentTagHandler(
                                resource, tagConfig) {
                        @Override
                        public void setAttributes(FaceletContext ctx, UIComponent c) {
                                // Set value expressions to the component
                                final Application application = ctx.getFacesContext()
                                                .getApplication();

                                c.setValueExpression("value", application
                                                .getExpressionFactory().createValueExpression(
                                                                facesContext.getELContext(),
                                                                "#{stringValue.value}", String.class));

                        }

                        @Override
                        public void populateComponent(FaceletContext ctx, UIComponent c)
                                        throws IOException, FacesException, ELException {
                                // Populate the component with facets and children
                                final Application application = ctx.getFacesContext()
                                                .getApplication();

                                UIOutput foo = (UIOutput) application
                                                .createComponent(HtmlOutputText.COMPONENT_TYPE);

                                foo.setValue("Foo");
                                c.getFacets().put("foo", foo);

                                UIOutput bar = (UIOutput) application
                                                .createComponent(HtmlOutputText.COMPONENT_TYPE);
                                bar.setValue("Bar");
                                c.getChildren().add(bar);
                        }

                };

                try {
                        final FaceletContext faceletContext = (FaceletContext) facesContext
                                        .getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY);
                        tagHandler.getTagHandlerDelegate().apply(faceletContext, component);
                } catch (IOException ioex) {
                        throw new FacesException(ioex);
                }

        }
[/pre]

This is already much much better. Get resource - create a tag handler for it - override component configuration methods - apply to the parent component. It's allright.

All I actually need is to be able to subclass the CompositeComponentTagHandler class. However implementing this idea was something like "mission impossible".
[Message sent by forum member 'lexi' (valikov_at_gmx.net)]

http://forums.java.net/jive/thread.jspa?messageID=389248