Search in sources :

Example 6 with LocalExplorerFileStore

use of org.knime.workbench.explorer.filesystem.LocalExplorerFileStore in project knime-core by knime.

the class WorkflowEditor method doSaveAs.

/**
 * {@inheritDoc}
 */
@Override
public void doSaveAs() {
    if (m_parentEditor != null) {
        // parent does it if this is a metanode editor
        m_parentEditor.doSaveAs();
        return;
    }
    URI fileResource = m_fileResource;
    Display display = Display.getDefault();
    Shell activeShell = display.getActiveShell();
    if (fileResource == null) {
        MessageDialog.openError(activeShell, "Workflow file resource", "Could not determine the save location to the workflow.");
        return;
    }
    File workflowDir = new File(fileResource);
    // Only continue if no  other editor to this workflow is open
    if (isOtherEditorToWorkflowOpen(workflowDir.toURI())) {
        MessageDialog.openError(activeShell, "\"Save As...\" not available", "\"Save As...\" is not possible while another editor to this workflow is open.");
        return;
    }
    OverwriteAndMergeInfo newLocationInfo = getNewLocation(fileResource, isTempRemoteWorkflowEditor());
    if (newLocationInfo == null) {
        // user canceled
        return;
    }
    AbstractExplorerFileStore newWorkflowDir;
    try {
        newWorkflowDir = ExplorerFileSystem.INSTANCE.getStore(new URI(newLocationInfo.getNewName()));
    } catch (URISyntaxException e2) {
        LOGGER.error("Unable to create a URI from the selected destination: " + e2.getMessage() + " Canceling the SaveAs.");
        MessageDialog.openError(getSite().getShell(), "Internal Error", "Unable to create a URI from the selected destination. \n" + e2.getMessage() + "\nCanceling the SaveAs.");
        return;
    }
    if (newLocationInfo.createSnapshot()) {
        try {
            ((RemoteExplorerFileStore) newWorkflowDir).createSnapshot(newLocationInfo.getComment());
        } catch (CoreException e) {
            String msg = "Unable to create the desired snapshot before overwriting the workflow:\n" + e.getMessage() + "\n\nCanceling the upload!";
            LOGGER.error("Unable to create the desired snapshot before overwriting the workflow: " + e.getMessage() + " Upload canceled!", e);
            MessageDialog.openError(getSite().getShell(), "Server Error", msg);
            return;
        }
    }
    if (newWorkflowDir instanceof RemoteExplorerFileStore) {
        // selected a remote location: save + upload
        if (isDirty()) {
            saveTo(m_fileResource, new NullProgressMonitor(), true, null);
        }
        AbstractExplorerFileStore localFS = getFileStore(fileResource);
        if (localFS == null || !(localFS instanceof LocalExplorerFileStore)) {
            LOGGER.error("Unable to resolve current workflow location. Flow not uploaded!");
            return;
        }
        try {
            m_workflowCanBeDeleted.acquire();
            newWorkflowDir.getContentProvider().performUploadAsync((LocalExplorerFileStore) localFS, (RemoteExplorerFileStore) newWorkflowDir, /*deleteSource=*/
            false, false, t -> m_workflowCanBeDeleted.release());
        } catch (CoreException | InterruptedException e) {
            String msg = "\"Save As...\" failed to upload the workflow to the selected remote location\n(" + e.getMessage() + ")";
            LOGGER.error(msg, e);
            MessageDialog.openError(activeShell, "\"Save As...\" failed.", msg);
        }
    // no need to change any registered locations as this was a save+upload (didn't change flow location)
    } else {
        // this is messy. Some methods want the URI with the folder, others the file store denoting workflow.knime
        AbstractExplorerFileStore newWorkflowFile = newWorkflowDir.getChild(WorkflowPersistor.WORKFLOW_FILE);
        File localNewWorkflowDir = null;
        try {
            localNewWorkflowDir = newWorkflowDir.toLocalFile();
        } catch (CoreException e1) {
            LOGGER.error("Unable to resolve selection to local file path: " + e1.getMessage(), e1);
            return;
        }
        File mountPointRoot = null;
        try {
            mountPointRoot = newWorkflowDir.getContentProvider().getFileStore("/").toLocalFile();
        } catch (CoreException ex) {
            LOGGER.warn("Could not determine mount point root for " + newWorkflowDir + ": " + ex.getMessage(), ex);
        }
        WorkflowContext context = new WorkflowContext.Factory(m_manager.getContext()).setCurrentLocation(localNewWorkflowDir).setMountpointRoot(mountPointRoot).setMountpointURI(newWorkflowDir.toURI()).createContext();
        saveTo(localNewWorkflowDir.toURI(), new NullProgressMonitor(), true, context);
        setInput(new FileStoreEditorInput(newWorkflowFile));
        if (newWorkflowDir.getParent() != null) {
            newWorkflowDir.getParent().refresh();
        }
        registerProject(localNewWorkflowDir);
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) LocalExplorerFileStore(org.knime.workbench.explorer.filesystem.LocalExplorerFileStore) WorkflowContext(org.knime.core.node.workflow.WorkflowContext) NodeFactory(org.knime.core.node.NodeFactory) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) Shell(org.eclipse.swt.widgets.Shell) OverwriteAndMergeInfo(org.knime.workbench.explorer.view.dialogs.OverwriteAndMergeInfo) CoreException(org.eclipse.core.runtime.CoreException) AbstractExplorerFileStore(org.knime.workbench.explorer.filesystem.AbstractExplorerFileStore) RemoteExplorerFileStore(org.knime.workbench.explorer.filesystem.RemoteExplorerFileStore) ReferencedFile(org.knime.core.internal.ReferencedFile) File(java.io.File) FileStoreEditorInput(org.eclipse.ui.ide.FileStoreEditorInput) Display(org.eclipse.swt.widgets.Display)

Example 7 with LocalExplorerFileStore

use of org.knime.workbench.explorer.filesystem.LocalExplorerFileStore 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

LocalExplorerFileStore (org.knime.workbench.explorer.filesystem.LocalExplorerFileStore)7 URI (java.net.URI)6 CoreException (org.eclipse.core.runtime.CoreException)5 AbstractExplorerFileStore (org.knime.workbench.explorer.filesystem.AbstractExplorerFileStore)5 File (java.io.File)4 IOException (java.io.IOException)4 URISyntaxException (java.net.URISyntaxException)3 WorkflowContext (org.knime.core.node.workflow.WorkflowContext)3 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)2 ReferencedFile (org.knime.core.internal.ReferencedFile)2 SubNodeContainer (org.knime.core.node.workflow.SubNodeContainer)2 WorkflowManager (org.knime.core.node.workflow.WorkflowManager)2 WorkflowManagerUI (org.knime.core.ui.node.workflow.WorkflowManagerUI)2 NodeContainerEditPart (org.knime.workbench.editor2.editparts.NodeContainerEditPart)2 RemoteExplorerFileStore (org.knime.workbench.explorer.filesystem.RemoteExplorerFileStore)2 AbstractContentProvider (org.knime.workbench.explorer.view.AbstractContentProvider)2 LinkType (org.knime.workbench.explorer.view.AbstractContentProvider.LinkType)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1