1 16 package org.apache.myfaces.wap.renderkit.wml; 17 18 import java.util.List ; 19 import java.util.Map ; 20 import java.util.StringTokenizer ; 21 22 import javax.faces.component.UIComponent; 23 import javax.faces.component.UISelectItem; 24 import javax.faces.component.UISelectItems; 25 import javax.faces.component.UISelectMany; 26 import javax.faces.context.FacesContext; 27 import javax.faces.context.ResponseWriter; 28 import javax.faces.model.SelectItem; 29 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 import org.apache.myfaces.wap.component.SelectMany; 33 import org.apache.myfaces.wap.renderkit.Attributes; 34 import org.apache.myfaces.wap.renderkit.RendererUtils; 35 import org.apache.myfaces.wap.renderkit.WmlRenderUtils; 36 import org.apache.myfaces.wap.renderkit.WmlRenderer; 37 38 46 public class SelectManyRenderer extends WmlRenderer { 47 private static Log log = LogFactory.getLog(SelectManyRenderer.class); 48 49 50 public SelectManyRenderer() { 51 super(); 52 log.debug("created object " + this.getClass().getName()); 53 } 54 55 public void encodeBegin(FacesContext context, UIComponent component) throws java.io.IOException { 56 log.debug("encodeBegin(" + component.getId() + ")"); 57 if (context == null || component == null) { 58 throw new NullPointerException (); 59 } 60 } 61 62 public void encodeChildren(FacesContext context, UIComponent component) throws java.io.IOException { 63 log.debug("encodeChildren(" + component.getId() + ")"); 64 if (context == null || component == null) { 65 throw new NullPointerException (); 66 } 67 } 68 69 public void encodeEnd(FacesContext context, UIComponent component) throws java.io.IOException { 70 log.debug("encodeEnd(" + component.getId() + ")"); 71 if (context == null || component == null) { 72 throw new NullPointerException (); 73 } 74 75 if (!component.isRendered()) return; 76 77 SelectMany comp = (SelectMany)component; 78 79 ResponseWriter writer = context.getResponseWriter(); 80 81 writer.startElement(Attributes.SELECT, component); 82 RendererUtils.writeAttribute(Attributes.ID, comp.getClientId(context), writer); 83 RendererUtils.writeAttribute(Attributes.STYLE_CLASS, comp.getStyleClass(), writer); 84 RendererUtils.writeAttribute(Attributes.XML_LANG, comp.getXmllang(), writer); 85 86 87 88 if (comp.getName() == null) { 89 log.debug("getName is null"); 90 comp.setName(comp.getClientId(context)); 91 } 92 RendererUtils.writeAttribute(Attributes.NAME, comp.getName(), writer); 93 94 RendererUtils.writeAttribute(Attributes.TABINDEX, comp.getTabindex(), writer); 95 RendererUtils.writeAttribute(Attributes.TITLE, comp.getTitle(), writer); 96 97 RendererUtils.writeAttribute(Attributes.VALUE, convertSelectedValuesToString(comp.getSelectedValues()), writer); 99 100 RendererUtils.writeAttribute(Attributes.MULTIPLE, "true", writer); 102 103 List child = component.getChildren(); 104 for (int i = 0; i < child.size(); i++){ 105 if (child.get(i) instanceof UISelectItem){ 106 UISelectItem item = (UISelectItem)child.get(i); 107 WmlRenderUtils.writeOption((SelectItem)item.getValue(), comp, writer); 108 } 109 if (child.get(i) instanceof UISelectItems){ 110 UISelectItems item = (UISelectItems)child.get(i); 111 WmlRenderUtils.writeOptions(item.getValue(), comp, writer); 112 } 113 } 114 115 writer.endElement(Attributes.SELECT); 116 } 117 118 public void decode(FacesContext context, UIComponent component) { 119 log.debug("decode(" + component.getId() + ")"); 120 if (component == null || context == null) throw new NullPointerException (); 121 if (!(component instanceof SelectMany)) 122 log.error("Component " + component.getClass().getName() + " is no SelectMany component, cannot be converted!"); 123 124 SelectMany comp = (SelectMany)component; 125 126 Map map = context.getExternalContext().getRequestParameterMap(); 127 128 if (map.containsKey(comp.getName())){ 130 log.debug("Parameter:" + comp.getName() + " was found in the request. Value: " + (String )map.get(comp.getName())); 131 132 String value = (String )map.get(comp.getName()); 133 comp.setSelectedValues(convertSubmittedValueToObject(value)); 134 } 135 } 136 137 138 public Object getConvertedValue(FacesContext context, UIComponent component, Object submittedValue) throws javax.faces.convert.ConverterException { 139 if (!(component instanceof UISelectMany)) 140 throw new IllegalArgumentException ("Expected component of type UISelectMany. Cannot convert selected value."); 141 142 UISelectMany comp = (UISelectMany)component; 143 144 return(RendererUtils.convertUISelectManyToObject(context, comp, submittedValue)); 145 } 146 147 151 private String convertSelectedValuesToString(Object obj){ 152 if (obj == null) return(null); 153 154 if (!(obj instanceof String [])) 155 throw new ClassCastException ("Value of SelectMany component must be a String[] type."); 156 157 StringBuffer buff = new StringBuffer (); 158 159 String [] selected = (String [])obj; 160 161 for (int i = 0 ; i < selected.length; i++){ 162 String item = selected[i]; 163 164 buff.append(item); 165 166 if (i < selected.length - 1) buff.append(Attributes.SELECT_MANY_SEPARATOR); 168 } 169 return (buff.toString()); 170 } 171 172 173 private String [] convertSubmittedValueToObject(String value){ 174 StringTokenizer st = new StringTokenizer (value, Attributes.SELECT_MANY_SEPARATOR); 175 176 String [] ret = new String [st.countTokens()]; 177 178 log.debug("Selected option:"); 179 for (int i = 0; st.hasMoreTokens(); i++){ 180 ret[i] = st.nextToken(); 181 log.debug(ret[i]); 182 } 183 return (ret); 184 } 185 } 186 187 | Popular Tags |