001    package market;
002    
003    /**
004     * A ValueChecker, which checks if a String is in the format of a Double (e.g. 15.256).<br>
005     * Notations like 15,256 are also allowed. The number must not be negative.<br>
006     * <br>
007     * As this ValueChecker is only used by {@link market.swing.JTFCheckable checkable text fields},
008     * an identifier is passed via the constructor. This descriptive identifier is used in the error string,
009     * to enable the user to associate an error message with the causative text field.
010     */
011    public class VCPositiveDouble implements ValueChecker {
012    
013        /**
014             * ID for serialization.
015             */
016            private static final long serialVersionUID = -4228996753013103729L;
017    
018            /**
019         * The identifier used by {@link #getErrorString()}.
020         */
021        protected String identifier;
022    
023        /**
024         * Defines if an empty String is considered to be an error or not.
025         */
026        protected boolean mayBeEmpty;
027    
028        /**
029         * Internal error code. Set by {@link #isValidValue(String)}, queried by {@link #getErrorString()}.
030         */
031        private int error;
032    
033        /**
034         * @param identifier the identifier.
035         * @param mayBeEmpty <code>true</code> if an empty should be considered valid, otherwise <code>false</code>.
036         */
037        public VCPositiveDouble(String identifier, boolean mayBeEmpty) {
038            this.identifier = identifier;
039            this.mayBeEmpty = mayBeEmpty;
040        }
041    
042        /**
043         * @param identifier the identifier.
044         */
045        public VCPositiveDouble(String identifier) {
046            this.identifier = identifier;
047            this.mayBeEmpty = false;
048        }
049    
050        /**
051         * Checks String for validity and, if necessary, sets an internal error code according to the
052         * detected error.
053         * @param content the String to be checked.
054         * @return <code>true</code> if the String represents a Double, otherwise <code>false</code>.
055         */
056        public boolean isValidValue(String content) {
057            error = 0;
058            if (!mayBeEmpty && content.equals("")) error = 1;
059            if (!content.equals("") && error == 0) {
060                if (Conversions.convertComma(content) == null) error = 2;
061            }
062            if (!content.equals("") && error == 0) {
063                if (Double.valueOf(Conversions.convertComma(content)).doubleValue() < 0) error = 3;
064            }
065            return error == 0;
066        }
067    
068        /**
069         * @return an error message depending on the value of {@link #error}.
070         */
071        public String getErrorString() {
072            String returnValue = "";
073            switch (error) {
074                case 1:  returnValue =  "Fehlerhafte Eingabe im Feld " + identifier + ": Das Feld darf nicht leer sein."; break;
075                case 2:  returnValue = "Fehlerhafte Eingabe im Feld " + identifier + ": Es werden Kommazahlen erwartet."; break;
076                case 3:  returnValue = "Fehlerhafte Eingabe im Feld " + identifier + ": Es werden positive Zahlen erwartet."; break;
077            }
078            error = 0;
079            return returnValue;
080        }
081    }