Search in sources :

Example 6 with FlowVariable

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));
        }
    }
}
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)

Example 7 with FlowVariable

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);
    }
}
Also used : DataColumnSpecListCellRenderer(org.knime.core.node.util.DataColumnSpecListCellRenderer) FlowVariableListCellRenderer(org.knime.core.node.util.FlowVariableListCellRenderer) DataColumnSpec(org.knime.core.data.DataColumnSpec) FlowVariable(org.knime.core.node.workflow.FlowVariable)

Example 8 with FlowVariable

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;
}
Also used : DataColumnSpec(org.knime.core.data.DataColumnSpec) JComboBox(javax.swing.JComboBox) FlowVariable(org.knime.core.node.workflow.FlowVariable) DefaultCellEditor(javax.swing.DefaultCellEditor)

Example 9 with FlowVariable

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());
}
Also used : DataColumnSpec(org.knime.core.data.DataColumnSpec) JTable(javax.swing.JTable) TableColumnModel(javax.swing.table.TableColumnModel) InVar(org.knime.base.node.jsnippet.util.field.InVar) InCol(org.knime.base.node.jsnippet.util.field.InCol) FlowVariable(org.knime.core.node.workflow.FlowVariable)

Example 10 with FlowVariable

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;
}
Also used : InVar(org.knime.base.node.jsnippet.util.field.InVar) InVarList(org.knime.base.node.jsnippet.util.JavaFieldList.InVarList) FlowVariable(org.knime.core.node.workflow.FlowVariable)

Aggregations

FlowVariable (org.knime.core.node.workflow.FlowVariable)93 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)24 DataColumnSpec (org.knime.core.data.DataColumnSpec)14 DataType (org.knime.core.data.DataType)13 DataTableSpec (org.knime.core.data.DataTableSpec)11 ArrayList (java.util.ArrayList)10 PortType (org.knime.core.node.port.PortType)8 DefaultListModel (javax.swing.DefaultListModel)7 Type (org.knime.core.node.workflow.FlowVariable.Type)7 IOException (java.io.IOException)6 Map (java.util.Map)6 PortObject (org.knime.core.node.port.PortObject)6 Optional (java.util.Optional)5 Collectors (java.util.stream.Collectors)5 OutVar (org.knime.base.node.jsnippet.util.field.OutVar)5 BufferedDataTable (org.knime.core.node.BufferedDataTable)5 URL (java.net.URL)4 ParseException (java.text.ParseException)4 Collection (java.util.Collection)4 HashSet (java.util.HashSet)4