예제 #1
0
        protected override bool AllowTextChange(string currentText, NSRange changedRange, string replacementText, string result)
        {
            int i;

            if (result.Length == 0)
            {
                return(true);
            }

            // If the user backspaced, allow the change to go through.
            if (replacementText.Length == 0)
            {
                return(true);
            }

            // Validate according to http://en.wikipedia.org/wiki/Aircraft_registration

            // First step is to validate that all characters are ASCII AlphaNumeric.
            for (i = 0; i < replacementText.Length; i++)
            {
                if ((replacementText[i] >= 'A' && replacementText[i] <= 'Z') ||
                    (replacementText[i] >= 'a' && replacementText[i] <= 'z') ||
                    (replacementText[i] >= '0' && replacementText[i] <= '9'))
                {
                    continue;
                }

                return(false);
            }

#if ENABLE_GLOBAL_SUPPORT
            // If the resulting tail number begins with a digit, make sure it is valid.
            if (result[0] >= '0' && result[0] <= '9')
            {
                bool matched = false;

                Console.WriteLine("Validating {0}...", result.ToString());
                foreach (var prefix in PrefixesStartingWithDigits)
                {
                    if (result[0] != prefix[0])
                    {
                        Console.WriteLine("0: {0} does not match {1}", result[0], prefix[0]);
                        continue;
                    }

                    if (result.Length > 1 && result[1] != prefix[1])
                    {
                        Console.WriteLine("1: {0} does not match {1}", result[1], prefix[1]);
                        continue;
                    }

                    matched = true;
                    break;
                }

                if (!matched)
                {
                    return(false);
                }
            }
#endif

            if (result.Length == 1)
            {
                return(true);
            }

            // Verify that the text length does not exceed the max length for a tail number.
            if (result.Length > GetMaxLength(result[0], result[1]))
            {
                return(false);
            }

            // If this is a U.S. tail number, verify that it doesn't contain an I or O.
            if (result[0] == 'N')
            {
                if (result.IndexOfAny(NotAllowedInTheUS) != -1)
                {
                    return(false);
                }
            }

            if (AutoComplete && result.Length > 2)
            {
                // Try to auto-complete the registration number from the database of known aircraft.
                var matches = LogBook.GetMatchingAircraft(result);

                // If we've only got 1 match, auto-complete for the user.
                if (matches != null && matches.Count == 1)
                {
                    Value = matches[0].TailNumber;
                    return(false);
                }
            }

            return(true);
        }