Example usage for org.apache.wicket.util.convert IConverter IConverter

List of usage examples for org.apache.wicket.util.convert IConverter IConverter

Introduction

In this page you can find the example usage for org.apache.wicket.util.convert IConverter IConverter.

Prototype

IConverter

Source Link

Usage

From source file:com.genericconf.bbbgateway.web.components.DateTimeLabel.java

License:Apache License

@Override
public IConverter getConverter(Class<?> type) {
    return new IConverter() {
        private static final long serialVersionUID = 1L;

        @SuppressWarnings("deprecation")
        @Override/*from w  ww .  j a  va 2  s.  c om*/
        public String convertToString(Object value, Locale locale) {
            return ((Date) value).toGMTString();
        }

        @Override
        public Object convertToObject(String value, Locale locale) {
            throw new UnsupportedOperationException("should not need to convert to object");
        }
    };
}

From source file:com.googlecode.wicket.jquery.ui.form.autocomplete.AutoCompleteTextField.java

License:Apache License

/**
 * Gets a new {@link IConverter}./*from   www . j av a2s.  c o  m*/
 * Used when/if the bean type has been supplied to the constructor.
 * @return the {@link IConverter}
 */
private IConverter<T> newConverter() {
    return new IConverter<T>() {

        private static final long serialVersionUID = 1L;

        @Override
        public T convertToObject(String value, Locale locale) {
            if (value != null && value.equals(AutoCompleteTextField.this.getModelValue())) {
                return AutoCompleteTextField.this.getModelObject();
            }

            return null; //if the TextField value (string) does not corresponds to the current object model (ie: user specific value), returns null.
        }

        @Override
        public String convertToString(T value, Locale locale) {
            return AutoCompleteTextField.this.renderer.getText(value);
        }
    };
}

From source file:com.googlecode.wicket.jquery.ui.plugins.datepicker.RangeDatePickerTextField.java

License:Apache License

@SuppressWarnings("unchecked")
protected <C> IConverter<C> newConverter() {
    return (IConverter<C>) new IConverter<DateRange>() {

        private static final long serialVersionUID = 1L;

        @Override// ww w.  ja v  a  2s . c o  m
        public DateRange convertToObject(String value, Locale locale) {
            DateFormat dateFormat = RangeDatePickerTextField.this.newDateFormat(locale);
            String[] dates = value.split(RangeDatePickerTextField.this.getSeparator());

            try {
                return new DateRange(dateFormat.parse(dates[0]), dateFormat.parse(dates[1]));
            } catch (ParseException e) {
                throw new ConversionException(e.getMessage());
            } catch (IndexOutOfBoundsException e) {
                throw new ConversionException(e.getMessage());
            }
        }

        @Override
        public String convertToString(DateRange value, Locale locale) {
            DateFormat dateFormat = RangeDatePickerTextField.this.newDateFormat(locale);
            Date start = value.getStart();
            Date end = value.getEnd();

            return String.format("%s%s%s", start != null ? dateFormat.format(start) : getNullString(),
                    RangeDatePickerTextField.this.getSeparator(),
                    end != null ? dateFormat.format(end) : getNullString());
        }
    };
}

From source file:com.googlecode.wicket.kendo.ui.form.autocomplete.AutoCompleteTextField.java

License:Apache License

/**
 * Gets a new {@link IConverter}.<br/>
 * Used when the form component is posted and the bean type has been supplied to the constructor.
 *
 * @return the {@link IConverter}/* w w  w .  j  a v a  2 s. com*/
 */
private final IConverter<T> newConverter() {
    return new IConverter<T>() {

        private static final long serialVersionUID = 1L;

        @Override
        public T convertToObject(String value, Locale locale) {
            if (value != null) {
                if (value.equals(getModelValue())) {
                    // the value already corresponds to the model object, no conversion to be done
                    return getModelObject();
                } else {
                    // the value has been entered manually; try getting the object...
                    List<T> choices = getChoices(value);

                    if (!choices.isEmpty()) {
                        for (T choice : choices) {
                            if (renderer.getText(choice).equals(value)) {
                                return choice;
                            }
                        }
                    }
                }
            }

            // the string value does not corresponds to the current object model (ie: user specific value)
            // and has not been found in the latest list of choice
            return null;
        }

        @Override
        public String convertToString(T value, Locale locale) {
            return renderer.getText(value);
        }
    };
}

From source file:com.googlecode.wicket.kendo.ui.form.datetime.local.AjaxDateTimePicker.java

License:Apache License

/**
 * Gets a new {@link LocalDateTime} {@link IConverter}.
 * //  w w w .ja v  a 2 s .c o m
 * @param format the time format
 * @return the converter
 */
private static IConverter<LocalDateTime> newConverter(final String pattern) {
    return new IConverter<LocalDateTime>() {

        private static final long serialVersionUID = 1L;

        @Override
        public LocalDateTime convertToObject(String value, Locale locale) throws ConversionException {
            try {
                return LocalDateTime.parse(value, DateTimeFormatter.ofPattern(pattern, locale));
            } catch (DateTimeParseException e) {
                throw new ConversionException(e.getMessage(), e);
            }
        }

        @Override
        public String convertToString(LocalDateTime datetime, Locale locale) {
            return datetime != null ? datetime.format(DateTimeFormatter.ofPattern(pattern, locale)) : null;
        }
    };
}

From source file:com.googlecode.wicket.kendo.ui.form.datetime.local.DatePicker.java

License:Apache License

/**
 * Gets a new date {@link IConverter}.//  w  w w . j  a v  a 2s . com
 * 
 * @param format the date format
 * @return the converter
 */
private static IConverter<LocalDate> newConverter(final String pattern) {
    return new IConverter<LocalDate>() {

        private static final long serialVersionUID = 1L;

        @Override
        public LocalDate convertToObject(String value, Locale locale) throws ConversionException {
            try {
                return LocalDate.parse(value, DateTimeFormatter.ofPattern(pattern, locale));
            } catch (DateTimeParseException e) {
                throw new ConversionException(e.getMessage(), e);
            }
        }

        @Override
        public String convertToString(LocalDate date, Locale locale) {
            return date != null ? date.format(DateTimeFormatter.ofPattern(pattern, locale)) : null;
        }
    };
}

From source file:com.googlecode.wicket.kendo.ui.form.datetime.local.TimePicker.java

License:Apache License

/**
 * Gets a new time {@link IConverter}./*from ww w.  j av a  2  s  .c  o  m*/
 * 
 * @param format the time format
 * @return the converter
 */
private static IConverter<LocalTime> newConverter(final String pattern) {
    return new IConverter<LocalTime>() {

        private static final long serialVersionUID = 1L;

        @Override
        public LocalTime convertToObject(String value, Locale locale) throws ConversionException {
            try {
                return LocalTime.parse(value, DateTimeFormatter.ofPattern(pattern, locale));
            } catch (DateTimeParseException e) {
                throw new ConversionException(e.getMessage(), e);
            }
        }

        @Override
        public String convertToString(LocalTime time, Locale locale) {
            return time != null ? time.format(DateTimeFormatter.ofPattern(pattern, locale)) : null;
        }
    };
}

From source file:com.premiumminds.webapp.wicket.JodaInstantTextField.java

License:Open Source License

public JodaInstantTextField(String id, IModel<T> model, String pattern, final Class<T> type) {
    super(id, model, type);
    this.pattern = pattern;
    converter = new IConverter<T>() {
        private static final long serialVersionUID = 1L;

        public String convertToString(ReadableInstant value, Locale locale) {
            if (null == locale) {
                return DateTimeFormat.forPattern(JodaInstantTextField.this.pattern).print(value);
            } else {
                return DateTimeFormat.forPattern(JodaInstantTextField.this.pattern).withLocale(locale)
                        .print(value);/*from w  w w.ja  v  a2 s.co  m*/
            }
        }

        public T convertToObject(String value, Locale locale) throws ConversionException {
            try {
                if (null == locale) {
                    return type.getConstructor(Long.TYPE).newInstance(DateTimeFormat
                            .forPattern(JodaInstantTextField.this.pattern).parseDateTime(value).getMillis());
                } else {
                    return type.getConstructor(Long.TYPE)
                            .newInstance(DateTimeFormat.forPattern(JodaInstantTextField.this.pattern)
                                    .withLocale(locale).parseDateTime(value).getMillis());
                }
            } catch (Exception e) {
                log.error("Could not convert [" + value + "] to a valid " + type.getSimpleName());
                return null;
            }
        }
    };
}

From source file:com.servoy.j2db.server.headlessclient.dataui.WebBaseButton.java

License:Open Source License

@Override
public IConverter getConverter(Class<?> cls) {
    if (cls.isArray() && cls.getComponentType().toString().equals("byte")) //$NON-NLS-1$
        return new IConverter() {
            private static final long serialVersionUID = 1L;

            public Object convertToObject(String value, Locale locale) {
                return null;
            }//from  w  w  w.  j  a v a 2s .  co m

            public String convertToString(Object value, Locale locale) {
                return null;
            }
        };
    return StripHTMLTagsConverter.htmlStripper;
}

From source file:com.servoy.j2db.server.headlessclient.dataui.WebDataField.java

License:Open Source License

public static IConverter getTextConverter(final ParsedFormat fp, final Locale l, final String name,
        final String dataProviderID) {
    if (fp.isAllUpperCase()) {
        return new IConverter() {
            public String convertToString(Object value, Locale locale) {
                if (value == null)
                    return "";
                return value.toString().toUpperCase(l);
            }/* w w  w.  j av  a  2s . c  om*/

            public Object convertToObject(String value, Locale locale) {
                if (value == null)
                    return null;
                return value.toUpperCase(l);
            }
        };
    }
    if (fp.isAllLowerCase()) {
        return new IConverter() {
            public String convertToString(Object value, Locale locale) {
                if (value == null)
                    return "";
                return value.toString().toLowerCase(l);
            }

            public Object convertToObject(String value, Locale locale) {
                if (value == null)
                    return null;
                return value.toLowerCase(l);
            }
        };
    }
    if (fp.getDisplayFormat() != null) {
        try {
            final FixedMaskFormatter displayFormatter = new FixedMaskFormatter(fp.getDisplayFormat());
            displayFormatter.setValueContainsLiteralCharacters(!fp.isRaw());
            if (fp.getPlaceHolderString() != null)
                displayFormatter.setPlaceholder(fp.getPlaceHolderString());
            else if (fp.getPlaceHolderCharacter() != 0)
                displayFormatter.setPlaceholderCharacter(fp.getPlaceHolderCharacter());

            return new IConverter() {
                public String convertToString(Object value, Locale locale) {
                    if (value == null)
                        return ""; //$NON-NLS-1$
                    try {
                        return displayFormatter.valueToString(value);
                    } catch (ParseException e) {
                        Debug.log(e);
                        return value.toString();
                    }
                }

                public Object convertToObject(String value, Locale locale) {
                    if (value == null || "".equals(value.trim())) //$NON-NLS-1$
                        return null;
                    try {
                        return displayFormatter.parse(value);
                    } catch (Exception e) {
                        String extraMsg = ""; //$NON-NLS-1$
                        if (name != null) {
                            extraMsg = " on component " + name; //$NON-NLS-1$
                        }
                        extraMsg += " with dataprovider: " + dataProviderID; //$NON-NLS-1$
                        throw new ConversionException(
                                "Can't convert from string '" + value + "' to object with format: " //$NON-NLS-1$//$NON-NLS-2$
                                        + fp.getEditFormat() + extraMsg, e).setConverter(this);
                    }
                }
            };
        } catch (ParseException e) {
            Debug.error("format problem: " + fp.getDisplayFormat(), e); //$NON-NLS-1$
            return null;
        }
    }
    return null;
}