Search in sources :

Example 1 with OverwriteAndMergeInfo

use of org.knime.workbench.explorer.view.dialogs.OverwriteAndMergeInfo 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 2 with OverwriteAndMergeInfo

use of org.knime.workbench.explorer.view.dialogs.OverwriteAndMergeInfo in project knime-core by knime.

the class WorkflowEditor method getNewLocation.

/**
 * For SaveAs...
 * @param currentLocation
 * @param allowRemoteLocation local and remote mount points are added to the selection dialog
 * @return new (different!) URI or null if user canceled. Caller should create a snapshot if told so.
 */
private OverwriteAndMergeInfo getNewLocation(final URI currentLocation, final boolean allowRemoteLocation) {
    final AbstractExplorerFileStore currentStore = getFileStore(currentLocation);
    AbstractExplorerFileStore currentParent = null;
    if (currentStore != null) {
        currentParent = currentStore.getParent();
    }
    String currentName = new Path(currentLocation.getPath()).lastSegment();
    List<String> selIDs = new LinkedList<String>();
    for (String id : ExplorerMountTable.getAllVisibleMountIDs()) {
        AbstractContentProvider provider = ExplorerMountTable.getMountPoint(id).getProvider();
        if (!provider.isRemote() || (allowRemoteLocation && provider.isWritable())) {
            selIDs.add(id);
        }
    }
    ContentObject preSel = ContentObject.forFile(currentParent);
    if (isTempRemoteWorkflowEditor()) {
        AbstractExplorerFileStore remoteStore = null;
        try {
            remoteStore = ExplorerFileSystem.INSTANCE.getStore(m_origRemoteLocation);
        } catch (IllegalArgumentException e) {
        /* don't preselect on unknown original location */
        }
        if (remoteStore != null) {
            preSel = ContentObject.forFile(remoteStore);
        } else {
            preSel = null;
        }
    }
    OverwriteAndMergeInfo result = null;
    while (result == null) {
        // keep the selection dialog open until we get a useful result
        final SpaceResourceSelectionDialog dialog = new SpaceResourceSelectionDialog(getSite().getShell(), selIDs.toArray(new String[selIDs.size()]), preSel);
        SaveAsValidator validator = new SaveAsValidator(dialog, currentStore);
        String defName = currentName + " - Copy";
        if (!isTempRemoteWorkflowEditor()) {
            if (currentParent != null) {
                try {
                    Set<String> childs = new HashSet<String>(Arrays.asList(currentParent.childNames(EFS.NONE, null)));
                    defName = guessNewWorkflowNameOnSaveAs(childs, currentName);
                } catch (CoreException e1) {
                // keep the simple default
                }
            }
        } else {
            defName = currentName;
            if (defName.endsWith("." + KNIMEConstants.KNIME_WORKFLOW_FILE_EXTENSION)) {
                defName = defName.substring(0, defName.length() - KNIMEConstants.KNIME_WORKFLOW_FILE_EXTENSION.length() - 1);
            }
        }
        dialog.setTitle("Save to new Location");
        dialog.setDescription("Select the new destination workflow group for the workflow.");
        dialog.setValidator(validator);
        // Setup the name field of the dialog
        dialog.setNameFieldEnabled(true);
        dialog.setNameFieldDefaultValue(defName);
        final AtomicBoolean proceed = new AtomicBoolean(false);
        Display.getDefault().syncExec(new Runnable() {

            @Override
            public void run() {
                proceed.set(dialog.open() == Window.OK);
            }
        });
        if (!proceed.get()) {
            return null;
        }
        AbstractExplorerFileStore newLocation = dialog.getSelection();
        if (newLocation.fetchInfo().isWorkflowGroup()) {
            newLocation = newLocation.getChild(dialog.getNameFieldValue());
        } else {
            // in case they have selected a flow but changed the name in the name field afterwards
            newLocation = newLocation.getParent().getChild(dialog.getNameFieldValue());
        }
        assert !newLocation.fetchInfo().exists() || newLocation.fetchInfo().isWorkflow();
        if (newLocation.fetchInfo().exists()) {
            // confirm overwrite (with snapshot?)
            final AtomicBoolean snapshotSupported = new AtomicBoolean(false);
            final AtomicReference<SnapshotPanel> snapshotPanel = new AtomicReference<SnapshotPanel>(null);
            if (newLocation.getContentProvider().supportsSnapshots() && (newLocation instanceof RemoteExplorerFileStore)) {
                snapshotSupported.set(true);
            }
            MessageDialog dlg = new MessageDialog(getSite().getShell(), "Confirm SaveAs Overwrite", null, "The selected destination\n\n\t" + newLocation.getMountIDWithFullPath() + "\n\nalready exists. Do you want to overwrite?\n", MessageDialog.QUESTION, new String[] { IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL, IDialogConstants.CANCEL_LABEL }, 1) {

                /**
                 * {@inheritDoc}
                 */
                @Override
                protected Control createCustomArea(final Composite parent) {
                    if (snapshotSupported.get()) {
                        snapshotPanel.set(new SnapshotPanel(parent, SWT.NONE));
                        snapshotPanel.get().setEnabled(true);
                        return snapshotPanel.get();
                    } else {
                        return null;
                    }
                }
            };
            int dlgResult = dlg.open();
            if (dlgResult == 2) /* CANCEL */
            {
                return null;
            }
            if (dlgResult == 0) {
                /* YES (= please overwrite) */
                if (snapshotPanel.get() != null) {
                    SnapshotPanel snapPanel = snapshotPanel.get();
                    result = new OverwriteAndMergeInfo(newLocation.toURI().toASCIIString(), false, true, snapPanel.createSnapshot(), snapPanel.getComment());
                } else {
                    result = new OverwriteAndMergeInfo(newLocation.toURI().toASCIIString(), false, true, false, "");
                }
            } else {
                /* NO, don't overwrite: continue while loop asking for a different location */
                preSel = ContentObject.forFile(newLocation);
                currentName = newLocation.getName();
            }
        } else {
            result = new OverwriteAndMergeInfo(newLocation.toURI().toASCIIString(), false, false, false, "");
        }
    }
    /* end of while (result != null) keep the target selection dialog open */
    return result;
}
Also used : Path(org.eclipse.core.runtime.Path) SaveAsValidator(org.knime.workbench.explorer.dialogs.SaveAsValidator) Composite(org.eclipse.swt.widgets.Composite) AtomicReference(java.util.concurrent.atomic.AtomicReference) SnapshotPanel(org.knime.workbench.explorer.view.dialogs.SnapshotPanel) LinkedList(java.util.LinkedList) ContentObject(org.knime.workbench.explorer.view.ContentObject) Point(org.eclipse.draw2d.geometry.Point) PrecisionPoint(org.eclipse.draw2d.geometry.PrecisionPoint) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) OverwriteAndMergeInfo(org.knime.workbench.explorer.view.dialogs.OverwriteAndMergeInfo) SpaceResourceSelectionDialog(org.knime.workbench.explorer.dialogs.SpaceResourceSelectionDialog) CoreException(org.eclipse.core.runtime.CoreException) AbstractExplorerFileStore(org.knime.workbench.explorer.filesystem.AbstractExplorerFileStore) AbstractContentProvider(org.knime.workbench.explorer.view.AbstractContentProvider) RemoteExplorerFileStore(org.knime.workbench.explorer.filesystem.RemoteExplorerFileStore) MessageDialog(org.eclipse.jface.dialogs.MessageDialog) HashSet(java.util.HashSet)

Aggregations

CoreException (org.eclipse.core.runtime.CoreException)2 AbstractExplorerFileStore (org.knime.workbench.explorer.filesystem.AbstractExplorerFileStore)2 RemoteExplorerFileStore (org.knime.workbench.explorer.filesystem.RemoteExplorerFileStore)2 OverwriteAndMergeInfo (org.knime.workbench.explorer.view.dialogs.OverwriteAndMergeInfo)2 File (java.io.File)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 Path (org.eclipse.core.runtime.Path)1 Point (org.eclipse.draw2d.geometry.Point)1 PrecisionPoint (org.eclipse.draw2d.geometry.PrecisionPoint)1 MessageDialog (org.eclipse.jface.dialogs.MessageDialog)1 Composite (org.eclipse.swt.widgets.Composite)1 Display (org.eclipse.swt.widgets.Display)1 Shell (org.eclipse.swt.widgets.Shell)1 FileStoreEditorInput (org.eclipse.ui.ide.FileStoreEditorInput)1