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();
}
}