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