IDEA-144339 diff: fix undo action for diff dialogs with a single editable side

This commit is contained in:
Aleksey Pivovarov
2015-09-29 18:09:48 +03:00
parent 38217a3e36
commit e7ecff31d3
3 changed files with 75 additions and 43 deletions
@@ -0,0 +1,66 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.diff.actions;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.EmptyAction;
import com.intellij.openapi.actionSystem.IdeActions;
import com.intellij.openapi.command.undo.UndoManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.TextEditor;
import com.intellij.openapi.fileEditor.impl.text.TextEditorProvider;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
public class ProxyUndoRedoAction extends DumbAwareAction {
@NotNull private final UndoManager myUndoManager;
@NotNull private final TextEditor myEditor;
private final boolean myUndo;
private ProxyUndoRedoAction(@NotNull UndoManager manager, @NotNull TextEditor editor, boolean undo) {
myUndoManager = manager;
myEditor = editor;
myUndo = undo;
}
public static void register(@Nullable Project project, @NotNull Editor editor, @NotNull JComponent component) {
UndoManager undoManager = project != null ? UndoManager.getInstance(project) : UndoManager.getGlobalInstance();
TextEditor textEditor = TextEditorProvider.getInstance().getTextEditor(editor);
if (undoManager != null) {
EmptyAction.setupAction(new ProxyUndoRedoAction(undoManager, textEditor, true), IdeActions.ACTION_UNDO, component);
EmptyAction.setupAction(new ProxyUndoRedoAction(undoManager, textEditor, false), IdeActions.ACTION_REDO, component);
}
}
@Override
public void update(AnActionEvent e) {
e.getPresentation().setEnabled(myUndo ? myUndoManager.isUndoAvailable(myEditor) : myUndoManager.isRedoAvailable(myEditor));
}
@Override
public void actionPerformed(AnActionEvent e) {
if (myUndo) {
myUndoManager.undo(myEditor);
}
else {
myUndoManager.redo(myEditor);
}
}
}
@@ -17,6 +17,7 @@ package com.intellij.diff.merge;
import com.intellij.diff.DiffContext;
import com.intellij.diff.FrameDiffTool;
import com.intellij.diff.actions.ProxyUndoRedoAction;
import com.intellij.diff.comparison.ByLine;
import com.intellij.diff.comparison.ComparisonMergeUtil;
import com.intellij.diff.comparison.ComparisonPolicy;
@@ -44,8 +45,6 @@ import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.event.DocumentEvent;
import com.intellij.openapi.editor.ex.EditorEx;
import com.intellij.openapi.fileEditor.TextEditor;
import com.intellij.openapi.fileEditor.impl.text.TextEditorProvider;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
@@ -225,10 +224,7 @@ public class TextMergeTool implements MergeTool {
DiffUtil.registerAction(new IgnoreSelectedChangesAction(Side.LEFT, true), myPanel);
DiffUtil.registerAction(new IgnoreSelectedChangesAction(Side.RIGHT, true), myPanel);
if (myUndoManager != null) {
new UndoRedoAction(true).register();
new UndoRedoAction(false).register();
}
ProxyUndoRedoAction.register(getProject(), getEditor(ThreeSide.BASE), myContentPanel);
}
@Override
@@ -1161,43 +1157,6 @@ public class TextMergeTool implements MergeTool {
// Helpers
//
private class UndoRedoAction extends DumbAwareAction {
private final boolean myUndo;
public UndoRedoAction(boolean undo) {
myUndo = undo;
}
public void register() {
EmptyAction.setupAction(this, myUndo ? IdeActions.ACTION_UNDO : IdeActions.ACTION_REDO, myContentPanel);
}
@Override
public void update(AnActionEvent e) {
assert myUndoManager != null;
TextEditor textEditor = getTextEditor();
e.getPresentation().setEnabled(myUndo ? myUndoManager.isUndoAvailable(textEditor) : myUndoManager.isRedoAvailable(textEditor));
}
@Override
public void actionPerformed(AnActionEvent e) {
assert myUndoManager != null;
TextEditor textEditor = getTextEditor();
if (myUndo) {
myUndoManager.undo(textEditor);
}
else {
myUndoManager.redo(textEditor);
}
}
@NotNull
private TextEditor getTextEditor() {
EditorEx editor = getEditor(ThreeSide.BASE);
return TextEditorProvider.getInstance().getTextEditor(editor);
}
}
private class MyDividerPaintable implements DiffDividerDrawUtil.DividerPaintable {
@NotNull private final Side mySide;
@@ -16,6 +16,7 @@
package com.intellij.diff.tools.util.side;
import com.intellij.diff.DiffContext;
import com.intellij.diff.actions.ProxyUndoRedoAction;
import com.intellij.diff.actions.impl.FocusOppositePaneAction;
import com.intellij.diff.actions.impl.OpenInEditorWithMouseAction;
import com.intellij.diff.actions.impl.SetEditorSettingsAction;
@@ -80,6 +81,12 @@ public abstract class TwosideTextDiffViewer extends TwosideDiffViewer<TextEditor
myEditableEditors = TextDiffViewerUtil.getEditableEditors(getEditors());
TextDiffViewerUtil.checkDifferentDocuments(myRequest);
boolean editable1 = DiffUtil.canMakeWritable(getContent1().getDocument());
boolean editable2 = DiffUtil.canMakeWritable(getContent2().getDocument());
if (editable1 ^ editable2) {
ProxyUndoRedoAction.register(getProject(), editable1 ? getEditor1() : getEditor2(), myPanel);
}
}
@Override