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 TimeValidator extends ControlValidator
31 {
32 
33     /** Creates a new instance of NumericValidator */
TimeValidator( )34     public TimeValidator( )
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.Time timeValue = (com.sun.star.util.Time)Value;
46             if ( isInvalidTime( timeValue ) )
47                 return "this is no valid time";
48             if ( !isFullHour( timeValue ) )
49                 return "time must denote a full hour";
50         }
51         catch( java.lang.Exception e )
52         {
53             return "this is no valid time";
54         }
55         return "";
56     }
57 
isValid( Object Value )58     public boolean isValid( Object Value )
59     {
60         try
61         {
62             if ( isVoid( Value ) )
63                 return false;
64 
65             com.sun.star.util.Time timeValue = (com.sun.star.util.Time)
66                 com.sun.star.uno.AnyConverter.toObject(
67                     com.sun.star.util.Time.class, Value);
68             if ( isInvalidTime( timeValue ) )
69                 return false;
70             if ( !isFullHour( timeValue ) )
71                 return false;
72             return true;
73         }
74         catch( java.lang.Exception e )
75         {
76             e.printStackTrace( System.err );
77         }
78         return false;
79     }
80 
isInvalidTime( com.sun.star.util.Time timeValue )81     private boolean isInvalidTime( com.sun.star.util.Time timeValue )
82     {
83         return ( timeValue.Hours == -1 ) && ( timeValue.Minutes == -1 ) && ( timeValue.Seconds == -1 ) && ( timeValue.HundredthSeconds == -1 );
84     }
85 
isFullHour( com.sun.star.util.Time timeValue )86     private boolean isFullHour( com.sun.star.util.Time timeValue )
87     {
88         return ( timeValue.Minutes == 0 ) && ( timeValue.Seconds == 0 ) && ( timeValue.HundredthSeconds == 0 );
89     }
90 }
91