public void processRender(WebuiRequestContext context) throws Exception { ResourceBundle res = context.getApplicationResourceBundle(); UIForm uiForm = getAncestorOfType(UIForm.class); String formId = null; if (uiForm.getId().equals("UISearchForm")) formId = uiForm.<UIComponent>getParent().getId(); else formId = uiForm.getId(); Writer w = context.getWriter(); w.write("<select class=\"selectbox\" id=\""); w.write(getId()); w.write("\" name=\""); w.write(name); w.write("\""); renderHTMLAttributes(w); if (onchange_ != null) { w.append(" onchange=\"").append(renderOnChangeEvent(uiForm)).append("\""); } if (isMultiple_) w.write(" multiple=\"true\""); if (size_ > 1) w.write(" size=\"" + size_ + "\""); if (isDisabled()) w.write(" disabled "); w.write(">\n"); for (SelectItem item : options_) { String label = item.getLabel(); if (item instanceof SelectOption) { try { label = res.getString(formId + ".label.option." + ((SelectOption) item).getValue()); } catch (MissingResourceException ex) { label = formId + ".label.option." + ((SelectOption) item).getValue(); } w.write(renderOption(((SelectOption) item), label)); } else if (item instanceof SelectOptionGroup) { label = item.getLabel(); try { label = res.getString(getFrom().getId() + ".optionGroup.label." + label); } catch (MissingResourceException ex) { log.info("Could not find: " + getFrom().getId() + ".optionGroup.label." + label); } w.write("<optgroup label=\""); w.write(label); w.write("\">\n"); for (SelectOption opt : ((SelectOptionGroup) item).getOptions()) { label = opt.getLabel(); try { label = res.getString(formId + ".label.option." + opt.getValue()); } catch (MissingResourceException ex) { label = formId + ".label.option." + opt.getValue(); } w.write(renderOption(opt, label)); } w.write("</optgroup>\n"); } } w.write("</select>\n"); if (this.isMandatory()) w.write(" *"); }
private String renderOption(SelectOption option, String label) { StringBuffer buf = new StringBuffer(); buf.append("<option value=\""); buf.append(option.getValue()); buf.append("\""); if (option.isSelected()) buf.append("selected=\"selected\""); buf.append(">"); buf.append(label); buf.append("</option>\n"); return buf.toString(); }
/** * Returns a HMTL representation of an option * * @param option option to HTML-ify * @return the HTML string */ public static String toHtml(SelectOption option) { StringBuffer html = new StringBuffer(); html.append("<option value=\"" + option.getValue() + "\""); if (option.selected) html.append(" selected"); html.append(">" + PortalUtil.htmlEscape(option.getDescription()) + "</option>"); return html.toString(); }