jsf - How to order SelectItems in custom renderer extending MyFaces HtmlMenuRenderer -
i´m trying migrate custom component render selectonemenu on myfaces 2.0, need use library because 8.5 moved mojarra 1.2 myfaces 2.0 implementation.
in mojarra 1.2, extended com.sun.faces.renderkit.html_basic.menurenderer
had method renderoptions
receives parameters facescontext context, uicomponent component, list<selectitem> listselectitems
, myfaces haven´t similar method.
this method used order items before render (mojarra 1.2):
@override protected void renderoptions(facescontext context, uicomponent component, list<selectitem> listselectitems) throws ioexception { cobisselectonemenuuicomponent uicomponent = (cobisselectonemenuuicomponent) component; string order = uicomponent.getorder(); if (order == null || order.isempty()) { order = default_order; } if (order.equals("asc")) { collections.sort(listselectitems, comparatorselectitemlabel); } else { if (order.equals("desc")) { collections.sort(listselectitems, comparatorselectitemlabelinvers); } } super.renderoptions(context, uicomponent, listselectitems); }
and whit myfaces dont find way order selectitems before render them.
i try build selectonemenu myfaces using code component (the relevant parts):
package cobiscorp.cobis.ce.components.jsf; import javax.faces.component.facescomponent; import javax.faces.component.uiselectitems; import javax.faces.component.html.htmlselectonemenu; @facescomponent(value=cobisselectonemenucomponent.component_type) public class cobisselectonemenucomponent extends htmlselectonemenu { /** * component family of {@link cobisselectonemenucomponent}. * */ public static final string component_family = "cobisselectonemenucomponent"; /** * component type of {@link cobisselectonemenucomponent}. * */ public static final string component_type = "cobisselectonemenucomponent"; @override public string getfamily() { return cobisselectonemenucomponent.component_family; } public static final string attr_order = "order"; public static final string attr_order_default = "asc"; public string getorder() { return (string) getstatehelper().eval(attr_order, attr_order_default); } public void setorder(string order) { getstatehelper().put(attr_order, order); } }
and render (the relevant parts):
package cobiscorp.cobis.ce.components.render; import java.io.ioexception; import java.util.arraylist; import java.util.collections; import java.util.list; import javax.faces.component.uicomponent; import javax.faces.component.uiselectitems; import javax.faces.component.uiselectone; import javax.faces.context.facescontext; import javax.faces.context.responsewriter; import javax.faces.convert.converter; import javax.faces.model.selectitem; import javax.faces.render.facesrenderer; import org.apache.myfaces.renderkit.html.htmlmenurenderer; import org.apache.myfaces.shared_impl.renderkit.rendererutils; import cobiscorp.cobis.ce.components.jsf.cobisselectonemenucomponent; import cobiscorp.cobis.ce.domains.icobisconstants; import cobiscorp.cobis.ce.util.comparatoruicomponentitemasc; import cobiscorp.cobis.ce.util.comparatoruicomponentitemdesc; @facesrenderer(componentfamily = cobisselectonemenucomponent.component_family, renderertype = cobisselectonemenurenderer.renderer_type) public class cobisselectonemenurenderer extends htmlmenurenderer { /** * renderer type of {@link cobisselectonemenurenderer}. * */ public static final string renderer_type = "cobiscorp.cobis.ce.components.cobisselectonemenurenderer"; /** * el atributo comparatorselectitemlabelasc tiene como finalidad comparar los * select item por los label en orden ascendente */ private comparatoruicomponentitemasc comparatorselectitemlabelasc; /** * el atributo comparatorselectitemlabeldesc tiene como finalidad comparar * los select item por los label en orden descendente */ private comparatoruicomponentitemdesc comparatorselectitemlabeldesc; /** * este constructor inicializa los comparadores */ private boolean loaddefaultoption = null; public cobisselectonemenurenderer() { comparatorselectitemlabelasc = new comparatoruicomponentitemasc(); comparatorselectitemlabeldesc = new comparatoruicomponentitemdesc(); } /* * (non-javadoc) * * @see * com.sun.faces.renderkit.html_basic.menurenderer#encodebegin(javax.faces * .context.facescontext, javax.faces.component.uicomponent) */ public void encodebegin(facescontext context, uicomponent component) throws ioexception { cobisselectonemenucomponent uicomponent = (cobisselectonemenucomponent) component; loaddefaultoption = uicomponent.getloaddefaultoption(); if(loaddefaultoption){ list<selectitem> listitems = new arraylist<selectitem>(1); listitems.add(new selectitem("", "")); uiselectitems uiselectitems = new uiselectitems(); uiselectitems.setvalue(listitems); uicomponent.addselectitemsdefault(uiselectitems); } super.encodebegin(context, uicomponent); } protected void rendermenu(facescontext context, uiselectone selectone, boolean disabled, converter converter) throws ioexception { system.out.println("rendermenu"); cobisselectonemenucomponent uicomponent = (cobisselectonemenucomponent) selectone; string order = uicomponent.getorder(); uicomponent.getchildren(); list selectitemlist = rendererutils.getselectitemlist( (uiselectone) uicomponent, context); system.out.println("rendermenu order -> " + order); if (order.equals("asc")) { collections.sort(selectitemlist, comparatorselectitemlabelasc); } else { if (order.equals("desc")) { collections.sort(selectitemlist, comparatorselectitemlabeldesc); } } } /* * (non-javadoc) * * @see * com.sun.faces.renderkit.html_basic.menurenderer#encodeend(javax.faces * .context.facescontext, javax.faces.component.uicomponent) */ public void encodeend(facescontext context, uicomponent component) throws ioexception { boolean autocomplete = null; string id = null; cobisselectonemenucomponent uicomponent = (cobisselectonemenucomponent) component; this.rendermenu(context, uicomponent, isdisabled(context, uicomponent), getconverter(context, uicomponent)); super.encodeend(context, component); if (context != null) { responsewriter writer = context.getresponsewriter(); if (component.isrendered() && uicomponent.isdisabled() == false) { autocomplete = uicomponent.getautocomplete(); id = uicomponent.getid().trim(); if (autocomplete != null && autocomplete.equals("true")) { writer.startelement("script", component); writer.writeattribute("type", "text/javascript", "type"); writer.writetext(string.format( icobisconstants.script_dhtml_list, id, uicomponent .getclientid(context)), null); writer.endelement("script"); } } } } }
whit previous code component render without order, sample component code in xhtml page:
<cobis:selectonemenu idform="form_cita_uno" order="asc"> <f:selectitem itemlabel="c" itemvalue="c"></f:selectitem> <f:selectitem itemlabel="a" itemvalue="a"></f:selectitem> <f:selectitem itemlabel="f" itemvalue="f"></f:selectitem> <f:selectitem itemlabel="d" itemvalue="d"></f:selectitem> <f:selectitem itemlabel="n" itemvalue="n"></f:selectitem> <f:selectitem itemlabel="l" itemvalue="l"></f:selectitem> <f:selectitem itemlabel="z" itemvalue="z"></f:selectitem> <f:selectitem itemlabel="x" itemvalue="z"></f:selectitem> </cobis:selectonemenu>
could give me ideas how order items version 2 of myfaces?
Comments
Post a Comment