Search in sources :

Example 1 with LinkType

use of org.knime.workbench.explorer.view.AbstractContentProvider.LinkType 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)

Example 2 with LinkType

use of org.knime.workbench.explorer.view.AbstractContentProvider.LinkType 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)2 IOException (java.io.IOException)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 CoreException (org.eclipse.core.runtime.CoreException)2 WorkflowManager (org.knime.core.node.workflow.WorkflowManager)2 LocalExplorerFileStore (org.knime.workbench.explorer.filesystem.LocalExplorerFileStore)2 LinkType (org.knime.workbench.explorer.view.AbstractContentProvider.LinkType)2 SubNodeContainer (org.knime.core.node.workflow.SubNodeContainer)1 ChangeMetaNodeLinkCommand (org.knime.workbench.editor2.commands.ChangeMetaNodeLinkCommand)1 ChangeSubNodeLinkCommand (org.knime.workbench.editor2.commands.ChangeSubNodeLinkCommand)1