Search in sources :

Example 1 with ManagedObject

use of org.jwildfire.base.ManagedObject in project JWildfire by thargor6.

the class Action method importProperties.

public void importProperties(ManagedObject pManagedObject) {
    BeanInfo beanInfo = pManagedObject.getBeanInfo();
    if (beanInfo != null) {
        PropertyDescriptor[] props = beanInfo.getPropertyDescriptors();
        if (props != null) {
            for (PropertyDescriptor prop : props) {
                Method readMethod = prop.getReadMethod();
                if (readMethod != null) {
                    try {
                        Object val = readMethod.invoke(pManagedObject);
                        Class<?> cls = prop.getPropertyType();
                        if (cls == Color.class) {
                            Color color = (Color) val;
                            String name = prop.getName();
                            Parameter param;
                            param = new Parameter();
                            parameterList.add(param);
                            param.setName(name + ".r");
                            param.setValue(color != null ? String.valueOf(color.getRed()) : null);
                            param = new Parameter();
                            parameterList.add(param);
                            param.setName(name + ".g");
                            param.setValue(color != null ? String.valueOf(color.getGreen()) : null);
                            param = new Parameter();
                            parameterList.add(param);
                            param.setName(name + ".b");
                            param.setValue(color != null ? String.valueOf(color.getBlue()) : null);
                        } else {
                            Parameter param = new Parameter();
                            parameterList.add(param);
                            param.setName(prop.getName());
                            param.setValue(val != null ? val.toString() : "");
                        }
                    } catch (Throwable ex) {
                        ex.printStackTrace();
                    }
                }
            }
        }
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) Color(java.awt.Color) ManagedObject(org.jwildfire.base.ManagedObject) Method(java.lang.reflect.Method)

Example 2 with ManagedObject

use of org.jwildfire.base.ManagedObject in project JWildfire by thargor6.

the class Action method setProperties.

@SuppressWarnings({ "unchecked", "rawtypes" })
public void setProperties(ManagedObject pManagedObject, BufferList pBufferList) throws Exception {
    BeanInfo beanInfo = pManagedObject.getBeanInfo();
    if (beanInfo != null) {
        PropertyDescriptor[] props = beanInfo.getPropertyDescriptors();
        if (props != null) {
            for (PropertyDescriptor prop : props) {
                Method writeMethod = prop.getWriteMethod();
                if (writeMethod != null) {
                    Object val = null;
                    Parameter parameter = getParameterByName(prop.getName());
                    if (parameter != null) {
                        String strVal = parameter.getValue();
                        Class<?> cls = prop.getPropertyType();
                        if (cls == Integer.class)
                            val = Integer.valueOf(strVal);
                        else if (cls == int.class)
                            val = Tools.FTOI(Double.parseDouble(strVal));
                        else if (cls == String.class)
                            val = strVal;
                        else if (cls == Double.class)
                            val = Double.valueOf(strVal);
                        else if (cls == double.class)
                            val = Double.parseDouble(strVal);
                        else if (cls == Boolean.class)
                            val = Boolean.valueOf(strVal);
                        else if (cls == boolean.class)
                            val = Boolean.parseBoolean(strVal);
                        else if (cls.isEnum())
                            val = Enum.valueOf((Class<Enum>) cls, strVal);
                        else if (cls == Buffer.class)
                            val = pBufferList.bufferByName(strVal);
                        else if (cls == Color.class) {
                            int r = 0, g = 0, b = 0;
                            Pattern pattern = Pattern.compile("java\\.awt\\.Color\\[r=([0-9]+),g=([0-9]+),b=([0-9]+)\\]");
                            Matcher matcher = pattern.matcher(strVal);
                            if (!matcher.find())
                                throw new IllegalArgumentException(strVal);
                            r = Integer.parseInt(matcher.group(1));
                            g = Integer.parseInt(matcher.group(2));
                            b = Integer.parseInt(matcher.group(3));
                            val = new Color(r, g, b);
                        } else
                            throw new IllegalArgumentException(cls.toString());
                    // System.out.println("set " + parameter.getName() + " " + val + " (" + cls + ")");
                    }
                    if (parameter == null) {
                        parameter = getParameterByName(prop.getName() + ".r");
                        if (parameter != null) {
                            Parameter parameterR = parameter;
                            Parameter parameterG = getParameterByName(prop.getName() + ".g");
                            Parameter parameterB = getParameterByName(prop.getName() + ".b");
                            int r = Tools.roundColor(Double.parseDouble(parameterR.getValue()));
                            int g = Tools.roundColor(Double.parseDouble(parameterG.getValue()));
                            int b = Tools.roundColor(Double.parseDouble(parameterB.getValue()));
                            val = new Color(r, g, b);
                        // System.out.println("set " + parameter.getName() + " " + val + " (" + java.awt.Color.class + ")");
                        }
                    }
                    if (val != null) {
                        try {
                            writeMethod.invoke(pManagedObject, val);
                        } catch (Exception ex) {
                            throw new Exception("Error setting property " + prop.getName() + " " + val, ex);
                        }
                    }
                }
            }
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) PropertyDescriptor(java.beans.PropertyDescriptor) Matcher(java.util.regex.Matcher) BeanInfo(java.beans.BeanInfo) Color(java.awt.Color) Method(java.lang.reflect.Method) ManagedObject(org.jwildfire.base.ManagedObject)

Aggregations

Color (java.awt.Color)2 BeanInfo (java.beans.BeanInfo)2 PropertyDescriptor (java.beans.PropertyDescriptor)2 Method (java.lang.reflect.Method)2 ManagedObject (org.jwildfire.base.ManagedObject)2 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1