Search in sources :

Example 1 with FixedMaskFormatter

use of com.servoy.j2db.util.text.FixedMaskFormatter in project servoy-client by Servoy.

the class WebDataField method getTextConverter.

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);
            }

            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) {
                    // $NON-NLS-1$
                    if (value == null)
                        return "";
                    try {
                        return displayFormatter.valueToString(value);
                    } catch (ParseException e) {
                        Debug.log(e);
                        return value.toString();
                    }
                }

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

Example 2 with FixedMaskFormatter

use of com.servoy.j2db.util.text.FixedMaskFormatter in project servoy-client by Servoy.

the class DataLookupField method fillValueListImpl.

/**
 * @param firstTime
 */
private void fillValueListImpl(final boolean firstTime) {
    try {
        if (list != null && changeListener != null)
            list.removeListDataListener(changeListener);
        String txt = getText();
        // if this Typeahead is using a masked formatter remove the mask characters before looking up the string
        if (getFormatter() != null && (getFormatter() instanceof FixedMaskFormatter)) {
            FixedMaskFormatter formatter = (FixedMaskFormatter) getFormatter();
            int invalidOffset = formatter.getInvalidOffset(txt, true);
            invalidOffset = (invalidOffset == -1 ? 0 : invalidOffset);
            txt = txt.substring(0, invalidOffset);
        }
        if (txt.length() > 0 || Boolean.TRUE.equals(UIUtils.getUIProperty(this, IApplication.TYPE_AHEAD_SHOW_POPUP_WHEN_EMPTY, Boolean.TRUE))) {
            dlm.fill(parentState, dataProviderID, txt, firstTime);
            if (dlm.getSize() == 0 && firstTime && dlm.getValueList() != null && dlm.getValueList().hasRealValues()) {
                dlm.fill(parentState, dataProviderID, null, firstTime);
            }
            if (jlist != null) {
                jlist.setModel(dlm);
                jlist.setSelectedValue(txt, true);
                jlist.setFont(getFont());
                // use bgcolor only for opaque popup
                if (isOpaque())
                    jlist.setBackground(getListBackgroundColor());
                jlist.setForeground(getListForegroundColor());
            }
            showPopup();
        } else if (popup != null) {
            popup.setVisible(false);
        }
    } catch (Exception e) {
        Debug.error(e);
    } finally {
        if (list != null && changeListener != null)
            list.addListDataListener(changeListener);
    }
}
Also used : DisplayString(com.servoy.j2db.dataprocessing.CustomValueList.DisplayString) FixedMaskFormatter(com.servoy.j2db.util.text.FixedMaskFormatter) Point(java.awt.Point) ParseException(java.text.ParseException)

Aggregations

FixedMaskFormatter (com.servoy.j2db.util.text.FixedMaskFormatter)2 ParseException (java.text.ParseException)2 DisplayString (com.servoy.j2db.dataprocessing.CustomValueList.DisplayString)1 Point (java.awt.Point)1 Locale (java.util.Locale)1 ConversionException (org.apache.wicket.util.convert.ConversionException)1 IConverter (org.apache.wicket.util.convert.IConverter)1