1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 import com.sun.star.form.binding.*;
29 
30 /**
31  *
32  * @author  fs@openoffice.org
33  */
34 public class TimeValidator extends ControlValidator
35 {
36 
37     /** Creates a new instance of NumericValidator */
38     public TimeValidator( )
39     {
40     }
41 
42     public String explainInvalid( Object Value )
43     {
44         try
45         {
46             if ( isVoid( Value ) )
47                 return "empty input";
48 
49             com.sun.star.util.Time timeValue = (com.sun.star.util.Time)Value;
50             if ( isInvalidTime( timeValue ) )
51                 return "this is no valid time";
52             if ( !isFullHour( timeValue ) )
53                 return "time must denote a full hour";
54         }
55         catch( java.lang.Exception e )
56         {
57             return "this is no valid time";
58         }
59         return "";
60     }
61 
62     public boolean isValid( Object Value )
63     {
64         try
65         {
66             if ( isVoid( Value ) )
67                 return false;
68 
69             com.sun.star.util.Time timeValue = (com.sun.star.util.Time)
70                 com.sun.star.uno.AnyConverter.toObject(
71                     com.sun.star.util.Time.class, Value);
72             if ( isInvalidTime( timeValue ) )
73                 return false;
74             if ( !isFullHour( timeValue ) )
75                 return false;
76             return true;
77         }
78         catch( java.lang.Exception e )
79         {
80             e.printStackTrace( System.err );
81         }
82         return false;
83     }
84 
85     private boolean isInvalidTime( com.sun.star.util.Time timeValue )
86     {
87         return ( timeValue.Hours == -1 ) && ( timeValue.Minutes == -1 ) && ( timeValue.Seconds == -1 ) && ( timeValue.HundredthSeconds == -1 );
88     }
89 
90     private boolean isFullHour( com.sun.star.util.Time timeValue )
91     {
92         return ( timeValue.Minutes == 0 ) && ( timeValue.Seconds == 0 ) && ( timeValue.HundredthSeconds == 0 );
93     }
94 }
95