1*b9b79128SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*b9b79128SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*b9b79128SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*b9b79128SAndrew Rist  * distributed with this work for additional information
6*b9b79128SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*b9b79128SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*b9b79128SAndrew Rist  * "License"); you may not use this file except in compliance
9*b9b79128SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*b9b79128SAndrew Rist  *
11*b9b79128SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*b9b79128SAndrew Rist  *
13*b9b79128SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*b9b79128SAndrew Rist  * software distributed under the License is distributed on an
15*b9b79128SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b9b79128SAndrew Rist  * KIND, either express or implied.  See the License for the
17*b9b79128SAndrew Rist  * specific language governing permissions and limitations
18*b9b79128SAndrew Rist  * under the License.
19*b9b79128SAndrew Rist  *
20*b9b79128SAndrew Rist  *************************************************************/
21*b9b79128SAndrew Rist 
22*b9b79128SAndrew Rist 
23cdf0e10cSrcweir package integration.forms;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import com.sun.star.form.binding.*;
26cdf0e10cSrcweir 
27cdf0e10cSrcweir /**
28cdf0e10cSrcweir  *
29cdf0e10cSrcweir  * @author  fs@openoffice.org
30cdf0e10cSrcweir  */
31cdf0e10cSrcweir public class TimeValidator extends integration.forms.ControlValidator
32cdf0e10cSrcweir {
33cdf0e10cSrcweir 
34cdf0e10cSrcweir     /** Creates a new instance of NumericValidator */
TimeValidator( )35cdf0e10cSrcweir     public TimeValidator( )
36cdf0e10cSrcweir     {
37cdf0e10cSrcweir     }
38cdf0e10cSrcweir 
explainInvalid( Object Value )39cdf0e10cSrcweir     public String explainInvalid( Object Value )
40cdf0e10cSrcweir     {
41cdf0e10cSrcweir         try
42cdf0e10cSrcweir         {
43cdf0e10cSrcweir             if ( isVoid( Value ) )
44cdf0e10cSrcweir                 return "empty input";
45cdf0e10cSrcweir 
46cdf0e10cSrcweir             com.sun.star.util.Time timeValue = (com.sun.star.util.Time)Value;
47cdf0e10cSrcweir             if ( isInvalidTime( timeValue ) )
48cdf0e10cSrcweir                 return "this is no valid time";
49cdf0e10cSrcweir             if ( !isFullHour( timeValue ) )
50cdf0e10cSrcweir                 return "time must denote a full hour";
51cdf0e10cSrcweir         }
52cdf0e10cSrcweir         catch( java.lang.Exception e )
53cdf0e10cSrcweir         {
54cdf0e10cSrcweir             return "this is no valid time";
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.Time timeValue = (com.sun.star.util.Time)
67cdf0e10cSrcweir                 com.sun.star.uno.AnyConverter.toObject(
68cdf0e10cSrcweir                     com.sun.star.util.Time.class, Value);
69cdf0e10cSrcweir             if ( isInvalidTime( timeValue ) )
70cdf0e10cSrcweir                 return false;
71cdf0e10cSrcweir             if ( !isFullHour( timeValue ) )
72cdf0e10cSrcweir                 return false;
73cdf0e10cSrcweir             return true;
74cdf0e10cSrcweir         }
75cdf0e10cSrcweir         catch( java.lang.Exception e )
76cdf0e10cSrcweir         {
77cdf0e10cSrcweir             e.printStackTrace( System.err );
78cdf0e10cSrcweir         }
79cdf0e10cSrcweir         return false;
80cdf0e10cSrcweir     }
81cdf0e10cSrcweir 
isInvalidTime( com.sun.star.util.Time timeValue )82cdf0e10cSrcweir     private boolean isInvalidTime( com.sun.star.util.Time timeValue )
83cdf0e10cSrcweir     {
84cdf0e10cSrcweir         return ( timeValue.Hours == -1 ) && ( timeValue.Minutes == -1 ) && ( timeValue.Seconds == -1 ) && ( timeValue.HundredthSeconds == -1 );
85cdf0e10cSrcweir     }
86cdf0e10cSrcweir 
isFullHour( com.sun.star.util.Time timeValue )87cdf0e10cSrcweir     private boolean isFullHour( com.sun.star.util.Time timeValue )
88cdf0e10cSrcweir     {
89cdf0e10cSrcweir         return ( timeValue.Minutes == 0 ) && ( timeValue.Seconds == 0 ) && ( timeValue.HundredthSeconds == 0 );
90cdf0e10cSrcweir     }
91cdf0e10cSrcweir }
92