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.");
}
}
Aggregations