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 package integration.forms;
24 
25 import com.sun.star.form.binding.*;
26 
27 /**
28  *
29  * @author  fs@openoffice.org
30  */
31 public class TimeValidator extends integration.forms.ControlValidator
32 {
33 
34     /** Creates a new instance of NumericValidator */
TimeValidator( )35     public TimeValidator( )
36     {
37     }
38 
explainInvalid( Object Value )39     public String explainInvalid( Object Value )
40     {
41         try
42         {
43             if ( isVoid( Value ) )
44                 return "empty input";
45 
46             com.sun.star.util.Time timeValue = (com.sun.star.util.Time)Value;
47             if ( isInvalidTime( timeValue ) )
48                 return "this is no valid time";
49             if ( !isFullHour( timeValue ) )
50                 return "time must denote a full hour";
51         }
52         catch( java.lang.Exception e )
53         {
54             return "this is no valid time";
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.Time timeValue = (com.sun.star.util.Time)
67                 com.sun.star.uno.AnyConverter.toObject(
68                     com.sun.star.util.Time.class, Value);
69             if ( isInvalidTime( timeValue ) )
70                 return false;
71             if ( !isFullHour( timeValue ) )
72                 return false;
73             return true;
74         }
75         catch( java.lang.Exception e )
76         {
77             e.printStackTrace( System.err );
78         }
79         return false;
80     }
81 
isInvalidTime( com.sun.star.util.Time timeValue )82     private boolean isInvalidTime( com.sun.star.util.Time timeValue )
83     {
84         return ( timeValue.Hours == -1 ) && ( timeValue.Minutes == -1 ) && ( timeValue.Seconds == -1 ) && ( timeValue.HundredthSeconds == -1 );
85     }
86 
isFullHour( com.sun.star.util.Time timeValue )87     private boolean isFullHour( com.sun.star.util.Time timeValue )
88     {
89         return ( timeValue.Minutes == 0 ) && ( timeValue.Seconds == 0 ) && ( timeValue.HundredthSeconds == 0 );
90     }
91 }
92