Example usage for org.apache.wicket MarkupContainer getLocale

List of usage examples for org.apache.wicket MarkupContainer getLocale

Introduction

In this page you can find the example usage for org.apache.wicket MarkupContainer getLocale.

Prototype

public Locale getLocale() 

Source Link

Document

Gets the locale for this component.

Usage

From source file:org.yes.cart.web.page.component.customer.dynaform.EditorFactory.java

License:Apache License

/**
 * Get the particular editor for given attribute value. Type of editor depends from type of attribute value.
 *
 * @param id editor id/*from  w w  w.j a v a  2 s  .  c  om*/
 * @param markupContainer markup component
 * @param attrValue give {@link org.yes.cart.domain.entity.AttrValue}
 * @param readOnly  if true this component is read only
 *
 * @return editor
 */
public Component getEditor(final String id, final MarkupContainer markupContainer, final String language,
        final AttrValue attrValue, final Boolean readOnly) {

    final boolean notEditable = readOnly == null || readOnly;

    final I18NModel nameModel = new FailoverStringI18NModel(attrValue.getAttribute().getDisplayName(),
            attrValue.getAttribute().getName());

    final String prop = attrValue.getAttribute().getVal();

    final IModel<String> labelModel = new AbstractReadOnlyModel<String>() {

        private final I18NModel m = nameModel;

        @Override
        public String getObject() {
            final String lang = markupContainer.getLocale().getLanguage();
            final String name = m.getValue(lang);
            if (StringUtils.isNotBlank(name)) {
                return name;
            }
            if (StringUtils.isNotBlank(prop)) {
                return markupContainer.getLocalizer().getString(prop, markupContainer);
            }
            return null;
        }
    };
    final String bType = attrValue.getAttribute().getEtype().getBusinesstype();

    if ("CommaSeparatedList".equals(bType)) {

        final I18NModel choices = new FailoverStringI18NModel(attrValue.getAttribute().getChoiceData(),
                attrValue.getAttribute().getChoiceData());

        final IModel<List<Pair<String, String>>> enumChoices = new AbstractReadOnlyModel<List<Pair<String, String>>>() {

            private final I18NModel m = choices;

            public List<Pair<String, String>> getObject() {
                final String lang = markupContainer.getLocale().getLanguage();
                final List<Pair<String, String>> list = (List<Pair<String, String>>) CONVERSION_SERVICE.convert(
                        choices.getValue(lang), TypeDescriptor.valueOf(String.class),
                        TypeDescriptor.valueOf(List.class));
                if (list == null) {
                    return new ArrayList<Pair<String, String>>();
                }
                return list;
            }
        };
        if (attrValue.getAttribute().isAllowduplicate()) {
            final IModel model = new MultiplePairModel(new PropertyModel(attrValue, VALUE_FILED),
                    enumChoices.getObject());
            return new MultipleChoicesEditor(id, markupContainer, model, labelModel, enumChoices, attrValue,
                    notEditable);
        } else {
            final IModel model = new PairModel(new PropertyModel(attrValue, VALUE_FILED),
                    enumChoices.getObject());
            return new SingleChoiceEditor(id, markupContainer, model, labelModel, enumChoices, attrValue,
                    notEditable);
        }
    } else if ("Boolean".equals(bType)) {
        final IModel model = new PropertyModel(attrValue, VALUE_FILED);
        return new BooleanEditor(id, markupContainer, model, labelModel, attrValue, notEditable);
    } else {

        final String regexError = new FailoverStringI18NModel(
                attrValue.getAttribute().getValidationFailedMessage(), attrValue.getAttribute().getCode())
                        .getValue(language);

        final IModel model = new PropertyModel(attrValue, VALUE_FILED);
        return new StringEditor(id, markupContainer, model, labelModel, new Model<String>(regexError),
                attrValue, notEditable);
    }
}