The optgroup code might be buggy, here is a fresh one:

  private String previouslyAppendedOptGroupLabel;
  private int choices;

  /**
   * @see
org.apache.wicket.markup.html.form.AbstractChoice#onComponentTagBody(org.apache.wicket.markup.MarkupStream,
org.apache.wicket.markup.ComponentTag)
   */
  @Override
  protected void onComponentTagBody(MarkupStream markupStream,
      ComponentTag openTag) {
    previouslyAppendedOptGroupLabel = null;
    choices = getChoices().size();
    super.onComponentTagBody(markupStream, openTag);
  }


/**
   * @see
org.apache.wicket.markup.html.form.AbstractChoice#appendOptionHtml(org.apache.wicket.util.string.AppendingStringBuffer,
java.lang.Object, int, java.lang.String)
   */
  @Override
  protected void appendOptionHtml(AppendingStringBuffer buffer, T choice,
      int index, String selected) {
    AppendingStringBuffer tmp = new AppendingStringBuffer(50);
    super.appendOptionHtml(tmp, choice, index, selected);

    if (getChoiceRenderer() instanceof IStyledChoiceRenderer) {
      IStyledChoiceRenderer<T> styledChoiceRenderer =
(IStyledChoiceRenderer<T>) getChoiceRenderer();

      String currentOptGroupLabel =
styledChoiceRenderer.getOptGroupLabel(choice);

      if (!Utils.equalsOrNull(currentOptGroupLabel,
previouslyAppendedOptGroupLabel)) {
        // OptGroup changed
        if (previouslyAppendedOptGroupLabel != null) {
          endOptGroup(buffer);
        }

        if (currentOptGroupLabel != null) {
          // OptGroup started
          int start = tmp.indexOf("<option");
          StringBuilder label = new
StringBuilder(currentOptGroupLabel.length() + 19);
          label.append("<optgroup
label=\"").append(currentOptGroupLabel).append("\">");
          tmp.insert(start, label);
        }
      }

      if ((currentOptGroupLabel != null) && (index == (choices-1))) {
        // Last option group must end too
        endOptGroup(tmp);
      }

      {
        String cssClass =
styledChoiceRenderer.getOptionCssClassName(choice);
        if (cssClass != null) {
          int start = tmp.indexOf("<option");
          tmp.insert(start + 7, getClass(cssClass));
        }
      }

      previouslyAppendedOptGroupLabel = currentOptGroupLabel;
    }

    buffer.append(tmp);
  }

  /**
   * @param cssClass
   * @return StringBuilder
   */
  private StringBuilder getClass(String cssClass) {
    return new StringBuilder(" class=\"").append(cssClass).append("\"");
  }

  /**
   * @see
org.apache.wicket.markup.html.form.AbstractSingleSelectChoice#getDefaultChoice(java.lang.Object)
   */
  @Override
  protected CharSequence getDefaultChoice(Object selected) {
    CharSequence charSequence = super.getDefaultChoice(selected);

    if (charSequence.toString().contains(TOKEN) && (getChoiceRenderer()
instanceof IStyledChoiceRenderer)) {
      AppendingStringBuffer buffer = new
AppendingStringBuffer(charSequence);
      IStyledChoiceRenderer<T> styledChoiceRenderer =
(IStyledChoiceRenderer<T>) getChoiceRenderer();
      String cssClass = styledChoiceRenderer.getOptionCssClassName(null);
      if (!Utils.isEmpty(cssClass)) {
        buffer.insert(buffer.indexOf(TOKEN), getClass(cssClass));
        return buffer;
      }
    }

    return charSequence;
  }

  /**
   * @param tmp
   */
  private void endOptGroup(AppendingStringBuffer tmp) {
    // OptGroup ended
    int start = tmp.lastIndexOf("</option>");
    tmp.insert(start + 9, "</optgroup>");
  }


2014-06-05 9:37 GMT+03:00 Thies Edeling <tedel...@gmail.com>:

> thanks!
>
>
> On Thu, Jun 5, 2014 at 1:48 AM, Martin Makundi <
> martin.maku...@koodaripalvelut.com> wrote:
>
> > Look at:
> >
> >
> >
> http://mail-archives.apache.org/mod_mbox/wicket-users/200912.mbox/%3c303141550912040641r1e00841dudaacfefda9497...@mail.gmail.com%3E
> >
> >
> > 2014-06-05 1:41 GMT+03:00 Thies Edeling <tedel...@gmail.com>:
> >
> > > Is there any support for optgroup's in Wicket? I can only find some
> > > examples using ListView's and other hacky solutions so I'm guessing
> there
> > > isn't :) Thanks !
> > >
> > > gr
> > > Thies
> > >
> >
>

Reply via email to