78
|
1 /*
|
|
2 * Copyright cast(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 * Tom Schindl<tom.schindl@bestsolution.at> - bugfix for 217940
|
|
12 */
|
|
13 module org.eclipse.core.internal.databinding.conversion.StringToBooleanPrimitiveConverter;
|
|
14
|
|
15 import java.lang.all;
|
|
16
|
|
17 import java.util.Arrays;
|
|
18 import java.util.LinkedList;
|
|
19 import java.util.List;
|
|
20 import java.util.StringTokenizer;
|
|
21
|
|
22 import org.eclipse.core.databinding.conversion.IConverter;
|
|
23 import org.eclipse.core.internal.databinding.BindingMessages;
|
|
24
|
|
25 /**
|
|
26 * StringToBooleanPrimitiveConverter.
|
|
27 */
|
|
28 public class StringToBooleanPrimitiveConverter : IConverter {
|
|
29 private static final String[] trueValues;
|
|
30
|
|
31 private static final String[] falseValues;
|
|
32
|
|
33 static this(){
|
|
34 String delimiter = BindingMessages.getStringcast(BindingMessages.VALUE_DELIMITER);
|
|
35 String values = BindingMessages.getStringcast(BindingMessages.TRUE_STRING_VALUES);
|
|
36 trueValues = valuesToSortedArray(delimiter, values);
|
|
37
|
|
38 values = BindingMessages.getStringcast(BindingMessages.FALSE_STRING_VALUES);
|
|
39 falseValues = valuesToSortedArray(delimiter, values);
|
|
40 }
|
|
41
|
|
42 /**
|
|
43 * Returns a sorted array with all values converted to upper case.
|
|
44 *
|
|
45 * @param delimiter
|
|
46 * @param values
|
|
47 * @return sorted array of values
|
|
48 */
|
|
49 private static String[] valuesToSortedArray(String delimiter, String values) {
|
|
50 List list = new LinkedList();
|
|
51 StringTokenizer tokenizer = new StringTokenizer(values, delimiter);
|
|
52 while (tokenizer.hasMoreTokens()) {
|
|
53 list.add(tokenizer.nextToken().toUpperCase());
|
|
54 }
|
|
55
|
|
56 String[] array = cast(String[]) list.toArray(new String[list.size()]);
|
|
57 Arrays.sort(array);
|
|
58
|
|
59 return array;
|
|
60 }
|
|
61
|
|
62 /*
|
|
63 * (non-Javadoc)
|
|
64 *
|
|
65 * @see org.eclipse.jface.binding.converter.IConverter#convert(java.lang.Object)
|
|
66 */
|
|
67 public Object convert(Object source) {
|
|
68 String s = cast(String) source;
|
|
69 s = s.toUpperCase();
|
|
70
|
|
71 if (Arrays.binarySearch(trueValues, s) > -1) {
|
|
72 return Boolean.TRUE;
|
|
73 }
|
|
74
|
|
75 if (Arrays.binarySearch(falseValues, s) > -1) {
|
|
76 return Boolean.FALSE;
|
|
77 }
|
|
78
|
|
79 throw new IllegalArgumentException(s ~ " is not a legal bool value"); //$NON-NLS-1$
|
|
80 }
|
|
81
|
|
82 public Object getFromType() {
|
|
83 return String.classinfo;
|
|
84 }
|
|
85
|
|
86 public Object getToType() {
|
|
87 return Boolean.TYPE;
|
|
88 }
|
|
89
|
|
90 }
|