You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Nick Stuart <ba...@gmail.com> on 2005/02/17 22:12:41 UTC

Adding a component through code

Ok, so I was wondering if its possible to add a component to the page
through code. It (almost) seems like its possible.

In theory, I should be able to get an instance of an enhanced
component, and create a new instance of the class. Then, set whatever
parameters I need to set through reflection (kinda messy, I know) and
use the components render() method to spit it out on the page.

So, I should be able to do something like:
protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) {
        IEngine engine = getPage().getEngine();
        ExternalLink link = null;
        Class linkClass =
engine.getComponentClassEnhancer().getEnhancedClass(getSpecification(),
"org.apache.tapestry.link.ExternalLink");  <---problem with this line
and getSpec()
        
        try{
            link = (ExternalLink)linkClass.newInstance();
        }catch(Exception e){}
        
        IPage page = cycle.getPage("SomePage");
        
        link.setPage(page);
        link.setProperty("parameters", new Object[]{"blah"});
        link.render(writer, cycle);
        super.renderComponent(writer, cycle);
    }

My current roadblock, how do I get a components specification with out
already having an actual object instatiated? I need the spec to get
the class, but I need the class to get the object to get the
spec.....doh!

Are there any utility methods out there that will get the spec for any
component given its class or is this not possible? What would be the
pit falls of doing something like this? This COULD be an incredibly
easy way to make more dynamic components with very little hard coding
needed.

-Nick

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org