Skip to content

Instantly share code, notes, and snippets.

@martin-g
Created July 8, 2010 19:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martin-g/468502 to your computer and use it in GitHub Desktop.
Save martin-g/468502 to your computer and use it in GitHub Desktop.
Custom component with custom markup
package com.mycompany;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.IMarkupCacheKeyProvider;
import org.apache.wicket.markup.IMarkupResourceStreamProvider;
import org.apache.wicket.markup.MarkupException;
import org.apache.wicket.markup.MarkupStream;
import org.apache.wicket.markup.html.WebMarkupContainerWithAssociatedMarkup;
import org.apache.wicket.util.resource.IResourceStream;
import org.apache.wicket.util.resource.StringResourceStream;
/**
* WebMarkupContainerWithAssociatedMarkup with a custom template
*/
public class CustomTemplateComponent extends WebMarkupContainerWithAssociatedMarkup
implements IMarkupResourceStreamProvider, IMarkupCacheKeyProvider {
private static final long serialVersionUID = 1L;
// use existing <wicket:xyz> that renders only its body => wicket:container
private final static String WICKET_TAG_NAME = "container";
public CustomTemplateComponent(String id) {
super(id);
}
@Override
protected void onComponentTagBody(final MarkupStream markupStream, final ComponentTag openTag)
{
// Use
renderAssociatedMarkup(WICKET_TAG_NAME,
"Markup for a panel component has to contain part '<wicket:"+WICKET_TAG_NAME+">'");
if (openTag.isOpenClose() == false)
{
// Skip any raw markup in the body
markupStream.skipRawMarkup();
if (markupStream.get().closes(openTag) == false)
{
throw new MarkupException(markupStream, "close tag not found for tag: " +
openTag.toString() + ". Component: " + this.toString());
}
}
}
public String getCacheKey(MarkupContainer container, Class<?> containerClass) {
// return 'null' to don't cache it
return null;
}
public IResourceStream getMarkupResourceStream(MarkupContainer container,
Class<?> containerClass) {
return new StringResourceStream(getOpenTag()+"custom template"+getCloseTag());
}
private String getOpenTag() {
return "<wicket:"+WICKET_TAG_NAME+">";
}
private String getCloseTag() {
return "</wicket:"+WICKET_TAG_NAME+">";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment