Package net.helipilot50.stocktrade.displayproject

Source Code of net.helipilot50.stocktrade.displayproject.DataFieldVerifier

/*
Copyright (c) 2003-2009 ITerative Consulting Pty Ltd. All Rights Reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

o Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
 
o Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the distribution.
   
o This jcTOOL Helper Class software, whether in binary or source form may not be used within,
or to derive, any other product without the specific prior written permission of the copyright holder

 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


*/
package net.helipilot50.stocktrade.displayproject;

import java.text.ParseException;
import java.util.Locale;

import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField.AbstractFormatter;
import javax.swing.text.DateFormatter;
/**
* This class verifies the input to a data field. It shows a default error dialog box, similar to Forte, when input is not valid.
* This verifier is added to the <code>DataField</code> in the <code>DataFieldFactory</code>.
*
*/
public class DataFieldVerifier extends InputVerifier {
    public static final String INVALID_MESSAGE;
    protected AbstractFormatter af;

    protected java.text.DateFormat df;

    static {
      if (Locale.getDefault().getLanguage().equals(Locale.FRENCH.getLanguage())){
        INVALID_MESSAGE = "Valeur incorrecte entr�e: S'il vous pla�t entrer de nouveau";
      } else if (Locale.getDefault().getLanguage().equals(Locale.ITALIAN.getLanguage())){
        INVALID_MESSAGE = "Valore non valido iscritti: si prega di immettere di nuovo";
      } else if (Locale.getDefault().getLanguage().equals(Locale.GERMAN.getLanguage())){
        INVALID_MESSAGE = "Ung�ltigen Wert eingegeben: Bitte erneut eingeben";
//      } else if (Locale.getDefault().getLanguage().equals(Locale.JAPANESE.getLanguage())){
//        INVALID_MESSAGE = "";
      } else {
        INVALID_MESSAGE = "The input you have supplied for the current field\n is only partially complete; "
                  + "before you can leave\n the field it must contain a valid value or NULL.";
      }
    }
    public DataFieldVerifier() {
        super();
    }

    public DataFieldVerifier(AbstractFormatter formatter) {
        super();
        af = formatter;
    }


    /**
     * Determine whether empty masks should be allowed.
     */
    private boolean allowsEmptyMasks = false;

    /**
     * if the allowsIncompleteFields flag is true, the user can exit the
     * field at any time, irrespective of whether a valid format has been
     * entered.
     */
    private boolean allowIncompleteFields = false;

    /**
     * Set a flag that determines if the user is allowed to exit an incomplete field. The default
     * value of this flag is <pre>false</pre>
     * @param allowIncompleteFields If true, the user can exit the field at any time, otherwise
     * they will only be allowed to exit the field when the field matches the mask specification.
     */
    public void setAllowIncompleteFields(boolean allowIncompleteFields) {
        this.allowIncompleteFields = allowIncompleteFields;
    }

    /**
     * Return whether the field should be allowed to be exited whilst containing an incompete value
     * @return
     */
    public boolean getAllowIncompleteFields() {
        return this.allowIncompleteFields;
    }

    public boolean verify(JComponent input) {

        if (input instanceof DataField && !this.getAllowIncompleteFields()){
            DataField df = (DataField)input;

            AbstractFormatter formatter = df.getFormatter();

            if (formatter != null) {
                //  attempt to apply the mask to the text ....
                String text = df.getText();
                try {
                    formatter.stringToValue(text);
                    df.thisFieldIsInError = false;
                    return true;
                }
                catch (ParseException pe) {
                    //  the text did not match the mask .... lets see if the text is really empty
                        if (df.firstKey) {
                            df.thisFieldIsInError = false;
                            return true;
                        }
                        else if (this.allowsEmptyMasks) {
                        try {
                            String mask = formatter.valueToString(null);

                            if (mask.equals(text)) {
                                //  the mask is empty ... this is OK ... make sure that the datafield is really empty
                                df.clearValue();
                                df.thisFieldIsInError = false;
                                return true;
                            }

                        } catch (ParseException e) {
                            // do nothing as it will invoke the error message below
                        }
                        }

                        df.setInputVerifier(null);
                        // PM:13/05/2008: LIB-12 added default beep;
                        java.awt.Toolkit.getDefaultToolkit().beep();
                        WindowManager.showErrorDialog(INVALID_MESSAGE);
                        // TF: Added setting of the focus to the component to ensure we're on the correct tab pane
                        FocusHelper.setFocus(df);
                        df.setInputVerifier(this);
                        df.thisFieldIsInError = true;
                        return false;
                }
            }
        }

        else {
            // No formatter, must always be true
            return true;
        }
        return false;
    }

    public AbstractFormatter getFormatter() {
        return af;
    }

    public void setFormatter(AbstractFormatter af) {
        this.af = af;
    }

    public java.text.DateFormat getDateFormat() {
        return df;
    }

    public void setDateFormat(java.text.DateFormat df) {
        df.setLenient(false);
        DateFormatter aDateFormatter = new DateFormatter(df);
        aDateFormatter.setAllowsInvalid(false);
        setFormatter(aDateFormatter);
        this.df = df;
    }

    public boolean isAllowsEmptyMasks() {
        return allowsEmptyMasks;
    }

    public void setAllowsEmptyMasks(boolean allowsEmptyMasks) {
        this.allowsEmptyMasks = allowsEmptyMasks;
    }
}
TOP

Related Classes of net.helipilot50.stocktrade.displayproject.DataFieldVerifier

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.