KickJava   Java API By Example, From Geeks To Geeks.

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


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  * DoubleValidator. Verify data input for Doubles
19  *
20  * @author djo
21  */

22 public class String2DoubleValidator implements IValidator {
23     
24     /* (non-Javadoc)
25      * @see org.eclipse.jface.databinding.validator.IValidator#isPartiallyValid(java.lang.Object)
26      */

27     public ValidationError isPartiallyValid(Object JavaDoc fragment) {
28         if (((String JavaDoc)fragment).matches("\\-?[0-9]*\\.?[0-9]*([0-9]+[e|E]\\-?([0-9]+\\.)?[0-9]*)?")) //$NON-NLS-1$
29
return null;
30
31         return ValidationError.error(getHint());
32     }
33     
34     /* (non-Javadoc)
35      * @see org.eclipse.jface.viewers.ICellEditorValidator#isValid(java.lang.Object)
36      */

37     public ValidationError isValid(Object JavaDoc value) {
38         try {
39             Double.parseDouble((String JavaDoc)value);
40             return null;
41         } catch (Throwable JavaDoc t) {
42             return ValidationError.error(getHint());
43         }
44     }
45
46     private String JavaDoc getHint() {
47         return BindingMessages.getString("Validate_Like") + //$NON-NLS-1$
48
BindingMessages.getString("Validate_Number_Examples") //$NON-NLS-1$
49
+ Double.MIN_VALUE +
50             ", " + Double.MAX_VALUE + "."; //$NON-NLS-1$ //$NON-NLS-2$
51
}
52
53 }
54
Popular Tags