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