How to render panel markup to string?

View: New views
3 Messages — Rating Filter:   Alert me  

How to render panel markup to string?

by MartinM :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi!

Can a wicket component be rendered to string in the following use case:

1. I have a table (multiple columns and rows). Each row corresponds to
a person. The columns could represent for rexample a calendar for the
person. Monda, tuesday, etc.

2. For each row, I have summary information. I have a SummaryPanel
that can show this information at the footer of the table.

3. In addition to the footer, I would like to show the SummaryPanel
contents as a tooltip.

4. I would like to attach the tooltip onto an icon in each column for
each row (tooltip content different for different rows, same for
different columns on the same row).

5. Ideally I would do like this:
populateitem(ListItem item) {
  item.add(new Tooltip("tooltip", new AbstractReadOnlyModel<String>()
{ public String getObject() {  RenderUtils.renderToString(new
SummaryPanel("dummy-id"))  } });
}

I.e., I need to render a panel to a string during an ongoing render.

I have found an example for rendering a wicket PAGE
* http://www.danwalmsley.com/2008/10/21/render-a-wicket-page-to-a-string-for-html-email/

Note: I need to render just a panel; so I would rather not frame it with a page.

Furthermore, I have found an example about rendering using a new request cycle:
* http://www.mail-archive.com/users@.../msg22167.html

But in my understanding this cannot be done if an ongoing request is
being processed?

**
Martin

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...


Re: How to render panel markup to string?

by MartinM :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Looking at AjaxRequestTarget, maybe something like this might work?

                // substitute our encoding response for the real one so we can capture
                // component's markup in a manner safe for transport inside CDATA block
                final Response originalResponse = response;
                StringResponse stringResponse = new StringResponse();
                RequestCycle.get().setResponse(stringResponse);

                // Initialize temporary variables
                final Page page = component.findParent(Page.class);
                if (page == null)
                {
                        // dont throw an exception but just ignore this component, somehow
                        // it got
                        // removed from the page.
                        // throw new IllegalStateException(
                        // "Ajax request attempted on a component that is not associated
                        // with a Page");
                        LOG.debug("component: " + component + " with markupid: " + markupId +
                                " not rendered because it was already removed from page");
                        return;
                }

                page.startComponentRender(component);

                try
                {
                        component.prepareForRender();

                        // render any associated headers of the component
                        respondHeaderContribution(response, component);
                }
                catch (RuntimeException e)
                {
                        try
                        {
                                component.afterRender();
                        }
                        catch (RuntimeException e2)
                        {
                                // ignore this one could be a result off.
                        }
                        // Restore original response
                        RequestCycle.get().setResponse(originalResponse);
                        stringResponse.reset();
                        throw e;
                }

                try
                {
                        component.renderComponent();
                }
                catch (RuntimeException e)
                {
                        RequestCycle.get().setResponse(originalResponse);
                        stringResponse.reset();
                        throw e;
                }

                page.endComponentRender(component);

                // Restore original response
                RequestCycle.get().setResponse(originalResponse);

                return stringResponse.toString();


2009/10/30 Martin Makundi <martin.makundi@...>:

> Hi!
>
> Can a wicket component be rendered to string in the following use case:
>
> 1. I have a table (multiple columns and rows). Each row corresponds to
> a person. The columns could represent for rexample a calendar for the
> person. Monda, tuesday, etc.
>
> 2. For each row, I have summary information. I have a SummaryPanel
> that can show this information at the footer of the table.
>
> 3. In addition to the footer, I would like to show the SummaryPanel
> contents as a tooltip.
>
> 4. I would like to attach the tooltip onto an icon in each column for
> each row (tooltip content different for different rows, same for
> different columns on the same row).
>
> 5. Ideally I would do like this:
> populateitem(ListItem item) {
>  item.add(new Tooltip("tooltip", new AbstractReadOnlyModel<String>()
> { public String getObject() {  RenderUtils.renderToString(new
> SummaryPanel("dummy-id"))  } });
> }
>
> I.e., I need to render a panel to a string during an ongoing render.
>
> I have found an example for rendering a wicket PAGE
> * http://www.danwalmsley.com/2008/10/21/render-a-wicket-page-to-a-string-for-html-email/
>
> Note: I need to render just a panel; so I would rather not frame it with a page.
>
> Furthermore, I have found an example about rendering using a new request cycle:
> * http://www.mail-archive.com/users@.../msg22167.html
>
> But in my understanding this cannot be done if an ongoing request is
> being processed?
>
> **
> Martin
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...


Re: How to render panel markup to string?

by MartinM :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Almost.... still needs some tweaking:

org.apache.wicket.WicketRuntimeException: Unable to find the markup
for the component. That may be due to transparent containers or
components implementing IComponentResolver: [MarkupContainer
[Component id = -]]
     at org.apache.wicket.MarkupFragmentFinder.find(MarkupFragmentFinder.java:125)
     at org.apache.wicket.Component.locateMarkupStream(Component.java:3813)
     at org.apache.wicket.Component.renderComponent(Component.java:2547)

**
Martin

2009/10/30 Martin Makundi <martin.makundi@...>:

> Looking at AjaxRequestTarget, maybe something like this might work?
>
>                // substitute our encoding response for the real one so we can capture
>                // component's markup in a manner safe for transport inside CDATA block
>                final Response originalResponse = response;
>                StringResponse stringResponse = new StringResponse();
>                RequestCycle.get().setResponse(stringResponse);
>
>                // Initialize temporary variables
>                final Page page = component.findParent(Page.class);
>                if (page == null)
>                {
>                        // dont throw an exception but just ignore this component, somehow
>                        // it got
>                        // removed from the page.
>                        // throw new IllegalStateException(
>                        // "Ajax request attempted on a component that is not associated
>                        // with a Page");
>                        LOG.debug("component: " + component + " with markupid: " + markupId +
>                                " not rendered because it was already removed from page");
>                        return;
>                }
>
>                page.startComponentRender(component);
>
>                try
>                {
>                        component.prepareForRender();
>
>                        // render any associated headers of the component
>                        respondHeaderContribution(response, component);
>                }
>                catch (RuntimeException e)
>                {
>                        try
>                        {
>                                component.afterRender();
>                        }
>                        catch (RuntimeException e2)
>                        {
>                                // ignore this one could be a result off.
>                        }
>                        // Restore original response
>                        RequestCycle.get().setResponse(originalResponse);
>                        stringResponse.reset();
>                        throw e;
>                }
>
>                try
>                {
>                        component.renderComponent();
>                }
>                catch (RuntimeException e)
>                {
>                        RequestCycle.get().setResponse(originalResponse);
>                        stringResponse.reset();
>                        throw e;
>                }
>
>                page.endComponentRender(component);
>
>                // Restore original response
>                RequestCycle.get().setResponse(originalResponse);
>
>                return stringResponse.toString();
>
>
> 2009/10/30 Martin Makundi <martin.makundi@...>:
>> Hi!
>>
>> Can a wicket component be rendered to string in the following use case:
>>
>> 1. I have a table (multiple columns and rows). Each row corresponds to
>> a person. The columns could represent for rexample a calendar for the
>> person. Monda, tuesday, etc.
>>
>> 2. For each row, I have summary information. I have a SummaryPanel
>> that can show this information at the footer of the table.
>>
>> 3. In addition to the footer, I would like to show the SummaryPanel
>> contents as a tooltip.
>>
>> 4. I would like to attach the tooltip onto an icon in each column for
>> each row (tooltip content different for different rows, same for
>> different columns on the same row).
>>
>> 5. Ideally I would do like this:
>> populateitem(ListItem item) {
>>  item.add(new Tooltip("tooltip", new AbstractReadOnlyModel<String>()
>> { public String getObject() {  RenderUtils.renderToString(new
>> SummaryPanel("dummy-id"))  } });
>> }
>>
>> I.e., I need to render a panel to a string during an ongoing render.
>>
>> I have found an example for rendering a wicket PAGE
>> * http://www.danwalmsley.com/2008/10/21/render-a-wicket-page-to-a-string-for-html-email/
>>
>> Note: I need to render just a panel; so I would rather not frame it with a page.
>>
>> Furthermore, I have found an example about rendering using a new request cycle:
>> * http://www.mail-archive.com/users@.../msg22167.html
>>
>> But in my understanding this cannot be done if an ongoing request is
>> being processed?
>>
>> **
>> Martin
>>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...