Search in sources :

Example 1 with TypeProvider

use of org.knime.base.node.jsnippet.type.TypeProvider in project knime-core by knime.

the class AddOutFieldDialog method initKnimeTypeComboBox.

/**
 * Initialize the selection list for the knime type.
 */
private void initKnimeTypeComboBox() {
    m_knimeType.removeAllItems();
    if (m_fieldType.getSelectedItem().equals(FieldType.Column)) {
        for (final DataType type : ConverterUtil.getAllDestinationDataTypes()) {
            m_knimeType.addItem(type);
        }
        m_knimeType.setRenderer(new DataTypeListCellRenderer());
    } else {
        TypeProvider typeProvider = TypeProvider.getDefault();
        for (FlowVariable.Type type : typeProvider.getTypes()) {
            m_knimeType.addItem(type);
        }
        m_knimeType.setRenderer(new TypeListCellRender());
    }
    // string is the default value
    m_knimeType.setSelectedItem(StringCell.TYPE);
}
Also used : DataType(org.knime.core.data.DataType) TypeProvider(org.knime.base.node.jsnippet.type.TypeProvider) DataTypeListCellRenderer(org.knime.core.node.util.DataTypeListCellRenderer) FlowVariable(org.knime.core.node.workflow.FlowVariable)

Example 2 with TypeProvider

use of org.knime.base.node.jsnippet.type.TypeProvider in project knime-core by knime.

the class AddOutFieldDialog method takeOverSettings.

/**
 * Save settings in field m_result.
 */
@SuppressWarnings("rawtypes")
private JavaField takeOverSettings() {
    if (m_fieldType.getSelectedItem().equals(FieldType.Column)) {
        OutCol outCol = new OutCol();
        boolean isReplacing = m_replace.isSelected();
        outCol.setReplaceExisting(isReplacing);
        String colName = isReplacing ? ((DataColumnSpec) m_replacedKnimeName.getSelectedItem()).getName() : m_knimeName.getText();
        outCol.setKnimeName(colName);
        Set<String> taken = new HashSet<>();
        for (int i = 0; i < m_model.getRowCount(); i++) {
            taken.add((String) m_model.getValueAt(i, Column.JAVA_FIELD));
        }
        // collection is now done by a separate checkbox
        // DataType dataType = (DataType)m_knimeType.getSelectedItem();
        // DataType elemType =
        // dataType.isCollectionType() ? dataType
        // .getCollectionElementType() : dataType;
        // boolean isCollection = dataType.isCollectionType();
        DataType dataType = (DataType) m_knimeType.getSelectedItem();
        if (m_isArray.isSelected()) {
            dataType = ListCell.getCollectionType(dataType);
        }
        final Optional<JavaToDataCellConverterFactory<?>> selectedFactory = ConverterUtil.getPreferredFactoryForDestinationType(dataType);
        if (!selectedFactory.isPresent()) {
            // Default to string converter, which always exists. Should not happen, though.
            outCol.setConverterFactory(ConverterUtil.getPreferredFactoryForDestinationType(StringCell.TYPE).get());
        } else {
            outCol.setConverterFactory(selectedFactory.get());
        }
        final String javaName = FieldsTableUtil.createUniqueJavaIdentifier(colName, taken, "out_");
        outCol.setJavaName(javaName);
        return outCol;
    } else {
        // flow variable
        OutVar outVar = new OutVar();
        boolean isReplacing = m_replace.isSelected();
        outVar.setReplaceExisting(isReplacing);
        String colName = isReplacing ? ((FlowVariable) m_replacedKnimeName.getSelectedItem()).getName() : m_knimeName.getText();
        outVar.setKnimeName(colName);
        Set<String> taken = new HashSet<>();
        for (int i = 0; i < m_model.getRowCount(); i++) {
            taken.add((String) m_model.getValueAt(i, Column.JAVA_FIELD));
        }
        String javaName = FieldsTableUtil.createUniqueJavaIdentifier(colName, taken, "out_");
        FlowVariable.Type type = (FlowVariable.Type) m_knimeType.getSelectedItem();
        TypeProvider typeProvider = TypeProvider.getDefault();
        Class javaType = typeProvider.getTypeConverter(type).getPreferredJavaType();
        outVar.setFlowVarType(type);
        outVar.setJavaName(javaName);
        outVar.setJavaType(javaType);
        return outVar;
    }
}
Also used : OutCol(org.knime.base.node.jsnippet.util.field.OutCol) TypeProvider(org.knime.base.node.jsnippet.type.TypeProvider) OutVar(org.knime.base.node.jsnippet.util.field.OutVar) FieldType(org.knime.base.node.jsnippet.util.field.JavaField.FieldType) DataType(org.knime.core.data.DataType) DataType(org.knime.core.data.DataType) HashSet(java.util.HashSet) JavaToDataCellConverterFactory(org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory) FlowVariable(org.knime.core.node.workflow.FlowVariable)

Aggregations

TypeProvider (org.knime.base.node.jsnippet.type.TypeProvider)2 DataType (org.knime.core.data.DataType)2 FlowVariable (org.knime.core.node.workflow.FlowVariable)2 HashSet (java.util.HashSet)1 FieldType (org.knime.base.node.jsnippet.util.field.JavaField.FieldType)1 OutCol (org.knime.base.node.jsnippet.util.field.OutCol)1 OutVar (org.knime.base.node.jsnippet.util.field.OutVar)1 JavaToDataCellConverterFactory (org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory)1 DataTypeListCellRenderer (org.knime.core.node.util.DataTypeListCellRenderer)1