Search in sources :

Example 1 with ChangeMetaNodeLinkCommand

use of org.knime.workbench.editor2.commands.ChangeMetaNodeLinkCommand in project knime-core by knime.

the class ChangeMetaNodeLinkAction method runOnNodes.

/**
 * {@inheritDoc}
 */
@Override
public void runOnNodes(final NodeContainerEditPart[] nodeParts) {
    if (nodeParts.length < 1) {
        return;
    }
    WorkflowManager metaNode = Wrapper.unwrapWFM(nodeParts[0].getNodeContainer());
    if (Role.Link.equals(metaNode.getTemplateInformation().getRole())) {
        WorkflowManager wfm = metaNode;
        while (!wfm.isProject()) {
            wfm = wfm.getParent();
        }
        URI targetURI = metaNode.getTemplateInformation().getSourceURI();
        LinkType linkType = LinkType.None;
        try {
            if (ResolverUtil.isMountpointRelativeURL(targetURI)) {
                linkType = LinkType.MountpointRelative;
            } else if (ResolverUtil.isWorkflowRelativeURL(targetURI)) {
                linkType = LinkType.WorkflowRelative;
            } else {
                linkType = LinkType.Absolute;
            }
        } catch (IOException e) {
            LOGGER.error("Unable to resolve current link to template " + targetURI + ": " + e.getMessage(), e);
            return;
        }
        String msg = "This is a linked (read-only) Metanode. Only the link type can be changed.\n";
        msg += "Please select the new type of the link to the metanode template.\n";
        msg += "(current type: " + linkType + ", current link: " + targetURI + ")\n";
        msg += "The origin of the template will not be changed - just the way it is referenced.";
        LinkPrompt dlg = new LinkPrompt(getEditor().getSite().getShell(), msg, linkType);
        dlg.open();
        if (dlg.getReturnCode() == Window.CANCEL) {
            return;
        }
        LinkType newLinkType = dlg.getLinkType();
        if (linkType.equals(newLinkType)) {
            LOGGER.info("Link type not changes as selected type equals existing type " + targetURI);
            return;
        }
        // as the workflow is local and the template in the same mountID, it should resolve to a file
        URI newURI = null;
        NodeContext.pushContext(metaNode);
        try {
            File targetFile = ResolverUtil.resolveURItoLocalFile(targetURI);
            LocalExplorerFileStore targetfs = ExplorerFileSystem.INSTANCE.fromLocalFile(targetFile);
            newURI = AbstractContentProvider.createMetanodeLinkUri(metaNode, targetfs, newLinkType);
        } catch (IOException e) {
            LOGGER.error("Unable to resolve metanode template URI " + targetURI + ": " + e.getMessage(), e);
            return;
        } catch (URISyntaxException e) {
            LOGGER.error("Unable to resolve metanode template URI " + targetURI + ": " + e.getMessage(), e);
            return;
        } catch (CoreException e) {
            LOGGER.error("Unable to resolve metanode template URI " + targetURI + ": " + e.getMessage(), e);
            return;
        } finally {
            NodeContext.removeLastContext();
        }
        ChangeMetaNodeLinkCommand cmd = new ChangeMetaNodeLinkCommand(wfm, metaNode, targetURI, newURI);
        getCommandStack().execute(cmd);
    } else {
        throw new IllegalStateException("Can only change the type of a template link if the metanode is actually linked to a template - " + metaNode + " is not.");
    }
}
Also used : ChangeMetaNodeLinkCommand(org.knime.workbench.editor2.commands.ChangeMetaNodeLinkCommand) CoreException(org.eclipse.core.runtime.CoreException) LocalExplorerFileStore(org.knime.workbench.explorer.filesystem.LocalExplorerFileStore) WorkflowManager(org.knime.core.node.workflow.WorkflowManager) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) LinkType(org.knime.workbench.explorer.view.AbstractContentProvider.LinkType) URI(java.net.URI) File(java.io.File)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 CoreException (org.eclipse.core.runtime.CoreException)1 WorkflowManager (org.knime.core.node.workflow.WorkflowManager)1 ChangeMetaNodeLinkCommand (org.knime.workbench.editor2.commands.ChangeMetaNodeLinkCommand)1 LocalExplorerFileStore (org.knime.workbench.explorer.filesystem.LocalExplorerFileStore)1 LinkType (org.knime.workbench.explorer.view.AbstractContentProvider.LinkType)1