Search in sources :

Example 1 with MaskBehavior

use of com.servoy.j2db.server.headlessclient.mask.MaskBehavior in project servoy-client by Servoy.

the class WebDataField method installFormat.

@SuppressWarnings("nls")
public void installFormat(com.servoy.j2db.component.ComponentFormat componentFormat) {
    int mappedType = Column.mapToDefaultType(componentFormat.uiType);
    // only add type validators the first time (not when format is set through script)
    if (this.dataType == 0) {
        if (mappedType == IColumnTypes.DATETIME) {
            setType(Date.class);
        } else if (mappedType == IColumnTypes.NUMBER) {
            setType(Double.class);
            if (list == null) {
                add(new FindModeDisabledSimpleAttributeModifier(getEventExecutor(), "onkeypress", new NumberValidationModel(true)));
            }
        } else if (mappedType == IColumnTypes.INTEGER) {
            setType(Integer.class);
            if (list == null) {
                add(new FindModeDisabledSimpleAttributeModifier(getEventExecutor(), "onkeypress", new NumberValidationModel(false)));
            }
        } else if (// media and list != null is binary uuid column as real in a valuelist.
        (mappedType == IColumnTypes.MEDIA || mappedType == IColumnTypes.TEXT) && list != null) {
            setType(String.class);
        }
    }
    this.dataType = componentFormat.uiType;
    getStylePropertyChanges().setChanged();
    converter = null;
    boolean emptyCustom = (list instanceof CustomValueList) && list.getSize() == 0;
    parsedFormat = componentFormat.parsedFormat;
    if (formatAttributeModifier != null) {
        remove(formatAttributeModifier);
        formatAttributeModifier = null;
    }
    if (formatPasteBehavior != null) {
        remove(formatPasteBehavior);
        formatPasteBehavior = null;
    }
    if (formatOnChangeBehavior != null) {
        remove(formatOnChangeBehavior);
        formatOnChangeBehavior = null;
    }
    addMaxLengthBehavior(-1);
    if (!componentFormat.parsedFormat.isEmpty() && (list == null || (!list.hasRealValues() && !emptyCustom))) {
        int maxLength = parsedFormat.getMaxLength() != null ? parsedFormat.getMaxLength().intValue() : 0;
        if (maxLength > 0) {
            addMaxLengthBehavior(parsedFormat.getMaxLength().intValue());
        }
        if (parsedFormat.isAllUpperCase()) {
            formatAttributeModifier = new ReadOnlyAndEnableTestAttributeModifier("onkeypress", "return Servoy.Validation.changeCase(this,event,true," + maxLength + ");");
            formatPasteBehavior = new ReadOnlyAndEnableTestAttributeModifier("onpaste", "Servoy.Validation.pasteHandler(this, function(el){el.value = el.value.toUpperCase();});");
            if (needsFormatOnchange()) {
                formatOnChangeBehavior = new AttributeModifier("onchange", true, new Model<String>() {

                    private static final long serialVersionUID = 1L;

                    @Override
                    public String getObject() {
                        // $NON-NLS-1$
                        return "if (this.value) this.value = this.value.toUpperCase()";
                    }
                }) {

                    @Override
                    protected String newValue(final String currentValue, final String replacementValue) {
                        return currentValue + ";" + replacementValue;
                    }
                };
            }
        } else if (parsedFormat.isAllLowerCase()) {
            formatAttributeModifier = new ReadOnlyAndEnableTestAttributeModifier("onkeypress", "return Servoy.Validation.changeCase(this,event,false," + maxLength + ");");
            formatPasteBehavior = new ReadOnlyAndEnableTestAttributeModifier("onpaste", "Servoy.Validation.pasteHandler(this, function(el){el.value = el.value.toLowerCase();});");
            if (needsFormatOnchange()) {
                formatOnChangeBehavior = new AttributeModifier("onchange", true, new Model<String>() {

                    private static final long serialVersionUID = 1L;

                    @Override
                    public String getObject() {
                        // $NON-NLS-1$
                        return "if (this.value) this.value = this.value.toLowerCase()";
                    }
                }) {

                    @Override
                    protected String newValue(final String currentValue, final String replacementValue) {
                        return currentValue + ";" + replacementValue;
                    }
                };
            }
        } else if (mappedType == IColumnTypes.DATETIME && parsedFormat.isMask()) {
            String maskPattern = parsedFormat.getDateMask();
            setType(Date.class);
            String placeHolder = parsedFormat.getDisplayFormat();
            if (parsedFormat.getPlaceHolderString() != null)
                placeHolder = parsedFormat.getPlaceHolderString();
            else if (parsedFormat.getPlaceHolderCharacter() != 0)
                placeHolder = Character.toString(parsedFormat.getPlaceHolderCharacter());
            formatAttributeModifier = new MaskBehavior(maskPattern.toString(), placeHolder, this);
        } else if (mappedType == IColumnTypes.TEXT && parsedFormat.isNumberValidator()) {
            setType(String.class);
            DecimalFormatSymbols dfs = RoundHalfUpDecimalFormat.getDecimalFormatSymbols(application.getLocale());
            formatAttributeModifier = new FindModeDisabledSimpleAttributeModifier(getEventExecutor(), "onkeypress", ("return Servoy.Validation.numbersonly(event, true, '" + dfs.getDecimalSeparator() + "','" + dfs.getGroupingSeparator() + "','" + dfs.getCurrencySymbol() + "','" + dfs.getPercent() + "');").intern());
            formatPasteBehavior = new FindModeDisabledSimpleAttributeModifier(getEventExecutor(), "onpaste", "Servoy.Validation.pasteHandler(this, function(el){el.value = el.value.replace(/[^0-9\\-\\,\\.\\$\\%]+/,'');});");
        } else if (mappedType == IColumnTypes.TEXT && parsedFormat.getDisplayFormat() != null) {
            setType(String.class);
            String placeHolder = null;
            if (parsedFormat.getPlaceHolderString() != null)
                placeHolder = parsedFormat.getPlaceHolderString();
            else if (parsedFormat.getPlaceHolderCharacter() != 0)
                placeHolder = Character.toString(parsedFormat.getPlaceHolderCharacter());
            formatAttributeModifier = new MaskBehavior(parsedFormat.getDisplayFormat(), placeHolder, this, parsedFormat.getAllowedCharacters());
        }
        if (formatAttributeModifier != null)
            add(formatAttributeModifier);
        if (formatPasteBehavior != null)
            add(formatPasteBehavior);
        if (formatOnChangeBehavior != null)
            add(formatOnChangeBehavior);
    }
    if (maxLength > 0 && maxLengthBehavior == null) {
        addMaxLengthBehavior(maxLength);
    }
}
Also used : CustomValueList(com.servoy.j2db.dataprocessing.CustomValueList) DecimalFormatSymbols(java.text.DecimalFormatSymbols) MaskBehavior(com.servoy.j2db.server.headlessclient.mask.MaskBehavior) AttributeModifier(org.apache.wicket.AttributeModifier) SimpleAttributeModifier(org.apache.wicket.behavior.SimpleAttributeModifier) Point(java.awt.Point) IModel(org.apache.wicket.model.IModel) AbstractReadOnlyModel(org.apache.wicket.model.AbstractReadOnlyModel) Model(org.apache.wicket.model.Model)

Aggregations

CustomValueList (com.servoy.j2db.dataprocessing.CustomValueList)1 MaskBehavior (com.servoy.j2db.server.headlessclient.mask.MaskBehavior)1 Point (java.awt.Point)1 DecimalFormatSymbols (java.text.DecimalFormatSymbols)1 AttributeModifier (org.apache.wicket.AttributeModifier)1 SimpleAttributeModifier (org.apache.wicket.behavior.SimpleAttributeModifier)1 AbstractReadOnlyModel (org.apache.wicket.model.AbstractReadOnlyModel)1 IModel (org.apache.wicket.model.IModel)1 Model (org.apache.wicket.model.Model)1