Search in sources :

Example 91 with PropertyEditor

use of java.beans.PropertyEditor in project geronimo-xbean by apache.

the class PropertyEditors method toString.

public static String toString(Object value) throws PropertyEditorException {
    if (value == null)
        throw new NullPointerException("value is null");
    // get an editor for this type
    Class type = value.getClass();
    PropertyEditor editor = findConverterOrEditor(type);
    if (editor instanceof Converter) {
        Converter converter = (Converter) editor;
        return converter.toString(value);
    }
    if (editor == null) {
        throw new PropertyEditorException("Unable to find PropertyEditor for " + type.getSimpleName());
    }
    // create the string value
    editor.setValue(value);
    String textValue;
    try {
        textValue = editor.getAsText();
    } catch (Exception e) {
        throw new PropertyEditorException("Error while converting a \"" + type.getSimpleName() + "\" to text " + " using the property editor " + editor.getClass().getSimpleName(), e);
    }
    return textValue;
}
Also used : PropertyEditor(java.beans.PropertyEditor)

Example 92 with PropertyEditor

use of java.beans.PropertyEditor in project JWildfire by thargor6.

the class DancingFractalsController method motionTableClicked.

public void motionTableClicked() {
    if (!refreshing) {
        if (motionPropertyPnl != null) {
            motionPropertyRootPnl.remove(motionPropertyPnl);
            motionPropertyPnl = null;
        }
        if (project.getMotions().size() > 0 && motionTable.getSelectedRow() >= 0 && motionTable.getSelectedRow() < project.getMotions().size()) {
            Motion motion = project.getMotions().get(motionTable.getSelectedRow());
            @SuppressWarnings("rawtypes") Map<Class, PropertyEditor> editors = new HashMap<Class, PropertyEditor>();
            editors.put(Motion.class, new MotionTypeEditor(project.getMotions()));
            motionPropertyPnl = new PropertyPanel(motion, editors);
            motionPropertyPnl.setDescriptionVisible(false);
            PropertyChangeListener listener = new PropertyChangeListener() {

                public void propertyChange(PropertyChangeEvent evt) {
                    refreshing = true;
                    try {
                        int oldSel = motionTable.getSelectedRow();
                        refreshMotionTable();
                        motionTable.getSelectionModel().setSelectionInterval(oldSel, oldSel);
                        enableControls();
                    } finally {
                        refreshing = false;
                    }
                }
            };
            motionPropertyPnl.addPropertySheetChangeListener(listener);
            motionPropertyRootPnl.add(motionPropertyPnl, BorderLayout.CENTER);
            enableControls();
        }
    }
    refreshMotionLinksTable();
    motionPropertyRootPnl.invalidate();
    motionPropertyRootPnl.validate();
}
Also used : Motion(org.jwildfire.create.tina.dance.motion.Motion) PropertyChangeEvent(java.beans.PropertyChangeEvent) PropertyChangeListener(java.beans.PropertyChangeListener) HashMap(java.util.HashMap) ComboBoxPropertyEditor(com.l2fprod.common.beans.editor.ComboBoxPropertyEditor) PropertyEditor(java.beans.PropertyEditor) PropertyPanel(org.jwildfire.swing.PropertyPanel)

Example 93 with PropertyEditor

use of java.beans.PropertyEditor in project opennms by OpenNMS.

the class Global method convertStringTo.

public <T> T convertStringTo(String value, Class<T> typeClass) {
    if (typeClass == String.class) {
        return typeClass.cast(value);
    } else {
        try {
            final PropertyEditor editor = getDefaultEditor(typeClass);
            editor.setAsText(value);
            return typeClass.cast(editor.getValue());
        } catch (Exception e) {
            throw new IllegalArgumentException("Unable to find a property editor for " + typeClass);
        }
    }
}
Also used : PropertyEditor(java.beans.PropertyEditor)

Example 94 with PropertyEditor

use of java.beans.PropertyEditor in project opennms by OpenNMS.

the class Coercions method coerceToObject.

// -------------------------------------
/**
 * Coerces a value to the specified Class that is not covered by any
 * of the above cases
 */
public static Object coerceToObject(Object pValue, Class pClass, Logger pLogger) throws ELException {
    if (pValue == null) {
        return null;
    } else if (pClass.isAssignableFrom(pValue.getClass())) {
        return pValue;
    } else if (pValue instanceof String) {
        String str = (String) pValue;
        PropertyEditor pe = PropertyEditorManager.findEditor(pClass);
        if (pe == null) {
            if ("".equals(str)) {
                return null;
            } else {
                if (pLogger.isLoggingError()) {
                    pLogger.logError(Constants.NO_PROPERTY_EDITOR, str, pClass.getName());
                }
                return null;
            }
        }
        try {
            pe.setAsText(str);
            return pe.getValue();
        } catch (IllegalArgumentException exc) {
            if ("".equals(str)) {
                return null;
            } else {
                if (pLogger.isLoggingError()) {
                    pLogger.logError(Constants.PROPERTY_EDITOR_ERROR, exc, pValue, pClass.getName());
                }
                return null;
            }
        }
    } else {
        if (pLogger.isLoggingError()) {
            pLogger.logError(Constants.COERCE_TO_OBJECT, pValue.getClass().getName(), pClass.getName());
        }
        return null;
    }
}
Also used : PropertyEditor(java.beans.PropertyEditor)

Example 95 with PropertyEditor

use of java.beans.PropertyEditor in project opennms by OpenNMS.

the class Global method convertStringTo.

/**
 * Convert string to.
 *
 * @param <T> the generic type
 * @param value the value
 * @param typeClass the type class
 * @return the t
 */
public <T> T convertStringTo(String value, Class<T> typeClass) {
    if (typeClass == String.class) {
        return typeClass.cast(value);
    } else {
        try {
            final PropertyEditor editor = getDefaultEditor(typeClass);
            editor.setAsText(value);
            return typeClass.cast(editor.getValue());
        } catch (Exception e) {
            throw new IllegalArgumentException("Unable to find a property editor for " + typeClass);
        }
    }
}
Also used : PropertyEditor(java.beans.PropertyEditor)

Aggregations

PropertyEditor (java.beans.PropertyEditor)167 Test (org.junit.jupiter.api.Test)59 HashMap (java.util.HashMap)14 Test (org.junit.Test)13 Map (java.util.Map)11 File (java.io.File)10 URI (java.net.URI)9 TestBean (org.springframework.beans.testfixture.beans.TestBean)9 IOException (java.io.IOException)7 Path (java.nio.file.Path)7 Resource (org.springframework.core.io.Resource)7 Method (java.lang.reflect.Method)6 URL (java.net.URL)6 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)6 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)6 PropertyDescriptor (java.beans.PropertyDescriptor)5 Field (java.lang.reflect.Field)4 ArrayList (java.util.ArrayList)4 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)4 StringArrayPropertyEditor (org.springframework.beans.propertyeditors.StringArrayPropertyEditor)4