Search in sources :

Example 1 with DynamicNodeFactory

use of org.knime.core.node.DynamicNodeFactory in project knime-core by knime.

the class RepositoryFactory method createNode.

/**
 * Creates a new node repository object. Throws an exception, if this fails
 *
 * @param element Configuration element from the contributing plugin
 * @return NodeTemplate object to be used within the repository.
 * @throws IllegalArgumentException If the element is not compatible (e.g.
 *             wrong attributes, or factory class not found)
 */
@SuppressWarnings("unchecked")
public static NodeTemplate createNode(final IConfigurationElement element) {
    // Try to load the node factory class...
    NodeFactory<? extends NodeModel> factory;
    // this ensures that the class is loaded by the correct eclipse
    // classloaders
    GlobalClassCreator.lock.lock();
    try {
        factory = (NodeFactory<? extends NodeModel>) element.createExecutableExtension("factory-class");
    } catch (Throwable e) {
        throw new IllegalArgumentException("Can't load factory class for node: " + element.getAttribute("factory-class"), e);
    } finally {
        GlobalClassCreator.lock.unlock();
    }
    if (factory instanceof DynamicNodeFactory) {
        throw new IllegalArgumentException("Dynamic node factory '" + element.getAttribute("factory-class") + "'" + " registered as normal node factory.");
    }
    String pluginID = element.getDeclaringExtension().getNamespaceIdentifier();
    NodeTemplate node = new NodeTemplate((Class<NodeFactory<? extends NodeModel>>) factory.getClass(), factory.getNodeName(), pluginID);
    node.setAfterID(str(element.getAttribute("after"), ""));
    node.setType(factory.getType());
    if (!Boolean.valueOf(System.getProperty("java.awt.headless", "false"))) {
        // Load images from declaring plugin
        Image icon = ImageRepository.getIconImage(factory);
        node.setIcon(icon);
    }
    node.setCategoryPath(str(element.getAttribute("category-path"), "/"));
    return node;
}
Also used : NodeModel(org.knime.core.node.NodeModel) NodeTemplate(org.knime.workbench.repository.model.NodeTemplate) DynamicNodeTemplate(org.knime.workbench.repository.model.DynamicNodeTemplate) MetaNodeTemplate(org.knime.workbench.repository.model.MetaNodeTemplate) NodeFactory(org.knime.core.node.NodeFactory) DynamicNodeFactory(org.knime.core.node.DynamicNodeFactory) Image(org.eclipse.swt.graphics.Image) DynamicNodeFactory(org.knime.core.node.DynamicNodeFactory)

Example 2 with DynamicNodeFactory

use of org.knime.core.node.DynamicNodeFactory in project knime-core by knime.

the class RepositoryFactory method createNodeSet.

/**
 * Creates the set of dynamic node templates.
 *
 * @param root the root to add the missing categories in
 * @param element from the extension points
 * @return the created dynamic node templates
 */
public static Collection<DynamicNodeTemplate> createNodeSet(final Root root, final IConfigurationElement element) {
    String iconPath = element.getAttribute("default-category-icon");
    // Try to load the node set factory class...
    NodeSetFactory nodeSet;
    // this ensures that the class is loaded by the correct eclipse
    // classloaders
    GlobalClassCreator.lock.lock();
    try {
        nodeSet = (NodeSetFactory) element.createExecutableExtension("factory-class");
    } catch (Throwable e) {
        throw new IllegalArgumentException("Can't load factory class for node: " + element.getAttribute("factory-class"), e);
    } finally {
        GlobalClassCreator.lock.unlock();
    }
    Collection<DynamicNodeTemplate> dynamicNodeTemplates = new ArrayList<DynamicNodeTemplate>();
    // for all nodes in the node set
    for (String factoryId : nodeSet.getNodeFactoryIds()) {
        @SuppressWarnings("unchecked") Class<NodeFactory<? extends NodeModel>> factoryClass = (Class<NodeFactory<? extends NodeModel>>) nodeSet.getNodeFactory(factoryId);
        // Try to load the node factory class...
        NodeFactory<? extends NodeModel> factory;
        // this ensures that the class is loaded by the correct eclipse
        // classloaders
        GlobalClassCreator.lock.lock();
        try {
            factory = DynamicNodeTemplate.createFactoryInstance(factoryClass, nodeSet, factoryId);
        } catch (Throwable e) {
            throw new IllegalArgumentException("Can't load factory class for node: " + factoryClass.getName() + "-" + factoryId, e);
        } finally {
            GlobalClassCreator.lock.unlock();
        }
        // DynamicNodeFactory implementations can set deprecation independently from extension
        if (factory.isDeprecated()) {
            continue;
        }
        DynamicNodeTemplate node = new DynamicNodeTemplate(factoryClass, factoryId, nodeSet, factory.getNodeName());
        node.setAfterID(nodeSet.getAfterID(factoryId));
        if (!Boolean.getBoolean("java.awt.headless")) {
            Image icon = ImageRepository.getIconImage(factory);
            node.setIcon(icon);
        }
        node.setCategoryPath(nodeSet.getCategoryPath(factoryId));
        dynamicNodeTemplates.add(node);
        String pluginID = element.getDeclaringExtension().getNamespaceIdentifier();
        // 
        // Insert in proper location, create all categories on
        // the path
        // if not already there
        // 
        String path = node.getCategoryPath();
        if (path.startsWith("/")) {
            path = path.substring(1);
        }
        // split the path
        String[] segments = path.split("/");
        // start at root
        IContainerObject container = root;
        String currentPath = "";
        for (int i = 0; i < segments.length; i++) {
            IRepositoryObject obj = container.getChildByID(segments[i], false);
            currentPath += segments[i];
            if (obj == null) {
                Category cat = createCategory(pluginID, segments[i], "", segments[i], "", iconPath, currentPath);
                // append the newly created category to the container
                container.addChild(cat);
                obj = cat;
            }
            currentPath += "/";
            // continue at this level
            container = (IContainerObject) obj;
        }
    }
    return dynamicNodeTemplates;
}
Also used : Category(org.knime.workbench.repository.model.Category) ArrayList(java.util.ArrayList) Image(org.eclipse.swt.graphics.Image) DynamicNodeTemplate(org.knime.workbench.repository.model.DynamicNodeTemplate) IRepositoryObject(org.knime.workbench.repository.model.IRepositoryObject) NodeModel(org.knime.core.node.NodeModel) NodeFactory(org.knime.core.node.NodeFactory) DynamicNodeFactory(org.knime.core.node.DynamicNodeFactory) NodeSetFactory(org.knime.core.node.NodeSetFactory) IContainerObject(org.knime.workbench.repository.model.IContainerObject)

Aggregations

Image (org.eclipse.swt.graphics.Image)2 DynamicNodeFactory (org.knime.core.node.DynamicNodeFactory)2 NodeFactory (org.knime.core.node.NodeFactory)2 NodeModel (org.knime.core.node.NodeModel)2 DynamicNodeTemplate (org.knime.workbench.repository.model.DynamicNodeTemplate)2 ArrayList (java.util.ArrayList)1 NodeSetFactory (org.knime.core.node.NodeSetFactory)1 Category (org.knime.workbench.repository.model.Category)1 IContainerObject (org.knime.workbench.repository.model.IContainerObject)1 IRepositoryObject (org.knime.workbench.repository.model.IRepositoryObject)1 MetaNodeTemplate (org.knime.workbench.repository.model.MetaNodeTemplate)1 NodeTemplate (org.knime.workbench.repository.model.NodeTemplate)1