~ubuntu-branches/ubuntu/trusty/libswingx-java/trusty

« back to all changes in this revision

Viewing changes to swingx-beaninfo/src/main/java/org/jdesktop/swingx/editors/PropertyEditorUtil.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2011-03-06 00:28:45 UTC
  • mfrom: (4.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20110306002845-escned3cbqp5qx0t
Tags: 1:1.6.2-1
* New upstream release.
* Switch to maven as build system:
  - d/control: drop ant, add maven-debian-helper
  - d/rules: use maven.mk
* d/patches/pom.diff: drop, uneeded since upstream fixed its dependencies.
* d/watch: update to use java.net directly.
* d/rules: force debian version for JARs (Closes: #603495).
* d/copyright: Update to lastest DEP-5 r166.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * PropertyEditorUtil.java
 
3
 *
 
4
 * Created on August 16, 2006, 7:09 PM
 
5
 *
 
6
 * To change this template, choose Tools | Template Manager
 
7
 * and open the template in the editor.
 
8
 */
 
9
 
 
10
package org.jdesktop.swingx.editors;
 
11
 
 
12
import java.lang.reflect.Constructor;
 
13
import java.lang.reflect.InvocationTargetException;
 
14
 
 
15
/**
 
16
 *
 
17
 * @author joshy
 
18
 */
 
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)
 
22
    //[x,y]
 
23
    //[x y]
 
24
    //x,y]
 
25
    //[x,y
 
26
    //[ x , y ] or any other arbitrary whitespace
 
27
    // x , y ] or any other arbitrary whitespace
 
28
    //[ x , y  or any other arbitrary whitespace
 
29
    //x,y
 
30
    // x , y (or any other arbitrary whitespace)
 
31
    //x y
 
32
    // (empty space)
 
33
    //null
 
34
    //[]
 
35
    //[ ]
 
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+"," ");
 
41
        text = text.trim();
 
42
//        u.p("text = " + text);
 
43
        if (text == null || text.equals("") || text.equals("null")) {
 
44
            return null;
 
45
        }
 
46
        // split by spaces
 
47
        String[] strs = text.split(" ");
 
48
//        u.p("split:");
 
49
//        u.p(strs);
 
50
//        u.p("len = " + strs.length);
 
51
        if(strs.length != count) {
 
52
            return null;
 
53
        }
 
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;
 
60
            }
 
61
            if(paramClass == double.class) {
 
62
                params[i] = Double.valueOf(strs[i]);
 
63
                paramClasses[i] = paramClass;
 
64
            }
 
65
        }
 
66
//        u.p("parms = ");
 
67
//        u.p(params);
 
68
        Constructor con = objectClass.getConstructor(paramClasses);
 
69
        return con.newInstance(params);
 
70
    }
 
71
    
 
72
}