Search in sources :

Example 1 with FieldType

use of org.knime.base.node.jsnippet.util.field.JavaField.FieldType in project knime-core by knime.

the class OutFieldsTableModel method setValueAt.

/**
 * {@inheritDoc}
 */
@Override
public void setValueAt(final Object aValue, final int row, final int column) {
    // make sure setValue(Object, int, Column) is always called.
    Column col = getColumnForIndex(column);
    final FieldType fieldType = (m_flowVarsOnly) ? FieldType.FlowVariable : (FieldType) getValueAt(row, Column.FIELD_TYPE);
    if (col == Column.COLUMN) {
        if (aValue instanceof FlowVariable) {
            // make sure we do not keep a ConverterFactory in the JavaType column
            // when changing from DataColumnSpec.
            Object type = getValueAt(row, Column.JAVA_TYPE);
            if (type instanceof JavaToDataCellConverterFactory) {
                // set java type to dest type of converter factory
                setValueAt(((JavaToDataCellConverterFactory<?>) type).getSourceType(), row, Column.JAVA_TYPE);
            }
        } else if (aValue instanceof DataColumnSpec) {
            // make sure we do not keep a Java class in the JavaType column for Columns
            final Object type = getValueAt(row, Column.JAVA_TYPE);
            final DataType dataType = (DataType) getValueAt(row, Column.DATA_TYPE);
            if (type instanceof Class) {
                // find a DataCell converter which is able to convert from the new column type to the current java type
                final Optional<?> factory = ConverterUtil.getConverterFactory((Class<?>) type, dataType);
                if (factory.isPresent()) {
                    setValueAt(factory.get(), row, Column.JAVA_TYPE);
                }
            }
        }
    } else if (fieldType == FieldType.Column && col == Column.DATA_TYPE) {
        Object type = getValueAt(row, Column.JAVA_TYPE);
        Boolean isCollection = (Boolean) getValueAt(row, Column.IS_COLLECTION);
        if (isCollection != null && type instanceof JavaToDataCellConverterFactory && aValue instanceof DataType) {
            final JavaToDataCellConverterFactory<?> converterFactory = (JavaToDataCellConverterFactory<?>) type;
            DataType dataType = (DataType) aValue;
            Class<?> javaType = converterFactory.getSourceType();
            if (isCollection && !dataType.isCollectionType()) {
                dataType = ListCell.getCollectionType(dataType);
                if (!(converterFactory instanceof ArrayToCollectionConverterFactory)) {
                    javaType = Array.newInstance(javaType, 0).getClass();
                }
            } else if (!isCollection && javaType.isArray()) {
                if (converterFactory instanceof ArrayToCollectionConverterFactory) {
                    javaType = javaType.getComponentType();
                }
            }
            // Try to find a converter factory which converts the newly selected DataType to the selected JavaType.
            final Optional<?> factory = ConverterUtil.getConverterFactory(javaType, dataType);
            if (factory.isPresent()) {
                setValueAt(factory.get(), row, Column.JAVA_TYPE);
            } else {
                // If there is no way to convert to the existing Java type, use the preferred type instead.
                final Optional<JavaToDataCellConverterFactory<?>> preferred = ConverterUtil.getPreferredFactoryForDestinationType(dataType);
                if (preferred.isPresent()) {
                    setValueAt(preferred.get(), row, Column.JAVA_TYPE);
                }
            }
        }
    }
    super.setValueAt(aValue, row, column);
    /* If this was the collection check box, post process to set the appropriate converter factory. */
    if (col == Column.IS_COLLECTION) {
        Object type = getValueAt(row, Column.DATA_TYPE);
        if (type != null) {
            // make sure DataType is updated to a Collection type or back to a single element type
            setValueAt(type, row, getIndex(Column.DATA_TYPE));
        }
    }
}
Also used : DataColumnSpec(org.knime.core.data.DataColumnSpec) ArrayToCollectionConverterFactory(org.knime.core.data.convert.datacell.ArrayToCollectionConverterFactory) Optional(java.util.Optional) DataType(org.knime.core.data.DataType) FieldType(org.knime.base.node.jsnippet.util.field.JavaField.FieldType) FlowVariable(org.knime.core.node.workflow.FlowVariable) JavaToDataCellConverterFactory(org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory)

Aggregations

Optional (java.util.Optional)1 FieldType (org.knime.base.node.jsnippet.util.field.JavaField.FieldType)1 DataColumnSpec (org.knime.core.data.DataColumnSpec)1 DataType (org.knime.core.data.DataType)1 ArrayToCollectionConverterFactory (org.knime.core.data.convert.datacell.ArrayToCollectionConverterFactory)1 JavaToDataCellConverterFactory (org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory)1 FlowVariable (org.knime.core.node.workflow.FlowVariable)1