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