From 5eccf5652a96e42187fa332611479371638be072 Mon Sep 17 00:00:00 2001 From: Heiko Klare Date: Thu, 9 Jul 2026 23:29:26 +0200 Subject: [PATCH] Find/Replace overlay: use handler enablement for button enabled state The enablement of the buttons for search options of the find/replace overlay are currently derived from the availability of the underlying search option. However, there are actions attached to the buttons (changing activation of the search option) and the button enablement should actually reflect if that action is available or not. Though these are effectively equal, mapping the button enablement to the action availability is the conceptually more correct representation. With this change, FindReplaceOverlayAction extends AbstractHandler, making the action itself a handler and providing and enablement state. Enablement of the buttons for the search options is not kept in sync with enablement of the action/handler via an according handler listener. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../overlay/AccessibleToolItem.java | 13 ++++++++++++- .../overlay/AccessibleToolItemBuilder.java | 8 ++------ .../overlay/FindReplaceOverlayAction.java | 15 ++++++++++++++- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/overlay/AccessibleToolItem.java b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/overlay/AccessibleToolItem.java index 864b828a9da..e35a3509ccc 100644 --- a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/overlay/AccessibleToolItem.java +++ b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/overlay/AccessibleToolItem.java @@ -18,6 +18,9 @@ import org.eclipse.swt.widgets.ToolBar; import org.eclipse.swt.widgets.ToolItem; +import org.eclipse.core.commands.HandlerEvent; +import org.eclipse.core.commands.IHandlerListener; + import org.eclipse.jface.layout.GridDataFactory; class AccessibleToolItem { @@ -25,7 +28,6 @@ class AccessibleToolItem { private String baseToolTipText; - AccessibleToolItem(Composite parent, int styleBits) { ToolBar toolbar = new ToolBar(parent, SWT.FLAT | SWT.HORIZONTAL); GridDataFactory.fillDefaults().grab(true, true).align(SWT.CENTER, SWT.CENTER).applyTo(toolbar); @@ -51,6 +53,15 @@ void setToolTipText(String text) { void setAction(FindReplaceOverlayAction action) { setToolTipText(toolItem.getToolTipText()); + toolItem.setEnabled(action.isEnabled()); + action.addHandlerListener(new IHandlerListener() { + @Override + public void handlerChanged(HandlerEvent event) { + if (event.isEnabledChanged() && !toolItem.isDisposed()) { + toolItem.setEnabled(action.isEnabled()); + } + } + }); toolItem.addSelectionListener(SelectionListener.widgetSelectedAdapter(__ -> action.execute())); action.addShortcutHintListener(hint -> { if (!toolItem.isDisposed()) { diff --git a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/overlay/AccessibleToolItemBuilder.java b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/overlay/AccessibleToolItemBuilder.java index 186fe5eb7c0..bb1cadd4a68 100644 --- a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/overlay/AccessibleToolItemBuilder.java +++ b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/overlay/AccessibleToolItemBuilder.java @@ -104,12 +104,8 @@ public ToolItem build() { toolItem.setSelection(invertSearchOption ? !state : state); } }); - toolItem.setEnabled(findReplaceLogic.isAvailable(searchOption)); - findReplaceLogic.addSearchOptionAvailabilityChangedListener(searchOption, state -> { - if (!toolItem.isDisposed()) { - toolItem.setEnabled(state); - } - }); + searchOptionAction.setAvailable(findReplaceLogic.isAvailable(searchOption)); + findReplaceLogic.addSearchOptionAvailabilityChangedListener(searchOption, searchOptionAction::setAvailable); return toolItem; } } diff --git a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/overlay/FindReplaceOverlayAction.java b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/overlay/FindReplaceOverlayAction.java index 9440853f829..25d9b74d045 100644 --- a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/overlay/FindReplaceOverlayAction.java +++ b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/overlay/FindReplaceOverlayAction.java @@ -15,9 +15,12 @@ import java.util.List; import java.util.function.Consumer; +import org.eclipse.core.commands.AbstractHandler; +import org.eclipse.core.commands.ExecutionEvent; + import org.eclipse.jface.bindings.keys.KeyStroke; -class FindReplaceOverlayAction { +class FindReplaceOverlayAction extends AbstractHandler { private final Runnable operation; private final List executionListeners = new ArrayList<>(); @@ -34,11 +37,21 @@ void addShortcuts(List shortcutsToAdd) { this.shortcuts.addAll(shortcutsToAdd); } + @Override + public Object execute(ExecutionEvent event) { + execute(); + return null; + } + void execute() { operation.run(); notifyExecutionListeners(); } + void setAvailable(boolean available) { + setBaseEnabled(available); + } + List getShortcuts() { return Collections.unmodifiableList(shortcuts); }