use of org.knime.core.data.DataColumnSpec in project knime-core by knime.
the class NodeViewUtil method renderDataTable.
/**
* Create HTML from the given table using column rowHeader as row headers.
* @param table the table
* @param rowHeader the column with row headers
* @param exclude columns to exclude
* @param colHeaders override column headers
* @param buffer append to this buffer
*/
public static void renderDataTable(final BufferedDataTable table, final String rowHeader, final Collection<String> exclude, final Map<String, String> colHeaders, final StringBuilder buffer) {
int rowHeaderI = table.getDataTableSpec().findColumnIndex(rowHeader);
Set<Integer> excludeI = new HashSet<Integer>();
for (String toExclude : exclude) {
excludeI.add(table.getDataTableSpec().findColumnIndex(toExclude));
}
buffer.append("<table>\n");
buffer.append("<tr>");
buffer.append("<th class=\"left\"></th>");
for (DataColumnSpec colSpec : table.getDataTableSpec()) {
String colName = colSpec.getName();
if (!exclude.contains(colName)) {
String value = colHeaders.containsKey(colName) ? colHeaders.get(colName) : colName;
buffer.append("<th>");
buffer.append(escapeHtml(value));
buffer.append("</th>");
}
}
buffer.append("</tr>");
int r = 0;
for (Iterator<DataRow> it = table.iteratorFailProve(); it.hasNext(); ) {
DataRow row = it.next();
buffer.append("<tr class=\"");
buffer.append(r % 2 == 0 ? "odd" : "even");
buffer.append("\">");
renderDataCell(row.getCell(rowHeaderI), buffer);
for (int i = 0; i < row.getNumCells(); i++) {
if (excludeI.contains(i)) {
continue;
}
DataCell cell = row.getCell(i);
renderDataCell(cell, buffer);
}
buffer.append("</tr>");
r++;
}
buffer.append("</table>\n");
}
use of org.knime.core.data.DataColumnSpec in project knime-core by knime.
the class TwoSampleTTestNodeDialog method initGroupComboBoxes.
private void initGroupComboBoxes(final JComboBox groupOne) {
Object selected = groupOne.getSelectedItem();
groupOne.removeAllItems();
String col = m_groupingColumn.getSelectedColumn();
if (col != null && m_spec.containsName(col)) {
DataColumnSpec colSpec = m_spec.getColumnSpec(col);
DataColumnDomain domain = colSpec.getDomain();
if (domain.hasValues()) {
for (DataCell cell : domain.getValues()) {
groupOne.addItem(cell.toString());
}
} else if (domain.hasBounds()) {
groupOne.addItem(domain.getLowerBound().toString());
groupOne.addItem(domain.getUpperBound().toString());
}
}
groupOne.setSelectedItem(selected);
}
use of org.knime.core.data.DataColumnSpec in project knime-core by knime.
the class DateTimeToStringNodeModel method createStreamableOperator.
/**
* {@inheritDoc}
*/
@Override
public StreamableOperator createStreamableOperator(final PartitionInfo partitionInfo, final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
return new StreamableOperator() {
@Override
public void runFinal(final PortInput[] inputs, final PortOutput[] outputs, final ExecutionContext exec) throws Exception {
final RowInput in = (RowInput) inputs[0];
final RowOutput out = (RowOutput) outputs[0];
final DataTableSpec inSpec = in.getDataTableSpec();
final String[] includeList = m_colSelect.applyTo(inSpec).getIncludes();
final int[] includeIndeces = Arrays.stream(m_colSelect.applyTo(inSpec).getIncludes()).mapToInt(s -> inSpec.findColumnIndex(s)).toArray();
final boolean isReplace = m_isReplaceOrAppend.getStringValue().equals(OPTION_REPLACE);
DataRow row;
while ((row = in.poll()) != null) {
exec.checkCanceled();
DataCell[] datacells = new DataCell[includeIndeces.length];
for (int i = 0; i < includeIndeces.length; i++) {
if (isReplace) {
final DataColumnSpecCreator dataColumnSpecCreator = new DataColumnSpecCreator(includeList[i], StringCell.TYPE);
final TimeToStringCellFactory cellFac = new TimeToStringCellFactory(dataColumnSpecCreator.createSpec(), includeIndeces[i]);
datacells[i] = cellFac.getCell(row);
} else {
final DataColumnSpec dataColSpec = new UniqueNameGenerator(inSpec).newColumn(includeList[i] + m_suffix.getStringValue(), StringCell.TYPE);
final TimeToStringCellFactory cellFac = new TimeToStringCellFactory(dataColSpec, includeIndeces[i]);
datacells[i] = cellFac.getCell(row);
}
}
if (isReplace) {
out.push(new ReplacedColumnsDataRow(row, datacells, includeIndeces));
} else {
out.push(new AppendedColumnRow(row, datacells));
}
}
in.close();
out.close();
}
};
}
use of org.knime.core.data.DataColumnSpec 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.data.DataColumnSpec 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);
}
}
Aggregations