Search in sources :

Example 1 with ServoyMaskFormatter

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

the class WebDataButton method onBeforeRender.

/**
 * @see org.apache.wicket.Component#onBeforeRender()
 */
@Override
protected void onBeforeRender() {
    super.onBeforeRender();
    IModel<?> model = getInnermostModel();
    if (needEntireState && model instanceof RecordItemModel) {
        if (dataProviderID != null) {
            Object val = getModelObject();
            if (val instanceof byte[]) {
                setIcon((byte[]) val);
            } else if (icon != null) {
                setIcon(null);
            } else {
                try {
                    ComponentFormat fp = getScriptObject().getComponentFormat();
                    if (fp == null) {
                        bodyText = Text.processTags((String) val, resolver);
                    } else {
                        bodyText = Text.processTags(TagResolver.formatObject(val, application.getLocale(), fp.parsedFormat, (fp.parsedFormat.getDisplayFormat() != null ? new ServoyMaskFormatter(fp.parsedFormat.getDisplayFormat(), true) : null)), resolver);
                    }
                } catch (ParseException e) {
                    Debug.error(e);
                }
            }
        } else {
            bodyText = Text.processTags(tagText, resolver);
        }
        if (bodyText != null) {
            if (HtmlUtils.startsWithHtml(bodyText)) {
                bodyText = StripHTMLTagsConverter.convertBodyText(this, bodyText, getScriptObject().trustDataAsHtml(), application.getFlattenedSolution()).getBodyTxt();
            } else {
                // convert the text
                final IConverter converter = getConverter(String.class);
                bodyText = converter.convertToString(bodyText, getLocale());
            }
        }
    } else {
        Object modelObject = getModelObject();
        if (modelObject instanceof byte[]) {
            setIcon((byte[]) modelObject);
        } else if (icon != null) {
            setIcon(null);
        } else {
            ComponentFormat cf = getScriptObject().getComponentFormat();
            if (cf == null) {
                bodyText = Text.processTags(getDefaultModelObjectAsString(), resolver);
            } else {
                try {
                    bodyText = TagResolver.formatObject(modelObject, application.getLocale(), cf.parsedFormat, (cf.parsedFormat.getDisplayFormat() != null ? new ServoyMaskFormatter(cf.parsedFormat.getDisplayFormat(), true) : null));
                } catch (ParseException e) {
                    Debug.error(e);
                }
            }
            if (HtmlUtils.startsWithHtml(modelObject)) {
                // ignore script/header contributions for now
                bodyText = StripHTMLTagsConverter.convertBodyText(this, bodyText, getScriptObject().trustDataAsHtml(), application.getFlattenedSolution()).getBodyTxt();
            }
        }
    }
    if (model instanceof RecordItemModel) {
        ((RecordItemModel) model).updateRenderedValue(this);
    }
}
Also used : ServoyMaskFormatter(com.servoy.j2db.util.text.ServoyMaskFormatter) ParseException(java.text.ParseException) IConverter(org.apache.wicket.util.convert.IConverter) ComponentFormat(com.servoy.j2db.component.ComponentFormat)

Example 2 with ServoyMaskFormatter

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

the class WebDataLabel method onBeforeRender.

/**
 * @see org.apache.wicket.Component#onBeforeRender()
 */
@Override
protected void onBeforeRender() {
    super.onBeforeRender();
    IModel<?> model = getInnermostModel();
    hasHTML = false;
    if (needEntireState && model instanceof RecordItemModel) {
        if (dataProviderID != null) {
            Object val = getDefaultModelObject();
            if (val instanceof byte[]) {
                setIcon((byte[]) val);
            } else if (icon != null) {
                setIcon(null);
            } else {
                ComponentFormat cf = getComponentFormat();
                if (cf == null) {
                    bodyText = Text.processTags((String) val, resolver);
                } else {
                    try {
                        bodyText = Text.processTags(TagResolver.formatObject(val, application.getLocale(), cf.parsedFormat, (cf.parsedFormat.getDisplayFormat() != null ? new ServoyMaskFormatter(cf.parsedFormat.getDisplayFormat(), true) : null)), resolver);
                    } catch (ParseException e) {
                        Debug.error(e);
                    }
                }
            }
        } else {
            bodyText = Text.processTags(tagText, resolver);
        }
        if (bodyText != null) {
            if (HtmlUtils.startsWithHtml(bodyText)) {
                bodyText = StripHTMLTagsConverter.convertBodyText(this, bodyText, getScriptObject().trustDataAsHtml(), application.getFlattenedSolution()).getBodyTxt();
                hasHTML = true;
            } else {
                // convert the text (strip html if needed)
                final IConverter converter = getConverter(String.class);
                bodyText = converter.convertToString(bodyText, getLocale());
            }
        }
    } else {
        Object modelObject = getDefaultModelObject();
        if (modelObject instanceof byte[]) {
            setIcon((byte[]) modelObject);
        } else if (icon != null) {
            setIcon(null);
        } else {
            ComponentFormat cf = getComponentFormat();
            if (cf == null) {
                bodyText = Text.processTags(getDefaultModelObjectAsString(), resolver);
            } else {
                try {
                    bodyText = TagResolver.formatObject(modelObject, application.getLocale(), cf.parsedFormat, (cf.parsedFormat.getDisplayFormat() != null ? new ServoyMaskFormatter(cf.parsedFormat.getDisplayFormat(), true) : null));
                } catch (ParseException e) {
                    Debug.error(e);
                }
            }
            if (HtmlUtils.startsWithHtml(modelObject)) {
                // ignore script/header contributions for now
                bodyText = StripHTMLTagsConverter.convertBodyText(this, bodyText, getScriptObject().trustDataAsHtml(), application.getFlattenedSolution()).getBodyTxt();
                hasHTML = true;
            }
        }
    }
    if (model instanceof RecordItemModel) {
        ((RecordItemModel) model).updateRenderedValue(this);
    }
}
Also used : ServoyMaskFormatter(com.servoy.j2db.util.text.ServoyMaskFormatter) ParseException(java.text.ParseException) IConverter(org.apache.wicket.util.convert.IConverter) ComponentFormat(com.servoy.j2db.component.ComponentFormat)

Example 3 with ServoyMaskFormatter

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

the class DataButton method setValueObject.

public void setValueObject(Object obj) {
    if (needEntireState) {
        if (resolver != null) {
            if (dataProviderID != null) {
                try {
                    ComponentFormat cf = getScriptObject().getComponentFormat();
                    setText(Text.processTags(obj != null ? TagResolver.formatObject(obj, application.getLocale(), cf.parsedFormat, (cf.parsedFormat.getDisplayFormat() != null ? new ServoyMaskFormatter(cf.parsedFormat.getDisplayFormat(), true) : null)) : "", resolver));
                } catch (ParseException e) {
                    Debug.error(e);
                }
            } else {
                setText(Text.processTags(tagText, resolver));
            }
        } else {
            // $NON-NLS-1$
            setText("");
        }
        if (tooltip != null) {
            // empty tooltip will unregister from tooltip manager //$NON-NLS-1$
            super.setToolTipText("button");
        }
    } else {
        if (obj != null) {
            if (obj instanceof byte[]) {
                setIcon((byte[]) obj);
            } else {
                ComponentFormat cf = getScriptObject().getComponentFormat();
                try {
                    setText(TagResolver.formatObject(obj, application.getLocale(), cf.parsedFormat, (cf.parsedFormat.getDisplayFormat() != null ? new ServoyMaskFormatter(cf.parsedFormat.getDisplayFormat(), true) : null)));
                } catch (ParseException e) {
                    Debug.error(e);
                }
            }
        } else {
            if (value instanceof byte[]) {
                setIcon((Icon) null);
            } else if (value != null) {
                // $NON-NLS-1$
                setText("");
            }
        }
        this.value = obj;
    }
    fireOnRender(false);
}
Also used : ServoyMaskFormatter(com.servoy.j2db.util.text.ServoyMaskFormatter) ParseException(java.text.ParseException) ComponentFormat(com.servoy.j2db.component.ComponentFormat)

Example 4 with ServoyMaskFormatter

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

the class DataLabel method setValueObject.

public void setValueObject(Object obj) {
    this.rawValue = obj;
    if (needEntireState) {
        String txt = "";
        if (resolver != null) {
            if (dataProviderID != null) {
                try {
                    ComponentFormat fp = getScriptObject().getComponentFormat();
                    txt = Text.processTags(TagResolver.formatObject(obj != null ? obj : "", application.getLocale(), fp.parsedFormat, (fp.parsedFormat.getDisplayFormat() != null ? new ServoyMaskFormatter(fp.parsedFormat.getDisplayFormat(), true) : null)), resolver);
                } catch (ParseException e) {
                    Debug.error(e);
                }
            } else {
                txt = Text.processTags(tagText, resolver);
            }
            if (txt == null)
                txt = "";
        }
        if (!txt.equals(value)) {
            setText(txt);
            value = txt;
        }
        if (tooltip != null) {
            super.setToolTipText(tooltip);
        }
    } else {
        if (obj != null) {
            if (obj.equals(value)) {
                if (scriptable != null && scriptable.getRenderEventExecutor().isRenderStateChanged())
                    fireOnRender(false);
                return;
            }
            if (obj instanceof byte[]) {
                setIconDirect((byte[]) obj, getNextSeq());
            // setIcon(new ImageIcon((byte[])obj));
            } else {
                try {
                    ComponentFormat fp = getScriptObject().getComponentFormat();
                    setText(TagResolver.formatObject(obj, application.getLocale(), fp.parsedFormat, (fp.parsedFormat.getDisplayFormat() != null ? new ServoyMaskFormatter(fp.parsedFormat.getDisplayFormat(), true) : null)));
                } catch (ParseException e) {
                    Debug.error(e);
                }
            }
        } else {
            if (value instanceof byte[]) {
                setIcon((byte[]) null);
            } else {
                // $NON-NLS-1$
                setText("");
            }
        }
        this.value = obj;
        if (tooltip != null) {
            super.setToolTipText(tooltip);
        }
    }
    fireOnRender(false);
}
Also used : ServoyMaskFormatter(com.servoy.j2db.util.text.ServoyMaskFormatter) ParseException(java.text.ParseException) ComponentFormat(com.servoy.j2db.component.ComponentFormat)

Example 5 with ServoyMaskFormatter

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

the class DataField method installFormat.

public void installFormat(ComponentFormat componentFormat) {
    fp = componentFormat.parsedFormat;
    this.dataType = componentFormat.uiType;
    this.displayFormat = null;
    this.editFormat = null;
    editorDocument.clearValidators();
    boolean emptyCustom = (list instanceof CustomValueList) && !(list instanceof GlobalMethodValueList) && list.getSize() == 0;
    if (!fp.isEmpty() && (list == null || (!list.hasRealValues() && !emptyCustom))) {
        displayFormat = fp.getDisplayFormat();
        editFormat = fp.getEditFormat();
        if (fp.getMaxLength() != null && fp.getMaxLength().intValue() > 0) {
            editorDocument.setValidator(MAX_LENGTH_VALIDATOR, new LengthDocumentValidator(fp.getMaxLength().intValue()));
        }
        if (fp.isAllLowerCase()) {
            // $NON-NLS-1$
            editorDocument.setValidator("LowerCaseDocumentValidator", new LowerCaseDocumentValidator());
            TextFormatter display = new TextFormatter();
            TextFormatter edit = new TextFormatter();
            setFormatterFactory(new EditingFixedDefaultFormatterFactory(display, display, edit, edit));
        } else if (fp.isAllUpperCase()) {
            // $NON-NLS-1$
            editorDocument.setValidator("UpperCaseDocumentValidator", new UpperCaseDocumentValidator());
            TextFormatter display = new TextFormatter();
            TextFormatter edit = new TextFormatter();
            setFormatterFactory(new EditingFixedDefaultFormatterFactory(display, display, edit, edit));
        } else if (fp.isNumberValidator()) {
            // $NON-NLS-1$
            editorDocument.setValidator("NumberDocumentValidator", new NumberDocumentValidator());
            TextFormatter display = new TextFormatter();
            TextFormatter edit = new TextFormatter();
            setFormatterFactory(new EditingFixedDefaultFormatterFactory(display, display, edit, edit));
        } else {
            int maxLength = fp.getMaxLength() == null ? -1 : fp.getMaxLength().intValue();
            // if there is no display format, but the max length is set, then generate a display format.
            if (maxLength != -1 && (displayFormat == null || displayFormat.length() == 0)) {
                // if this is just a text type textfield then just set those formatters (the max length is already set)
                if (Column.mapToDefaultType(dataType) == IColumnTypes.TEXT) {
                    TextFormatter display = new TextFormatter();
                    TextFormatter edit = new TextFormatter();
                    setFormatterFactory(new EditingFixedDefaultFormatterFactory(display, display, edit, edit));
                } else {
                    char[] chars = new char[maxLength];
                    for (int i = 0; i < chars.length; i++) chars[i] = '#';
                    displayFormat = new String(chars);
                }
            }
            if (displayFormat != null) {
                if (editFormat == null)
                    editFormat = displayFormat;
                try {
                    JFormattedTextField.AbstractFormatter displayFormatter = null;
                    JFormattedTextField.AbstractFormatter editFormatter = null;
                    switch(Column.mapToDefaultType(dataType)) {
                        case IColumnTypes.NUMBER:
                            // example: $#,###.##
                            displayFormatter = new NullNumberFormatter(new RoundHalfUpDecimalFormat(displayFormat, application.getLocale()));
                            editFormatter = new NullNumberFormatter(new RoundHalfUpDecimalFormat(editFormat, application.getLocale()), maxLength);
                            setFormatterFactory(new EditingFixedDefaultFormatterFactory(displayFormatter, displayFormatter, editFormatter, editFormatter));
                            break;
                        case IColumnTypes.INTEGER:
                            // example: $#,###.##
                            displayFormatter = new NullNumberFormatter(new RoundHalfUpDecimalFormat(displayFormat, application.getLocale()));
                            editFormatter = new NullNumberFormatter(new RoundHalfUpDecimalFormat(editFormat, application.getLocale()), maxLength);
                            setFormatterFactory(new EditingFixedDefaultFormatterFactory(displayFormatter, displayFormatter, editFormatter, editFormatter));
                            break;
                        case IColumnTypes.DATETIME:
                            boolean mask = fp.isMask();
                            char placeHolder = fp.getPlaceHolderCharacter();
                            if (mask)
                                editFormat = displayFormat;
                            displayFormatter = new NullDateFormatter(new StateFullSimpleDateFormat(displayFormat, false));
                            editFormatter = new NullDateFormatter(new StateFullSimpleDateFormat(editFormat, Boolean.TRUE.equals(UIUtils.getUIProperty(this, IApplication.DATE_FORMATTERS_LENIENT, Boolean.TRUE))), !mask);
                            if (mask) {
                                editFormatter = ((NullDateFormatter) editFormatter).getMaskFormatter(placeHolder);
                            } else {
                                // date formats are default in override mode
                                setCaret(getOvertypeCaret());
                            }
                            // example: MM/dd/yyyy
                            setFormatterFactory(new EditingFixedDefaultFormatterFactory(displayFormatter, displayFormatter, editFormatter));
                            break;
                        default:
                            displayFormatter = new ValueListMaskFormatter(displayFormat, true);
                            editFormatter = new ValueListMaskFormatter(displayFormat, false);
                            if (fp.isRaw()) {
                                ((ServoyMaskFormatter) editFormatter).setValueContainsLiteralCharacters(false);
                                ((ServoyMaskFormatter) displayFormatter).setValueContainsLiteralCharacters(false);
                            }
                            if (fp.getAllowedCharacters() != null) {
                                ((ServoyMaskFormatter) editFormatter).setValidCharacters(fp.getAllowedCharacters());
                                ((ServoyMaskFormatter) displayFormatter).setValidCharacters(fp.getAllowedCharacters());
                            }
                            if (editFormat != null) {
                                if (editFormat.length() == 1) {
                                    ((ServoyMaskFormatter) editFormatter).setPlaceholderCharacter(editFormat.charAt(0));
                                } else {
                                    ((ServoyMaskFormatter) editFormatter).setPlaceholder(editFormat);
                                }
                            }
                            setFormatterFactory(new EditingFixedDefaultFormatterFactory(displayFormatter, displayFormatter, editFormatter));
                            // format overrules max length check
                            editorDocument.setValidator(MAX_LENGTH_VALIDATOR, new LengthDocumentValidator(0));
                            break;
                    }
                } catch (Exception ex) {
                    Debug.error(ex);
                }
            }
        }
    } else // for text fields
    {
        TextFormatter display = new TextFormatter();
        TextFormatter edit = new TextFormatter();
        setFormatterFactory(new EditingFixedDefaultFormatterFactory(display, display, edit, edit));
    }
    if (maxLength >= 0 && editorDocument.getValidator(MAX_LENGTH_VALIDATOR) == null) {
        editorDocument.setValidator(MAX_LENGTH_VALIDATOR, new LengthDocumentValidator(maxLength));
    }
}
Also used : CustomValueList(com.servoy.j2db.dataprocessing.CustomValueList) NumberDocumentValidator(com.servoy.j2db.util.docvalidator.NumberDocumentValidator) ServoyMaskFormatter(com.servoy.j2db.util.text.ServoyMaskFormatter) LowerCaseDocumentValidator(com.servoy.j2db.util.docvalidator.LowerCaseDocumentValidator) JFormattedTextField(javax.swing.JFormattedTextField) LengthDocumentValidator(com.servoy.j2db.util.docvalidator.LengthDocumentValidator) GlobalMethodValueList(com.servoy.j2db.dataprocessing.GlobalMethodValueList) Point(java.awt.Point) ParseException(java.text.ParseException) StateFullSimpleDateFormat(com.servoy.j2db.util.StateFullSimpleDateFormat) RoundHalfUpDecimalFormat(com.servoy.j2db.util.RoundHalfUpDecimalFormat) UpperCaseDocumentValidator(com.servoy.j2db.util.docvalidator.UpperCaseDocumentValidator)

Aggregations

ServoyMaskFormatter (com.servoy.j2db.util.text.ServoyMaskFormatter)6 ParseException (java.text.ParseException)6 ComponentFormat (com.servoy.j2db.component.ComponentFormat)5 IConverter (org.apache.wicket.util.convert.IConverter)2 CustomValueList (com.servoy.j2db.dataprocessing.CustomValueList)1 GlobalMethodValueList (com.servoy.j2db.dataprocessing.GlobalMethodValueList)1 StrippedText (com.servoy.j2db.server.headlessclient.dataui.StripHTMLTagsConverter.StrippedText)1 RoundHalfUpDecimalFormat (com.servoy.j2db.util.RoundHalfUpDecimalFormat)1 StateFullSimpleDateFormat (com.servoy.j2db.util.StateFullSimpleDateFormat)1 LengthDocumentValidator (com.servoy.j2db.util.docvalidator.LengthDocumentValidator)1 LowerCaseDocumentValidator (com.servoy.j2db.util.docvalidator.LowerCaseDocumentValidator)1 NumberDocumentValidator (com.servoy.j2db.util.docvalidator.NumberDocumentValidator)1 UpperCaseDocumentValidator (com.servoy.j2db.util.docvalidator.UpperCaseDocumentValidator)1 Point (java.awt.Point)1 JFormattedTextField (javax.swing.JFormattedTextField)1