Search in sources :

Example 6 with UtilityFactory

use of org.knime.core.data.DataValue.UtilityFactory in project knime-core by knime.

the class TypeFilterPanelImpl method addDataValues.

/**
 * Add data values to the selection.
 *
 * @param values The data values to add
 */
void addDataValues(final Iterable<Class<? extends DataValue>> values) {
    for (Class<? extends DataValue> value : values) {
        if (isIncludedByFilter(value)) {
            if (value != null && !m_selections.containsKey(value.getName())) {
                UtilityFactory utilityFor = DataType.getUtilityFor(value);
                if (utilityFor instanceof ExtensibleUtilityFactory) {
                    ExtensibleUtilityFactory eu = (ExtensibleUtilityFactory) utilityFor;
                    String label = eu.getName();
                    String key = value.getName();
                    JCheckBox newCheckbox = addCheckBox(label, false);
                    m_selections.put(key, newCheckbox);
                    newCheckbox.setEnabled(isEnabled());
                }
            }
        }
    }
}
Also used : UtilityFactory(org.knime.core.data.DataValue.UtilityFactory) ExtensibleUtilityFactory(org.knime.core.data.ExtensibleUtilityFactory) JCheckBox(javax.swing.JCheckBox) ExtensibleUtilityFactory(org.knime.core.data.ExtensibleUtilityFactory)

Example 7 with UtilityFactory

use of org.knime.core.data.DataValue.UtilityFactory in project knime-core by knime.

the class DataTypeNameRenderer method getListCellRendererComponent.

/**
 * {@inheritDoc}
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public Component getListCellRendererComponent(final JList list, final Object value, final int index, final boolean isSelected, final boolean cellHasFocus) {
    /* Almost all has been copied from the super implementation */
    setComponentOrientation(list.getComponentOrientation());
    if (isSelected) {
        setBackground(list.getSelectionBackground());
        setForeground(list.getSelectionForeground());
    } else {
        setBackground(list.getBackground());
        setForeground(list.getForeground());
    }
    if (value instanceof Class && DataValue.class.isAssignableFrom((Class) value)) {
        Class<? extends DataValue> type = (Class<? extends DataValue>) value;
        String s = type.getName();
        int dot = s.lastIndexOf('.');
        if (dot >= 0 && dot < s.length() - 1) {
            s = s.substring(dot + 1);
        }
        s = s.trim();
        UtilityFactory fac = DataType.getUtilityFor(type);
        Icon icon = fac.getIcon();
        setIcon(icon);
        setText(s);
    } else {
        if (value instanceof Icon) {
            setIcon((Icon) value);
            setText("");
        } else {
            setIcon(null);
            setText((value == null) ? "" : value.toString());
        }
    }
    setEnabled(list.isEnabled());
    setFont(list.getFont());
    setBorder((cellHasFocus) ? UIManager.getBorder("List.focusCellHighlightBorder") : noFocusBorder);
    return this;
}
Also used : UtilityFactory(org.knime.core.data.DataValue.UtilityFactory) DataValue(org.knime.core.data.DataValue) Icon(javax.swing.Icon)

Example 8 with UtilityFactory

use of org.knime.core.data.DataValue.UtilityFactory in project knime-core by knime.

the class DataType method getRendererFactories.

/**
 * Returns the list of registered renderer factories, using an extension point driven mechanism to collect all
 * instances registered with the implemented data value interfaces. If no renderer was declared by the
 * {@link org.knime.core.data.DataValue} interfaces, this method will make sure that at least a default renderer
 * (using the {@link DataCell#toString()} method) is returned.
 *
 * @return an ordered, non-empty collection of factories.
 * @since 2.12
 */
public Collection<DataValueRendererFactory> getRendererFactories() {
    Map<String, DataValueRendererFactory> map = new LinkedHashMap<>();
    Collection<Class<? extends DataValue>> allValueClasses = new LinkedHashSet<>();
    // first value will be the preferred one - if any
    allValueClasses.addAll(m_valueClasses);
    allValueClasses.addAll(m_adapterValueList);
    for (Class<? extends DataValue> cl : allValueClasses) {
        UtilityFactory fac = getUtilityFor(cl);
        if (!(fac instanceof ExtensibleUtilityFactory)) {
            continue;
        }
        ExtensibleUtilityFactory efac = (ExtensibleUtilityFactory) fac;
        // make sure the preferred and default renderers come first
        DataValueRendererFactory prefRendererFac = efac.getPreferredRenderer();
        if (prefRendererFac != null) {
            map.put(prefRendererFac.getId(), prefRendererFac);
        }
        DataValueRendererFactory defaultRendererFac = efac.getDefaultRenderer();
        if (defaultRendererFac != null) {
            map.put(defaultRendererFac.getId(), defaultRendererFac);
        }
        for (DataValueRendererFactory rf : efac.getAvailableRenderers()) {
            map.put(rf.getId(), rf);
        }
    }
    if (map.isEmpty()) {
        DefaultDataValueRenderer.Factory f = new DefaultDataValueRenderer.Factory();
        map.put(f.getId(), f);
    }
    return map.values();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) CollectionDataValue(org.knime.core.data.collection.CollectionDataValue) DefaultDataValueRenderer(org.knime.core.data.renderer.DefaultDataValueRenderer) UtilityFactory(org.knime.core.data.DataValue.UtilityFactory) DataValueRendererFactory(org.knime.core.data.renderer.DataValueRendererFactory) LinkedHashMap(java.util.LinkedHashMap) UtilityFactory(org.knime.core.data.DataValue.UtilityFactory) DataValueRendererFactory(org.knime.core.data.renderer.DataValueRendererFactory)

Example 9 with UtilityFactory

use of org.knime.core.data.DataValue.UtilityFactory in project knime-core by knime.

the class DataType method getRenderer.

/**
 * Returns a family of all renderers that are available for this <code>DataType</code>. The returned
 * {@link org.knime.core.data.renderer.DataValueRendererFamily} will contain all renderers that are supported or
 * available through the compatible {@link org.knime.core.data.DataValue} interfaces. If no renderer was declared by
 * the {@link org.knime.core.data.DataValue} interfaces, this method will make sure that at least a default renderer
 * (using the {@link DataCell#toString()} method) is returned.
 *
 * <p>
 * The {@link DataColumnSpec} is passed to all renderer families retrieved from the underlying
 * {@link UtilityFactory}. Most of the renderer implementations won't need column domain information but some do.
 * For instance a class that renders the double value in the column according to the minimum/maximum values in the
 * {@link DataColumnDomain}.
 *
 * @param spec the column spec to the column for which the renderer will be used
 * @return a family of all renderers that are available for this <code>DataType</code>
 * @deprecated Replaced by {@link #getRendererFactories()}
 */
@Deprecated
public DataValueRendererFamily getRenderer(final DataColumnSpec spec) {
    ArrayList<DataValueRendererFamily> list = new ArrayList<DataValueRendererFamily>();
    // first add the preferred value class, if any
    for (Class<? extends DataValue> cl : m_valueClasses) {
        UtilityFactory fac = getUtilityFor(cl);
        DataValueRendererFamily fam = fac.getRendererFamily(spec);
        if (fam != null) {
            list.add(fam);
        }
    }
    for (Class<? extends DataValue> cl : m_adapterValueList) {
        if (!m_valueClasses.contains(cl)) {
            UtilityFactory fac = getUtilityFor(cl);
            DataValueRendererFamily fam = fac.getRendererFamily(spec);
            if (fam != null) {
                list.add(fam);
            }
        }
    }
    if (list.isEmpty()) {
        list.add(new DefaultDataValueRendererFamily());
    }
    return new SetOfRendererFamilies(list);
}
Also used : UtilityFactory(org.knime.core.data.DataValue.UtilityFactory) SetOfRendererFamilies(org.knime.core.data.renderer.SetOfRendererFamilies) DataValueRendererFamily(org.knime.core.data.renderer.DataValueRendererFamily) DefaultDataValueRendererFamily(org.knime.core.data.renderer.DefaultDataValueRendererFamily) ArrayList(java.util.ArrayList) DefaultDataValueRendererFamily(org.knime.core.data.renderer.DefaultDataValueRendererFamily)

Example 10 with UtilityFactory

use of org.knime.core.data.DataValue.UtilityFactory in project knime-core by knime.

the class DataType method getUtilityFor.

/**
 * Determines the <code>UtilityFactory</code> for a given
 * {@link org.knime.core.data.DataValue} implementation.
 * This method tries to access a static field in the
 * {@link org.knime.core.data.DataValue} class:
 * <pre>
 *   public static final UtilityFactory UTILITY;
 * </pre>
 * If no such field exists, this method returns the factory of one of the
 * super interfaces. If it exists but has the wrong access scope or if it
 * is not static, a warning message is logged and the member of one of the
 * super interfaces is returned. If no <code>UTILITY</code> member can be
 * found, finally the {@link org.knime.core.data.DataValue} class returns a
 * correct implementation, since it is at the top of the hierarchy.
 *
 * @param value the runtime class of the
 *        {@link org.knime.core.data.DataCell}
 * @return the <code>UtilityFactory</code> given in the cell implementation
 * @throws NullPointerException if the argument or the found
 * <code>UTILITY</code> member is <code>null</code>
 */
public static UtilityFactory getUtilityFor(final Class<? extends DataValue> value) {
    if (value == null) {
        throw new NullPointerException("Class argument must not be null.");
    }
    UtilityFactory result = VALUE_CLASS_TO_UTILITY.get(value);
    if (result == null) {
        Exception exception = null;
        try {
            // Java will fetch a static field that is public, if you
            // declare it to be non-static or give it the wrong scope, it
            // automatically retrieves the static field from a super
            // class/interface (from which super interface it gets it,
            // depends pretty much on the order after the "extends ..."
            // statement) If this field has the wrong type, a coding
            // problem is reported.
            Field typeField = value.getField("UTILITY");
            Object typeObject = typeField.get(null);
            result = (DataValue.UtilityFactory) typeObject;
            if (result == null) {
                throw new NullPointerException("UTILITY is null.");
            }
        } catch (NoSuchFieldException nsfe) {
            exception = nsfe;
        } catch (NullPointerException npe) {
            exception = npe;
        } catch (IllegalAccessException iae) {
            exception = iae;
        } catch (ClassCastException cce) {
            exception = cce;
        }
        if (exception != null) {
            LOGGER.coding("DataValue interface \"" + value.getSimpleName() + "\" seems to have a problem with the static field " + "\"UTILITY\"", exception);
            // fall back - no meta information available
            result = DataValue.UTILITY;
        }
        VALUE_CLASS_TO_UTILITY.put(value, result);
    }
    return result;
}
Also used : UtilityFactory(org.knime.core.data.DataValue.UtilityFactory) Field(java.lang.reflect.Field) CollectionDataValue(org.knime.core.data.collection.CollectionDataValue) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

UtilityFactory (org.knime.core.data.DataValue.UtilityFactory)11 ExtensibleUtilityFactory (org.knime.core.data.ExtensibleUtilityFactory)5 Test (org.junit.Test)3 Icon (javax.swing.Icon)2 JCheckBox (javax.swing.JCheckBox)2 CollectionDataValue (org.knime.core.data.collection.CollectionDataValue)2 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 IEclipsePreferences (org.eclipse.core.runtime.preferences.IEclipsePreferences)1 DataColumnSpec (org.knime.core.data.DataColumnSpec)1 DataValue (org.knime.core.data.DataValue)1 DataValueRendererFactory (org.knime.core.data.renderer.DataValueRendererFactory)1 DataValueRendererFamily (org.knime.core.data.renderer.DataValueRendererFamily)1 DefaultDataValueRenderer (org.knime.core.data.renderer.DefaultDataValueRenderer)1 DefaultDataValueRendererFamily (org.knime.core.data.renderer.DefaultDataValueRendererFamily)1 SetOfRendererFamilies (org.knime.core.data.renderer.SetOfRendererFamilies)1 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)1