Search in sources :

Example 1 with LengthDocumentValidator

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

the class InputDialog method init.

private void init(int maxlength, int identType) {
    getContentPane().setLayout(new BorderLayout());
    field = new JTextField();
    if (maxlength == 0) {
        field.setDocument(new ValidatingDocument(new IdentDocumentValidator(identType)));
    } else {
        field.setDocument(new ValidatingDocument(new ValidatingDocument.IDocumentValidator[] { new IdentDocumentValidator(identType), new LengthDocumentValidator(maxlength) }));
    }
    JPanel pane = new JPanel();
    pane.setBorder(BorderFactory.createEmptyBorder(10, 10, 0, 10));
    pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
    msg = new JLabel();
    msg.setHorizontalAlignment(SwingConstants.LEFT);
    msg.setAlignmentX(Component.LEFT_ALIGNMENT);
    msg.setPreferredSize(new Dimension(170, 15));
    pane.add(msg);
    pane.add(Box.createRigidArea(new Dimension(0, J2DBClient.BUTTON_SPACING)));
    field.setAlignmentX(Component.LEFT_ALIGNMENT);
    pane.add(field);
    // msg.setSize(field.getPreferredSize());
    getContentPane().add(pane, BorderLayout.CENTER);
    // $NON-NLS-1$
    JButton setButton = new JButton(Messages.getString("servoy.button.ok"));
    setButton.addActionListener(this);
    // $NON-NLS-1$
    setButton.setActionCommand("ok");
    // $NON-NLS-1$
    JButton cancelButton = new JButton(Messages.getString("servoy.button.cancel"));
    cancelButton.addActionListener(this);
    // $NON-NLS-1$
    cancelButton.setActionCommand("cancel");
    JPanel buttonPane = new JPanel();
    buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.X_AXIS));
    buttonPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    buttonPane.add(Box.createHorizontalGlue());
    buttonPane.add(setButton);
    buttonPane.add(Box.createRigidArea(new Dimension(J2DBClient.BUTTON_SPACING, 0)));
    buttonPane.add(cancelButton);
    buttonPane.setPreferredSize(new Dimension(300, 50));
    getContentPane().add(buttonPane, BorderLayout.SOUTH);
    getRootPane().setDefaultButton(setButton);
}
Also used : JPanel(javax.swing.JPanel) BorderLayout(java.awt.BorderLayout) ValidatingDocument(com.servoy.j2db.util.docvalidator.ValidatingDocument) BoxLayout(javax.swing.BoxLayout) JButton(javax.swing.JButton) LengthDocumentValidator(com.servoy.j2db.util.docvalidator.LengthDocumentValidator) JLabel(javax.swing.JLabel) Dimension(java.awt.Dimension) JTextField(javax.swing.JTextField) IdentDocumentValidator(com.servoy.j2db.util.docvalidator.IdentDocumentValidator)

Example 2 with LengthDocumentValidator

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

LengthDocumentValidator (com.servoy.j2db.util.docvalidator.LengthDocumentValidator)2 CustomValueList (com.servoy.j2db.dataprocessing.CustomValueList)1 GlobalMethodValueList (com.servoy.j2db.dataprocessing.GlobalMethodValueList)1 RoundHalfUpDecimalFormat (com.servoy.j2db.util.RoundHalfUpDecimalFormat)1 StateFullSimpleDateFormat (com.servoy.j2db.util.StateFullSimpleDateFormat)1 IdentDocumentValidator (com.servoy.j2db.util.docvalidator.IdentDocumentValidator)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 ValidatingDocument (com.servoy.j2db.util.docvalidator.ValidatingDocument)1 ServoyMaskFormatter (com.servoy.j2db.util.text.ServoyMaskFormatter)1 BorderLayout (java.awt.BorderLayout)1 Dimension (java.awt.Dimension)1 Point (java.awt.Point)1 ParseException (java.text.ParseException)1 BoxLayout (javax.swing.BoxLayout)1 JButton (javax.swing.JButton)1 JFormattedTextField (javax.swing.JFormattedTextField)1 JLabel (javax.swing.JLabel)1 JPanel (javax.swing.JPanel)1