KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > databinding > provisional > validation > String2FloatValidator


1 /*
2  * Copyright (C) 2005 db4objects Inc. http://www.db4o.com
3  *
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * db4objects - Initial API and implementation
11  */

12 package org.eclipse.jface.internal.databinding.provisional.validation;
13
14 import org.eclipse.jface.internal.databinding.internal.BindingMessages;
15
16
17 /**
18  * FloatValidator. Verify string to float data conversion
19  */

20 public class String2FloatValidator implements IValidator {
21     
22     public ValidationError isPartiallyValid(Object JavaDoc fragment) {
23         if (((String JavaDoc)fragment).matches("\\-?[0-9]*\\.?[0-9]*([0-9]+[e|E]\\-?([0-9]+\\.)?[0-9]*)?")) //$NON-NLS-1$
24
return null;
25
26         return ValidationError.error(getHint());
27     }
28     
29     public ValidationError isValid(Object JavaDoc value) {
30         try {
31             Float.parseFloat((String JavaDoc)value);
32             return null;
33         } catch (Exception JavaDoc e) {
34             return ValidationError.error(getHint());
35         }
36     }
37
38     private String JavaDoc getHint() {
39         return BindingMessages.getString("Validate_Like") + //$NON-NLS-1$
40
BindingMessages.getString("Validate_Number_Examples") //$NON-NLS-1$
41
+ Float.MIN_VALUE +
42         ", " + Float.MAX_VALUE + "."; //$NON-NLS-1$ //$NON-NLS-2$
43
}
44
45 }
46
Popular Tags