Custom component - Pivot Table
marchewk Jun 12, 2009 3:53 AMHi.
I want to write custom component that shows in my application an Pivot Table with links in header such as Down, Up etc...
I dont want to use CDK, because this component is too complicated to write it with any template.
I write already component, tag and renderer class and configure them in eclipse project. My component works as simple JSF component and writes to output html table, but I dont know how can I write link to raise custom events.
What should I do to enable Ajax events for custom component?
Can someone send me any custom component with custom events, that would be base for my component?
My code is below:
DataSubgroupRenderer:
package bdr.components; import java.io.IOException; import java.util.Map; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.context.ResponseWriter; import javax.faces.render.Renderer; public class DataSubgroupRenderer extends Renderer { @SuppressWarnings("unchecked") @Override public void decode(FacesContext context, UIComponent component) { //super.decode(context, component); Map requestMap = context.getExternalContext().getRequestParameterMap(); String clientId = component.getClientId(context); UIDataSubgroup dsComp = (UIDataSubgroup)component; try { if (requestMap.get(clientId+ ".vertical")!=null) { dsComp.setHorizontal(false); } else if (requestMap.get(clientId+ ".horizontal")!=null) { dsComp.setHorizontal(true); } dsComp.setValid(true); } catch (NumberFormatException ex) { dsComp.setSubmittedValue((String) requestMap.get(clientId)); } } @Override public void encodeBegin(FacesContext context, UIComponent component) throws IOException { //super(context, component); ResponseWriter writer = context.getResponseWriter(); String clientId = component.getClientId(context); UIDataSubgroup dsComp = (UIDataSubgroup)component; //generate buttons if (dsComp.isHorizontal()) { writer.startElement("input", dsComp); writer.writeAttribute("type", "Submit", null); writer.writeAttribute("name", clientId+".vertical", "clientId"); writer.writeAttribute("value", "Goto Vertical", null); writer.endElement("input"); } else { writer.startElement("input", dsComp); writer.writeAttribute("type", "Submit", null); writer.writeAttribute("name", clientId+".horizontal", "clientId"); writer.writeAttribute("value", "Goto Horizontal", null); writer.endElement("input"); } //generate table if (dsComp.isHorizontal()) { writer.write("<table border='1' cellspacing='0' cellpadding='0'>"); for (int i=0;i<Integer.valueOf(dsComp.getRows()).intValue();i++) { writer.write("<tr>"); for (int j=0;j<Integer.valueOf(dsComp.getColumns()).intValue();j++) { writer.write("<td>"+i+","+j+"</td>"); } writer.write("</tr>"); } writer.write("</table>"); } else { writer.write("<table border='1' cellspacing='0' cellpadding='0'>"); for (int i=0;i<Integer.valueOf(dsComp.getColumns()).intValue();i++) { writer.write("<tr>"); for (int j=0;j<Integer.valueOf(dsComp.getRows()).intValue();j++) { writer.write("<td>"+i+","+j+"</td>"); } writer.write("</tr>"); } writer.write("</table>"); } } }
DataSubgroupTag:
package bdr.components; import javax.el.ValueExpression; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.webapp.UIComponentELTag; public class DataSubgroupTag extends UIComponentELTag { public ValueExpression rows; public ValueExpression columns; @Override public String getComponentType() { return UIDataSubgroup.COMPONENT_TYPE; } @Override public String getRendererType() { return UIDataSubgroup.RENDERER_TYPE; } @Override protected void setProperties(UIComponent component) { super.setProperties(component); UIDataSubgroup dsComp = null; try { dsComp = (UIDataSubgroup)component; } catch (ClassCastException ex) { throw new IllegalStateException("Component: "+component.toString()+" not expected type. Expected bdr.components.UIDataSubgroup."); } if (rows != null) { component.setValueExpression("rows", this.rows); } if (columns != null) { component.setValueExpression("columns", this.columns); } } public void release() { super.release(); rows = null; columns = null; } public ValueExpression getRows() { return rows; } public void setRows(ValueExpression rows) { this.rows = rows; } public ValueExpression getColumns() { return columns; } public void setColumns(ValueExpression columns) { this.columns = columns; } }
UIDataSubgroup:
package bdr.components; import javax.el.ValueExpression; import javax.faces.component.UIInput; import javax.faces.context.FacesContext; public class UIDataSubgroup extends UIInput { public static final String COMPONENT_TYPE = "bdr.components.DataSubgroup"; public static final String RENDERER_TYPE = "bdr.components.DataSubgroupRenderer"; private boolean horizontal = true; private Integer rows; private Integer columns; public UIDataSubgroup() { setRendererType(RENDERER_TYPE); this.horizontal = true; } @Override public String getFamily() { return COMPONENT_TYPE; } public boolean isHorizontal() { return horizontal; } public void setHorizontal(boolean horizontal) { this.horizontal = horizontal; } public Integer getRows() { if (null!=this.rows) { return this.rows; } ValueExpression _ve = getValueExpression("rows"); Object val = _ve.getValue(getFacesContext().getELContext()); return (_ve!=null)?(Integer)_ve.getValue(getFacesContext().getELContext()):null; } public void setRows(Integer rows) { this.rows = rows; } public Integer getColumns() { if (null!=this.columns) { return this.columns; } ValueExpression _ve = getValueExpression("columns"); return (_ve!=null)?(Integer)_ve.getValue(getFacesContext().getELContext()):null; } public void setColumns(Integer columns) { this.columns = columns; } @Override public void restoreState(FacesContext context, Object state) { Object values[] = (Object[]) state; super.restoreState(context, values[0]); if (((String)values[1]).equals("H")) this.horizontal = true; else this.horizontal = false; this.rows = (Integer)values[2]; this.columns = (Integer)values[3]; } @Override public Object saveState(FacesContext context) { Object values[] = new Object[4]; values[0] = super.saveState(context); if (this.horizontal) values[1] = "H"; else values[1] = "V"; values[2] = this.rows; values[3] = this.columns; return values; } }
bdr-components-taglib.xml
<taglib xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd" version="2.1"> <tlib-version>1.0</tlib-version> <short-name>bdr-components-taglib</short-name> <uri>bdr-components-taglib</uri> <tag> <name>dataSubgroup</name> <tag-class>bdr.components.DataSubgroupTag</tag-class> <attribute> <name>binding</name> <description> A value binding that points to a bean property </description> </attribute> <attribute> <name>id</name> <description>The client id of this component</description> </attribute> <attribute> <name>rendered</name> <description>Is this component rendered?</description> </attribute> <attribute> <name>rows</name> <description>Is this component rendered?</description> <rtexprvalue>true</rtexprvalue> <deferred-value><type>java.lang.Object</type></deferred-value> </attribute> <attribute> <name>columns</name> <description>Is this component rendered?</description> <rtexprvalue>true</rtexprvalue> <deferred-value><type>java.lang.Object</type></deferred-value> </attribute> </tag> </taglib>
faces-config.xml
<?xml version="1.0" encoding="UTF-8"?> <faces-config xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd" version="1.2"> <application> <message-bundle>resources.application</message-bundle> <locale-config> <default-locale>en</default-locale> <supported-locale>pl</supported-locale> <supported-locale>en</supported-locale> </locale-config> </application> <component> <component-type>bdr.components.DataSubgroup</component-type> <component-class>bdr.components.UIDataSubgroup</component-class> <component-extension> <renderer-type>bdr.components.DataSubgroupRenderer</renderer-type> </component-extension> </component> <render-kit> <description>Renderkit implementation for the DataSubgroup component</description> <renderer> <component-family>bdr.components.DataSubgroup</component-family> <renderer-type>bdr.components.DataSubgroupRenderer</renderer-type> <renderer-class>bdr.components.DataSubgroupRenderer</renderer-class> </renderer> </render-kit> <managed-bean> <managed-bean-name>dataSubgrBean</managed-bean-name> <managed-bean-class>bdr.beans.DataSubgroupBean</managed-bean-class> <managed-bean-scope>session</managed-bean-scope> </managed-bean> </faces-config>
Please help me. Thanks.
SM