Hi!

Thanks for pointing that out.. must have been drunk when I coded that >8-)

Here is an improved version:

/**
   * @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, new StringBuilder("
class=\"").append(cssClass).append("\""));
        }
      }

      previouslyAppendedOptGroupLabel = currentOptGroupLabel;
    }

    buffer.append(tmp);
  }

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


It produces:

<select wicketpath="ddc" name="ddc">

<option value="" selected="selected">Valitse yksi</option>

<optgroup label="group0">
  <option value="0">text0</option>

  <option value="1">text1</option>

  <option value="2">text2</option>
</optgroup>

<optgroup label="group1">
  <option value="3">text0</option>

  <option value="4">text1</option>

  <option value="5">text2</option>
</optgroup>

<optgroup label="group2">
  <option value="6">text0</option>

  <option value="7">text1</option>

  <option value="8">text2</option>
</optgroup>
</select>

2010/1/11 aditsu <[email protected]>:
>
>
> MartinM wrote:
>>
>> Hmm.. mine seems to work fine with some other test cases, is this the
>> same code you have or is it different:
>>
>
> It's the same, except for "Utils.equalsOrNull" which must be from your
> unpublished code, and I replaced it with one of my own utility methods. Just
> try my test case.
>
> Adrian
> --
> View this message in context: 
> http://old.nabble.com/DropDownChoiceWithStylingOptions%3CT%3E-tp26642690p27112556.html
> Sent from the Wicket - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to