Skip to content

Commit

Permalink
Replace text/tooltip/... with explicit ID matching
Browse files Browse the repository at this point in the history
  • Loading branch information
HeikoKlare committed Sep 28, 2024
1 parent 1d95851 commit 846956c
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ private final class KeyboardShortcuts {
KeyStroke.getInstance(SWT.MOD1, 'R'), KeyStroke.getInstance(SWT.MOD1, 'r'));
}

public static final String ID_DATA_KEY = "org.eclipse.ui.internal.findreplace.overlay.FindReplaceOverlay.id"; //$NON-NLS-1$

private static final String REPLACE_BAR_OPEN_DIALOG_SETTING = "replaceBarOpen"; //$NON-NLS-1$
private static final double WORST_CASE_RATIO_EDITOR_TO_OVERLAY = 0.95;
private static final double BIG_WIDTH_RATIO_EDITOR_TO_OVERLAY = 0.7;
Expand All @@ -117,7 +119,7 @@ private final class KeyboardShortcuts {
private boolean replaceBarOpen;

private Composite container;
private Button replaceToggle;
private Button replaceBarToggle;
private FindReplaceOverlayAction replaceToggleShortcut;

private Composite contentGroup;
Expand All @@ -130,9 +132,9 @@ private final class KeyboardShortcuts {
private ToolItem wholeWordSearchButton;
private ToolItem caseSensitiveSearchButton;
private ToolItem regexSearchButton;
private ToolItem searchUpButton;
private ToolItem searchDownButton;
private ToolItem searchAllButton;
private ToolItem searchBackwardButton;
private ToolItem searchForwardButton;
private ToolItem selectAllButton;
private AccessibleToolBar closeTools;
private ToolItem closeButton;

Expand Down Expand Up @@ -370,6 +372,7 @@ public int open() {
}
overlayOpen = true;
applyOverlayColors(backgroundToUse, true);
assignIDs();
updateFromTargetSelection();
searchBar.forceFocus();

Expand All @@ -391,6 +394,25 @@ private void restoreOverlaySettings() {
}
}

@SuppressWarnings("nls")
private void assignIDs() {
replaceBarToggle.setData(ID_DATA_KEY, "replaceBarToggle");
searchBar.setData(ID_DATA_KEY, "searchInput");
searchBackwardButton.setData(ID_DATA_KEY, "searchBackward");
searchForwardButton.setData(ID_DATA_KEY, "searchForward");
selectAllButton.setData(ID_DATA_KEY, "selectAll");
searchInSelectionButton.setData(ID_DATA_KEY, "searchInSelection");
wholeWordSearchButton.setData(ID_DATA_KEY, "wholeWordSearch");
regexSearchButton.setData(ID_DATA_KEY, "regExSearch");
caseSensitiveSearchButton.setData(ID_DATA_KEY, "caseSensitiveSearch");

if (replaceBarOpen) {
replaceBar.setData(ID_DATA_KEY, "replaceInput");
replaceButton.setData(ID_DATA_KEY, "replaceOne");
replaceAllButton.setData(ID_DATA_KEY, "replaceAll");
}
}

private void applyOverlayColors(Color color, boolean tryToColorReplaceBar) {
closeTools.setBackground(color);
closeButton.setBackground(color);
Expand All @@ -400,9 +422,9 @@ private void applyOverlayColors(Color color, boolean tryToColorReplaceBar) {
wholeWordSearchButton.setBackground(color);
regexSearchButton.setBackground(color);
caseSensitiveSearchButton.setBackground(color);
searchAllButton.setBackground(color);
searchUpButton.setBackground(color);
searchDownButton.setBackground(color);
selectAllButton.setBackground(color);
searchBackwardButton.setBackground(color);
searchForwardButton.setBackground(color);

searchBarContainer.setBackground(color);
searchBar.setBackground(color);
Expand Down Expand Up @@ -511,20 +533,20 @@ private void createSearchTools() {

searchTools.createToolItem(SWT.SEPARATOR);

searchUpButton = new AccessibleToolItemBuilder(searchTools).withStyleBits(SWT.PUSH)
searchBackwardButton = new AccessibleToolItemBuilder(searchTools).withStyleBits(SWT.PUSH)
.withImage(FindReplaceOverlayImages.get(FindReplaceOverlayImages.KEY_FIND_PREV))
.withToolTipText(FindReplaceMessages.FindReplaceOverlay_upSearchButton_toolTip)
.withOperation(() -> performSearch(false))
.withShortcuts(KeyboardShortcuts.SEARCH_BACKWARD).build();

searchDownButton = new AccessibleToolItemBuilder(searchTools).withStyleBits(SWT.PUSH)
searchForwardButton = new AccessibleToolItemBuilder(searchTools).withStyleBits(SWT.PUSH)
.withImage(FindReplaceOverlayImages.get(FindReplaceOverlayImages.KEY_FIND_NEXT))
.withToolTipText(FindReplaceMessages.FindReplaceOverlay_downSearchButton_toolTip)
.withOperation(() -> performSearch(true))
.withShortcuts(KeyboardShortcuts.SEARCH_FORWARD).build();
searchDownButton.setSelection(true); // by default, search down
searchForwardButton.setSelection(true); // by default, search down

searchAllButton = new AccessibleToolItemBuilder(searchTools).withStyleBits(SWT.PUSH)
selectAllButton = new AccessibleToolItemBuilder(searchTools).withStyleBits(SWT.PUSH)
.withImage(FindReplaceOverlayImages.get(FindReplaceOverlayImages.KEY_SEARCH_ALL))
.withToolTipText(FindReplaceMessages.FindReplaceOverlay_searchAllButton_toolTip)
.withOperation(this::performSelectAll).withShortcuts(KeyboardShortcuts.SEARCH_ALL).build();
Expand Down Expand Up @@ -714,24 +736,24 @@ private void createMainContainer(final Composite parent) {
private void createReplaceToggle() {
replaceToggleShortcut = new FindReplaceOverlayAction(this::toggleReplace);
replaceToggleShortcut.addShortcuts(KeyboardShortcuts.TOGGLE_REPLACE);
replaceToggle = new Button(container, SWT.FLAT | SWT.PUSH);
replaceBarToggle = new Button(container, SWT.FLAT | SWT.PUSH);
GridDataFactory.fillDefaults().grab(false, true).align(GridData.BEGINNING, GridData.FILL)
.applyTo(replaceToggle);
replaceToggle.setToolTipText(replaceToggleShortcut
.applyTo(replaceBarToggle);
replaceBarToggle.setToolTipText(replaceToggleShortcut
.addShortcutHintToTooltipText(FindReplaceMessages.FindReplaceOverlay_replaceToggle_toolTip));
replaceToggle.setImage(FindReplaceOverlayImages.get(FindReplaceOverlayImages.KEY_OPEN_REPLACE_AREA));
replaceToggle.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> toggleReplace()));
replaceBarToggle.setImage(FindReplaceOverlayImages.get(FindReplaceOverlayImages.KEY_OPEN_REPLACE_AREA));
replaceBarToggle.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> toggleReplace()));
}

private void toggleReplace() {
if (!replaceBarOpen && findReplaceLogic.getTarget().isEditable()) {
createReplaceDialog();
replaceToggle.setImage(FindReplaceOverlayImages.get(FindReplaceOverlayImages.KEY_CLOSE_REPLACE_AREA));
replaceBarToggle.setImage(FindReplaceOverlayImages.get(FindReplaceOverlayImages.KEY_CLOSE_REPLACE_AREA));
} else {
hideReplace();
replaceToggle.setImage(FindReplaceOverlayImages.get(FindReplaceOverlayImages.KEY_OPEN_REPLACE_AREA));
replaceBarToggle.setImage(FindReplaceOverlayImages.get(FindReplaceOverlayImages.KEY_OPEN_REPLACE_AREA));
}
replaceToggle.setSelection(false); // We don't want the button to look "locked in", so don't
replaceBarToggle.setSelection(false); // We don't want the button to look "locked in", so don't
// use it's selectionState
updateContentAssistAvailability();
}
Expand Down Expand Up @@ -759,6 +781,7 @@ private void createReplaceDialog() {

updatePlacementAndVisibility();
applyOverlayColors(backgroundToUse, true);
assignIDs();
replaceBar.forceFocus();
}

Expand All @@ -774,12 +797,12 @@ private void enableSearchTools(boolean enable) {
}

private void enableReplaceToggle(boolean enable) {
if (!okayToUse(replaceToggle)) {
if (!okayToUse(replaceBarToggle)) {
return;
}
boolean visible = enable && findReplaceLogic.getTarget().isEditable();
((GridData) replaceToggle.getLayoutData()).exclude = !visible;
replaceToggle.setVisible(visible);
((GridData) replaceBarToggle.getLayoutData()).exclude = !visible;
replaceBarToggle.setVisible(visible);
}

private void enableReplaceTools(boolean enable) {
Expand Down Expand Up @@ -822,8 +845,8 @@ private int calculateOverlayWidthWithToolbars(String searchInput) {

private int calculateOverlayWidthWithoutToolbars(String searchInput) {
int replaceToggleWidth = 0;
if (okayToUse(replaceToggle)) {
replaceToggleWidth = replaceToggle.getBounds().width;
if (okayToUse(replaceBarToggle)) {
replaceToggleWidth = replaceBarToggle.getBounds().width;
}
int closeButtonWidth = closeTools.getSize().x;
int searchInputWidth = getTextWidthInSearchBar(searchInput);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@
*/
class FindReplaceDialog extends Dialog {

public static final String ID_DATA_KEY = "org.eclipse.ui.texteditor.FindReplaceDialog.id"; //$NON-NLS-1$

private static final int CLOSE_BUTTON_ID = 101;
private IFindReplaceLogic findReplaceLogic;

Expand Down Expand Up @@ -275,6 +277,7 @@ public void create() {
shell.setText(FindReplaceMessages.FindReplace_Dialog_Title);

updateButtonState();
assignIDs();
}

/**
Expand Down Expand Up @@ -1353,4 +1356,23 @@ private String getCurrentSelection() {
return null;
return target.getSelectionText();
}

@SuppressWarnings("nls")
private void assignIDs() {
fFindField.setData(ID_DATA_KEY, "searchInput");
fReplaceField.setData(ID_DATA_KEY, "replaceInput");
fForwardRadioButton.setData(ID_DATA_KEY, "searchForward");
fGlobalRadioButton.setData(ID_DATA_KEY, "globalSearch");
fSelectedRangeRadioButton.setData(ID_DATA_KEY, "searchInSelection");
fCaseCheckBox.setData(ID_DATA_KEY, "caseSensitiveSearch");
fWrapCheckBox.setData(ID_DATA_KEY, "wrappedSearch");
fWholeWordCheckBox.setData(ID_DATA_KEY, "wholeWordSearch");
fIncrementalCheckBox.setData(ID_DATA_KEY, "incrementalSearch");
fIsRegExCheckBox.setData(ID_DATA_KEY, "regExSearch");

fReplaceSelectionButton.setData(ID_DATA_KEY, "replaceOne");
fReplaceFindButton.setData(ID_DATA_KEY, "replaceFindOne");
fReplaceAllButton.setData(ID_DATA_KEY, "replaceAll");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
*******************************************************************************/
package org.eclipse.ui.internal.findandreplace;

import static org.junit.Assert.assertFalse;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;

import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
Expand All @@ -29,53 +29,39 @@

public final class WidgetExtractor {

private final Composite container;
private final Composite rootContainer;

private final String idDataKey;

public WidgetExtractor(Composite container) {
this.container= container;
public WidgetExtractor(String idDataKey, Composite container) {
this.idDataKey= idDataKey;
this.rootContainer= container;
}

public HistoryTextWrapper findHistoryTextWrapperWithMessage(String includedMessageText) {
List<HistoryTextWrapper> widgets= findWidgets(container, HistoryTextWrapper.class, candidate -> {
String message= removeMnemonicsAndMakeLowercase(candidate.getTextBar().getMessage());
return message.contains(includedMessageText);
});
return widgets.isEmpty() ? null : widgets.get(0);
public HistoryTextWrapper findHistoryTextWrapper(String id) {
return findWidget(rootContainer, HistoryTextWrapper.class, id);
}

public List<Combo> findCombos() {
return findWidgets(container, Combo.class, candidate -> true);
public Combo findCombo(String id) {
return findWidget(rootContainer, Combo.class, id);
}

public Button findButtonWithText(String includedText, String... excludedTexts) {
List<Button> widgets= findWidgets(container, Button.class, candidate -> {
String text= removeMnemonicsAndMakeLowercase(candidate.getText());
boolean containsIncludeString= text.contains(includedText);
boolean containsExcludeStrings= Arrays.stream(excludedTexts).anyMatch(it -> text.contains(it));
return containsIncludeString && !containsExcludeStrings;
});
return widgets.isEmpty() ? null : widgets.get(0);
public Button findButton(String id) {
return findWidget(rootContainer, Button.class, id);
}

public Button findButtonWithTooltipText(String includedToolTipText) {
List<Button> widgets= findWidgets(container, Button.class, candidate -> {
String toolTipText= candidate.getToolTipText();
return toolTipText != null && removeMnemonicsAndMakeLowercase(toolTipText).contains(includedToolTipText);
});
return widgets.isEmpty() ? null : widgets.get(0);
public ToolItem findToolItem(String id) {
return findWidget(rootContainer, ToolItem.class, id);
}

public ToolItem findToolItemWithTooltipText(String includedToolTipText, String... excludedToolTipTexts) {
List<ToolItem> widgets= findWidgets(container, ToolItem.class, candidate -> {
String toolTipText= removeMnemonicsAndMakeLowercase(candidate.getToolTipText());
boolean containsIncludeString= toolTipText.contains(includedToolTipText);
boolean containsExcludeStrings= Arrays.stream(excludedToolTipTexts).anyMatch(it -> toolTipText.contains(it));
return containsIncludeString && !containsExcludeStrings;
});
private <T extends Widget> T findWidget(Composite container, Class<T> type, String id) {
List<T> widgets= findWidgets(container, type, id);
assertFalse("more than one matching widget found for id '" + id + "':" + widgets, widgets.size() > 1);
return widgets.isEmpty() ? null : widgets.get(0);
}

private static <T extends Widget> List<T> findWidgets(Composite container, Class<T> type, Function<T, Boolean> matcher) {
@SuppressWarnings("unchecked")
private <T extends Widget> List<T> findWidgets(Composite container, Class<T> type, String id) {
List<Widget> children= new ArrayList<>();
children.addAll(List.of(container.getChildren()));
if (container instanceof ToolBar toolbar) {
Expand All @@ -84,20 +70,15 @@ private static <T extends Widget> List<T> findWidgets(Composite container, Class
List<T> result= new ArrayList<>();
for (Widget child : children) {
if (type.isInstance(child)) {
@SuppressWarnings("unchecked")
T typedChild= (T) child;
if (matcher.apply(typedChild)) {
result.add(typedChild);
if (id.equals(child.getData(idDataKey))) {
result.add((T) child);
}
}
if (child instanceof Composite compositeChild) {
result.addAll(findWidgets(compositeChild, type, matcher));
result.addAll(findWidgets(compositeChild, type, id));
}
}
return result;
}

private static String removeMnemonicsAndMakeLowercase(String string) {
return string == null ? "" : string.replaceAll("&", "").toLowerCase();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,24 @@ class OverlayAccess implements IFindReplaceUIAccess {
this.findReplaceTarget= findReplaceTarget;
overlay= findReplaceOverlay;
shell= overlay.getShell();
WidgetExtractor widgetExtractor= new WidgetExtractor(shell);
find= widgetExtractor.findHistoryTextWrapperWithMessage("find");
caseSensitive= widgetExtractor.findToolItemWithTooltipText("case");
wholeWord= widgetExtractor.findToolItemWithTooltipText("whole");
regEx= widgetExtractor.findToolItemWithTooltipText("regular");
inSelection= widgetExtractor.findToolItemWithTooltipText("selected");
searchForward= widgetExtractor.findToolItemWithTooltipText("forward");
searchBackward= widgetExtractor.findToolItemWithTooltipText("backward");
openReplaceDialog= widgetExtractor.findButtonWithTooltipText("toggle");
WidgetExtractor widgetExtractor= new WidgetExtractor(FindReplaceOverlay.ID_DATA_KEY, shell);
find= widgetExtractor.findHistoryTextWrapper("searchInput");
caseSensitive= widgetExtractor.findToolItem("caseSensitiveSearch");
wholeWord= widgetExtractor.findToolItem("wholeWordSearch");
regEx= widgetExtractor.findToolItem("regExSearch");
inSelection= widgetExtractor.findToolItem("searchInSelection");
searchForward= widgetExtractor.findToolItem("searchForward");
searchBackward= widgetExtractor.findToolItem("searchBackward");
openReplaceDialog= widgetExtractor.findButton("replaceBarToggle");
extractReplaceWidgets();
}

private void extractReplaceWidgets() {
if (!isReplaceDialogOpen() && Objects.nonNull(openReplaceDialog)) {
WidgetExtractor widgetExtractor= new WidgetExtractor(shell);
replace= widgetExtractor.findHistoryTextWrapperWithMessage("replace");
replaceButton= widgetExtractor.findToolItemWithTooltipText("replace", "all");
replaceAllButton= widgetExtractor.findToolItemWithTooltipText("replace all");
WidgetExtractor widgetExtractor= new WidgetExtractor(FindReplaceOverlay.ID_DATA_KEY, shell);
replace= widgetExtractor.findHistoryTextWrapper("replaceInput");
replaceButton= widgetExtractor.findToolItem("replaceOne");
replaceAllButton= widgetExtractor.findToolItem("replaceAll");
}
}

Expand Down
Loading

0 comments on commit 846956c

Please sign in to comment.