Search in sources :

Example 86 with FlowVariable

use of org.knime.core.node.workflow.FlowVariable in project knime-core by knime.

the class FlowVariableList method onSelectionInVariableList.

private void onSelectionInVariableList(final Object selected) {
    if (selected != null && selected instanceof FlowVariable) {
        if (null == m_fields) {
            return;
        }
        FlowVariable v = (FlowVariable) selected;
        String enter = m_fields.getFieldReadStatement(v);
        clearSelection();
        if (null != m_snippet) {
            m_snippet.replaceSelection(enter);
            m_snippet.requestFocus();
        }
    }
}
Also used : FlowVariable(org.knime.core.node.workflow.FlowVariable)

Example 87 with FlowVariable

use of org.knime.core.node.workflow.FlowVariable in project knime-core by knime.

the class InFieldsTableModel method getAllowedJavaTypes.

/**
 * {@inheritDoc}
 */
@Override
public Object[] getAllowedJavaTypes(final int row) {
    Object input = getValueAt(row, Column.COLUMN);
    if (input instanceof DataColumnSpec) {
        DataColumnSpec colSpec = (DataColumnSpec) input;
        final Collection<DataCellToJavaConverterFactory<?, ?>> factories = ConverterUtil.getFactoriesForSourceType(colSpec.getType()).stream().filter(factory -> JavaSnippet.getBuildPathFromCache(factory.getIdentifier()) != null).sorted((a, b) -> a.getName().compareToIgnoreCase(b.getName())).collect(Collectors.toList());
        final Object[] array = factories.toArray(new Object[factories.size()]);
        return array;
    } else if (input instanceof FlowVariable) {
        FlowVariable flowVar = (FlowVariable) input;
        TypeConverter typeConversion = TypeProvider.getDefault().getTypeConverter(flowVar.getType());
        return typeConversion.canProvideJavaTypes();
    } else {
        return new Class[0];
    }
}
Also used : Collection(java.util.Collection) FlowVariable(org.knime.core.node.workflow.FlowVariable) TypeConverter(org.knime.base.node.jsnippet.type.flowvar.TypeConverter) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) TypeProvider(org.knime.base.node.jsnippet.type.TypeProvider) DataCellToJavaConverterFactory(org.knime.core.data.convert.java.DataCellToJavaConverterFactory) JavaSnippet(org.knime.base.node.jsnippet.JavaSnippet) ConverterUtil(org.knime.base.node.jsnippet.type.ConverterUtil) DataColumnSpec(org.knime.core.data.DataColumnSpec) Map(java.util.Map) Optional(java.util.Optional) DataType(org.knime.core.data.DataType) TypeConverter(org.knime.base.node.jsnippet.type.flowvar.TypeConverter) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataCellToJavaConverterFactory(org.knime.core.data.convert.java.DataCellToJavaConverterFactory) FlowVariable(org.knime.core.node.workflow.FlowVariable)

Example 88 with FlowVariable

use of org.knime.core.node.workflow.FlowVariable in project knime-core by knime.

the class InFieldsTableModel method validateJavaTypeColumn.

/**
 * Validate java type in the given row.
 * @param row the row to check
 * @return error message if error occurs
 */
private String validateJavaTypeColumn(final int row) {
    Object value = this.getValueAt(row, Column.JAVA_TYPE);
    if (null == value) {
        return "Please select a value";
    }
    Object input = this.getValueAt(row, Column.COLUMN);
    if (value instanceof DataCellToJavaConverterFactory) {
        if (input instanceof FlowVariable) {
            return "DataCell converters do not work for flow variables.";
        } else if (input instanceof DataColumnSpec) {
            final DataType dataType = ((DataColumnSpec) input).getType();
            if (!dataType.getValueClasses().contains(((DataCellToJavaConverterFactory<?, ?>) value).getSourceType())) {
                return "Input DataCell is incompatible with this java type.";
            }
        }
    // otherwise user has to fix column/flowvar first.
    } else if (value instanceof String) {
        // converter factory id
        final String id = (String) value;
        if (input instanceof DataColumnSpec) {
            final DataColumnSpec colSpec = (DataColumnSpec) input;
            final Optional<DataCellToJavaConverterFactory<?, ?>> factory = ConverterUtil.getDataCellToJavaConverterFactory(id);
            if (!factory.isPresent()) {
                return "Converter with id " + id + " could not be found.";
            }
            if (!colSpec.getType().getValueClasses().contains(factory.get().getSourceType())) {
                // But just in case it does, this will make sure we know.
                return "Selected factory does not support the input type.";
            }
            return null;
        } else if (input instanceof FlowVariable) {
            return "DataCell converters do not work for flow variables.";
        }
    } else if (value instanceof Class) {
        Class<?> javaType = (Class<?>) value;
        if (input instanceof DataColumnSpec) {
            DataColumnSpec colSpec = (DataColumnSpec) input;
            final Collection<?> factories = ConverterUtil.getFactoriesForSourceType(colSpec.getType());
            return factories.isEmpty() ? "The java type \"" + javaType.getSimpleName() + "\" is not supported." : null;
        } else if (input instanceof FlowVariable) {
            FlowVariable flowVar = (FlowVariable) input;
            TypeConverter typeConversion = TypeProvider.getDefault().getTypeConverter(flowVar.getType());
            return typeConversion.canProvideJavaType(javaType) ? null : "The java type \"" + javaType.getSimpleName() + "\" is not supported.";
        }
    } else {
        return "Cannot find type or converter " + value.toString();
    }
    return null;
}
Also used : TypeConverter(org.knime.base.node.jsnippet.type.flowvar.TypeConverter) DataColumnSpec(org.knime.core.data.DataColumnSpec) Optional(java.util.Optional) DataType(org.knime.core.data.DataType) DataCellToJavaConverterFactory(org.knime.core.data.convert.java.DataCellToJavaConverterFactory) FlowVariable(org.knime.core.node.workflow.FlowVariable)

Example 89 with FlowVariable

use of org.knime.core.node.workflow.FlowVariable 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)

Example 90 with FlowVariable

use of org.knime.core.node.workflow.FlowVariable in project knime-core by knime.

the class InFieldsTable method createAddButtonListener.

/**
 * {@inheritDoc}
 */
@Override
protected ActionListener createAddButtonListener() {
    return new ActionListener() {

        @Override
        public void actionPerformed(final ActionEvent e) {
            DataColumnSpec defaultColTarget = null;
            if (null != m_spec) {
                Set<String> cols = new HashSet<>();
                for (int r = 0; r < m_model.getRowCount(); r++) {
                    Object value = m_model.getValueAt(r, Column.COLUMN);
                    if (value instanceof DataColumnSpec) {
                        cols.add(((DataColumnSpec) value).getName());
                    }
                }
                for (DataColumnSpec colSpec : m_spec) {
                    if (null == defaultColTarget) {
                        defaultColTarget = colSpec;
                    }
                    if (!cols.contains(colSpec.getName())) {
                        // Add a row and fill it
                        boolean rowAdded = addRow(colSpec);
                        if (rowAdded) {
                            firePropertyChange(PROP_FIELD_ADDED, m_model.getRowCount() - 1, m_model.getRowCount());
                        }
                        return;
                    }
                }
            }
            FlowVariable defaultVarTarget = null;
            if (null != m_flowVars) {
                Set<String> flowVars = new HashSet<>();
                for (int r = 0; r < m_model.getRowCount(); r++) {
                    Object value = m_model.getValueAt(r, Column.COLUMN);
                    if (value instanceof FlowVariable) {
                        flowVars.add(((FlowVariable) value).getName());
                    }
                }
                defaultVarTarget = !m_flowVars.isEmpty() ? m_flowVars.values().iterator().next() : null;
                for (FlowVariable flowVar : m_flowVars.values()) {
                    if (!flowVars.contains(flowVar.getName())) {
                        // Add a row and fill it
                        boolean rowAdded = addRow(flowVar);
                        if (rowAdded) {
                            firePropertyChange(PROP_FIELD_ADDED, m_model.getRowCount() - 1, m_model.getRowCount());
                        }
                        return;
                    }
                }
            }
            boolean rowAdded = false;
            if (null != defaultColTarget) {
                rowAdded = addRow(defaultColTarget);
            } else if (null != defaultVarTarget) {
                rowAdded = addRow(defaultVarTarget);
            } else {
                m_model.addRow();
                rowAdded = true;
            }
            if (rowAdded) {
                firePropertyChange(PROP_FIELD_ADDED, m_model.getRowCount() - 1, m_model.getRowCount());
            }
        }
    };
}
Also used : DataColumnSpec(org.knime.core.data.DataColumnSpec) ActionListener(java.awt.event.ActionListener) ActionEvent(java.awt.event.ActionEvent) HashSet(java.util.HashSet) 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