1*ae15d43aSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ae15d43aSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ae15d43aSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ae15d43aSAndrew Rist  * distributed with this work for additional information
6*ae15d43aSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ae15d43aSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ae15d43aSAndrew Rist  * "License"); you may not use this file except in compliance
9*ae15d43aSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ae15d43aSAndrew Rist  *
11*ae15d43aSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ae15d43aSAndrew Rist  *
13*ae15d43aSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ae15d43aSAndrew Rist  * software distributed under the License is distributed on an
15*ae15d43aSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ae15d43aSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ae15d43aSAndrew Rist  * specific language governing permissions and limitations
18*ae15d43aSAndrew Rist  * under the License.
19*ae15d43aSAndrew Rist  *
20*ae15d43aSAndrew Rist  *************************************************************/
21*ae15d43aSAndrew Rist 
22*ae15d43aSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir import com.sun.star.form.binding.*;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir /**
27cdf0e10cSrcweir  *
28cdf0e10cSrcweir  * @author  fs@openoffice.org
29cdf0e10cSrcweir  */
30cdf0e10cSrcweir public class DateValidator extends ControlValidator
31cdf0e10cSrcweir {
32cdf0e10cSrcweir 
33cdf0e10cSrcweir     /** Creates a new instance of NumericValidator */
DateValidator( )34cdf0e10cSrcweir     public DateValidator( )
35cdf0e10cSrcweir     {
36cdf0e10cSrcweir     }
37cdf0e10cSrcweir 
explainInvalid( Object Value )38cdf0e10cSrcweir     public String explainInvalid( Object Value )
39cdf0e10cSrcweir     {
40cdf0e10cSrcweir         try
41cdf0e10cSrcweir         {
42cdf0e10cSrcweir             if ( isVoid( Value ) )
43cdf0e10cSrcweir                 return "empty input";
44cdf0e10cSrcweir 
45cdf0e10cSrcweir             com.sun.star.util.Date dateValue = (com.sun.star.util.Date)Value;
46cdf0e10cSrcweir             if ( isDedicatedInvalidDate( dateValue ) )
47cdf0e10cSrcweir                 return "this is no valid date";
48cdf0e10cSrcweir 
49cdf0e10cSrcweir             if ( !isNextMonthsDate( dateValue ) )
50cdf0e10cSrcweir                 return "date must denote a day in the current month";
51cdf0e10cSrcweir         }
52cdf0e10cSrcweir         catch( java.lang.Exception e )
53cdf0e10cSrcweir         {
54cdf0e10cSrcweir             return "oops. What did you enter for this to happen?";
55cdf0e10cSrcweir         }
56cdf0e10cSrcweir         return "";
57cdf0e10cSrcweir     }
58cdf0e10cSrcweir 
isValid( Object Value )59cdf0e10cSrcweir     public boolean isValid( Object Value )
60cdf0e10cSrcweir     {
61cdf0e10cSrcweir         try
62cdf0e10cSrcweir         {
63cdf0e10cSrcweir             if ( isVoid( Value ) )
64cdf0e10cSrcweir                 return false;
65cdf0e10cSrcweir 
66cdf0e10cSrcweir             com.sun.star.util.Date dateValue = (com.sun.star.util.Date)
67cdf0e10cSrcweir                 com.sun.star.uno.AnyConverter.toObject(
68cdf0e10cSrcweir                     com.sun.star.util.Date.class, Value);
69cdf0e10cSrcweir             if ( isDedicatedInvalidDate( dateValue ) )
70cdf0e10cSrcweir                 return false;
71cdf0e10cSrcweir 
72cdf0e10cSrcweir             if ( !isNextMonthsDate( dateValue ) )
73cdf0e10cSrcweir                 return false;
74cdf0e10cSrcweir             return true;
75cdf0e10cSrcweir         }
76cdf0e10cSrcweir         catch( java.lang.Exception e )
77cdf0e10cSrcweir         {
78cdf0e10cSrcweir             e.printStackTrace( System.err );
79cdf0e10cSrcweir         }
80cdf0e10cSrcweir         return false;
81cdf0e10cSrcweir     }
82cdf0e10cSrcweir 
isDedicatedInvalidDate( com.sun.star.util.Date dateValue )83cdf0e10cSrcweir     private boolean isDedicatedInvalidDate( com.sun.star.util.Date dateValue )
84cdf0e10cSrcweir     {
85cdf0e10cSrcweir         return ( dateValue.Day == 0 ) && ( dateValue.Month == 0 ) && ( dateValue.Year == 0 );
86cdf0e10cSrcweir     }
87cdf0e10cSrcweir 
isNextMonthsDate( com.sun.star.util.Date dateValue )88cdf0e10cSrcweir     private boolean isNextMonthsDate( com.sun.star.util.Date dateValue )
89cdf0e10cSrcweir     {
90cdf0e10cSrcweir         int overallMonth = dateValue.Year * 12 + dateValue.Month - 1;
91cdf0e10cSrcweir 
92cdf0e10cSrcweir         int todaysMonth = new java.util.Date().getMonth();
93cdf0e10cSrcweir         int todaysYear = new java.util.Date().getYear() + 1900;
94cdf0e10cSrcweir         int todaysOverallMonth = todaysYear * 12 + todaysMonth;
95cdf0e10cSrcweir 
96cdf0e10cSrcweir         return overallMonth == todaysOverallMonth;
97cdf0e10cSrcweir     }
98cdf0e10cSrcweir }
99