Search in sources :

Example 1 with ConfigurableDataCellFactory

use of org.knime.core.data.ConfigurableDataCellFactory in project knime-core by knime.

the class NewColumnDialog method typeChanged.

private void typeChanged() {
    DataType type = (DataType) m_typeChooser.getSelectedItem();
    m_formatParameterChooser.removeAllItems();
    m_formatParameterLabel.setEnabled(false);
    m_formatParameterChooser.setEnabled(false);
    m_formatParameterLabel.setToolTipText(null);
    m_formatParameterChooser.setToolTipText(null);
    if (type != null) {
        DataCellFactory f = type.getCellFactory(null).orElse(null);
        if (f instanceof ConfigurableDataCellFactory) {
            ConfigurableDataCellFactory cfac = (ConfigurableDataCellFactory) f;
            for (String s : cfac.getPredefinedParameters()) {
                m_formatParameterChooser.addItem(s);
            }
            if (m_allColumns.size() > m_colIdx) {
                m_formatParameterChooser.setSelectedItem(m_allColumns.get(m_colIdx).getFormatParameter().orElse(null));
            } else {
                m_formatParameterChooser.setSelectedItem(null);
            }
            m_formatParameterLabel.setEnabled(true);
            m_formatParameterChooser.setEnabled(true);
            m_formatParameterLabel.setToolTipText(cfac.getParameterDescription());
            m_formatParameterChooser.setToolTipText(cfac.getParameterDescription());
        }
    }
    pack();
}
Also used : ConfigurableDataCellFactory(org.knime.core.data.ConfigurableDataCellFactory) DataCellFactory(org.knime.core.data.DataCellFactory) ConfigurableDataCellFactory(org.knime.core.data.ConfigurableDataCellFactory) DataType(org.knime.core.data.DataType) FromSimpleString(org.knime.core.data.DataCellFactory.FromSimpleString)

Example 2 with ConfigurableDataCellFactory

use of org.knime.core.data.ConfigurableDataCellFactory in project knime-core by knime.

the class DataCellFactory method createDataCellOfType.

/**
 * Creates a {@link DataCell} of the specified type from the data passed.
 * A {@link DataCell} with a missing value is created if the passed data equals (see
 * {@link String#equals(Object)} the currently set missing value pattern
 * (disabled by default, see {@link #setMissingValuePattern(String)}).<br>
 * A {@link StringCell} can always be created, returns only
 * <code>null</code>, if <code>null</code> was provided as data (a
 * {@link StringCell} can't hold <code>null</code>). <br>
 * Creating an {@link IntCell} fails, if the provided data is not a valid
 * string representation of an integer number (see
 * {@link Integer#parseInt(String)}).<br>
 * Creation of a {@link DoubleCell} fails, if the provided data is not a
 * valid string representation of a double number (see
 * {@link Double#parseDouble(String)}, with respect to the currently set
 * decimal and thousands separators.
 *
 * @param type the type of the data cell to create. If the provided data
 *            can't be translated to the corresponding type, null is
 *            returned. The error message can be retrieved through the
 *            {@link #getErrorMessage()}.
 * @param data the string representation of the data to store in the newly
 *            created cell. Can't be null.
 * @return a data cell of the specified type carrying the provided data.
 *         <code>Null</code> is returned if the cell couldn't be created.
 *         Mostly due to incompatible string data, probably. The error
 *         message can then be retrieved through the
 *         {@link #getErrorMessage()} method.
 * @see #setMissingValuePattern(String)
 * @throws IllegalArgumentException if the passed type is not supported.
 * @throws NullPointerException if the passed data or type is null.
 * @see #getErrorMessage()
 * @see DataType#getMissingCell()
 */
public final DataCell createDataCellOfType(final DataType type, String data) {
    if (type == null || data == null) {
        throw new NullPointerException("DataType and the data can't be null.");
    }
    // clear any previous error message
    m_lastErrorMessage = null;
    if (data.equals(m_missingValuePattern)) {
        return DataType.getMissingCell();
    }
    org.knime.core.data.DataCellFactory cellFactory = m_cellFactoryMap.get(type);
    if (cellFactory == null) {
        cellFactory = type.getCellFactory(m_execContext).orElseThrow(() -> new IllegalArgumentException("No data cell factory for data type '" + type + "' found"));
        m_cellFactoryMap.put(type, cellFactory);
    }
    if (cellFactory instanceof ConfigurableDataCellFactory) {
        ((ConfigurableDataCellFactory) cellFactory).configure(m_formatParameter);
    }
    if (type.equals(DoubleCell.TYPE)) {
        // remove thousands grouping
        if (m_thousandsRegExpr != null) {
            Matcher thousandMatcher = m_thousandPattern.matcher(data);
            if (thousandMatcher.matches()) {
                // Only continue processing if input is a valid number (wrong thousands separators are targeted to identify dates
                data = data.replaceAll(m_thousandsRegExpr, "");
            } else {
                m_lastErrorMessage = "Wrong data format. Got '" + data + "' for a floating point.";
                return null;
            }
        }
        // replace decimal separator with java separator '.'
        if (m_decimalSeparator != '.') {
            // we must reject tokens with a '.'.
            if (data.indexOf('.') >= 0) {
                m_lastErrorMessage = "Wrong data format. Got '" + data + "' for a floating point.";
                return null;
            }
            data = data.replace(m_decimalSeparator, '.');
        }
    }
    try {
        return ((FromString) cellFactory).createCell(data);
    } catch (Throwable t) {
        m_lastErrorMessage = t.getMessage();
        if (m_lastErrorMessage == null) {
            m_lastErrorMessage = "No details.";
        }
        return null;
    }
}
Also used : ConfigurableDataCellFactory(org.knime.core.data.ConfigurableDataCellFactory) Matcher(java.util.regex.Matcher) FromString(org.knime.core.data.DataCellFactory.FromString)

Example 3 with ConfigurableDataCellFactory

use of org.knime.core.data.ConfigurableDataCellFactory in project knime-core by knime.

the class ColPropertyDialog method configureParameterField.

private void configureParameterField() {
    m_optionalParameterField.removeAllItems();
    m_optionalParameterLabel.setEnabled(false);
    m_optionalParameterField.setEnabled(false);
    m_optionalParameterLabel.setToolTipText(null);
    m_optionalParameterField.setToolTipText(null);
    if (!m_skipColumn.isSelected()) {
        DataType selectedType = (DataType) m_typeChooser.getSelectedItem();
        if (selectedType != null) {
            DataCellFactory fac = selectedType.getCellFactory(null).orElseGet(null);
            if (fac instanceof ConfigurableDataCellFactory) {
                ConfigurableDataCellFactory cfac = (ConfigurableDataCellFactory) fac;
                m_optionalParameterLabel.setEnabled(true);
                m_optionalParameterField.setEnabled(true);
                for (String s : cfac.getPredefinedParameters()) {
                    m_optionalParameterField.addItem(s);
                }
                m_optionalParameterField.setSelectedItem(m_allColProps.get(m_colIdx).getFormatParameter().orElse(null));
                m_optionalParameterLabel.setToolTipText(cfac.getParameterDescription());
                m_optionalParameterField.setToolTipText(cfac.getParameterDescription());
                pack();
            }
        }
    }
}
Also used : ConfigurableDataCellFactory(org.knime.core.data.ConfigurableDataCellFactory) DataCellFactory(org.knime.core.data.DataCellFactory) ConfigurableDataCellFactory(org.knime.core.data.ConfigurableDataCellFactory) DataType(org.knime.core.data.DataType) FromSimpleString(org.knime.core.data.DataCellFactory.FromSimpleString)

Aggregations

ConfigurableDataCellFactory (org.knime.core.data.ConfigurableDataCellFactory)3 DataCellFactory (org.knime.core.data.DataCellFactory)2 FromSimpleString (org.knime.core.data.DataCellFactory.FromSimpleString)2 DataType (org.knime.core.data.DataType)2 Matcher (java.util.regex.Matcher)1 FromString (org.knime.core.data.DataCellFactory.FromString)1