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 NumericValidator extends integration.forms.ControlValidator
32 {
33 
34     /** Creates a new instance of NumericValidator */
NumericValidator( )35     public NumericValidator( )
36     {
37     }
38 
explainInvalid( Object Value )39     public String explainInvalid( Object Value )
40     {
41         try
42         {
43             double value = ((Double)Value).doubleValue();
44             if ( Double.compare( Double.NaN, value ) == 0 )
45                 return "This is NotANumber";
46             if ( !isProperRange( value ) )
47                 return "The value must be between 0 and 100";
48             if ( !isProperDigitCount( value ) )
49                 return "The value must have at most one decimal digit";
50         }
51         catch( java.lang.Exception e )
52         {
53             return "This is no valid number";
54         }
55         return "";
56     }
57 
isValid( Object Value )58     public boolean isValid( Object Value )
59     {
60         try
61         {
62             double value = ((Double)Value).doubleValue();
63             if ( Double.compare( Double.NaN, value ) == 0 )
64                 return false;
65             if ( !isProperRange( value ) )
66                 return false;
67             if ( !isProperDigitCount( value ) )
68                 return false;
69             return true;
70         }
71         catch( java.lang.Exception e )
72         {
73         }
74         return false;
75     }
76 
isProperRange( double value)77     private boolean isProperRange( double value)
78     {
79         return ( value >= 0 ) && ( value <= 100 );
80     }
81 
isProperDigitCount( double value)82     private boolean isProperDigitCount( double value)
83     {
84         return ( java.lang.Math.floor( value * 10 ) == value * 10 );
85     }
86 }
87