com.anrisoftware.prefdialog.miscswing.validatingfields.ValidatingTextField.java Source code

Java tutorial

Introduction

Here is the source code for com.anrisoftware.prefdialog.miscswing.validatingfields.ValidatingTextField.java

Source

/*
 * Copyright 2013-2016 Erwin Mller <erwin.mueller@deventm.org>
 *
 * This file is part of prefdialog-misc-swing.
 *
 * prefdialog-misc-swing is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * prefdialog-misc-swing is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with prefdialog-misc-swing. If not, see <http://www.gnu.org/licenses/>.
 */
package com.anrisoftware.prefdialog.miscswing.validatingfields;

import static javax.swing.SwingUtilities.invokeLater;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JTextField;
import javax.swing.text.Document;

import org.apache.commons.lang3.builder.ToStringBuilder;

import com.anrisoftware.prefdialog.miscswing.tooltip.ToolTipShower;

/**
 * Using an input verifier to verify the input on focus lost and input enter.
 * Mark the field and show a tool-tip text if the input is not valid.
 *
 * @author Erwin Mueller, erwin.mueller@deventm.org
 * @since 3.0
 */
@SuppressWarnings("serial")
public class ValidatingTextField extends JTextField {

    private InputVerifier verifier;

    private ActionListener validateAction;

    private InputVerifier parentVerifier;

    private ValidatingComponent validating;

    /**
     * @see JTextField#JTextField()
     */
    public ValidatingTextField() {
        super();
        setupField();
    }

    /**
     * @see JTextField#JTextField(Document, String, int)
     */
    public ValidatingTextField(Document doc, String text, int columns) {
        super(doc, text, columns);
        setupField();
    }

    /**
     * @see JTextField#JTextField(int)
     */
    public ValidatingTextField(int columns) {
        super(columns);
        setupField();
    }

    /**
     * @see JTextField#JTextField(String, int)
     */
    public ValidatingTextField(String text, int columns) {
        super(text, columns);
        setupField();
    }

    /**
     * @see JTextField#JTextField(String)
     */
    public ValidatingTextField(String text) {
        super(text);
        setupField();
    }

    private void setupField() {
        this.validating = new ValidatingComponent(this, new ToolTipShower(this));
        this.parentVerifier = new InputVerifier() {

            @Override
            public boolean verify(JComponent input) {
                return validating.isValid();
            }
        };
        this.verifier = new InputVerifier() {

            @Override
            public boolean verify(JComponent input) {
                JTextField field = ValidatingTextField.this;
                boolean valid = verifyField(parentVerifier, field);
                setValidInAWT(valid);
                return valid;
            }

        };
        this.validateAction = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                verifyField();
            }

        };
        setInputVerifier(verifier);
        addActionListener(validateAction);
    }

    /**
     * Verify the field from an external event.
     */
    protected void verifyField() {
        verifier.shouldYieldFocus(this);
    }

    /**
     * Verifies the field.
     *
     * @param verifier
     *            the {@link InputVerifier} that verifiers the input.
     *
     * @param input
     *            the {@link JTextField}.
     *
     * @return {@code true} if the input is valid and the focus can be yield.
     */
    protected boolean verifyField(InputVerifier verifier, JTextField input) {
        return verifier.verify(input);
    }

    private void setValidInAWT(final boolean valid) {
        invokeLater(new Runnable() {

            @Override
            public void run() {
                setInputValid(valid);
            }
        });
    }

    /**
     * Sets the input verifier that validates the input.
     *
     * @param verifier
     *            the {@link InputVerifier}.
     */
    public void setVerifier(InputVerifier verifier) {
        this.parentVerifier = verifier;
    }

    /**
     * Returns the input verifier that validated the input.
     *
     * @return the {@link InputVerifier}.
     */
    public InputVerifier getVerifier() {
        return parentVerifier;
    }

    /**
     * @see ValidatingComponent#setInvalidBackground(Color)
     */
    public void setInvalidBackground(Color color) {
        validating.setInvalidBackground(color);
    }

    /**
     * @see ValidatingComponent#getInvalidBackground()
     */
    public Color getInvalidBackground() {
        return validating.getInvalidBackground();
    }

    /**
     * @see ValidatingComponent#setValid(boolean)
     */
    public void setInputValid(boolean valid) {
        validating.setValid(valid);
    }

    /**
     * @see ValidatingComponent#isValid()
     */
    public boolean isInputValid() {
        return validating.isValid();
    }

    /**
     * @see ValidatingComponent#setInvalidText(String)
     */
    public void setInvalidText(String text) {
        validating.setInvalidText(text);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        validating.paint(g);
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this).appendSuper(super.toString()).toString();
    }
}