Search in sources :

Example 1 with ChangeSubNodeLinkCommand

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

the class ChangeSubNodeLinkAction method runOnNodes.

/**
 * {@inheritDoc}
 */
@Override
public void runOnNodes(final NodeContainerEditPart[] nodeParts) {
    if (nodeParts.length < 1) {
        return;
    }
    SubNodeContainer subNode = Wrapper.unwrap(nodeParts[0].getNodeContainer(), SubNodeContainer.class);
    if (Role.Link.equals(subNode.getTemplateInformation().getRole())) {
        WorkflowManager wfm = subNode.getParent();
        URI targetURI = subNode.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) Wrapped Metanode. Only the link type can be changed.\n";
        msg += "Please select the new type of the link to the Wrapped 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(subNode);
        try {
            File targetFile = ResolverUtil.resolveURItoLocalFile(targetURI);
            LocalExplorerFileStore targetfs = ExplorerFileSystem.INSTANCE.fromLocalFile(targetFile);
            newURI = AbstractContentProvider.createMetanodeLinkUri(subNode, targetfs, newLinkType);
        } catch (IOException e) {
            LOGGER.error("Unable to resolve Wrapped Metanode template URI " + targetURI + ": " + e.getMessage(), e);
            return;
        } catch (URISyntaxException e) {
            LOGGER.error("Unable to resolve Wrapped Metanode template URI " + targetURI + ": " + e.getMessage(), e);
            return;
        } catch (CoreException e) {
            LOGGER.error("Unable to resolve Wrapped Metanode template URI " + targetURI + ": " + e.getMessage(), e);
            return;
        } finally {
            NodeContext.removeLastContext();
        }
        ChangeSubNodeLinkCommand cmd = new ChangeSubNodeLinkCommand(wfm, subNode, targetURI, newURI);
        getCommandStack().execute(cmd);
    } else {
        throw new IllegalStateException("Can only change the type of a template link if the Wrapped Metanode is actually linked to a template - " + subNode + " is not.");
    }
}
Also used : LocalExplorerFileStore(org.knime.workbench.explorer.filesystem.LocalExplorerFileStore) WorkflowManager(org.knime.core.node.workflow.WorkflowManager) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) SubNodeContainer(org.knime.core.node.workflow.SubNodeContainer) CoreException(org.eclipse.core.runtime.CoreException) LinkType(org.knime.workbench.explorer.view.AbstractContentProvider.LinkType) File(java.io.File) ChangeSubNodeLinkCommand(org.knime.workbench.editor2.commands.ChangeSubNodeLinkCommand)

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 SubNodeContainer (org.knime.core.node.workflow.SubNodeContainer)1 WorkflowManager (org.knime.core.node.workflow.WorkflowManager)1 ChangeSubNodeLinkCommand (org.knime.workbench.editor2.commands.ChangeSubNodeLinkCommand)1 LocalExplorerFileStore (org.knime.workbench.explorer.filesystem.LocalExplorerFileStore)1 LinkType (org.knime.workbench.explorer.view.AbstractContentProvider.LinkType)1