Example usage for java.beans PropertyEditor setValue

List of usage examples for java.beans PropertyEditor setValue

Introduction

In this page you can find the example usage for java.beans PropertyEditor setValue.

Prototype

void setValue(Object value);

Source Link

Document

Set (or change) the object that is to be edited.

Usage

From source file:org.hdiv.web.servlet.tags.form.ValueFormatterHDIV.java

/**
 * Build the display value of the supplied <code>Object</code>, HTML escaped
 * as required. If the supplied value is not a {@link String} and the supplied
 * {@link PropertyEditor} is not null then the {@link PropertyEditor} is used
 * to obtain the display value./*from   w w w .j a  v a 2  s .  c  o m*/
 * @see #getDisplayString(Object, boolean)
 */
public static String getDisplayString(Object value, PropertyEditor propertyEditor, boolean htmlEscape) {
    if (propertyEditor != null && !(value instanceof String)) {
        try {
            propertyEditor.setValue(value);
            String text = propertyEditor.getAsText();
            if (text != null) {
                return getDisplayString(text, htmlEscape);
            }
        } catch (Throwable ex) {
            // The PropertyEditor might not support this value... pass through.
        }
    }
    return getDisplayString(value, htmlEscape);
}

From source file:org.fusesource.meshkeeper.util.internal.IntrospectionSupport.java

public static String convertToString(Object value, Class<?> type) {
    PropertyEditor editor = PropertyEditorManager.findEditor(type);
    if (editor != null) {
        editor.setValue(value);
        return editor.getAsText();
    }//from ww  w  .j a v  a2s  .  co m
    return null;
}

From source file:it.cilea.osd.jdyna.web.tag.JDynATagLibraryFunctions.java

/**
 * Restituisce la stringa corrispondente al valore di object passato come
 * argomento utilizzando il property editor associato alla tipologia di
 * proprieta' passata come primo argomento
 * /*  ww  w .j ava 2s.  co  m*/
 * @param tp
 * @param object
 * @return
 */
public static String display(PropertiesDefinition tp, Object object) {
    if (object == null) {
        return "";
    }
    // Passiamo un application service null ma il property editor lo usa
    // solo per il passaggio da text -> object e non viceversa
    PropertyEditor propertyEditor = tp.getRendering().getPropertyEditor(null);
    propertyEditor.setValue(object);
    return propertyEditor.getAsText();
}

From source file:org.jdal.beans.SimpleTypeConverter.java

/**
 * {@inheritDoc}/*from  w ww. ja  v  a2s  .co  m*/
 */
@SuppressWarnings("unchecked")
@Override
public <T> T convertIfNecessary(Object value, Class<T> requiredType, MethodParameter methodParam)
        throws TypeMismatchException {

    T convertedValue = null;
    try {
        convertedValue = super.convertIfNecessary(value, requiredType, methodParam);
    } catch (TypeMismatchException tme) {
        // Try Object to String conversion
        if (ClassUtils.isAssignable(String.class, requiredType)) {
            if (value != null) {
                PropertyEditor pe = findCustomEditor(value.getClass(), null);
                if (pe != null) {
                    pe.setValue(value);
                    return (T) pe.getAsText();
                } else { // Object to String
                    return (T) value.toString();
                }
            } else { // null to String
                return (T) "";
            }
        } else {
            throw tme;
        }
    }

    return convertedValue;
}

From source file:it.cilea.osd.jdyna.model.AWidget.java

public String toString(Object valore) {
    if (valore == null) {
        return "";
    }//from   w  w w .j  av  a2 s.  c  o m
    //Passiamo un application service null ma il property editor lo usa solo per il passaggio da text -> object e non viceversa
    PropertyEditor propertyEditor = getPropertyEditor(null);
    if (propertyEditor != null) {
        propertyEditor.setValue(valore);
        return propertyEditor.getAsText();
    } else {
        return valore.toString();
    }
    //throw new UnsupportedOperationException();
}

From source file:org.uimafit.propertyeditors.GetAsTextStringEditor.java

@Override
public void setValue(Object value) {
    if (value == null || value instanceof String) {
        super.setValue(value);
    } else {//from  w w  w  . j a v a  2 s. c  om
        PropertyEditor editor = editorRegistry.findCustomEditor(value.getClass(), null);
        if (editor == null) {
            editor = editorRegistrySupport.getDefaultEditor(value.getClass());
        }
        if (editor != null) {
            editor.setValue(value);
            super.setValue(editor.getAsText());
        } else if (Enum.class.isAssignableFrom(value.getClass())) {
            super.setValue(String.valueOf(value));
        } else {
            throw new IllegalArgumentException(
                    "Unable to convert " + value.getClass() + " to String. No PropertyEditor found.");
        }
    }
}

From source file:com.alibaba.citrus.service.requestcontext.session.valueencoder.AbstractSessionValueEncoder.java

protected final String convertToString(Class<?> type, Object value, TypeConverter converter) {
    if (value instanceof String) {
        return (String) value;
    } else {//w  w w .  j  a v  a 2  s  .  c om
        if (converter instanceof PropertyEditorRegistry && type != null && type.isInstance(value)) {
            PropertyEditor editor = ((PropertyEditorRegistry) converter).findCustomEditor(type, null);

            if (editor != null) {
                editor.setValue(value);
                return editor.getAsText();
            }
        }

        return (String) getTypeConverter().convertIfNecessary(value, String.class);
    }
}

From source file:org.jdal.vaadin.ui.table.ConfigurableTable.java

/**
 * {@inheritDoc}/*w  w  w .j  a  v  a 2s . c om*/
 */
@SuppressWarnings("rawtypes")
@Override
protected Object getPropertyValue(Object rowId, Object colId, Property property) {
    Column column = columnMap.get(colId);
    if (column != null) {
        if (isEditable()) {
            Class<? extends Component> editorClass = column.getCellEditor();
            if (editorClass == null)
                return super.getPropertyValue(rowId, colId, property);
            else {
                return getComponentForProperty(property, editorClass);
            }
        }
        // Test cell component 
        Class<? extends Component> cellComponentClass = column.getCellComponent();
        if (cellComponentClass != null) {
            return getComponentForProperty(property, cellComponentClass);
        }

        // Last try, test property editor
        PropertyEditor pe = column.getPropertyEditor();
        if (pe != null) {
            pe.setValue(property.getValue());
            return pe.getAsText();
        }
    }

    // Default behavior
    return super.getPropertyValue(rowId, colId, property);
}

From source file:org.fornax.cartridges.sculptor.framework.propertyeditor.OptionEditor.java

/**
 * Format the Object as String of concatenated properties.
 *//*  ww  w.  j a v  a  2  s .  co  m*/
public String getAsText() {

    Object value = getValue();
    if (value == null) {
        return "";
    }

    String propertyName = null; // used in error handling below
    try {
        StringBuffer label = new StringBuffer();

        for (int i = 0; i < properties.length; i++) {
            propertyName = properties[i];
            Class<?> propertyType = PropertyUtils.getPropertyType(value, propertyName);
            Object propertyValue = PropertyUtils.getNestedProperty(value, propertyName);
            PropertyEditor editor = registry.findCustomEditor(propertyType,
                    registryPropertyNamePrefix + propertyName);
            if (editor == null) {
                label.append(propertyValue);
            } else {
                editor.setValue(propertyValue);
                label.append(editor.getAsText());
                editor.setValue(null);
            }

            if (i < (properties.length - 1)) {
                label.append(separator);
            }
        }

        return label.toString();

    } catch (Exception e) {
        throw new IllegalArgumentException("Couldn't access " + propertyName + " of "
                + value.getClass().getName() + " : " + e.getMessage(), e);
    }

}

From source file:net.solarnetwork.web.support.SimpleCsvHttpMessageConverter.java

private Object getRowPropertyValue(Object row, String name, Object val, BeanWrapper wrapper) {
    if (val != null) {
        if (getPropertySerializerRegistrar() != null) {
            val = getPropertySerializerRegistrar().serializeProperty(name, val.getClass(), row, val);
        } else if (wrapper != null) {
            // Spring does not apply PropertyEditors on read methods, so manually handle
            PropertyEditor editor = wrapper.findCustomEditor(null, name);
            if (editor != null) {
                editor.setValue(val);
                val = editor.getAsText();
            }/*from   w w  w.  java 2 s  . c  om*/
        }
        if (val instanceof Enum<?> || javaBeanTreatAsStringValues != null
                && javaBeanTreatAsStringValues.contains(val.getClass())) {
            val = val.toString();
        }
    }
    return val;
}