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