use of org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory in project knime-core by knime.
the class OutFieldsTableModel method validateJavaTypeColumn.
/**
* Validate java type in the given row.
* @param row the row to check
* @return error message if error occurs
*/
@SuppressWarnings("rawtypes")
private String validateJavaTypeColumn(final int row) {
Object value = this.getValueAt(row, Column.JAVA_TYPE);
if (null == value) {
return "Please select a value";
}
if (value instanceof JavaToDataCellConverterFactory) {
if (getValueAt(row, Column.FIELD_TYPE) == FieldType.FlowVariable) {
return "Cannot use DataCell converters for flow variables.";
}
} else if (value instanceof String) {
final String id = (String) value;
final Optional<JavaToDataCellConverterFactory<?>> factory = ConverterUtil.getJavaToDataCellConverterFactory(id);
return factory.isPresent() ? null : "Converter factory " + id + " was not found.";
} else if (value instanceof Class) {
Class javaType = (Class) value;
Object outType = getValueAt(row, Column.DATA_TYPE);
if (outType instanceof DataType) {
DataType elemType = (DataType) outType;
boolean isCollection = (Boolean) getValueAt(row, Column.IS_COLLECTION);
DataType dataType = isCollection ? ListCell.getCollectionType(elemType) : elemType;
final Collection<JavaToDataCellConverterFactory<?>> factories = ConverterUtil.getFactoriesForDestinationType(dataType);
if (factories.isEmpty()) {
return "The java type \"" + javaType.getSimpleName() + "\" is not supported for output columns.";
}
if (dataType.isCollectionType() && !javaType.isArray()) {
return "Please choose an java array for collection types.";
}
if (!dataType.isCollectionType() && javaType.isArray()) {
return "An array cannot be written to a non collection column";
}
} else if (outType instanceof Type) {
Type type = (Type) outType;
TypeConverter typeConversion = TypeProvider.getDefault().getTypeConverter(type);
if (!typeConversion.canProvideJavaType(javaType)) {
return "The java type \"" + javaType.getSimpleName() + "\" is not supported.";
}
}
} else {
return "Cannot find class " + value.toString();
}
// no error found
return null;
}
use of org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory 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));
}
}
}
use of org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory in project knime-core by knime.
the class OutFieldsTableModel method getAllowedJavaTypes.
/**
* {@inheritDoc}
*/
@Override
public Object[] getAllowedJavaTypes(final int row) {
Object input = getValueAt(row, Column.DATA_TYPE);
if (input instanceof DataType) {
DataType elemType = (DataType) input;
boolean isCollection = (Boolean) getValueAt(row, Column.IS_COLLECTION);
final DataType dataType = isCollection ? ListCell.getCollectionType(elemType) : elemType;
final Collection<JavaToDataCellConverterFactory<?>> factories = ConverterUtil.getFactoriesForDestinationType(dataType).stream().filter(factory -> JavaSnippet.getBuildPathFromCache(factory.getIdentifier()) != null).collect(Collectors.toList());
return factories.toArray(new Object[factories.size()]);
} else if (input instanceof Type) {
Type type = (Type) input;
TypeConverter typeConversion = TypeProvider.getDefault().getTypeConverter(type);
return typeConversion.canCreatedFromJavaTypes();
} else {
return new Class[] { String.class };
}
}
use of org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory in project knime-core by knime.
the class ConverterFactoryJavaTypeListCellRenderer method getListCellRendererComponent.
@Override
public Component getListCellRendererComponent(final JList list, final Object value, final int index, final boolean isSelected, final boolean cellHasFocus) {
// The super method will reset the icon if we call this method
// last. So we let super do its job first and then we take care
// that everything is properly set.
final Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
assert c == this;
if (value instanceof String) {
setText((String) value);
} else if (value instanceof Class) {
setText(((Class) value).getSimpleName());
} else if (value instanceof JavaToDataCellConverterFactory<?>) {
final JavaToDataCellConverterFactory<?> factory = (JavaToDataCellConverterFactory<?>) value;
setText(factory.getName());
} else if (value instanceof DataCellToJavaConverterFactory) {
final DataCellToJavaConverterFactory<?, ?> factory = (DataCellToJavaConverterFactory<?, ?>) value;
setText(factory.getName());
}
super.setToolTipText(getText());
return this;
}
use of org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory in project knime-core by knime.
the class JavaSnippet method getClassLoadersFor.
/**
* Get the class loaders required for a specific converter factory
*
* @param converterFactoryId ID of the converter factory
* @return A list of class loaders required for given converter factory
* @noreference This method is not intended to be referenced by clients.
*/
private static Collection<ClassLoader> getClassLoadersFor(final String converterFactoryId) {
final Optional<DataCellToJavaConverterFactory<?, ?>> factory = ConverterUtil.getDataCellToJavaConverterFactory(converterFactoryId);
if (factory.isPresent()) {
final ArrayList<ClassLoader> clsLoaders = new ArrayList<>(2);
final ClassLoader sourceCL = factory.get().getSourceType().getClassLoader();
if (sourceCL != null) {
clsLoaders.add(sourceCL);
}
final ClassLoader destCL = factory.get().getDestinationType().getClassLoader();
if (destCL != null) {
clsLoaders.add(destCL);
}
return clsLoaders;
} else {
final Optional<JavaToDataCellConverterFactory<?>> factory2 = ConverterUtil.getJavaToDataCellConverterFactory(converterFactoryId);
if (factory2.isPresent()) {
final ClassLoader cl = factory2.get().getSourceType().getClassLoader();
if (cl != null) {
return Collections.singleton(cl);
}
}
return Collections.emptyList();
}
}
Aggregations