Skip to content

Commit

Permalink
Find/replace overlay: allow pasting into replace input field eclipse-…
Browse files Browse the repository at this point in the history
…platform#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
eclipse-platform#2509
  • Loading branch information
HeikoKlare committed Nov 12, 2024
1 parent 7ae2005 commit 7e9c897
Showing 1 changed file with 33 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 -> {
Expand All @@ -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);
Expand Down

0 comments on commit 7e9c897

Please sign in to comment.