Free4U

public class DropDownChoiceWithStylingOptions<T> extends DropDownChoice<T> {
  private String previouslyAppendedOptGroupLabel;
  private int choices;

  /**
   * @param id
   * @param choices
   * @param renderer
   */
  public DropDownChoiceWithStylingOptions(String id,
      IModel<? extends List<? extends T>> choices,
      IChoiceRenderer<? super T> renderer) {
    super(id, choices, renderer);
  }

  /**
   * @param id
   * @param choices
   */
  public DropDownChoiceWithStylingOptions(String id,
      IModel<? extends List<? extends T>> choices) {
    super(id, choices);
  }

  /**
   * @param id
   * @param model
   * @param choices
   * @param renderer
   */
  public DropDownChoiceWithStylingOptions(String id, IModel<T> model,
      IModel<? extends List<? extends T>> choices,
      IChoiceRenderer<? super T> renderer) {
    super(id, model, choices, renderer);
  }

  /**
   * @param id
   * @param model
   * @param choices
   */
  public DropDownChoiceWithStylingOptions(String id, IModel<T> model,
      IModel<? extends List<? extends T>> choices) {
    super(id, model, choices);
  }

  /**
   * @param id
   * @param model
   * @param data
   * @param renderer
   */
  public DropDownChoiceWithStylingOptions(String id, IModel<T> model,
      List<? extends T> data, IChoiceRenderer<? super T> renderer) {
    super(id, model, data, renderer);
  }

  /**
   * @param id
   * @param model
   * @param choices
   */
  public DropDownChoiceWithStylingOptions(String id, IModel<T> model,
      List<? extends T> choices) {
    super(id, model, choices);
  }

  /**
   * @param id
   * @param data
   * @param renderer
   */
  public DropDownChoiceWithStylingOptions(String id, List<? extends T> data,
      IChoiceRenderer<? super T> renderer) {
    super(id, data, renderer);
  }

  /**
   * @param id
   * @param choices
   */
  public DropDownChoiceWithStylingOptions(String id, List<? extends T>
choices) {
    super(id, choices);
  }

  /**
   * @param id
   */
  public DropDownChoiceWithStylingOptions(String id) {
    super(id);
  }

  /**
   * @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(tmp);
        }
        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.indexOf("</option>");
    tmp.insert(start + 9, "</optgroup>");
  }
}



public interface IStyledChoiceRenderer<T> extends IChoiceRenderer<T> {
  /**
   * @param t
   * @return String
   */
  public String getOptGroupLabel(T t);

  /**
   * @param t
   * @return String
   */
  public String getOptionCssClassName(T t);
}

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org

Reply via email to