001    package market;
002    
003    import java.text.SimpleDateFormat;
004    
005    /**
006     * A ValueChecker, which checks if a String is in the format dd.MM.yyyy (e.g. 01.01.2000).<br>
007     * Notations like 1.1.2000 are also not allowed.<br>
008     * <br>
009     * As this ValueChecker is only used by {@link market.swing.JTFCheckable checkable text fields},
010     * an identifier is passed via the constructor. This descriptive identifier is used in the error string,
011     * to enable the user to associate an error message with the causative text field.
012     */
013    public class VCDate implements ValueChecker {
014    
015        /**
016         * The identifier used by {@link #getErrorString()}.
017         */
018        protected String identifier;
019    
020        /**
021         * Defines if an empty String is considered to be an error or not.
022         */
023        protected boolean mayBeEmpty;
024    
025        /**
026         * Internal error code. Set by {@link #isValidValue(String)}, queried by {@link #getErrorString()}.
027         */
028        private int error = 0;
029    
030        /**
031         * @param identifier the identifier.
032         * @param mayBeEmpty <code>true</code> if an empty should be considered valid, otherwise <code>false</code>.
033         */
034        public VCDate(String identifier, boolean mayBeEmpty) {
035            this.identifier = identifier;
036            this.mayBeEmpty = mayBeEmpty;
037        }
038    
039        /**
040         * @param identifier the identifier.
041         */
042        public VCDate(String identifier) {
043            this.identifier = identifier;
044            this.mayBeEmpty = false;
045        }
046    
047        /**
048         * Checks String for validity and, if necessary, sets an internal error code according to the
049         * detected error.
050         * @param content the String to be checked.
051         * @return <code>true</code> if the String is in a valid date format, otherwise <code>false</code>.
052         */
053        public boolean isValidValue(String content) {
054            error = 0;
055            if (!mayBeEmpty && content.equals("")) {
056                error = 1;
057            } else {
058                //dd.MM.yyyy: strict check - the days' values are checked regarding the month, including leap years
059                SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
060                sdf.setLenient(false);
061                try {
062                    sdf.parse(content);
063                    //Parsing also accepts dates without left handed zeros, so this extra check for the
064                    //String's length is being done
065                    if (content.length() != 10) {
066                        error = 2;
067                    }
068                }
069                catch (java.text.ParseException e) {
070                    error = 2;
071                }
072            }
073            return error == 0;
074        }
075    
076        /**
077         * @return an error message depending on the value of {@link #error}.
078         */
079        public String getErrorString() {
080            String returnValue = "";
081            switch (error) {
082                case 1:
083                    returnValue = "Fehlerhafte Eingabe im Feld " + identifier +
084                            ": Das Feld darf nicht leer sein.";
085                    break;
086                case 2:
087                    returnValue = "Fehlerhafte Eingabe im Feld " + identifier +
088                            ": Das Datum muss in der Form 01.01.2000 angegeben werden.";
089                    break;
090            }
091            error = 0;
092            return returnValue;
093        }
094    }