Mercurial > projects > dwt2
diff org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/StringToBooleanPrimitiveConverter.d @ 78:0a55d2d5a946
Added file for databinding
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Tue, 14 Apr 2009 11:35:29 +0200 |
parents | |
children | 6be48cf9f95c |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/StringToBooleanPrimitiveConverter.d Tue Apr 14 11:35:29 2009 +0200 @@ -0,0 +1,90 @@ +/* + * Copyright cast(C) 2005 db4objects Inc. http://www.db4o.com + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * db4objects - Initial API and implementation + * Tom Schindl<tom.schindl@bestsolution.at> - bugfix for 217940 + */ +module org.eclipse.core.internal.databinding.conversion.StringToBooleanPrimitiveConverter; + +import java.lang.all; + +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; +import java.util.StringTokenizer; + +import org.eclipse.core.databinding.conversion.IConverter; +import org.eclipse.core.internal.databinding.BindingMessages; + +/** + * StringToBooleanPrimitiveConverter. + */ +public class StringToBooleanPrimitiveConverter : IConverter { + private static final String[] trueValues; + + private static final String[] falseValues; + + static this(){ + String delimiter = BindingMessages.getStringcast(BindingMessages.VALUE_DELIMITER); + String values = BindingMessages.getStringcast(BindingMessages.TRUE_STRING_VALUES); + trueValues = valuesToSortedArray(delimiter, values); + + values = BindingMessages.getStringcast(BindingMessages.FALSE_STRING_VALUES); + falseValues = valuesToSortedArray(delimiter, values); + } + + /** + * Returns a sorted array with all values converted to upper case. + * + * @param delimiter + * @param values + * @return sorted array of values + */ + private static String[] valuesToSortedArray(String delimiter, String values) { + List list = new LinkedList(); + StringTokenizer tokenizer = new StringTokenizer(values, delimiter); + while (tokenizer.hasMoreTokens()) { + list.add(tokenizer.nextToken().toUpperCase()); + } + + String[] array = cast(String[]) list.toArray(new String[list.size()]); + Arrays.sort(array); + + return array; + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.jface.binding.converter.IConverter#convert(java.lang.Object) + */ + public Object convert(Object source) { + String s = cast(String) source; + s = s.toUpperCase(); + + if (Arrays.binarySearch(trueValues, s) > -1) { + return Boolean.TRUE; + } + + if (Arrays.binarySearch(falseValues, s) > -1) { + return Boolean.FALSE; + } + + throw new IllegalArgumentException(s ~ " is not a legal bool value"); //$NON-NLS-1$ + } + + public Object getFromType() { + return String.classinfo; + } + + public Object getToType() { + return Boolean.TYPE; + } + +}