Example usage for org.apache.wicket.markup.html.form IChoiceRenderer getDisplayValue

List of usage examples for org.apache.wicket.markup.html.form IChoiceRenderer getDisplayValue

Introduction

In this page you can find the example usage for org.apache.wicket.markup.html.form IChoiceRenderer getDisplayValue.

Prototype

Object getDisplayValue(T object);

Source Link

Document

Get the value for displaying to an end user.

Usage

From source file:ca.travelagency.components.fields.GenericDropDownChoiceTest.java

License:Apache License

@Test
public void testRender() throws Exception {
    // setup//from w ww .j  a  v  a  2 s .  c o  m
    SystemUser systemUser = SystemUserHelper.makeSystemUser();
    // Execute
    IChoiceRenderer<SystemUser> renderer = fixture.makeRenderer();
    // validate
    Assert.assertEquals(systemUser.getName(), renderer.getDisplayValue(systemUser));
    Assert.assertEquals("" + systemUser.getId(), renderer.getIdValue(systemUser, 0));
}

From source file:com.evolveum.midpoint.web.page.admin.configuration.PageDebugList.java

License:Apache License

private IModel<List<ObjectTypes>> createChoiceModel(final IChoiceRenderer<ObjectTypes> renderer) {
    return new LoadableModel<List<ObjectTypes>>(false) {

        @Override//from   w  w  w  . j av  a  2s .  c o m
        protected List<ObjectTypes> load() {
            List<ObjectTypes> choices = new ArrayList<ObjectTypes>();

            Collections.addAll(choices, ObjectTypes.values());
            choices.remove(ObjectTypes.OBJECT);

            Collections.sort(choices, new Comparator<ObjectTypes>() {

                @Override
                public int compare(ObjectTypes o1, ObjectTypes o2) {
                    String str1 = (String) renderer.getDisplayValue(o1);
                    String str2 = (String) renderer.getDisplayValue(o2);
                    return String.CASE_INSENSITIVE_ORDER.compare(str1, str2);
                }
            });

            return choices;
        }
    };
}

From source file:com.francetelecom.clara.cloud.presentation.releases.ReleaseOverrideProfilePanelTest.java

License:Apache License

@Test
public void profile_renderer_render_status_for_each_profile() throws Exception {
    IChoiceRenderer<MiddlewareProfileStatus> statusRenderer = mock(IChoiceRenderer.class);
    when(statusRenderer.getDisplayValue(any(MiddlewareProfileStatus.class))).thenReturn("mockedstatus");

    ProfileChoiceRenderer renderer = spy(
            new ReleaseOverrideProfilePanel.ProfileChoiceRenderer(null, statusRenderer));
    doReturn("default, ").when(renderer).getDefaultLabel();

    for (MiddlewareProfile profile : MiddlewareProfile.values()) {
        assertThat(renderer.getDisplayValue(profile).contains("mockedstatus"));
    }//from  w w  w. j av  a2s  .  c  o  m
}

From source file:com.francetelecom.clara.cloud.presentation.releases.ReleaseOverrideProfilePanelTest.java

License:Apache License

@Test
public void profile_renderer_render_default_label_only_on_default_profile() throws Exception {
    IChoiceRenderer<MiddlewareProfileStatus> statusRenderer = mock(IChoiceRenderer.class);
    when(statusRenderer.getDisplayValue(any(MiddlewareProfileStatus.class))).thenReturn("mockedstatus");

    ProfileChoiceRenderer renderer = spy(
            new ReleaseOverrideProfilePanel.ProfileChoiceRenderer(null, statusRenderer));
    doReturn("default, ").when(renderer).getDefaultLabel();

    for (MiddlewareProfile profile : MiddlewareProfile.values()) {
        if (MiddlewareProfile.getDefault() == profile) {
            assertThat(renderer.getDisplayValue(profile)).contains("default, ");
        } else {/*from  w ww .  jav  a2s.co  m*/

            assertThat(renderer.getDisplayValue(profile)).doesNotContain("default, ");
        }
    }
}

From source file:com.googlecode.wicketwebbeans.fields.DropDownChoiceField.java

License:Apache License

/**
 * Construct a new DropDownChoiceField.// ww w  . j av  a  2 s. c o m
 *
 * @param id the Wicket id for the editor.
 * @param model the model.
 * @param metaData the meta data for the property.
 * @param viewOnly true if the component should be view-only.
 * @param valueModel the Bean List of drop down choices 
 * @param choiceRenderer displays the bean value
 *  produce the value displayed to the user.
 */
public DropDownChoiceField(String id, IModel<Serializable> model, ElementMetaData metaData, boolean viewOnly,
        IModel<? extends List<? extends Serializable>> valueModel, final IChoiceRenderer choiceRenderer) {
    super(id, model, metaData, viewOnly);

    Fragment fragment;
    if (viewOnly) {
        fragment = new Fragment("frag", "viewer");
        fragment.add(new LabelWithMinSize("component", model) {
            protected void onComponentTagBody(MarkupStream markupStream, ComponentTag openTag) {
                String value = choiceRenderer.getDisplayValue(getDefaultModelObject()).toString();
                if (Strings.isEmpty(value)) {
                    value = "&nbsp;";
                    setEscapeModelStrings(false);
                }
                replaceComponentTagBody(markupStream, openTag, value);
            }
        });
    } else {
        fragment = new Fragment("frag", "editor");
        choice = new DropDownChoice("component", model, valueModel, choiceRenderer);
        // Always allow the null choice.
        choice.setNullValid(true);
        fragment.add(choice);
    }
    add(fragment);
}

From source file:com.googlecode.wicketwebbeans.fields.EnumField.java

License:Apache License

/**
 * Construct a new EnumField.//from  w  ww .  ja  va  2  s .c om
 *
 * @param id the Wicket id for the editor.
 * @param model the model.
 * @param metaData the meta data for the property.
 * @param viewOnly true if the component should be view-only.
 * @param valueModel an IModel that returns a List of values to be selected from. The element's toString() is used to 
 *  produce the value displayed to the user.
 * @param choiceRenderer a renderer that produces the value displayed to the user. May be null to use the default rendering.
 */
public EnumField(String id, IModel model, ElementMetaData metaData, boolean viewOnly, IModel valueModel,
        final IChoiceRenderer choiceRenderer) {
    super(id, model, metaData, viewOnly);

    this.choiceRenderer = choiceRenderer;
    Fragment fragment;
    if (viewOnly) {
        fragment = new Fragment("frag", "viewer");
        fragment.add(new LabelWithMinSize("component", model) {
            private static final long serialVersionUID = 1L;

            @Override
            protected void onComponentTagBody(MarkupStream markupStream, ComponentTag openTag) {
                if (choiceRenderer == null) {
                    super.onComponentTagBody(markupStream, openTag);
                } else {
                    String value = choiceRenderer.getDisplayValue(getDefaultModelObject()).toString();
                    if (Strings.isEmpty(value)) {
                        value = "&nbsp;";
                        setEscapeModelStrings(false);
                    }
                    replaceComponentTagBody(markupStream, openTag, value);
                }
            }
        });
    } else {
        fragment = new Fragment("frag", "editor");
        choice = new DropDownChoice("component", model, valueModel, choiceRenderer);
        // Always allow the null choice.
        choice.setNullValid(true);
        fragment.add(choice);

        initDefault();
    }

    add(fragment);
}

From source file:com.userweave.pages.components.twoColumnPanel.multiColumnRadioChoicePanel.RadioChoiceColumnPanel.java

License:Open Source License

public static String getLabel(Component component, IModel model, IChoiceRenderer renderer) {
    Object objectValue = renderer.getDisplayValue(model.getObject());
    Class objectClass = objectValue.getClass();
    String displayValue = "";
    if (objectClass != null && objectClass != String.class) {
        displayValue = component.getApplication().getConverterLocator().getConverter(objectClass)
                .convertToString(objectValue, component.getLocale());

        //convertToString(objectValue, component.getLocale());
    } else if (objectValue != null) {
        displayValue = objectValue.toString();
    }/*  w w w  .  ja  v  a 2  s .  c  o m*/
    return displayValue;
}

From source file:de.invesdwin.nowicket.component.palette.component.AOptions.java

License:Apache License

/**
 * {@inheritDoc}/* ww  w. j a v  a  2 s. c om*/
 */
@Override
public void onComponentTagBody(final MarkupStream markupStream, final ComponentTag openTag) {
    final StringBuilder buffer = new StringBuilder(128);
    final Iterator<T> options = getOptionsIterator();
    final IChoiceRenderer<T> renderer = getPalette().getChoiceRenderer();

    final boolean localizeDisplayValues = localizeDisplayValues();

    while (options.hasNext()) {
        final T choice = options.next();

        final CharSequence id;
        //CHECKSTYLE:OFF
        {
            //CHECKSTYLE:ON
            final String value = renderer.getIdValue(choice, 0);

            if (getEscapeModelStrings()) {
                id = Strings.escapeMarkup(value);
            } else {
                id = value;
            }
        }

        final CharSequence value;
        //CHECKSTYLE:OFF
        {
            //CHECKSTYLE:ON
            final Object displayValue = renderer.getDisplayValue(choice);
            final Class<?> displayClass = displayValue == null ? null : displayValue.getClass();

            @SuppressWarnings("unchecked")
            final IConverter<Object> converter = (IConverter<Object>) getConverter(displayClass);
            String displayString = converter.convertToString(displayValue, getLocale());
            if (localizeDisplayValues) {
                displayString = getLocalizer().getString(displayString, this, displayString);
            }

            if (getEscapeModelStrings()) {
                value = Strings.escapeMarkup(displayString);
            } else {
                value = displayString;
            }
        }

        buffer.append("\n<option value=\"").append(id).append("\"");

        final Map<String, String> additionalAttributesMap = getAdditionalAttributes(choice);
        if (additionalAttributesMap != null) {
            for (final Map.Entry<String, String> entry : additionalAttributesMap.entrySet()) {
                buffer.append(' ').append(entry.getKey()).append("=\"").append(entry.getValue()).append("\"");
            }
        }

        buffer.append(">").append(value).append("</option>");
    }

    buffer.append("\n");

    replaceComponentTagBody(markupStream, openTag, buffer);
}

From source file:gr.abiss.calipso.wicket.components.formfields.CheckBoxMultipleChoice.java

License:Open Source License

/**
 * code adapted from onComponentTagBody implementation of wicket's built-in
 * CheckBoxMultipleChoice component/*from   w w w .j  a v a 2  s.  com*/
 */
@Override
public void onComponentTagBody(final MarkupStream markupStream, final ComponentTag openTag) {

    final List choices = getChoices();
    boolean scrollable = choices.size() > 6;

    final StringBuilder buffer = new StringBuilder();
    final StringBuilder selectedBuffer = new StringBuilder();

    if (scrollable) {
        selectedBuffer.append("<div class=\"multiselect scrollable\">");
    } else {
        selectedBuffer.append("<div class=\"multiselect\">");
    }

    final String selected = getValue();

    boolean hasSelected = false;

    Locale locale = getLocale();

    for (int index = 0; index < choices.size(); index++) {

        final Object choice = choices.get(index);
        IChoiceRenderer choiceRenderer = getChoiceRenderer();
        // logger.info("choiceRenderer: "+choiceRenderer);
        // logger.info("choice: "+choice);
        // logger.info(".getDisplayValue(choice): "+choiceRenderer.getDisplayValue(choice));
        //
        final String label = getConverter(String.class)
                .convertToString(choiceRenderer.getDisplayValue(choice).toString(), locale);

        if (label != null) {

            String id = getChoiceRenderer().getIdValue(choice, index);
            final String idAttr = getInputName() + "_" + id;

            String display = label;
            if (localizeDisplayValues()) {
                display = getLocalizer().getString(label, this, label);
            }
            CharSequence escaped = Strings.escapeMarkup(display, false, true);
            boolean isSelected = false;
            StringBuilder whichBuffer = buffer;
            if (isSelected(choice, index, selected)) {
                isSelected = true;
                if (scrollable) {
                    hasSelected = true;
                    whichBuffer = selectedBuffer;
                }
            }
            whichBuffer.append("<input name=\"").append(getInputName()).append("\"")
                    .append(" type=\"checkbox\"").append(isSelected ? " checked=\"checked\"" : "")
                    .append((isEnabled() ? "" : " disabled=\"disabled\"")).append(" value=\"").append(id)
                    .append("\" id=\"").append(idAttr).append("\"/>").append("<label for=\"").append(idAttr)
                    .append("\">").append(escaped).append("</label>").append("<br />");
        }
    }

    if (hasSelected) {
        selectedBuffer.append("<hr />");
    }

    selectedBuffer.append(buffer).append("</div>");

    replaceComponentTagBody(markupStream, openTag, selectedBuffer);

}

From source file:gr.interamerican.wicket.bo2.utils.SelfDrawnUtils.java

License:Open Source License

/**
 * Sort a Collection of {@link Pair}s by Left Value.
 * Sort a Collection of {@link Pair}s by Left Value.
 * //w  ww. j av  a 2 s  .c  o  m
 * @param <T>
 *        Type of Values.
 * @param choices
 *        List of choices.
 * @param choiceRenderer
 *        The ChoiceRenderer that returns the translatedValue.
 * @return List<T>
 */
public static <T> List<T> sortPairByLeftValue(List<? extends T> choices, IChoiceRenderer<T> choiceRenderer) {
    List<PairWithComparableLeft<String, T>> pairList = new ArrayList<PairWithComparableLeft<String, T>>();
    for (T choice : choices) {
        Object translatedValue = choiceRenderer.getDisplayValue(choice);
        PairWithComparableLeft<String, T> pair = new PairWithComparableLeft<String, T>(
                StringUtils.toString(translatedValue), choice);
        pairList.add(pair);
    }
    Collections.sort(pairList);

    List<T> sortedValueList = new ArrayList<T>();
    for (PairWithComparableLeft<String, T> pair : pairList) {
        sortedValueList.add(pair.getRight());
    }
    return sortedValueList;
}