From 7e9c897e0c3dc6eabae68e6f5bcb854f6f728c42 Mon Sep 17 00:00:00 2001 From: Heiko Klare Date: Mon, 11 Nov 2024 16:19:22 +0100 Subject: [PATCH] Find/replace overlay: allow pasting into replace input field #2509 While actions of the target editor are properly deactivated when the find input field of the FindReplaceOverlay has focus, the same does not happen for the replace input field. In consequence, for example, you cannot paste clipboard content into the replace input field via the according keyboard shortcut (CTRL+V). With this change, the functionality to deactivate target editor actions is also applied when the the replace input field has focus, in addition to the find input field. Contributes to https://github.com/eclipse-platform/eclipse.platform.ui/issues/2509 --- .../overlay/FindReplaceOverlay.java | 57 +++++++++++-------- 1 file changed, 33 insertions(+), 24 deletions(-) diff --git a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/overlay/FindReplaceOverlay.java b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/overlay/FindReplaceOverlay.java index 9586c243543..235b584a566 100644 --- a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/overlay/FindReplaceOverlay.java +++ b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/overlay/FindReplaceOverlay.java @@ -147,6 +147,37 @@ private final class KeyboardShortcuts { private ControlDecoration searchBarDecoration; private ContentAssistCommandAdapter contentAssistSearchField, contentAssistReplaceField; + private FocusListener targetActionActivationHandling = new FocusListener() { + @Override + public void focusGained(FocusEvent e) { + setTextEditorActionsActivated(false); + } + + @Override + public void focusLost(FocusEvent e) { + setTextEditorActionsActivated(true); + } + + /* + * Adapted from + * org.eclipse.jdt.internal.ui.javaeditor.JavaEditor#setActionsActivated( + * boolean) + */ + private void setTextEditorActionsActivated(boolean state) { + if (!(targetPart instanceof AbstractTextEditor)) { + return; + } + 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) { + TextEditorPlugin.getDefault().getLog() + .log(Status.error("cannot (de-)activate actions for text editor", ex)); //$NON-NLS-1$ + } + } + }; + public FindReplaceOverlay(Shell parent, IWorkbenchPart part, IFindReplaceTarget target) { targetPart = part; targetControl = getTargetControl(parent, part); @@ -561,39 +592,16 @@ private void createSearchBar() { updateIncrementalSearch(); }); searchBar.addFocusListener(new FocusListener() { - @Override public void focusGained(FocusEvent e) { findReplaceLogic.resetIncrementalBaseLocation(); - setTextEditorActionsActivated(false); } - @Override public void focusLost(FocusEvent e) { showUserFeedback(normalTextForegroundColor, false); - setTextEditorActionsActivated(true); } - - /* - * Adapted from - * org.eclipse.jdt.internal.ui.javaeditor.JavaEditor#setActionsActivated( - * boolean) - */ - private void setTextEditorActionsActivated(boolean state) { - if (!(targetPart instanceof AbstractTextEditor)) { - return; - } - 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) { - TextEditorPlugin.getDefault().getLog() - .log(Status.error("cannot (de-)activate actions for text editor", ex)); //$NON-NLS-1$ - } - } - }); + searchBar.addFocusListener(targetActionActivationHandling); searchBar.setMessage(FindReplaceMessages.FindReplaceOverlay_searchBar_message); contentAssistSearchField = createContentAssistField(searchBar, true); searchBar.addModifyListener(Event -> { @@ -618,6 +626,7 @@ private void createReplaceBar() { replaceBar.addModifyListener(e -> { findReplaceLogic.setReplaceString(replaceBar.getText()); }); + replaceBar.addFocusListener(targetActionActivationHandling); replaceBar.addFocusListener(FocusListener.focusLostAdapter(e -> { replaceBar.setForeground(normalTextForegroundColor); searchBar.setForeground(normalTextForegroundColor);