2
* PropertyEditorUtil.java
4
* Created on August 16, 2006, 7:09 PM
6
* To change this template, choose Tools | Template Manager
7
* and open the template in the editor.
10
package org.jdesktop.swingx.editors;
12
import java.lang.reflect.Constructor;
13
import java.lang.reflect.InvocationTargetException;
19
public class PropertyEditorUtil {
20
//the text could be in many different formats. All of the supported formats are as follows:
21
//(where x and y are doubles of some form)
26
//[ x , y ] or any other arbitrary whitespace
27
// x , y ] or any other arbitrary whitespace
28
//[ x , y or any other arbitrary whitespace
30
// x , y (or any other arbitrary whitespace)
36
//any other value throws an IllegalArgumentException
37
public static Object createValueFromString(String text, int count, Class objectClass, Class paramClass) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
38
// strip all extra whitespace
39
text = text.replaceAll("[\\[|,| |\\]]"," ");
40
text = text.replaceAll("\\s+"," ");
42
// u.p("text = " + text);
43
if (text == null || text.equals("") || text.equals("null")) {
47
String[] strs = text.split(" ");
50
// u.p("len = " + strs.length);
51
if(strs.length != count) {
54
Object[] params = new Object[count];
55
Class[] paramClasses = new Class[count];
56
for(int i=0; i<strs.length; i++) {
57
if(paramClass == int.class) {
58
params[i] = Integer.valueOf(strs[i]);
59
paramClasses[i] = paramClass;
61
if(paramClass == double.class) {
62
params[i] = Double.valueOf(strs[i]);
63
paramClasses[i] = paramClass;
68
Constructor con = objectClass.getConstructor(paramClasses);
69
return con.newInstance(params);