Search in sources :

Example 1 with AbstractExplorerFileStore

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

the class ChangeMetaNodeLinkAction method internalCalculateEnabled.

/**
 * @return true, if underlying model instance of <code>WorkflowManager</code>, otherwise false
 */
@Override
protected boolean internalCalculateEnabled() {
    NodeContainerEditPart[] nodes = getSelectedParts(NodeContainerEditPart.class);
    if (nodes.length != 1) {
        return false;
    }
    NodeContainerUI nc = nodes[0].getNodeContainer();
    if (!(nc instanceof WorkflowManagerUI)) {
        return false;
    }
    WorkflowManagerUI metaNode = (WorkflowManagerUI) nc;
    if (!Role.Link.equals(unwrapWFM(metaNode).getTemplateInformation().getRole()) || metaNode.getParent().isWriteProtected()) {
        // metanode must be linked and parent must not forbid the change
        return false;
    }
    // we can reconfigure the template link - but only if template and flow are in the same mountpoint
    URI targetURI = unwrapWFM(metaNode).getTemplateInformation().getSourceURI();
    try {
        if (ResolverUtil.isMountpointRelativeURL(targetURI) || ResolverUtil.isWorkflowRelativeURL(targetURI)) {
            return true;
        }
    } catch (IOException e) {
        return false;
    }
    // we can change absolute links if the mount points of flow and template are the same
    AbstractContentProvider workflowMountPoint = null;
    WorkflowContext wfc = metaNode.getProjectWFM().getContext();
    LocalExplorerFileStore fs = ExplorerFileSystem.INSTANCE.fromLocalFile(wfc.getMountpointRoot());
    if (fs != null) {
        workflowMountPoint = fs.getContentProvider();
    }
    if (workflowMountPoint == null) {
        return false;
    }
    AbstractExplorerFileStore targetfs = ExplorerFileSystem.INSTANCE.getStore(targetURI);
    if (targetfs == null) {
        return false;
    }
    return workflowMountPoint.equals(targetfs.getContentProvider());
}
Also used : NodeContainerUI(org.knime.core.ui.node.workflow.NodeContainerUI) NodeContainerEditPart(org.knime.workbench.editor2.editparts.NodeContainerEditPart) AbstractExplorerFileStore(org.knime.workbench.explorer.filesystem.AbstractExplorerFileStore) LocalExplorerFileStore(org.knime.workbench.explorer.filesystem.LocalExplorerFileStore) WorkflowContext(org.knime.core.node.workflow.WorkflowContext) WorkflowManagerUI(org.knime.core.ui.node.workflow.WorkflowManagerUI) AbstractContentProvider(org.knime.workbench.explorer.view.AbstractContentProvider) IOException(java.io.IOException) URI(java.net.URI)

Example 2 with AbstractExplorerFileStore

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

the class MetaNodeTemplateDropTargetListener method isEnabled.

/**
 * {@inheritDoc}
 */
@Override
public boolean isEnabled(final DropTargetEvent event) {
    if (!super.isEnabled(event)) {
        return false;
    }
    AbstractExplorerFileStore fileStore = getDragResources(event).getObject();
    boolean isMetaNodeTemplate = AbstractExplorerFileStore.isWorkflowTemplate(fileStore);
    if (isMetaNodeTemplate) {
        event.feedback = DND.FEEDBACK_SELECT;
        event.operations = DND.DROP_COPY | DND.DROP_LINK;
        event.detail = DND.DROP_COPY;
    }
    return isMetaNodeTemplate;
}
Also used : AbstractExplorerFileStore(org.knime.workbench.explorer.filesystem.AbstractExplorerFileStore)

Example 3 with AbstractExplorerFileStore

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

the class ChangeSubNodeLinkAction method internalCalculateEnabled.

/**
 * @return true, if underlying model instance of <code>WorkflowManager</code>, otherwise false
 */
@Override
protected boolean internalCalculateEnabled() {
    NodeContainerEditPart[] nodes = getSelectedParts(NodeContainerEditPart.class);
    if (nodes.length != 1) {
        return false;
    }
    NodeContainer nc = Wrapper.unwrapNC(nodes[0].getNodeContainer());
    if (!(nc instanceof SubNodeContainer)) {
        return false;
    }
    SubNodeContainer subNode = (SubNodeContainer) nc;
    if (!Role.Link.equals(subNode.getTemplateInformation().getRole()) || subNode.getParent().isWriteProtected()) {
        // sub node must be linked and parent must not forbid the change
        return false;
    }
    // we can reconfigure the template link - but only if template and flow are in the same mountpoint
    URI targetURI = subNode.getTemplateInformation().getSourceURI();
    try {
        if (ResolverUtil.isMountpointRelativeURL(targetURI) || ResolverUtil.isWorkflowRelativeURL(targetURI)) {
            return true;
        }
    } catch (IOException e) {
        return false;
    }
    // we can change absolute links if the mount points of flow and template are the same
    AbstractContentProvider workflowMountPoint = null;
    WorkflowContext wfc = subNode.getProjectWFM().getContext();
    LocalExplorerFileStore fs = ExplorerFileSystem.INSTANCE.fromLocalFile(wfc.getMountpointRoot());
    if (fs != null) {
        workflowMountPoint = fs.getContentProvider();
    }
    if (workflowMountPoint == null) {
        return false;
    }
    AbstractExplorerFileStore targetfs = ExplorerFileSystem.INSTANCE.getStore(targetURI);
    if (targetfs == null) {
        return false;
    }
    return workflowMountPoint.equals(targetfs.getContentProvider());
}
Also used : SubNodeContainer(org.knime.core.node.workflow.SubNodeContainer) NodeContainerEditPart(org.knime.workbench.editor2.editparts.NodeContainerEditPart) AbstractExplorerFileStore(org.knime.workbench.explorer.filesystem.AbstractExplorerFileStore) LocalExplorerFileStore(org.knime.workbench.explorer.filesystem.LocalExplorerFileStore) WorkflowContext(org.knime.core.node.workflow.WorkflowContext) AbstractContentProvider(org.knime.workbench.explorer.view.AbstractContentProvider) SubNodeContainer(org.knime.core.node.workflow.SubNodeContainer) NodeContainer(org.knime.core.node.workflow.NodeContainer) IOException(java.io.IOException) URI(java.net.URI)

Example 4 with AbstractExplorerFileStore

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

the class WorkflowEditor method setInput.

/**
 * Sets the editor input, that is, the file that contains the serialized
 * workflow manager.
 *
 * {@inheritDoc}
 */
@Override
protected void setInput(final IEditorInput input) {
    LOGGER.debug("Setting input into editor...");
    super.setInput(input);
    m_origRemoteLocation = null;
    if (input instanceof WorkflowManagerInput) {
        // metanode and subnode
        setWorkflowManagerInput((WorkflowManagerInput) input);
    } else if (input instanceof IURIEditorInput) {
        File wfFile;
        AbstractExplorerFileStore wfFileFileStore = null;
        File mountPointRoot = null;
        URI uri = ((IURIEditorInput) input).getURI();
        if (input instanceof RemoteWorkflowInput) {
            m_origRemoteLocation = ((RemoteWorkflowInput) input).getRemoteOriginalLocation();
        }
        if ("file".equals(uri.getScheme())) {
            wfFile = new File(uri);
            try {
                LocalExplorerFileStore fs = ExplorerFileSystem.INSTANCE.fromLocalFile(wfFile);
                if ((fs == null) || (fs.getContentProvider() == null)) {
                    LOGGER.info("Could not determine mount point root for " + wfFile.getParent() + ", looks like it is a linked resource");
                } else {
                    wfFileFileStore = fs;
                    mountPointRoot = fs.getContentProvider().getFileStore("/").toLocalFile();
                }
            } catch (CoreException ex) {
                LOGGER.warn("Could not determine mount point root for " + wfFile.getParent() + ": " + ex.getMessage(), ex);
            }
        } else if (ExplorerFileSystem.SCHEME.equals(uri.getScheme())) {
            AbstractExplorerFileStore filestore = ExplorerFileSystem.INSTANCE.getStore(uri);
            if (filestore == null) {
                LOGGER.error("Could not find filestore for URI " + uri);
                openErrorDialogAndCloseEditor("Could not find filestore for URI " + uri);
                return;
            }
            wfFileFileStore = filestore;
            try {
                wfFile = filestore.toLocalFile();
            } catch (CoreException ex) {
                LOGGER.error(ex.getMessage(), ex);
                openErrorDialogAndCloseEditor(ex.getMessage());
                return;
            }
            if (wfFile == null) {
                LOGGER.error("URI " + uri + " is not a local workflow");
                openErrorDialogAndCloseEditor("URI " + uri + " is not a local workflow");
                return;
            }
            try {
                mountPointRoot = filestore.getContentProvider().getFileStore("/").toLocalFile();
            } catch (CoreException ex) {
                LOGGER.warn("Could not determine mount point root for " + wfFile.getParent() + ": " + ex.getMessage(), ex);
            }
        } else {
            LOGGER.error("Unsupported scheme for workflow URI: " + uri);
            openErrorDialogAndCloseEditor("Unsupported scheme for workflow URI: " + uri);
            return;
        }
        URI oldFileResource = m_fileResource;
        WorkflowManagerUI oldManager = m_manager;
        final File wfDir = wfFile.getParentFile();
        m_fileResource = wfDir.toURI();
        LOGGER.debug("Resource File's project: " + m_fileResource);
        boolean isEnableAutoSave = true;
        try {
            if (oldManager != null) {
                // doSaveAs called
                assert oldFileResource != null;
                WorkflowManagerUI managerForOldResource = (WorkflowManagerUI) ProjectWorkflowMap.getWorkflowUI(oldFileResource);
                if (m_manager != managerForOldResource) {
                    throw new IllegalStateException(String.format("Cannot set new input for workflow editor " + "as there was already a workflow manager set (old resource: \"%s\", " + "new resource: \"%s\", old manager: \"%s\", manager to old resource: \"%s\"", oldFileResource, m_fileResource, oldManager, managerForOldResource));
                }
                ProjectWorkflowMap.replace(m_fileResource, oldManager, oldFileResource);
                isEnableAutoSave = m_isAutoSaveAllowed;
            } else {
                m_manager = (WorkflowManagerUI) ProjectWorkflowMap.getWorkflowUI(m_fileResource);
            }
            if (m_manager != null) {
                // in case the workflow manager was edited somewhere else
                if (m_manager.isDirty()) {
                    markDirty();
                }
            } else {
                File autoSaveDirectory = WorkflowSaveHelper.getAutoSaveDirectory(new ReferencedFile(wfDir));
                if (autoSaveDirectory.exists()) {
                    if (!autoSaveDirectory.isDirectory() || !autoSaveDirectory.canRead()) {
                        LOGGER.warnWithFormat("Found existing auto-save location to workflow \"%s\" (\"%s\") but %s" + " - disabling auto-save", wfDir.getName(), autoSaveDirectory.getAbsolutePath(), (!autoSaveDirectory.isDirectory() ? "it is not a directory" : "cannot read it"));
                        isEnableAutoSave = false;
                    } else {
                        File parentDir = autoSaveDirectory.getParentFile();
                        String date = DateFormatUtils.format(autoSaveDirectory.lastModified(), "yyyy-MM-dd HH-mm");
                        String newName = wfDir.getName() + " (Auto-Save Copy - " + date + ")";
                        int unique = 1;
                        File restoredAutoSaveDirectory;
                        while ((restoredAutoSaveDirectory = new File(parentDir, newName)).exists()) {
                            newName = wfDir.getName() + " (Auto-Save Copy - " + date + " #" + (unique++) + ")";
                        }
                        // this is the file store object to autoSaveDirectory - if we can resolve it
                        // we use it below in user messages and to do the rename in order to trigger a refresh
                        // in the explorer tree - if we can't resolve it (dunno why) we use java.io.File operation
                        AbstractExplorerFileStore autoSaveDirFileStore = null;
                        AbstractExplorerFileStore restoredAutoSaveDirFileStore = null;
                        if (wfFileFileStore != null) {
                            try {
                                // first parent is workflow dir, parent of that is the workflow group
                                AbstractExplorerFileStore parFS = wfFileFileStore.getParent().getParent();
                                AbstractExplorerFileStore temp = parFS.getChild(autoSaveDirectory.getName());
                                if (autoSaveDirectory.equals(temp.toLocalFile())) {
                                    autoSaveDirFileStore = temp;
                                }
                                restoredAutoSaveDirFileStore = parFS.getChild(newName);
                            } catch (CoreException e) {
                                LOGGER.warn("Unable to resolve parent file store for \"" + wfFileFileStore + "\"", e);
                            }
                        }
                        int action = openQuestionDialogWhenLoadingWorkflowWithAutoSaveCopy(wfDir.getName(), restoredAutoSaveDirectory.getName());
                        final boolean openCopy;
                        switch(action) {
                            case // Open Copy
                            0:
                                openCopy = true;
                                break;
                            case // Open Original
                            1:
                                openCopy = false;
                                break;
                            default:
                                // Cancel
                                String error = "Canceling due to auto-save copy conflict";
                                openErrorDialogAndCloseEditor(error);
                                throw new OperationCanceledException(error);
                        }
                        boolean couldRename = false;
                        if (autoSaveDirFileStore != null) {
                            // preferred way to rename, updates explorer tree
                            try {
                                autoSaveDirFileStore.move(restoredAutoSaveDirFileStore, EFS.NONE, null);
                                couldRename = true;
                            } catch (CoreException e) {
                                String message = "Could not rename auto-save copy\n" + "from\n  " + autoSaveDirFileStore.getMountIDWithFullPath() + "\nto\n  " + newName;
                                LOGGER.error(message, e);
                            }
                        } else {
                            LOGGER.warnWithFormat("Could not resolve explorer file store to \"%s\" - " + "renaming on file system directly", autoSaveDirectory.getAbsolutePath());
                            // just rename on file system and ignore explorer tree
                            couldRename = autoSaveDirectory.renameTo(restoredAutoSaveDirectory);
                        }
                        if (!couldRename) {
                            isEnableAutoSave = false;
                            String message = "Could not rename auto-save copy\n" + "from\n  " + autoSaveDirectory.getAbsolutePath() + "\nto\n  " + restoredAutoSaveDirectory.getAbsolutePath() + "";
                            if (openCopy) {
                                openErrorDialogAndCloseEditor(message);
                                throw new OperationCanceledException(message);
                            } else {
                                MessageDialog.openWarning(Display.getDefault().getActiveShell(), "Auto-Save Rename Problem", message + "\nAuto-Save will be disabled.");
                            }
                        }
                        if (openCopy) {
                            m_fileResource = restoredAutoSaveDirectory.toURI();
                            wfFile = new File(restoredAutoSaveDirectory, wfFile.getName());
                        }
                    }
                }
                IWorkbench wb = PlatformUI.getWorkbench();
                IProgressService ps = wb.getProgressService();
                // this one sets the workflow manager in the editor
                LoadWorkflowRunnable loadWorkflowRunnable = new LoadWorkflowRunnable(this, m_origRemoteLocation != null ? m_origRemoteLocation : uri, wfFile, mountPointRoot);
                ps.busyCursorWhile(loadWorkflowRunnable);
                // non-null if set by workflow runnable above
                if (m_manager == null) {
                    if (loadWorkflowRunnable.hasLoadingBeenCanceled()) {
                        String cancelError = loadWorkflowRunnable.getLoadingCanceledMessage();
                        openErrorDialogAndCloseEditor(cancelError);
                        throw new OperationCanceledException(cancelError);
                    } else if (loadWorkflowRunnable.getThrowable() != null) {
                        throw new RuntimeException(loadWorkflowRunnable.getThrowable());
                    }
                }
                ProjectWorkflowMap.putWorkflowUI(m_fileResource, m_manager);
            }
            if (oldManager == null) {
                // not null if via doSaveAs
                // in any case register as client (also if the workflow was already loaded by another client
                ProjectWorkflowMap.registerClientTo(m_fileResource, this);
            }
        } catch (InterruptedException ie) {
            LOGGER.fatal("Workflow loading thread interrupted", ie);
        } catch (InvocationTargetException e) {
            LOGGER.fatal("Workflow could not be loaded.", e);
        }
        m_isAutoSaveAllowed = m_parentEditor == null && isEnableAutoSave;
        setupAutoSaveSchedule();
        m_manuallySetToolTip = null;
        updatePartName();
        if (getGraphicalViewer() != null) {
            loadProperties();
            updateTempRemoteWorkflowMessage();
        }
        // update Actions, as now there's everything available
        updateActions();
    } else {
        throw new IllegalArgumentException("Unsupported editor input: " + input.getClass());
    }
}
Also used : IURIEditorInput(org.eclipse.ui.IURIEditorInput) LocalExplorerFileStore(org.knime.workbench.explorer.filesystem.LocalExplorerFileStore) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) WorkflowManagerUI(org.knime.core.ui.node.workflow.WorkflowManagerUI) URI(java.net.URI) ReferencedFile(org.knime.core.internal.ReferencedFile) InvocationTargetException(java.lang.reflect.InvocationTargetException) IWorkbench(org.eclipse.ui.IWorkbench) CoreException(org.eclipse.core.runtime.CoreException) AbstractExplorerFileStore(org.knime.workbench.explorer.filesystem.AbstractExplorerFileStore) IProgressService(org.eclipse.ui.progress.IProgressService) ReferencedFile(org.knime.core.internal.ReferencedFile) File(java.io.File) RemoteWorkflowInput(org.knime.workbench.explorer.RemoteWorkflowInput)

Example 5 with AbstractExplorerFileStore

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

the class WorkflowEditor method saveBackToServer.

private void saveBackToServer() {
    if (m_parentEditor != null) {
        // parent does it if this is a metanode editor
        m_parentEditor.saveBackToServer();
        return;
    }
    assert m_origRemoteLocation != null : "No remote workflow";
    AbstractExplorerFileStore remoteStore = ExplorerFileSystem.INSTANCE.getStore(m_origRemoteLocation);
    AbstractExplorerFileInfo fetchInfo = remoteStore.fetchInfo();
    if (fetchInfo.exists()) {
        if (!fetchInfo.isModifiable()) {
            MessageDialog.openError(getSite().getShell(), "Workflow not writable", "You don't have permissions to overwrite the workflow. Use \"Save As...\" in order to save it to " + "a different location.");
            return;
        }
        boolean snapshotSupported = remoteStore.getContentProvider().supportsSnapshots();
        final AtomicReference<SnapshotPanel> snapshotPanel = new AtomicReference<SnapshotPanel>(null);
        MessageDialog dlg = new MessageDialog(getSite().getShell(), "Overwrite on server?", null, "The workflow\n\n\t" + remoteStore.getMountIDWithFullPath() + "\n\nalready exists on the server. Do you want to overwrite it?\n", MessageDialog.QUESTION, new String[] { IDialogConstants.NO_LABEL, IDialogConstants.YES_LABEL }, 1) {

            /**
             * {@inheritDoc}
             */
            @Override
            protected Control createCustomArea(final Composite parent) {
                if (snapshotSupported) {
                    snapshotPanel.set(new SnapshotPanel(parent, SWT.NONE));
                    snapshotPanel.get().setEnabled(true);
                    return snapshotPanel.get();
                } else {
                    return null;
                }
            }
        };
        int dlgResult = dlg.open();
        if (dlgResult != 1) {
            return;
        }
        if ((snapshotPanel.get() != null) && (snapshotPanel.get().createSnapshot())) {
            try {
                ((RemoteExplorerFileStore) remoteStore).createSnapshot(snapshotPanel.get().getComment());
            } catch (CoreException e) {
                String msg = "Unable to create snapshot before overwriting the workflow:\n" + e.getMessage() + "\n\nUpload was canceled.";
                LOGGER.error("Unable to create snapshot before overwriting the workflow: " + e.getMessage() + " Upload was canceled.", e);
                MessageDialog.openError(getSite().getShell(), "Server Error", msg);
                return;
            }
        }
    } else if (!remoteStore.getParent().fetchInfo().isModifiable()) {
        MessageDialog.openError(getSite().getShell(), "Workflow not writable", "You don't have permissions to write into the workflow's parent folder. Use \"Save As...\" in order to" + " save it to a different location.");
        return;
    }
    // selected a remote location: save + upload
    if (isDirty()) {
        saveTo(m_fileResource, new NullProgressMonitor(), true, null);
    }
    AbstractExplorerFileStore localFS = getFileStore(m_fileResource);
    if ((localFS == null) || !(localFS instanceof LocalExplorerFileStore)) {
        LOGGER.error("Unable to resolve current workflow location. Workflow not uploaded!");
        return;
    }
    try {
        m_workflowCanBeDeleted.acquire();
        remoteStore.getContentProvider().performUploadAsync((LocalExplorerFileStore) localFS, (RemoteExplorerFileStore) remoteStore, /*deleteSource=*/
        false, false, t -> m_workflowCanBeDeleted.release());
    } catch (CoreException | InterruptedException e) {
        String msg = "Failed to upload the workflow to its remote location\n(" + e.getMessage() + ")";
        LOGGER.error(msg, e);
        MessageDialog.openError(Display.getCurrent().getActiveShell(), "Upload failed.", msg);
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) Composite(org.eclipse.swt.widgets.Composite) LocalExplorerFileStore(org.knime.workbench.explorer.filesystem.LocalExplorerFileStore) AtomicReference(java.util.concurrent.atomic.AtomicReference) SnapshotPanel(org.knime.workbench.explorer.view.dialogs.SnapshotPanel) Point(org.eclipse.draw2d.geometry.Point) PrecisionPoint(org.eclipse.draw2d.geometry.PrecisionPoint) CoreException(org.eclipse.core.runtime.CoreException) AbstractExplorerFileStore(org.knime.workbench.explorer.filesystem.AbstractExplorerFileStore) AbstractExplorerFileInfo(org.knime.workbench.explorer.filesystem.AbstractExplorerFileInfo) MessageDialog(org.eclipse.jface.dialogs.MessageDialog) RemoteExplorerFileStore(org.knime.workbench.explorer.filesystem.RemoteExplorerFileStore)

Aggregations

AbstractExplorerFileStore (org.knime.workbench.explorer.filesystem.AbstractExplorerFileStore)19 URI (java.net.URI)6 AbstractContentProvider (org.knime.workbench.explorer.view.AbstractContentProvider)6 CoreException (org.eclipse.core.runtime.CoreException)5 LocalExplorerFileStore (org.knime.workbench.explorer.filesystem.LocalExplorerFileStore)5 ArrayList (java.util.ArrayList)4 Shell (org.eclipse.swt.widgets.Shell)4 NodeID (org.knime.core.node.workflow.NodeID)4 WorkflowManager (org.knime.core.node.workflow.WorkflowManager)4 NodeContainerEditPart (org.knime.workbench.editor2.editparts.NodeContainerEditPart)4 ContentObject (org.knime.workbench.explorer.view.ContentObject)4 File (java.io.File)3 IOException (java.io.IOException)3 ReferencedFile (org.knime.core.internal.ReferencedFile)3 SubNodeContainer (org.knime.core.node.workflow.SubNodeContainer)3 WorkflowContext (org.knime.core.node.workflow.WorkflowContext)3 WorkflowManagerUI (org.knime.core.ui.node.workflow.WorkflowManagerUI)3 SpaceResourceSelectionDialog (org.knime.workbench.explorer.dialogs.SpaceResourceSelectionDialog)3 AbstractExplorerFileInfo (org.knime.workbench.explorer.filesystem.AbstractExplorerFileInfo)3 RemoteExplorerFileStore (org.knime.workbench.explorer.filesystem.RemoteExplorerFileStore)3