After grepping through the Facelets source code, I discovered that HtmlComponentHandler is the TagHandler associated with the dataTable component (and all other HTML components as well). So I extended this with my own class, HtmlDataTableHandler:
package portletfacelets;
import javax.faces.component.UIComponent;
import javax.faces.component.html.HtmlDataTable;
import com.sun.facelets.FaceletContext;
import com.sun.facelets.tag.jsf.ComponentConfig;
import com.sun.facelets.tag.jsf.html.HtmlComponentHandler;
public class HtmlDataTableHandler extends HtmlComponentHandler {
public HtmlDataTableHandler(ComponentConfig config) {
super(config);
}
@Override
protected void onComponentCreated(FaceletContext ctx,
UIComponent c, UIComponent parent) {
super.onComponentCreated(ctx, c, parent);
if (c instanceof HtmlDataTable) {
HtmlDataTable table = (HtmlDataTable) c;
if (table.getFooterClass() == null) {
table.setFooterClass("portlet-section-footer");
}
if (table.getHeaderClass() == null) {
table.setHeaderClass("portlet-section-header");
}
if (table.getRowClasses() == null) {
table.setRowClasses("portlet-section-body," +
"portlet-section-alternate");
}
if (table.getStyleClass() == null) {
table.setStyleClass("portlet-section-body");
}
}
}
}
I check if the classes are null, to allow the user of the TagHandler to override the styles, but that's about it. The facelets taglib entry looks like this:
<tag>
<tag-name>dataTable2</tag-name>
<component>
<component-type>javax.faces.HtmlDataTable</component-type>
<renderer-type>javax.faces.Table</renderer-type>
<handler-class>portletfacelets.HtmlDataTableHandler</handler-class>
</component>
</tag>
I'm not sure which method I prefer, but I'm leaning toward the former, since it seems simpler and I like the idea of keeping things like CSS styles in a template file than in Java code, but in some sense this second approach also seems to be the cleaner one.
No comments:
Post a Comment