blob: a64e0ae440d91868244c21f4ebbedf1cab34021c (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
/*******************************************************************************
* Copyright (c) 2006, 2010 Soyatec (http://www.soyatec.com) and others.
* 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:
* Soyatec - initial API and implementation
*******************************************************************************/
package org.eclipse.papyrus.xwt.converters;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import org.eclipse.core.databinding.conversion.IConverter;
import org.eclipse.papyrus.xwt.XWTException;
/**
* String to int[] converter
*
* @author jliu
*/
public class StringToIntArray implements IConverter {
public static StringToIntArray instance = new StringToIntArray();
/*
* (non-Javadoc)
*
* @see org.eclipse.core.databinding.conversion.IConverter#convert(java.lang. Object)
*/
public Object convert(Object fromObject) {
String source = (String)fromObject;
List<Integer> result = new ArrayList<Integer>();
StringTokenizer stk = new StringTokenizer(source, ",");
while(stk.hasMoreTokens()) {
String nextToken = stk.nextToken();
try {
result.add(Integer.parseInt(nextToken.trim()));
} catch (NumberFormatException e) {
throw new XWTException(source + " is not integer array");
}
}
int[] intArray = new int[result.size()];
for(int i = 0; i < result.size(); i++) {
intArray[i] = result.get(i);
}
return intArray;
}
/*
* (non-Javadoc)
*
* @see org.eclipse.core.databinding.conversion.IConverter#getFromType()
*/
public Object getFromType() {
return String.class;
}
/*
* (non-Javadoc)
*
* @see org.eclipse.core.databinding.conversion.IConverter#getToType()
*/
public Object getToType() {
return int[].class;
}
}
|