use of org.knime.core.node.workflow.FlowVariable 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.node.workflow.FlowVariable in project knime-core by knime.
the class AddOutFieldDialog method initKnimeNameComboBox.
/**
* Initialize the selection list for the knime name.
*/
private void initKnimeNameComboBox() {
m_replacedKnimeName.removeAllItems();
if (m_fieldType.getSelectedItem().equals(FieldType.Column)) {
for (DataColumnSpec colSpec : m_spec) {
m_replacedKnimeName.addItem(colSpec);
}
m_replacedKnimeName.setRenderer(new DataColumnSpecListCellRenderer());
} else {
for (FlowVariable flowVar : m_flowVars.values()) {
// created.
if (FieldsTableUtil.verifyNameOfFlowVariable(flowVar.getName())) {
m_replacedKnimeName.addItem(flowVar);
}
}
m_replacedKnimeName.setRenderer(new FlowVariableListCellRenderer());
}
if (m_replacedKnimeName.getItemCount() <= 0) {
m_replacedKnimeName.setEnabled(false);
m_knimeName.setEnabled(true);
m_replace.setEnabled(false);
m_replace.setSelected(false);
m_append.setSelected(true);
} else {
m_replacedKnimeName.setEnabled(true);
m_replace.setEnabled(true);
}
}
use of org.knime.core.node.workflow.FlowVariable in project knime-core by knime.
the class InFieldsTable method createInputCellEditor.
/**
* Create cell editor for for the input columns / flow variables.
*/
private TableCellEditor createInputCellEditor() {
JComboBox comboBox = new JComboBox();
comboBox.setRenderer(new InputListCellRenderer());
if (null != m_spec) {
for (DataColumnSpec colSpec : m_spec) {
comboBox.addItem(colSpec);
}
}
if (null != m_flowVars) {
for (FlowVariable flowVar : m_flowVars.values()) {
comboBox.addItem(flowVar);
}
}
DefaultCellEditor editor = new DefaultCellEditor(comboBox);
editor.setClickCountToStart(2);
return editor;
}
use of org.knime.core.node.workflow.FlowVariable in project knime-core by knime.
the class InFieldsTable method updateData.
/**
* Set the table data.
*
* @param fields the fields
* @param spec the input spec might be null
* @param flowVars the flow variables
*/
void updateData(final JavaSnippetFields fields, final DataTableSpec spec, final Map<String, FlowVariable> flowVars) {
m_spec = spec;
m_flowVars = flowVars;
m_model.clear();
for (int r = 0; r < fields.getInColFields().size(); r++) {
InCol field = fields.getInColFields().get(r);
m_model.addRow();
String colName = field.getKnimeName();
DataColumnSpec colSpec = spec.getColumnSpec(colName);
Object value = null != colSpec ? colSpec : colName;
m_model.setValueAt(value, r, Column.COLUMN);
m_model.setValueAt(field.getJavaName(), r, Column.JAVA_FIELD);
Optional<?> factory = ConverterUtil.getDataCellToJavaConverterFactory(field.getConverterFactoryId());
if (!factory.isPresent()) {
// seems to be missing.
if (field.getJavaType() != null) {
factory = ConverterUtil.getConverterFactory(field.getDataType(), field.getJavaType());
}
}
if (factory.isPresent()) {
m_model.setValueAt(factory.get(), r, Column.JAVA_TYPE);
} else {
final Class<?> type = field.getJavaType();
if (type != null) {
m_model.setValueAt(type, r, Column.JAVA_TYPE);
} else {
m_model.setValueAt(field.getJavaTypeName(), r, Column.JAVA_TYPE);
}
}
}
int offset = m_model.getRowCount();
for (int r = 0; r < fields.getInVarFields().size(); r++) {
InVar field = fields.getInVarFields().get(r);
m_model.addRow();
String name = field.getKnimeName();
FlowVariable flowVar = m_flowVars.get(name);
Object value = null != flowVar ? flowVar : name;
m_model.setValueAt(value, offset + r, Column.COLUMN);
m_model.setValueAt(field.getJavaName(), offset + r, Column.JAVA_FIELD);
m_model.setValueAt(field.getJavaType(), offset + r, Column.JAVA_TYPE);
}
JTable table = getTable();
final TableColumnModel columnModel = table.getColumnModel();
columnModel.getColumn(m_model.getIndex(Column.COLUMN)).setCellRenderer(new InputTableCellRenderer());
columnModel.getColumn(m_model.getIndex(Column.COLUMN)).setCellEditor(createInputCellEditor());
columnModel.getColumn(m_model.getIndex(Column.JAVA_FIELD)).setCellRenderer(FieldsTableUtil.createJavaFieldTableCellRenderer());
columnModel.getColumn(m_model.getIndex(Column.JAVA_TYPE)).setCellRenderer(FieldsTableUtil.createJavaTypeTableCellRenderer());
columnModel.getColumn(m_model.getIndex(Column.JAVA_TYPE)).setCellEditor(FieldsTableUtil.createJavaTypeTableCellEditor());
}
use of org.knime.core.node.workflow.FlowVariable in project knime-core by knime.
the class InFieldsTable method getInVarFields.
/**
* Get the field definitions representing input flow variables.
*
* @return fields representing input flow variables
*/
public InVarList getInVarFields() {
InVarList inCols = new InVarList();
for (int r = 0; r < m_model.getRowCount(); r++) {
if (!m_model.validateValues(r)) {
// there are errors in this row
continue;
}
Object value = m_model.getValueAt(r, Column.COLUMN);
if (value instanceof FlowVariable) {
FlowVariable colSpec = (FlowVariable) value;
InVar inVar = new InVar();
inVar.setFlowVarType(colSpec.getType());
inVar.setKnimeName(colSpec.getName());
inVar.setJavaName((String) m_model.getValueAt(r, Column.JAVA_FIELD));
inVar.setJavaType((Class) m_model.getValueAt(r, Column.JAVA_TYPE));
inCols.add(inVar);
}
}
return inCols;
}
Aggregations