render nested component

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

render nested component

Qbyte Consulting
I am creating a Menu component (for a ul tag) that needs to
programmatically generate the child links.

I already have a MenuItem component, but this is parameter bound to a
template, whereas now I need to programmatically drive the parameters.

I either need to be able to create the existing MenuItem objects somehow in
the Menu component and render them, or I simply write the links out in the
menu - however since my Menu component isn't extending AbstractLink (like
MenuItem) I can't use writeLink().

Any ideas what is the simplest approach to resolve this? Code below.

John


public class Menu {

    /** The text. */
    @Parameter(required = true, allowNull = false, defaultPrefix =
BindingConstants.LITERAL)
    private String name;

    /** The resources. */
    @Inject
    private ComponentResources resources;

    /** The component source. */
    @Inject
    private ComponentSource componentSource;

    @Inject
    private PageRenderLinkSource linkSource;

/** The messages. */
@Inject
private Messages messages;

    @Inject
    private MenuService menuService;

    /** The state bean. */
    @SessionState(create = false)
    private SessionStateBean stateBean;

    /**
     * If provided, this is the activation context for the target page (the
     * information will be encoded into the URL). If not provided, then the
     * target page will provide its own activation context.
     */
    @Parameter
    private Object[] context;

    /**
     * Begin render.
     *
     * @param writer the writer
     */
    void beginRender(MarkupWriter writer) {
    writer.element("ul");
    for (String pageName :
menuService.getPageNames(MenuService.Menu.valueOf(name))) {
    renderMenuItem(pageName, writer);
    }
    }

    private void renderMenuItem(String page, MarkupWriter writer) {
        Link link = resources.createPageLink(page,
       resources.isBound("context"), context);
       writer.element("li");
NO!!        writeLink(writer, link);
 writer.writeRaw(messages.get(page.toLowerCase().concat(".link")));
    writer.end();
    writer.end();
    }

    /**
     * After render.
     *
     * @param writer the writer
     */
    void afterRender(MarkupWriter writer) {
   writer.end();
    }
}


public class MenuItem extends AbstractLink {

    /**
     * The logical name of the page to link to.
     */
    @Parameter(required = true, allowNull = false, defaultPrefix =
BindingConstants.LITERAL)
    private String page;

    /** The text. */
    @Parameter(required = true, allowNull = false, defaultPrefix =
BindingConstants.LITERAL)
    private String text;

    /** The resources. */
    @Inject
    private ComponentResources resources;

    /** The component source. */
    @Inject
    private ComponentSource componentSource;

    /** The state bean. */
    @SessionState(create = false)
    private SessionStateBean stateBean;

    /**
     * If provided, this is the activation context for the target page (the
     * information will be encoded into the URL). If not provided, then the
     * target page will provide its own activation context.
     */
    @Parameter
    private Object[] context;

    /**
     * Begin render.
     *
     * @param writer the writer
     */
    void beginRender(MarkupWriter writer) {
if (isDisabled())
   return;
   Link link = resources.createPageLink(page,
   resources.isBound("context"), context);
   writer.element("li");
   writeLink(writer, link);
   writer.writeRaw(text);
    }

    /**
     * After render.
     *
     * @param writer the writer
     */
    void afterRender(MarkupWriter writer) {
if (isDisabled())
   return;
writer.end();
writer.end();
    }
}
Reply | Threaded
Open this post in threaded view
|

Re: render nested component

Chris Poulsen
Hi,

Take a look at the Tapestry Tree component to see how recursive rendering
can be done.

--
Chris

On Fri, Mar 18, 2016 at 6:17 AM, Qbyte Consulting <[hidden email]
> wrote:

> I am creating a Menu component (for a ul tag) that needs to
> programmatically generate the child links.
>
> I already have a MenuItem component, but this is parameter bound to a
> template, whereas now I need to programmatically drive the parameters.
>
> I either need to be able to create the existing MenuItem objects somehow in
> the Menu component and render them, or I simply write the links out in the
> menu - however since my Menu component isn't extending AbstractLink (like
> MenuItem) I can't use writeLink().
>
> Any ideas what is the simplest approach to resolve this? Code below.
>
> John
>
>
> public class Menu {
>
>     /** The text. */
>     @Parameter(required = true, allowNull = false, defaultPrefix =
> BindingConstants.LITERAL)
>     private String name;
>
>     /** The resources. */
>     @Inject
>     private ComponentResources resources;
>
>     /** The component source. */
>     @Inject
>     private ComponentSource componentSource;
>
>     @Inject
>     private PageRenderLinkSource linkSource;
>
> /** The messages. */
> @Inject
> private Messages messages;
>
>     @Inject
>     private MenuService menuService;
>
>     /** The state bean. */
>     @SessionState(create = false)
>     private SessionStateBean stateBean;
>
>     /**
>      * If provided, this is the activation context for the target page (the
>      * information will be encoded into the URL). If not provided, then the
>      * target page will provide its own activation context.
>      */
>     @Parameter
>     private Object[] context;
>
>     /**
>      * Begin render.
>      *
>      * @param writer the writer
>      */
>     void beginRender(MarkupWriter writer) {
>     writer.element("ul");
>     for (String pageName :
> menuService.getPageNames(MenuService.Menu.valueOf(name))) {
>     renderMenuItem(pageName, writer);
>     }
>     }
>
>     private void renderMenuItem(String page, MarkupWriter writer) {
>         Link link = resources.createPageLink(page,
>        resources.isBound("context"), context);
>        writer.element("li");
> NO!!        writeLink(writer, link);
>  writer.writeRaw(messages.get(page.toLowerCase().concat(".link")));
>     writer.end();
>     writer.end();
>     }
>
>     /**
>      * After render.
>      *
>      * @param writer the writer
>      */
>     void afterRender(MarkupWriter writer) {
>    writer.end();
>     }
> }
>
>
> public class MenuItem extends AbstractLink {
>
>     /**
>      * The logical name of the page to link to.
>      */
>     @Parameter(required = true, allowNull = false, defaultPrefix =
> BindingConstants.LITERAL)
>     private String page;
>
>     /** The text. */
>     @Parameter(required = true, allowNull = false, defaultPrefix =
> BindingConstants.LITERAL)
>     private String text;
>
>     /** The resources. */
>     @Inject
>     private ComponentResources resources;
>
>     /** The component source. */
>     @Inject
>     private ComponentSource componentSource;
>
>     /** The state bean. */
>     @SessionState(create = false)
>     private SessionStateBean stateBean;
>
>     /**
>      * If provided, this is the activation context for the target page (the
>      * information will be encoded into the URL). If not provided, then the
>      * target page will provide its own activation context.
>      */
>     @Parameter
>     private Object[] context;
>
>     /**
>      * Begin render.
>      *
>      * @param writer the writer
>      */
>     void beginRender(MarkupWriter writer) {
> if (isDisabled())
>    return;
>    Link link = resources.createPageLink(page,
>    resources.isBound("context"), context);
>    writer.element("li");
>    writeLink(writer, link);
>    writer.writeRaw(text);
>     }
>
>     /**
>      * After render.
>      *
>      * @param writer the writer
>      */
>     void afterRender(MarkupWriter writer) {
> if (isDisabled())
>    return;
> writer.end();
> writer.end();
>     }
> }
>
Reply | Threaded
Open this post in threaded view
|

Re: render nested component

Qbyte Consulting
Tree uses BaseComponent that doesn't seem to be in tapestry5?

On Fri, Mar 18, 2016 at 2:15 PM, Chris Poulsen <[hidden email]>
wrote:

> Hi,
>
> Take a look at the Tapestry Tree component to see how recursive rendering
> can be done.
>
> --
> Chris
>
> On Fri, Mar 18, 2016 at 6:17 AM, Qbyte Consulting <
> [hidden email]
> > wrote:
>
> > I am creating a Menu component (for a ul tag) that needs to
> > programmatically generate the child links.
> >
> > I already have a MenuItem component, but this is parameter bound to a
> > template, whereas now I need to programmatically drive the parameters.
> >
> > I either need to be able to create the existing MenuItem objects somehow
> in
> > the Menu component and render them, or I simply write the links out in
> the
> > menu - however since my Menu component isn't extending AbstractLink (like
> > MenuItem) I can't use writeLink().
> >
> > Any ideas what is the simplest approach to resolve this? Code below.
> >
> > John
> >
> >
> > public class Menu {
> >
> >     /** The text. */
> >     @Parameter(required = true, allowNull = false, defaultPrefix =
> > BindingConstants.LITERAL)
> >     private String name;
> >
> >     /** The resources. */
> >     @Inject
> >     private ComponentResources resources;
> >
> >     /** The component source. */
> >     @Inject
> >     private ComponentSource componentSource;
> >
> >     @Inject
> >     private PageRenderLinkSource linkSource;
> >
> > /** The messages. */
> > @Inject
> > private Messages messages;
> >
> >     @Inject
> >     private MenuService menuService;
> >
> >     /** The state bean. */
> >     @SessionState(create = false)
> >     private SessionStateBean stateBean;
> >
> >     /**
> >      * If provided, this is the activation context for the target page
> (the
> >      * information will be encoded into the URL). If not provided, then
> the
> >      * target page will provide its own activation context.
> >      */
> >     @Parameter
> >     private Object[] context;
> >
> >     /**
> >      * Begin render.
> >      *
> >      * @param writer the writer
> >      */
> >     void beginRender(MarkupWriter writer) {
> >     writer.element("ul");
> >     for (String pageName :
> > menuService.getPageNames(MenuService.Menu.valueOf(name))) {
> >     renderMenuItem(pageName, writer);
> >     }
> >     }
> >
> >     private void renderMenuItem(String page, MarkupWriter writer) {
> >         Link link = resources.createPageLink(page,
> >        resources.isBound("context"), context);
> >        writer.element("li");
> > NO!!        writeLink(writer, link);
> >  writer.writeRaw(messages.get(page.toLowerCase().concat(".link")));
> >     writer.end();
> >     writer.end();
> >     }
> >
> >     /**
> >      * After render.
> >      *
> >      * @param writer the writer
> >      */
> >     void afterRender(MarkupWriter writer) {
> >    writer.end();
> >     }
> > }
> >
> >
> > public class MenuItem extends AbstractLink {
> >
> >     /**
> >      * The logical name of the page to link to.
> >      */
> >     @Parameter(required = true, allowNull = false, defaultPrefix =
> > BindingConstants.LITERAL)
> >     private String page;
> >
> >     /** The text. */
> >     @Parameter(required = true, allowNull = false, defaultPrefix =
> > BindingConstants.LITERAL)
> >     private String text;
> >
> >     /** The resources. */
> >     @Inject
> >     private ComponentResources resources;
> >
> >     /** The component source. */
> >     @Inject
> >     private ComponentSource componentSource;
> >
> >     /** The state bean. */
> >     @SessionState(create = false)
> >     private SessionStateBean stateBean;
> >
> >     /**
> >      * If provided, this is the activation context for the target page
> (the
> >      * information will be encoded into the URL). If not provided, then
> the
> >      * target page will provide its own activation context.
> >      */
> >     @Parameter
> >     private Object[] context;
> >
> >     /**
> >      * Begin render.
> >      *
> >      * @param writer the writer
> >      */
> >     void beginRender(MarkupWriter writer) {
> > if (isDisabled())
> >    return;
> >    Link link = resources.createPageLink(page,
> >    resources.isBound("context"), context);
> >    writer.element("li");
> >    writeLink(writer, link);
> >    writer.writeRaw(text);
> >     }
> >
> >     /**
> >      * After render.
> >      *
> >      * @param writer the writer
> >      */
> >     void afterRender(MarkupWriter writer) {
> > if (isDisabled())
> >    return;
> > writer.end();
> > writer.end();
> >     }
> > }
> >
>
Reply | Threaded
Open this post in threaded view
|

Re: render nested component

Qbyte Consulting
ok ignore that, bumped into some tapestry4 source

would this be the approach?

              JSONObject
<http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-json/5.3.1/org/apache/tapestry5/json/JSONObject.java#JSONObject>
spec = new JSONObject
<http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-json/5.3.1/org/apache/tapestry5/json/JSONObject.java#JSONObject>("clientId",
clientId);

171 <http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#171>

<http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#>

172 <http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#172>

<http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#>

                e.attribute
<http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/dom/Element.java#Element.attribute%28java.lang.String%2Cjava.lang.String%29>("id",
clientId);

173 <http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#173>

<http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#>

174 <http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#174>

<http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#>

                spec.put
<http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-json/5.3.1/org/apache/tapestry5/json/JSONObject.java#JSONObject.put%28java.lang.String%2Cjava.lang.Object%29>("leaf",
node.isLeaf <http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/tree/TreeNode.java#TreeNode.isLeaf%28%29>());

175 <http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#175>

<http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#>

176 <http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#176>

<http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#>

                if (hasChildren)

177 <http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#177>

<http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#>

                {

178 <http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#178>

<http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#>

                    Link
<http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/Link.java#Link>
expandChildren = resources
<http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#Tree.0resources>.createEventLink
<http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/ComponentResourcesCommon.java#ComponentResourcesCommon.createEventLink%28java.lang.String%2Cjava.lang.Object%5B%5D%29>("expandChildren",
node.getId <http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/tree/TreeNode.java#TreeNode.getId%28%29>());

179 <http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#179>

<http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#>

                    Link
<http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/Link.java#Link>
markExpanded = resources
<http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#Tree.0resources>.createEventLink
<http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/ComponentResourcesCommon.java#ComponentResourcesCommon.createEventLink%28java.lang.String%2Cjava.lang.Object%5B%5D%29>("markExpanded",
node.getId <http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/tree/TreeNode.java#TreeNode.getId%28%29>());

180 <http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#180>

<http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#>

                    Link
<http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/Link.java#Link>
markCollapsed = resources
<http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#Tree.0resources>.createEventLink
<http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/ComponentResourcesCommon.java#ComponentResourcesCommon.createEventLink%28java.lang.String%2Cjava.lang.Object%5B%5D%29>("markCollapsed",
node.getId <http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/tree/TreeNode.java#TreeNode.getId%28%29>());

181 <http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#181>

<http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#>

182 <http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#182>

<http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#>

                    spec.put
<http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-json/5.3.1/org/apache/tapestry5/json/JSONObject.java#JSONObject.put%28java.lang.String%2Cjava.lang.Object%29>("expandChildrenURL",
expandChildren.toString
<http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b27/java/lang/Object.java#Object.toString%28%29>())

183 <http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#183>

<http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#>

                            .put
<http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-json/5.3.1/org/apache/tapestry5/json/JSONObject.java#JSONObject.put%28java.lang.String%2Cjava.lang.Object%29>("markExpandedURL",
markExpanded.toString
<http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b27/java/lang/Object.java#Object.toString%28%29>())

184 <http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#184>

<http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#>

                            .put
<http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-json/5.3.1/org/apache/tapestry5/json/JSONObject.java#JSONObject.put%28java.lang.String%2Cjava.lang.Object%29>("markCollapsedURL",
markCollapsed.toString
<http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b27/java/lang/Object.java#Object.toString%28%29>());


On Mon, Mar 21, 2016 at 10:47 AM, Qbyte Consulting <
[hidden email]> wrote:

> Tree uses BaseComponent that doesn't seem to be in tapestry5?
>
> On Fri, Mar 18, 2016 at 2:15 PM, Chris Poulsen <[hidden email]>
> wrote:
>
>> Hi,
>>
>> Take a look at the Tapestry Tree component to see how recursive rendering
>> can be done.
>>
>> --
>> Chris
>>
>> On Fri, Mar 18, 2016 at 6:17 AM, Qbyte Consulting <
>> [hidden email]
>> > wrote:
>>
>> > I am creating a Menu component (for a ul tag) that needs to
>> > programmatically generate the child links.
>> >
>> > I already have a MenuItem component, but this is parameter bound to a
>> > template, whereas now I need to programmatically drive the parameters.
>> >
>> > I either need to be able to create the existing MenuItem objects
>> somehow in
>> > the Menu component and render them, or I simply write the links out in
>> the
>> > menu - however since my Menu component isn't extending AbstractLink
>> (like
>> > MenuItem) I can't use writeLink().
>> >
>> > Any ideas what is the simplest approach to resolve this? Code below.
>> >
>> > John
>> >
>> >
>> > public class Menu {
>> >
>> >     /** The text. */
>> >     @Parameter(required = true, allowNull = false, defaultPrefix =
>> > BindingConstants.LITERAL)
>> >     private String name;
>> >
>> >     /** The resources. */
>> >     @Inject
>> >     private ComponentResources resources;
>> >
>> >     /** The component source. */
>> >     @Inject
>> >     private ComponentSource componentSource;
>> >
>> >     @Inject
>> >     private PageRenderLinkSource linkSource;
>> >
>> > /** The messages. */
>> > @Inject
>> > private Messages messages;
>> >
>> >     @Inject
>> >     private MenuService menuService;
>> >
>> >     /** The state bean. */
>> >     @SessionState(create = false)
>> >     private SessionStateBean stateBean;
>> >
>> >     /**
>> >      * If provided, this is the activation context for the target page
>> (the
>> >      * information will be encoded into the URL). If not provided, then
>> the
>> >      * target page will provide its own activation context.
>> >      */
>> >     @Parameter
>> >     private Object[] context;
>> >
>> >     /**
>> >      * Begin render.
>> >      *
>> >      * @param writer the writer
>> >      */
>> >     void beginRender(MarkupWriter writer) {
>> >     writer.element("ul");
>> >     for (String pageName :
>> > menuService.getPageNames(MenuService.Menu.valueOf(name))) {
>> >     renderMenuItem(pageName, writer);
>> >     }
>> >     }
>> >
>> >     private void renderMenuItem(String page, MarkupWriter writer) {
>> >         Link link = resources.createPageLink(page,
>> >        resources.isBound("context"), context);
>> >        writer.element("li");
>> > NO!!        writeLink(writer, link);
>> >  writer.writeRaw(messages.get(page.toLowerCase().concat(".link")));
>> >     writer.end();
>> >     writer.end();
>> >     }
>> >
>> >     /**
>> >      * After render.
>> >      *
>> >      * @param writer the writer
>> >      */
>> >     void afterRender(MarkupWriter writer) {
>> >    writer.end();
>> >     }
>> > }
>> >
>> >
>> > public class MenuItem extends AbstractLink {
>> >
>> >     /**
>> >      * The logical name of the page to link to.
>> >      */
>> >     @Parameter(required = true, allowNull = false, defaultPrefix =
>> > BindingConstants.LITERAL)
>> >     private String page;
>> >
>> >     /** The text. */
>> >     @Parameter(required = true, allowNull = false, defaultPrefix =
>> > BindingConstants.LITERAL)
>> >     private String text;
>> >
>> >     /** The resources. */
>> >     @Inject
>> >     private ComponentResources resources;
>> >
>> >     /** The component source. */
>> >     @Inject
>> >     private ComponentSource componentSource;
>> >
>> >     /** The state bean. */
>> >     @SessionState(create = false)
>> >     private SessionStateBean stateBean;
>> >
>> >     /**
>> >      * If provided, this is the activation context for the target page
>> (the
>> >      * information will be encoded into the URL). If not provided, then
>> the
>> >      * target page will provide its own activation context.
>> >      */
>> >     @Parameter
>> >     private Object[] context;
>> >
>> >     /**
>> >      * Begin render.
>> >      *
>> >      * @param writer the writer
>> >      */
>> >     void beginRender(MarkupWriter writer) {
>> > if (isDisabled())
>> >    return;
>> >    Link link = resources.createPageLink(page,
>> >    resources.isBound("context"), context);
>> >    writer.element("li");
>> >    writeLink(writer, link);
>> >    writer.writeRaw(text);
>> >     }
>> >
>> >     /**
>> >      * After render.
>> >      *
>> >      * @param writer the writer
>> >      */
>> >     void afterRender(MarkupWriter writer) {
>> > if (isDisabled())
>> >    return;
>> > writer.end();
>> > writer.end();
>> >     }
>> > }
>> >
>>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: render nested component

Chris Poulsen
Tree can be viewed as an example of how to construct a recursive structure
using Blocks/ RenderCommands, understanding the render queue may require a
little reading/experimentation, but the advantage of this approach is that
it allows you to use the normal tapestry components for creating your
markup (opposed to simply building and returning a HTML string, which would
most likely end in a maintenance nightmare pretty fast)

--
Chris

On Mon, Mar 21, 2016 at 5:02 AM, Qbyte Consulting <[hidden email]
> wrote:

> ok ignore that, bumped into some tapestry4 source
>
> would this be the approach?
>
>               JSONObject
> <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-json/5.3.1/org/apache/tapestry5/json/JSONObject.java#JSONObject
> >
> spec = new JSONObject
> <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-json/5.3.1/org/apache/tapestry5/json/JSONObject.java#JSONObject
> >("clientId",
> clientId);
>
> 171 <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#171
> >
>
> <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#
> >
>
> 172 <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#172
> >
>
> <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#
> >
>
>                 e.attribute
> <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/dom/Element.java#Element.attribute%28java.lang.String%2Cjava.lang.String%29
> >("id",
> clientId);
>
> 173 <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#173
> >
>
> <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#
> >
>
> 174 <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#174
> >
>
> <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#
> >
>
>                 spec.put
> <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-json/5.3.1/org/apache/tapestry5/json/JSONObject.java#JSONObject.put%28java.lang.String%2Cjava.lang.Object%29
> >("leaf",
> node.isLeaf <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/tree/TreeNode.java#TreeNode.isLeaf%28%29
> >());
>
> 175 <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#175
> >
>
> <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#
> >
>
> 176 <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#176
> >
>
> <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#
> >
>
>                 if (hasChildren)
>
> 177 <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#177
> >
>
> <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#
> >
>
>                 {
>
> 178 <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#178
> >
>
> <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#
> >
>
>                     Link
> <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/Link.java#Link
> >
> expandChildren = resources
> <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#Tree.0resources
> >.createEventLink
> <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/ComponentResourcesCommon.java#ComponentResourcesCommon.createEventLink%28java.lang.String%2Cjava.lang.Object%5B%5D%29
> >("expandChildren",
> node.getId <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/tree/TreeNode.java#TreeNode.getId%28%29
> >());
>
> 179 <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#179
> >
>
> <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#
> >
>
>                     Link
> <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/Link.java#Link
> >
> markExpanded = resources
> <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#Tree.0resources
> >.createEventLink
> <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/ComponentResourcesCommon.java#ComponentResourcesCommon.createEventLink%28java.lang.String%2Cjava.lang.Object%5B%5D%29
> >("markExpanded",
> node.getId <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/tree/TreeNode.java#TreeNode.getId%28%29
> >());
>
> 180 <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#180
> >
>
> <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#
> >
>
>                     Link
> <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/Link.java#Link
> >
> markCollapsed = resources
> <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#Tree.0resources
> >.createEventLink
> <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/ComponentResourcesCommon.java#ComponentResourcesCommon.createEventLink%28java.lang.String%2Cjava.lang.Object%5B%5D%29
> >("markCollapsed",
> node.getId <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/tree/TreeNode.java#TreeNode.getId%28%29
> >());
>
> 181 <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#181
> >
>
> <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#
> >
>
> 182 <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#182
> >
>
> <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#
> >
>
>                     spec.put
> <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-json/5.3.1/org/apache/tapestry5/json/JSONObject.java#JSONObject.put%28java.lang.String%2Cjava.lang.Object%29
> >("expandChildrenURL",
> expandChildren.toString
> <
> http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b27/java/lang/Object.java#Object.toString%28%29
> >())
>
> 183 <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#183
> >
>
> <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#
> >
>
>                             .put
> <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-json/5.3.1/org/apache/tapestry5/json/JSONObject.java#JSONObject.put%28java.lang.String%2Cjava.lang.Object%29
> >("markExpandedURL",
> markExpanded.toString
> <
> http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b27/java/lang/Object.java#Object.toString%28%29
> >())
>
> 184 <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#184
> >
>
> <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-core/5.3.1/org/apache/tapestry5/corelib/components/Tree.java#
> >
>
>                             .put
> <
> http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tapestry/tapestry-json/5.3.1/org/apache/tapestry5/json/JSONObject.java#JSONObject.put%28java.lang.String%2Cjava.lang.Object%29
> >("markCollapsedURL",
> markCollapsed.toString
> <
> http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b27/java/lang/Object.java#Object.toString%28%29
> >());
>
>
> On Mon, Mar 21, 2016 at 10:47 AM, Qbyte Consulting <
> [hidden email]> wrote:
>
> > Tree uses BaseComponent that doesn't seem to be in tapestry5?
> >
> > On Fri, Mar 18, 2016 at 2:15 PM, Chris Poulsen <[hidden email]>
> > wrote:
> >
> >> Hi,
> >>
> >> Take a look at the Tapestry Tree component to see how recursive
> rendering
> >> can be done.
> >>
> >> --
> >> Chris
> >>
> >> On Fri, Mar 18, 2016 at 6:17 AM, Qbyte Consulting <
> >> [hidden email]
> >> > wrote:
> >>
> >> > I am creating a Menu component (for a ul tag) that needs to
> >> > programmatically generate the child links.
> >> >
> >> > I already have a MenuItem component, but this is parameter bound to a
> >> > template, whereas now I need to programmatically drive the parameters.
> >> >
> >> > I either need to be able to create the existing MenuItem objects
> >> somehow in
> >> > the Menu component and render them, or I simply write the links out in
> >> the
> >> > menu - however since my Menu component isn't extending AbstractLink
> >> (like
> >> > MenuItem) I can't use writeLink().
> >> >
> >> > Any ideas what is the simplest approach to resolve this? Code below.
> >> >
> >> > John
> >> >
> >> >
> >> > public class Menu {
> >> >
> >> >     /** The text. */
> >> >     @Parameter(required = true, allowNull = false, defaultPrefix =
> >> > BindingConstants.LITERAL)
> >> >     private String name;
> >> >
> >> >     /** The resources. */
> >> >     @Inject
> >> >     private ComponentResources resources;
> >> >
> >> >     /** The component source. */
> >> >     @Inject
> >> >     private ComponentSource componentSource;
> >> >
> >> >     @Inject
> >> >     private PageRenderLinkSource linkSource;
> >> >
> >> > /** The messages. */
> >> > @Inject
> >> > private Messages messages;
> >> >
> >> >     @Inject
> >> >     private MenuService menuService;
> >> >
> >> >     /** The state bean. */
> >> >     @SessionState(create = false)
> >> >     private SessionStateBean stateBean;
> >> >
> >> >     /**
> >> >      * If provided, this is the activation context for the target page
> >> (the
> >> >      * information will be encoded into the URL). If not provided,
> then
> >> the
> >> >      * target page will provide its own activation context.
> >> >      */
> >> >     @Parameter
> >> >     private Object[] context;
> >> >
> >> >     /**
> >> >      * Begin render.
> >> >      *
> >> >      * @param writer the writer
> >> >      */
> >> >     void beginRender(MarkupWriter writer) {
> >> >     writer.element("ul");
> >> >     for (String pageName :
> >> > menuService.getPageNames(MenuService.Menu.valueOf(name))) {
> >> >     renderMenuItem(pageName, writer);
> >> >     }
> >> >     }
> >> >
> >> >     private void renderMenuItem(String page, MarkupWriter writer) {
> >> >         Link link = resources.createPageLink(page,
> >> >        resources.isBound("context"), context);
> >> >        writer.element("li");
> >> > NO!!        writeLink(writer, link);
> >> >  writer.writeRaw(messages.get(page.toLowerCase().concat(".link")));
> >> >     writer.end();
> >> >     writer.end();
> >> >     }
> >> >
> >> >     /**
> >> >      * After render.
> >> >      *
> >> >      * @param writer the writer
> >> >      */
> >> >     void afterRender(MarkupWriter writer) {
> >> >    writer.end();
> >> >     }
> >> > }
> >> >
> >> >
> >> > public class MenuItem extends AbstractLink {
> >> >
> >> >     /**
> >> >      * The logical name of the page to link to.
> >> >      */
> >> >     @Parameter(required = true, allowNull = false, defaultPrefix =
> >> > BindingConstants.LITERAL)
> >> >     private String page;
> >> >
> >> >     /** The text. */
> >> >     @Parameter(required = true, allowNull = false, defaultPrefix =
> >> > BindingConstants.LITERAL)
> >> >     private String text;
> >> >
> >> >     /** The resources. */
> >> >     @Inject
> >> >     private ComponentResources resources;
> >> >
> >> >     /** The component source. */
> >> >     @Inject
> >> >     private ComponentSource componentSource;
> >> >
> >> >     /** The state bean. */
> >> >     @SessionState(create = false)
> >> >     private SessionStateBean stateBean;
> >> >
> >> >     /**
> >> >      * If provided, this is the activation context for the target page
> >> (the
> >> >      * information will be encoded into the URL). If not provided,
> then
> >> the
> >> >      * target page will provide its own activation context.
> >> >      */
> >> >     @Parameter
> >> >     private Object[] context;
> >> >
> >> >     /**
> >> >      * Begin render.
> >> >      *
> >> >      * @param writer the writer
> >> >      */
> >> >     void beginRender(MarkupWriter writer) {
> >> > if (isDisabled())
> >> >    return;
> >> >    Link link = resources.createPageLink(page,
> >> >    resources.isBound("context"), context);
> >> >    writer.element("li");
> >> >    writeLink(writer, link);
> >> >    writer.writeRaw(text);
> >> >     }
> >> >
> >> >     /**
> >> >      * After render.
> >> >      *
> >> >      * @param writer the writer
> >> >      */
> >> >     void afterRender(MarkupWriter writer) {
> >> > if (isDisabled())
> >> >    return;
> >> > writer.end();
> >> > writer.end();
> >> >     }
> >> > }
> >> >
> >>
> >
> >
>
Reply | Threaded
Open this post in threaded view
|

Re: render nested component

Thiago H de Paula Figueiredo
In reply to this post by Qbyte Consulting
On Fri, 18 Mar 2016 02:17:44 -0300, Qbyte Consulting  
<[hidden email]> wrote:

> I am creating a Menu component (for a ul tag) that needs to
> programmatically generate the child links.

Hi!

At my job, I've come up with a technique for a component which allows  
other components to render their template in a recursive way. I'll be  
asking my bosses for bringing this to Tapestry itself.

--
Thiago H. de Paula Figueiredo
Tapestry, Java and Hibernate consultant and developer
http://machina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]