Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Wolf2020-02-12 17:46:55 +0000
committerMichael Keppler2020-02-23 19:10:11 +0000
commit6617b62bdb4112a7e7d9500302df4c574373f62d (patch)
tree409a150c0bd7e7faa4defb8cf5d20fb1a0e14f28
parent73ed65ee65d3da284d12c25badb2887e17d6aa3f (diff)
downloadegit-master.tar.gz
egit-master.tar.xz
egit-master.zip
[diff editor] Enable "Save As..." on unified diffsHEADmaster
Contrary to the normal "Save As..." implementation, do _not_ reload the editor with the newly created FileEditorInput. Just save the document to a file; leave opening the file in an editor to the user. To make the "Save As..." handler update its enablement properly in all cases when the nested editor pages switch force a re-evaluation of the active part context variable on page changes. Change-Id: Ia3e8bc1cd81e40fbf7511a32fee1967104b67098 Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
-rw-r--r--org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/UIText.java3
-rw-r--r--org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/commit/CommitEditor.java28
-rw-r--r--org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/commit/DiffEditor.java68
-rw-r--r--org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/uitext.properties1
4 files changed, 87 insertions, 13 deletions
diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/UIText.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/UIText.java
index c12dc7c27..45edd1c69 100644
--- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/UIText.java
+++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/UIText.java
@@ -3953,6 +3953,9 @@ public class UIText extends NLS {
public static String DialogsPreferencePage_ShowCloneFailedDialog;
/** */
+ public static String DiffEditor_SaveError;
+
+ /** */
public static String DiffEditor_TaskGeneratingDiff;
/** */
diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/commit/CommitEditor.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/commit/CommitEditor.java
index da8f56502..c7df97ef5 100644
--- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/commit/CommitEditor.java
+++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/commit/CommitEditor.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2011, 2016 GitHub Inc. and others.
+ * Copyright (c) 2011, 2020 GitHub Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
@@ -34,6 +34,7 @@ import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.ToolBarManager;
+import org.eclipse.jface.dialogs.IPageChangedListener;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.viewers.ISelection;
@@ -57,6 +58,7 @@ import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IPartListener;
import org.eclipse.ui.IPartService;
import org.eclipse.ui.ISharedImages;
+import org.eclipse.ui.ISources;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
@@ -76,6 +78,7 @@ import org.eclipse.ui.part.IShowInTargetList;
import org.eclipse.ui.part.MultiPageEditorSite;
import org.eclipse.ui.part.ShowInContext;
import org.eclipse.ui.progress.UIJob;
+import org.eclipse.ui.services.IEvaluationService;
import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
/**
@@ -160,6 +163,8 @@ public class CommitEditor extends SharedHeaderFormEditor implements
private IToolBarManager toolbar;
+ private IPageChangedListener pageListener;
+
/** Ensures that the toolbar buttons in the header are properly updated. */
private final IPartListener activationListener = new IPartListener() {
@@ -252,6 +257,15 @@ public class CommitEditor extends SharedHeaderFormEditor implements
refListenerHandle = org.eclipse.egit.core.Activator.getDefault()
.getRepositoryCache().getGlobalListenerList()
.addRefsChangedListener(this);
+ pageListener = event -> {
+ IEvaluationService service = PlatformUI.getWorkbench()
+ .getService(IEvaluationService.class);
+ if (service != null) {
+ // Update enablement of "Save As..."
+ service.requestEvaluation(ISources.ACTIVE_PART_NAME);
+ }
+ };
+ addPageChangedListener(pageListener);
}
private IContributionItem createActionContributionItem(String commandId,
@@ -489,6 +503,10 @@ public class CommitEditor extends SharedHeaderFormEditor implements
@Override
public void dispose() {
+ if (pageListener != null) {
+ removePageChangedListener(pageListener);
+ pageListener = null;
+ }
CommonUtils.getService(getSite(), IPartService.class)
.removePartListener(activationListener);
refListenerHandle.remove();
@@ -509,7 +527,10 @@ public class CommitEditor extends SharedHeaderFormEditor implements
*/
@Override
public void doSaveAs() {
- // Save as not supported
+ IEditorPart editor = getActiveEditor();
+ if (editor != null && editor.isSaveAsAllowed()) {
+ editor.doSaveAs();
+ }
}
/**
@@ -517,7 +538,8 @@ public class CommitEditor extends SharedHeaderFormEditor implements
*/
@Override
public boolean isSaveAsAllowed() {
- return false;
+ IEditorPart editor = getActiveEditor();
+ return editor != null && editor.isSaveAsAllowed();
}
@Override
diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/commit/DiffEditor.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/commit/DiffEditor.java
index ca1bbdbfe..7b2f0a151 100644
--- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/commit/DiffEditor.java
+++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/commit/DiffEditor.java
@@ -18,14 +18,19 @@ import static org.eclipse.egit.ui.UIPreferences.THEME_DiffAddBackgroundColor;
import static org.eclipse.egit.ui.UIPreferences.THEME_DiffRemoveBackgroundColor;
import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IWorkspace;
+import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Adapters;
import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
@@ -78,17 +83,22 @@ import org.eclipse.jgit.treewalk.filter.TreeFilter;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Shell;
import org.eclipse.team.ui.history.IHistoryView;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.actions.WorkspaceModifyOperation;
+import org.eclipse.ui.dialogs.SaveAsDialog;
import org.eclipse.ui.editors.text.EditorsUI;
+import org.eclipse.ui.editors.text.FileDocumentProvider;
import org.eclipse.ui.editors.text.TextEditor;
+import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.ui.part.IShowInSource;
import org.eclipse.ui.part.IShowInTargetList;
import org.eclipse.ui.part.ShowInContext;
+import org.eclipse.ui.progress.IProgressService;
import org.eclipse.ui.progress.UIJob;
import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
-import org.eclipse.ui.texteditor.AbstractDocumentProvider;
import org.eclipse.ui.texteditor.ChainedPreferenceStore;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.ITextEditorActionConstants;
@@ -195,7 +205,49 @@ public class DiffEditor extends TextEditor
@Override
public boolean isSaveAsAllowed() {
- return false;
+ IDocumentProvider provider = getDocumentProvider();
+ return provider != null && provider
+ .getDocument(getEditorInput()) instanceof DiffDocument;
+ }
+
+ @Override
+ protected void performSaveAs(IProgressMonitor progressMonitor) {
+ Shell shell = getSite().getShell();
+ SaveAsDialog dialog = new SaveAsDialog(shell);
+ dialog.open();
+ IPath path = dialog.getResult();
+
+ if (path == null) {
+ if (progressMonitor != null) {
+ progressMonitor.setCanceled(true);
+ }
+ return;
+ }
+
+ IWorkspace workspace = ResourcesPlugin.getWorkspace();
+ IFile file = workspace.getRoot().getFile(path);
+ IEditorInput newInput = new FileEditorInput(file);
+
+ WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
+ @Override
+ public void execute(final IProgressMonitor monitor)
+ throws CoreException {
+ IDocumentProvider provider = getDocumentProvider();
+ if (provider != null) {
+ provider.saveDocument(monitor, newInput,
+ provider.getDocument(getEditorInput()), true);
+ }
+ }
+ };
+
+ try {
+ getSite().getService(IProgressService.class).busyCursorWhile(op);
+ } catch (InterruptedException e) {
+ // Cancelled.
+ } catch (InvocationTargetException e) {
+ Activator.handleError(UIText.DiffEditor_SaveError, e.getCause(),
+ true);
+ }
}
@Override
@@ -615,9 +667,11 @@ public class DiffEditor extends TextEditor
}
/**
- * A document provider that knows about {@link DiffEditorInput}.
+ * A document provider that knows about {@link DiffEditorInput}. Derived
+ * from {@link FileDocumentProvider} to get the default save implementation
+ * needed for "Save As...".
*/
- private static class DiffDocumentProvider extends AbstractDocumentProvider {
+ private static class DiffDocumentProvider extends FileDocumentProvider {
@Override
public IStatus getStatus(Object element) {
@@ -657,12 +711,6 @@ public class DiffEditor extends TextEditor
}
@Override
- protected void doSaveDocument(IProgressMonitor monitor, Object element,
- IDocument document, boolean overwrite) throws CoreException {
- // Cannot save
- }
-
- @Override
protected IRunnableContext getOperationRunner(
IProgressMonitor monitor) {
return null;
diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/uitext.properties b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/uitext.properties
index 5bd09616b..badc3ca88 100644
--- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/uitext.properties
+++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/uitext.properties
@@ -1396,6 +1396,7 @@ DialogsPreferencePage_RunningLaunchOnCheckout=Warn about running &launches when
DialogsPreferencePage_ShowInitialConfigCheckbox=&Initial configuration
DialogsPreferencePage_ShowCheckoutConfirmation=Chec&kout confirmation
DialogsPreferencePage_ShowCloneFailedDialog=Clo&ne failed error
+DiffEditor_SaveError=Could not save the diff
DiffEditor_TaskGeneratingDiff=Generating diff
DiffEditor_TaskUpdatingViewer=Updating diff viewer
DiffEditor_Title=Diff

Back to the top