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(" *");
  }
  public void decode(Object input, WebuiRequestContext context) throws Exception {
    String[] values = context.getRequestParameterValues(getId());
    if (values == null) {
      value_ = null;
      for (SelectItem option : options_) {
        if (option instanceof SelectOption) ((SelectOption) option).setSelected(false);
        else if (option instanceof SelectOptionGroup) {
          for (SelectOption opt : ((SelectOptionGroup) option).getOptions()) {
            opt.setSelected(false);
          }
        }
      }
      return;
    }

    int i = 0;
    value_ = values[0];
    for (SelectItem item : options_) {
      if (item instanceof SelectOption) {
        if (i > -1 && ((SelectOption) item).getValue().equals(values[i])) {
          ((SelectOption) item).setSelected(true);
          if (values.length == ++i) i = -1;
        } else ((SelectOption) item).setSelected(false);
      } else if (item instanceof SelectOptionGroup) {
        for (SelectOption opt : ((SelectOptionGroup) item).getOptions()) {
          if (i > -1 && (opt).getValue().equals(values[i])) {
            (opt).setSelected(true);
            if (values.length == ++i) i = -1;
          } else {
            (opt).setSelected(false);
          }
        }
      }
    }
  }
 @Override
 protected int compareInstances(final SelectOption option1, final SelectOption option2) {
   int result = compareValues(option1.getName(), option2.getName(), false);
   if (EQUAL == result) {
     result = compareValues(option1.getCode(), option2.getCode(), false);
   }
   return result;
 }
 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();
  }
  public UIFormSelectBoxWithGroups setValue(String value) {
    value_ = value;
    for (SelectItem option : options_) {
      if (option instanceof SelectOption) {
        if (((SelectOption) option).getValue().equals(value_))
          ((SelectOption) option).setSelected(true);
        else ((SelectOption) option).setSelected(false);
      } else if (option instanceof SelectOptionGroup) {
        ((SelectOptionGroup) option).setValue(value);
      }
    }

    return this;
  }
  public UIFormSelectBoxWithGroups setSelectedValues(String[] values) {
    for (SelectItem option : options_) {
      if (option instanceof SelectOption) {
        ((SelectOption) option).setSelected(false);
        for (String value : values) {
          if (value.equals(((SelectOption) option).getValue())) {
            ((SelectOption) option).setSelected(true);
            break;
          }
        }
      } else if (option instanceof SelectOptionGroup) {
        ((SelectOptionGroup) option).setSelectedValue(values);
      }
    }

    return this;
  }
 @Override
 public void reset() {
   if (options_ == null || options_.size() < 1) return;
   for (SelectItem option : options_) {
     if (option instanceof SelectOption) ((SelectOption) option).setSelected(false);
     else if (option instanceof SelectOptionGroup) {
       ((SelectOptionGroup) option).reset();
     }
   }
   for (SelectItem option : options_) {
     if (option instanceof SelectOption) {
       value_ = ((SelectOption) option).getValue();
       ((SelectOption) option).setSelected(true);
       break;
     }
   }
 }