use of org.knime.core.data.DataType 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.DataType 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.DataType 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.DataType in project knime-core by knime.
the class ExtractDateTimeFieldsNodeDialog method refreshFieldsSelectionsEnabled.
private void refreshFieldsSelectionsEnabled() {
if (m_dialogCompColSelect.getSelectedAsSpec() != null) {
final DataType type = m_dialogCompColSelect.getSelectedAsSpec().getType();
final boolean isDate = ExtractDateTimeFieldsNodeModel.isDateType(type);
for (final DialogComponentBoolean dc : m_dialogCompDateFields) {
dc.getModel().setEnabled(isDate);
}
final boolean isTime = ExtractDateTimeFieldsNodeModel.isTimeType(type);
for (final DialogComponentBoolean dc : m_dialogCompTimeFields) {
dc.getModel().setEnabled(isTime);
}
final boolean isZoned = ExtractDateTimeFieldsNodeModel.isZonedType(type);
for (final DialogComponentBoolean dc : m_dialogCompTimeZoneFields) {
dc.getModel().setEnabled(isZoned);
}
}
}
use of org.knime.core.data.DataType in project knime-core by knime.
the class DateTimeBasedRowFilterNodeDialog method updateEnableStatusOfDateTimeComponents.
private void updateEnableStatusOfDateTimeComponents(final SettingsModelDateTime startDateTimeModel, final SettingsModelDateTime endDateTimeModel) {
try {
final DataType type = m_dialogCompColSelection.getSelectedAsSpec().getType();
startDateTimeModel.setEnabled(!((SettingsModelBoolean) m_dialogCompStartAlwaysNow.getModel()).getBooleanValue() && ((SettingsModelBoolean) m_dialogCompStartBoolean.getModel()).getBooleanValue());
startDateTimeModel.setUseDate(!type.isCompatible(LocalTimeValue.class));
startDateTimeModel.setUseTime(!type.isCompatible(LocalDateValue.class));
startDateTimeModel.setUseZone(type.isCompatible(ZonedDateTimeValue.class));
endDateTimeModel.setEnabled(!((SettingsModelBoolean) m_dialogCompEndAlwaysNow.getModel()).getBooleanValue() && ((SettingsModelBoolean) m_dialogCompEndBoolean.getModel()).getBooleanValue());
endDateTimeModel.setUseDate(!type.isCompatible(LocalTimeValue.class));
endDateTimeModel.setUseTime(!type.isCompatible(LocalDateValue.class));
endDateTimeModel.setUseZone(type.isCompatible(ZonedDateTimeValue.class));
} catch (NullPointerException e) {
}
}
Aggregations