Search in sources :

Example 1 with StateFullSimpleDateFormat

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

the class WebDataField method getConverter.

/**
 * @see wicket.Component#getConverter()
 */
@SuppressWarnings("nls")
@Override
public IConverter getConverter(Class<?> cls) {
    if (converter != null)
        return converter;
    int mappedType = Column.mapToDefaultType(dataType);
    String displayFormat = parsedFormat.getDisplayFormat();
    if (list == null && mappedType == IColumnTypes.TEXT) {
        converter = getTextConverter(parsedFormat, getLocale(), getName(), getDataProviderID());
        if (converter == null) {
            converter = super.getConverter(cls);
        }
    } else if (displayFormat == null && list == null) {
        converter = super.getConverter(cls);
    } else if (mappedType == IColumnTypes.DATETIME) {
        boolean lenient = Boolean.TRUE.equals(UIUtils.getUIProperty(this.getScriptObject(), application, IApplication.DATE_FORMATTERS_LENIENT, Boolean.TRUE));
        StateFullSimpleDateFormat displayFormatter = new StateFullSimpleDateFormat(displayFormat, null, application.getLocale(), lenient);
        if (!parsedFormat.isMask() && parsedFormat.getEditFormat() != null) {
            StateFullSimpleDateFormat editFormatter = new StateFullSimpleDateFormat(parsedFormat.getEditFormat(), null, application.getLocale(), lenient);
            converter = new FormatConverter(this, eventExecutor, displayFormatter, editFormatter, parsedFormat);
        } else {
            converter = new FormatConverter(this, eventExecutor, displayFormatter, parsedFormat);
        }
    } else if ((mappedType == IColumnTypes.INTEGER || mappedType == IColumnTypes.NUMBER) && (list == null || !list.hasRealValues())) {
        int maxLength = parsedFormat.getMaxLength() == null ? -1 : parsedFormat.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)) {
            char[] chars = new char[maxLength];
            for (int i = 0; i < chars.length; i++) chars[i] = '#';
            displayFormat = new String(chars);
        }
        if (displayFormat != null) {
            RoundHalfUpDecimalFormat displayFormatter = new RoundHalfUpDecimalFormat(displayFormat, application.getLocale());
            if (parsedFormat.getEditFormat() != null) {
                RoundHalfUpDecimalFormat editFormatter = new RoundHalfUpDecimalFormat(parsedFormat.getEditFormat(), application.getLocale());
                converter = new FormatConverter(this, eventExecutor, displayFormatter, editFormatter, parsedFormat);
            } else {
                converter = new FormatConverter(this, eventExecutor, displayFormatter, parsedFormat);
            }
        }
    }
    if (list != null) {
        if (converter == null && mappedType == IColumnTypes.TEXT && list instanceof GlobalMethodValueList) {
            converter = getTextConverter(parsedFormat, getLocale(), getName(), getDataProviderID());
        }
        converter = new ValuelistValueConverter(list, this, converter);
    }
    if (converter == null)
        converter = super.getConverter(cls);
    return converter;
}
Also used : RoundHalfUpDecimalFormat(com.servoy.j2db.util.RoundHalfUpDecimalFormat) GlobalMethodValueList(com.servoy.j2db.dataprocessing.GlobalMethodValueList) Point(java.awt.Point) StateFullSimpleDateFormat(com.servoy.j2db.util.StateFullSimpleDateFormat)

Example 2 with StateFullSimpleDateFormat

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

the class DataComboBox method setValueObject.

public void setValueObject(Object data) {
    try {
        if (format instanceof StateFullSimpleDateFormat && (data instanceof Date || data == null)) {
            // set original date for date merging in StateFullSimpleDateFormat, see DataField.NullDateFormatter.stringToValue()
            ((StateFullSimpleDateFormat) format).setOriginal((Date) data);
        }
        adjusting = true;
        if (editProvider != null)
            editProvider.setAdjusting(true);
        if (needEntireState) {
            if (tooltip != null) {
                // $NON-NLS-1$
                super.setToolTipText("");
            }
        } else {
            if (tooltip != null) {
                super.setToolTipText(tooltip);
            }
        }
        if (getSelectedItem() == null || !Utils.equalObjects(selectedItemReminder, data)) {
            int index = getListModelWrapper().realValueIndexOf(data);
            if (index == -1) {
                if (isEditable() && accesibleStateHolder.isEditable()) {
                    setSelectedItem(data);
                } else {
                    if (// $NON-NLS-1$
                    data == null || "".equals(data) || getListModelWrapper().hasRealValues()) {
                        setSelectedItem(null);
                        // data does not resolve now, when underlying data changes real selected data may map.
                        if (getListModelWrapper().hasRealValues())
                            getListModelWrapper().setRealSelectedObject(data);
                    } else {
                        getModel().setSelectedItem(data);
                    }
                }
            } else if (dataType == IColumnTypes.DATETIME) {
                setSelectedItem(data);
            } else {
                setSelectedIndex(index);
            }
            setValueValid(true, null);
        }
    } finally {
        adjusting = false;
        if (editProvider != null)
            editProvider.setAdjusting(false);
    }
    fireOnRender(false);
}
Also used : Date(java.util.Date) Point(java.awt.Point) StateFullSimpleDateFormat(com.servoy.j2db.util.StateFullSimpleDateFormat)

Example 3 with StateFullSimpleDateFormat

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

the class DataComboBox method installFormat.

public void installFormat(ComponentFormat componentFormat) {
    this.dataType = componentFormat.uiType;
    if (isEditable()) {
        FormattedComboBoxEditor ed = (FormattedComboBoxEditor) getEditor();
        ed.setFormat(componentFormat);
    }
    if (!componentFormat.parsedFormat.isEmpty()) {
        String displayFormat = componentFormat.parsedFormat.getDisplayFormat();
        try {
            switch(Column.mapToDefaultType(dataType)) {
                case IColumnTypes.NUMBER:
                    format = new RoundHalfUpDecimalFormat(displayFormat, application.getLocale());
                    break;
                case IColumnTypes.INTEGER:
                    format = new RoundHalfUpDecimalFormat(displayFormat, application.getLocale());
                    break;
                case IColumnTypes.DATETIME:
                    format = new StateFullSimpleDateFormat(displayFormat, Boolean.TRUE.equals(UIUtils.getUIProperty(this, IApplication.DATE_FORMATTERS_LENIENT, Boolean.TRUE)));
                    // format = new SimpleDateFormat(formatString);
                    break;
                default:
                    // TODO Jan/Johan what to do here? Should we create our own MaskFormatter? Where we can insert a mask??
                    break;
            }
        } catch (Exception ex) {
            Debug.error(ex);
        }
    }
}
Also used : RoundHalfUpDecimalFormat(com.servoy.j2db.util.RoundHalfUpDecimalFormat) ParseException(java.text.ParseException) StateFullSimpleDateFormat(com.servoy.j2db.util.StateFullSimpleDateFormat)

Example 4 with StateFullSimpleDateFormat

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

the class DataComboBox method setSelectedItem.

@Override
public void setSelectedItem(Object anObject) {
    Object oldSelection = selectedItemReminder;
    if (oldSelection == null || !oldSelection.equals(anObject)) {
        if (IValueList.SEPARATOR.equals(anObject)) {
            // here we can't know the actual index to choose a nearby item, maybe there are multiple separators, so just ignore it (it's probably nicer anyway);
            // reset the editor's value, cause it might have been set as well by an Enter key press
            getEditor().setItem(oldSelection);
            return;
        }
        if (anObject != null && !isEditable()) {
            // For non editable combo boxes, an invalid selection
            // will be rejected.
            boolean found = false;
            // as the equals value and not the complete date, that can have seperate input fields.
            if (anObject instanceof Date && format instanceof StateFullSimpleDateFormat) {
                StateFullSimpleDateFormat sfsd = (StateFullSimpleDateFormat) format;
                String selectedFormat = sfsd.format(anObject);
                for (int i = 0; i < dataModel.getSize(); i++) {
                    try {
                        Object element = dataModel.getElementAt(i);
                        if (!(element instanceof Date))
                            continue;
                        String elementFormat = sfsd.format(element);
                        if (selectedFormat.equals(elementFormat)) {
                            found = true;
                            break;
                        }
                    } catch (RuntimeException e) {
                        Debug.error(e);
                    }
                }
            } else {
                if (dataModel instanceof ComboModelListModelWrapper) {
                    found = ((ComboModelListModelWrapper) dataModel).indexOf(anObject) != -1;
                } else
                    for (int i = 0; i < dataModel.getSize(); i++) {
                        if (anObject.equals(dataModel.getElementAt(i))) {
                            found = true;
                            break;
                        }
                    }
            }
            if (!found) {
                return;
            }
        }
        // Must toggle the state of this flag since this method
        // call may result in ListDataEvents being fired.
        selectingItem = true;
        dataModel.setSelectedItem(anObject);
        selectingItem = false;
        if (selectedItemReminder != dataModel.getSelectedItem()) {
            // in case a users implementation of ComboBoxModel
            // doesn't fire a ListDataEvent when the selection
            // changes.
            selectedItemChanged();
        }
    }
    fireActionEvent();
}
Also used : ComboModelListModelWrapper(com.servoy.j2db.util.model.ComboModelListModelWrapper) Date(java.util.Date) Point(java.awt.Point) StateFullSimpleDateFormat(com.servoy.j2db.util.StateFullSimpleDateFormat)

Example 5 with StateFullSimpleDateFormat

use of com.servoy.j2db.util.StateFullSimpleDateFormat 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

StateFullSimpleDateFormat (com.servoy.j2db.util.StateFullSimpleDateFormat)5 Point (java.awt.Point)4 RoundHalfUpDecimalFormat (com.servoy.j2db.util.RoundHalfUpDecimalFormat)3 GlobalMethodValueList (com.servoy.j2db.dataprocessing.GlobalMethodValueList)2 ParseException (java.text.ParseException)2 Date (java.util.Date)2 CustomValueList (com.servoy.j2db.dataprocessing.CustomValueList)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 ComboModelListModelWrapper (com.servoy.j2db.util.model.ComboModelListModelWrapper)1 ServoyMaskFormatter (com.servoy.j2db.util.text.ServoyMaskFormatter)1 JFormattedTextField (javax.swing.JFormattedTextField)1