KickJava   Java API By Example, From Geeks To Geeks.

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


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  * ByteValidator. Validate String input for Bytes
19  */

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

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

35     public ValidationError isValid(Object JavaDoc value) {
36         try {
37             Byte.parseByte((String JavaDoc)value);
38             return null;
39         } catch (Throwable JavaDoc t) {
40             return ValidationError.error(getHint());
41         }
42     }
43
44     private String JavaDoc getHint() {
45         return BindingMessages.getString("Validate_RangeStart") + Byte.MIN_VALUE + //$NON-NLS-1$
46
BindingMessages.getString("and") + Byte.MAX_VALUE + "."; //$NON-NLS-1$ //$NON-NLS-2$
47
}
48
49 }
50
Popular Tags