Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find/replace overlay: allow pasting in multi-page editors #2509 #2516

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
*******************************************************************************/
package org.eclipse.ui.internal.findandreplace.overlay;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

import org.osgi.framework.FrameworkUtil;
Expand Down Expand Up @@ -46,6 +47,7 @@

import org.eclipse.core.runtime.Status;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.bindings.keys.KeyStroke;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogSettings;
Expand All @@ -60,6 +62,7 @@
import org.eclipse.jface.text.IFindReplaceTarget;
import org.eclipse.jface.text.ITextViewer;

import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.fieldassist.ContentAssistCommandAdapter;
Expand All @@ -70,10 +73,12 @@
import org.eclipse.ui.internal.findandreplace.SearchOptions;
import org.eclipse.ui.internal.findandreplace.status.IFindReplaceStatus;
import org.eclipse.ui.internal.texteditor.TextEditorPlugin;
import org.eclipse.ui.part.MultiPageEditorSite;

import org.eclipse.ui.texteditor.AbstractTextEditor;
import org.eclipse.ui.texteditor.FindReplaceAction;
import org.eclipse.ui.texteditor.IAbstractTextEditorHelpContextIds;
import org.eclipse.ui.texteditor.ITextEditorActionConstants;
import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
import org.eclipse.ui.texteditor.StatusTextEditor;

Expand Down Expand Up @@ -148,6 +153,8 @@ private final class KeyboardShortcuts {
private ContentAssistCommandAdapter contentAssistSearchField, contentAssistReplaceField;

private FocusListener targetActionActivationHandling = new FocusListener() {
private DeactivateGlobalActionHandlers globalActionHandlerDeaction;

@Override
public void focusGained(FocusEvent e) {
setTextEditorActionsActivated(false);
Expand All @@ -167,15 +174,48 @@ private void setTextEditorActionsActivated(boolean state) {
if (!(targetPart instanceof AbstractTextEditor)) {
return;
}
if (targetPart.getSite() instanceof MultiPageEditorSite multiEditorSite) {
if (!state && globalActionHandlerDeaction == null) {
globalActionHandlerDeaction = new DeactivateGlobalActionHandlers(multiEditorSite.getActionBars());
} else if (state && globalActionHandlerDeaction != null) {
globalActionHandlerDeaction.reactivate();
globalActionHandlerDeaction = null;
}
}
try {
Method method = AbstractTextEditor.class.getDeclaredMethod("setActionActivation", boolean.class); //$NON-NLS-1$
method.setAccessible(true);
method.invoke(targetPart, Boolean.valueOf(state));
} catch (IllegalArgumentException | InvocationTargetException | IllegalAccessException | SecurityException | NoSuchMethodException ex) {
} catch (IllegalArgumentException | ReflectiveOperationException ex) {
TextEditorPlugin.getDefault().getLog()
.log(Status.error("cannot (de-)activate actions for text editor", ex)); //$NON-NLS-1$
}
}

static final class DeactivateGlobalActionHandlers {
private final static List<String> ACTIONS = List.of(ITextEditorActionConstants.CUT,
ITextEditorActionConstants.COPY, ITextEditorActionConstants.PASTE,
ITextEditorActionConstants.DELETE, ITextEditorActionConstants.SELECT_ALL,
ITextEditorActionConstants.FIND);

private final Map<String, IAction> deactivatedActions = new HashMap<>();
private final IActionBars actionBars;

public DeactivateGlobalActionHandlers(IActionBars actionBars) {
this.actionBars = actionBars;
for (String actionID : ACTIONS) {
deactivatedActions.putIfAbsent(actionID, actionBars.getGlobalActionHandler(actionID));
actionBars.setGlobalActionHandler(actionID, null);
}
}

public void reactivate() {
for (String actionID : deactivatedActions.keySet()) {
actionBars.setGlobalActionHandler(actionID, deactivatedActions.get(actionID));
}
}
}

};

public FindReplaceOverlay(Shell parent, IWorkbenchPart part, IFindReplaceTarget target) {
Expand Down
Loading