001    package market;
002    
003    /**
004     * A ValueChecker, which checks if a String is in the format of an Integer (e.g. 1500).<br>
005     * 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 VCPositiveInteger implements ValueChecker {
012    
013        /**
014             * ID for serialization.
015             */
016            private static final long serialVersionUID = -4216805874516986303L;
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 VCPositiveInteger(String identifier, boolean mayBeEmpty) {
038            this.identifier = identifier;
039            this.mayBeEmpty = mayBeEmpty;
040        }
041    
042        /**
043         * @param identifier the identifier.
044         */
045        public VCPositiveInteger(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 an Integer, otherwise <code>false</code>.
055         */
056        public boolean isValidValue(String content) {
057            error = 0;
058            if (!mayBeEmpty && content.equals("")) {
059                error = 1;
060            }
061            if (!content.equals("") && error == 0) {
062                try {
063                    if (!content.equals("")) {
064                        Integer.parseInt(content);
065                    }
066                }
067                catch (NumberFormatException e) {
068                    error = 2;
069                }
070            }
071            if (!content.equals("") && error == 0) {
072                if (Integer.valueOf(content).intValue() < 0) error = 3;
073            }
074            return error == 0;
075        }
076    
077        /**
078         * @return an error message depending on the value of {@link #error}.
079         */
080        public String getErrorString() {
081            String returnValue = "";
082            switch (error) {
083                case 1:
084                    returnValue = "Fehlerhafte Eingabe im Feld " + identifier + ": Das Feld darf nicht leer sein.";
085                    break;
086                case 2:
087                    returnValue = "Fehlerhafte Eingabe im Feld " + identifier + ": Es sind nur ganzzahlige Werte erlaubt.";
088                    break;
089                case 3:
090                    returnValue = "Fehlerhafte Eingabe im Feld " + identifier + ": Es werden positive Zahlen erwartet.";
091                    break;
092            }
093            error = 0;
094            return returnValue;
095        }
096    }