diff --git a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/FindReplaceLogic.java b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/FindReplaceLogic.java index 4faf1a80e8a..822e7d4340e 100644 --- a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/FindReplaceLogic.java +++ b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/FindReplaceLogic.java @@ -58,7 +58,9 @@ public class FindReplaceLogic implements IFindReplaceLogic { @Override public void activate(SearchOptions searchOption) { - searchOptions.add(searchOption); + if (!searchOptions.add(searchOption)) { + return; + } switch (searchOption) { case GLOBAL: @@ -78,7 +80,10 @@ public void activate(SearchOptions searchOption) { @Override public void deactivate(SearchOptions searchOption) { - searchOptions.remove(searchOption); + if (!searchOptions.remove(searchOption)) { + return; + } + if (searchOption == SearchOptions.GLOBAL) { useSelectedLines(true); } @@ -241,7 +246,7 @@ public void run() { String msg = NLSUtility.format(FindReplaceMessages.FindReplace_Status_noMatchWithValue_label, findString); statusLineMessage(false, msg); - status = new FindStatus(FindStatus.StatusCode.NO_MATCH, false); + status = new FindStatus(FindStatus.StatusCode.NO_MATCH); } } catch (PatternSyntaxException ex) { status = new InvalidRegExStatus(ex); @@ -286,7 +291,7 @@ public void run() { String msg = NLSUtility.format(FindReplaceMessages.FindReplace_Status_noMatchWithValue_label, findString); statusLineMessage(false, msg); - status = new FindStatus(FindStatus.StatusCode.NO_MATCH, false); + status = new FindStatus(FindStatus.StatusCode.NO_MATCH); } } catch (PatternSyntaxException ex) { status = new InvalidRegExStatus(ex); @@ -310,7 +315,7 @@ private boolean prepareTargetForEditing() { if (target instanceof IFindReplaceTargetExtension2) { IFindReplaceTargetExtension2 extension = (IFindReplaceTargetExtension2) target; if (!extension.validateTargetState()) { - status = new FindStatus(FindStatus.StatusCode.READONLY, true); + status = new FindStatus(FindStatus.StatusCode.READONLY); return false; } } @@ -490,10 +495,10 @@ private int findIndex(String findString, int startPosition) { if (isActive(SearchOptions.WRAP)) { statusLineMessage(FindReplaceMessages.FindReplace_Status_wrapped_label); - status = new FindStatus(FindStatus.StatusCode.WRAPPED, false); + status = new FindStatus(FindStatus.StatusCode.WRAPPED); index = findAndSelect(-1, findString); } else { - status = new FindStatus(FindStatus.StatusCode.NO_MATCH, false); + status = new FindStatus(FindStatus.StatusCode.NO_MATCH); } } return index; @@ -564,7 +569,7 @@ private boolean findNext(String findString, boolean forwardSearch) { if (index == -1) { String msg = NLSUtility.format(FindReplaceMessages.FindReplace_Status_noMatchWithValue_label, findString); statusLineMessage(false, msg); - status = new FindStatus(FindStatus.StatusCode.NO_MATCH, false); + status = new FindStatus(FindStatus.StatusCode.NO_MATCH); return false; } diff --git a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/status/FindAllStatus.java b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/status/FindAllStatus.java index 464c91f4d56..d0e16452e26 100644 --- a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/status/FindAllStatus.java +++ b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/status/FindAllStatus.java @@ -30,8 +30,8 @@ public T accept(IStatusVisitor visitor) { } @Override - public boolean isError() { - return false; + public boolean isInputValid() { + return true; } } diff --git a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/status/FindStatus.java b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/status/FindStatus.java index 287b9954a4c..e2288b26f58 100644 --- a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/status/FindStatus.java +++ b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/status/FindStatus.java @@ -21,11 +21,9 @@ public enum StatusCode { } private StatusCode messageCode; - private boolean isError; - public FindStatus(StatusCode errorCode, boolean isError) { + public FindStatus(StatusCode errorCode) { this.messageCode = errorCode; - this.isError = isError; } public StatusCode getMessageCode() { @@ -38,8 +36,8 @@ public T accept(IStatusVisitor visitor) { } @Override - public boolean isError() { - return this.isError; + public boolean isInputValid() { + return messageCode != StatusCode.READONLY; } } diff --git a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/status/IStatus.java b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/status/IStatus.java index e39dc6c8960..e9dd104ef5c 100644 --- a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/status/IStatus.java +++ b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/status/IStatus.java @@ -21,9 +21,8 @@ public interface IStatus { public T accept(IStatusVisitor visitor); /** - * Whether the status represents an Error in the Find/Replace-Operation. - * - * @return the error state. + * {@return whether the input is valid, e.g., that the find string is valid and + * that the target is writable on replace operations} */ - public boolean isError(); + public boolean isInputValid(); } diff --git a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/status/InvalidRegExStatus.java b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/status/InvalidRegExStatus.java index b771d536d3c..763e22048d4 100644 --- a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/status/InvalidRegExStatus.java +++ b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/status/InvalidRegExStatus.java @@ -37,8 +37,8 @@ public T accept(IStatusVisitor visitor) { } @Override - public boolean isError() { - return true; + public boolean isInputValid() { + return false; } } diff --git a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/status/NoStatus.java b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/status/NoStatus.java index 6e1f05851fc..4ac77edbe46 100644 --- a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/status/NoStatus.java +++ b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/status/NoStatus.java @@ -28,8 +28,8 @@ public T accept(IStatusVisitor visitor) { } @Override - public boolean isError() { - return false; + public boolean isInputValid() { + return true; } } diff --git a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/status/ReplaceAllStatus.java b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/status/ReplaceAllStatus.java index 65fbf793b0a..6d055dc5bab 100644 --- a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/status/ReplaceAllStatus.java +++ b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/status/ReplaceAllStatus.java @@ -30,8 +30,8 @@ public T accept(IStatusVisitor visitor) { } @Override - public boolean isError() { - return false; + public boolean isInputValid() { + return true; } } diff --git a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/FindReplaceDialog.java b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/FindReplaceDialog.java index 65f2fbced3f..ffebf9ab85a 100644 --- a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/FindReplaceDialog.java +++ b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/FindReplaceDialog.java @@ -145,7 +145,7 @@ public void modifyText(ModifyEvent e) { } findReplaceLogic.performIncrementalSearch(getFindString()); - evaluateFindReplacerStatus(false); + evaluateFindReplaceStatus(false); updateButtonState(!findReplaceLogic.isActive(SearchOptions.INCREMENTAL)); } @@ -290,18 +290,18 @@ private Composite createButtonSection(Composite parent) { new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { - setupFindReplacer(); - boolean eventRequiresBackwardSearch = (e.stateMask & SWT.MODIFIER_MASK) == SWT.SHIFT; - boolean oldSearchDirection = findReplaceLogic.isActive(SearchOptions.FORWARD); - activateInFindReplacerIf(SearchOptions.FORWARD, - !eventRequiresBackwardSearch && oldSearchDirection); + setupFindReplaceLogic(); + boolean eventRequiresInverseSearchDirection = (e.stateMask & SWT.MODIFIER_MASK) == SWT.SHIFT; + boolean forwardSearchActivated = findReplaceLogic.isActive(SearchOptions.FORWARD); + activateInFindReplaceLogicIf(SearchOptions.FORWARD, + eventRequiresInverseSearchDirection != forwardSearchActivated); boolean somethingFound = findReplaceLogic.performSearch(getFindString()); - activateInFindReplacerIf(SearchOptions.FORWARD, oldSearchDirection); + activateInFindReplaceLogicIf(SearchOptions.FORWARD, forwardSearchActivated); writeSelection(); updateButtonState(!somethingFound); updateFindHistory(); - evaluateFindReplacerStatus(); + evaluateFindReplaceStatus(); } }); setGridData(fFindNextButton, SWT.FILL, true, SWT.FILL, false); @@ -314,7 +314,7 @@ public void widgetSelected(SelectionEvent e) { writeSelection(); updateButtonState(); updateFindAndReplaceHistory(); - evaluateFindReplacerStatus(); + evaluateFindReplaceStatus(); } }); setGridData(fSelectAllButton, SWT.FILL, true, SWT.FILL, false); @@ -330,7 +330,7 @@ public void widgetSelected(SelectionEvent e) { } updateButtonState(); updateFindAndReplaceHistory(); - evaluateFindReplacerStatus(); + evaluateFindReplaceStatus(); } }); setGridData(fReplaceFindButton, SWT.FILL, false, SWT.FILL, false); @@ -344,7 +344,7 @@ public void widgetSelected(SelectionEvent e) { } updateButtonState(); updateFindAndReplaceHistory(); - evaluateFindReplacerStatus(); + evaluateFindReplaceStatus(); } }); setGridData(fReplaceSelectionButton, SWT.FILL, false, SWT.FILL, false); @@ -358,7 +358,7 @@ public void widgetSelected(SelectionEvent e) { writeSelection(); updateButtonState(); updateFindAndReplaceHistory(); - evaluateFindReplacerStatus(); + evaluateFindReplaceStatus(); } }); setGridData(fReplaceAllButton, SWT.FILL, true, SWT.FILL, false); @@ -506,7 +506,7 @@ private Composite createDirectionGroup(Composite parent) { SelectionListener selectionListener = new SelectionListener() { @Override public void widgetSelected(SelectionEvent e) { - activateInFindReplacerIf(SearchOptions.FORWARD, fForwardRadioButton.getSelection()); + activateInFindReplaceLogicIf(SearchOptions.FORWARD, fForwardRadioButton.getSelection()); } @Override @@ -528,7 +528,7 @@ public void widgetDefaultSelected(SelectionEvent e) { backwardRadioButton.addSelectionListener(selectionListener); storeButtonWithMnemonicInMap(backwardRadioButton); - activateInFindReplacerIf(SearchOptions.FORWARD, true); // search forward by default + activateInFindReplaceLogicIf(SearchOptions.FORWARD, true); // search forward by default backwardRadioButton.setSelection(!findReplaceLogic.isActive(SearchOptions.FORWARD)); fForwardRadioButton.setSelection(findReplaceLogic.isActive(SearchOptions.FORWARD)); @@ -671,7 +671,7 @@ private Composite createOptionsGroup(Composite parent) { SelectionListener selectionListener = new SelectionListener() { @Override public void widgetSelected(SelectionEvent e) { - setupFindReplacer(); + setupFindReplaceLogic(); storeSettings(); } @@ -708,7 +708,7 @@ public void widgetDefaultSelected(SelectionEvent e) { fIncrementalCheckBox.addSelectionListener(new SelectionListener() { @Override public void widgetSelected(SelectionEvent e) { - setupFindReplacer(); + setupFindReplaceLogic(); storeSettings(); } @@ -728,7 +728,7 @@ public void widgetDefaultSelected(SelectionEvent e) { public void widgetSelected(SelectionEvent e) { boolean newState = fIsRegExCheckBox.getSelection(); fIncrementalCheckBox.setEnabled(!newState); - setupFindReplacer(); + setupFindReplaceLogic(); storeSettings(); updateButtonState(); setContentAssistsEnablement(newState); @@ -1019,7 +1019,7 @@ private void updateButtonState() { * @since 3.0 */ private void updateButtonState(boolean disableReplace) { - setupFindReplacer(); + setupFindReplaceLogic(); if (okToUse(getShell()) && okToUse(fFindNextButton)) { boolean hasActiveSelection = false; @@ -1243,21 +1243,21 @@ protected int getDialogBoundsStrategy() { private void readConfiguration() { IDialogSettings s = getDialogSettings(); - activateInFindReplacerIf(SearchOptions.WRAP, s.get("wrap") == null || s.getBoolean("wrap")); //$NON-NLS-1$ //$NON-NLS-2$ - activateInFindReplacerIf(SearchOptions.CASE_SENSITIVE, s.getBoolean("casesensitive")); //$NON-NLS-1$ - activateInFindReplacerIf(SearchOptions.WHOLE_WORD, s.getBoolean("wholeword")); //$NON-NLS-1$ - activateInFindReplacerIf(SearchOptions.INCREMENTAL, s.getBoolean("incremental")); //$NON-NLS-1$ - activateInFindReplacerIf(SearchOptions.REGEX, s.getBoolean("isRegEx")); //$NON-NLS-1$ + activateInFindReplaceLogicIf(SearchOptions.WRAP, s.get("wrap") == null || s.getBoolean("wrap")); //$NON-NLS-1$ //$NON-NLS-2$ + activateInFindReplaceLogicIf(SearchOptions.CASE_SENSITIVE, s.getBoolean("casesensitive")); //$NON-NLS-1$ + activateInFindReplaceLogicIf(SearchOptions.WHOLE_WORD, s.getBoolean("wholeword")); //$NON-NLS-1$ + activateInFindReplaceLogicIf(SearchOptions.INCREMENTAL, s.getBoolean("incremental")); //$NON-NLS-1$ + activateInFindReplaceLogicIf(SearchOptions.REGEX, s.getBoolean("isRegEx")); //$NON-NLS-1$ } - private void setupFindReplacer() { - activateInFindReplacerIf(SearchOptions.WRAP, fWrapCheckBox.getSelection()); - activateInFindReplacerIf(SearchOptions.FORWARD, fForwardRadioButton.getSelection()); - activateInFindReplacerIf(SearchOptions.CASE_SENSITIVE, fCaseCheckBox.getSelection()); - activateInFindReplacerIf(SearchOptions.WHOLE_WORD, fWholeWordCheckBox.getSelection()); - activateInFindReplacerIf(SearchOptions.INCREMENTAL, fIncrementalCheckBox.getSelection()); - activateInFindReplacerIf(SearchOptions.REGEX, fIsRegExCheckBox.getSelection()); + private void setupFindReplaceLogic() { + activateInFindReplaceLogicIf(SearchOptions.WRAP, fWrapCheckBox.getSelection()); + activateInFindReplaceLogicIf(SearchOptions.FORWARD, fForwardRadioButton.getSelection()); + activateInFindReplaceLogicIf(SearchOptions.CASE_SENSITIVE, fCaseCheckBox.getSelection()); + activateInFindReplaceLogicIf(SearchOptions.WHOLE_WORD, fWholeWordCheckBox.getSelection()); + activateInFindReplaceLogicIf(SearchOptions.INCREMENTAL, fIncrementalCheckBox.getSelection()); + activateInFindReplaceLogicIf(SearchOptions.REGEX, fIsRegExCheckBox.getSelection()); } /** @@ -1279,7 +1279,7 @@ private void writeConfiguration() { replaceHistory.add(replaceString); } - private void activateInFindReplacerIf(SearchOptions option, boolean shouldActivate) { + private void activateInFindReplaceLogicIf(SearchOptions option, boolean shouldActivate) { if (shouldActivate) { findReplaceLogic.activate(option); } else { @@ -1287,8 +1287,8 @@ private void activateInFindReplacerIf(SearchOptions option, boolean shouldActiva } } - private void evaluateFindReplacerStatus() { - evaluateFindReplacerStatus(true); + private void evaluateFindReplaceStatus() { + evaluateFindReplaceStatus(true); } /** @@ -1296,13 +1296,13 @@ private void evaluateFindReplacerStatus() { * * @param allowBeep Whether the evaluation should beep on some codes. */ - private void evaluateFindReplacerStatus(boolean allowBeep) { + private void evaluateFindReplaceStatus(boolean allowBeep) { IStatus status = findReplaceLogic.getStatus(); String dialogMessage = status.accept(new FindReplaceLogicMessageGenerator()); fStatusLabel.setText(dialogMessage); fStatusLabel.setForeground(null); - if (status.isError()) { + if (!status.isInputValid()) { fStatusLabel.setForeground(JFaceColors.getErrorText(fStatusLabel.getDisplay())); } diff --git a/tests/org.eclipse.ui.workbench.texteditor.tests/src/org/eclipse/ui/internal/findandreplace/FindReplaceLogicTest.java b/tests/org.eclipse.ui.workbench.texteditor.tests/src/org/eclipse/ui/internal/findandreplace/FindReplaceLogicTest.java index d73bb4887ca..6a10604b3f2 100644 --- a/tests/org.eclipse.ui.workbench.texteditor.tests/src/org/eclipse/ui/internal/findandreplace/FindReplaceLogicTest.java +++ b/tests/org.eclipse.ui.workbench.texteditor.tests/src/org/eclipse/ui/internal/findandreplace/FindReplaceLogicTest.java @@ -97,7 +97,6 @@ public void testPerformReplaceAllForwards() { * @param textViewer textviewer-object that contains the contents on which findReplaceLogic * operates */ - @SuppressWarnings("boxing") private void performReplaceAllBaseTestcases(IFindReplaceLogic findReplaceLogic, TextViewer textViewer) { Display display= parentShell.getDisplay(); textViewer.setDocument(new Document("aaaa")); @@ -215,7 +214,6 @@ public void testPerformSelectAndReplaceBackward() { } - @SuppressWarnings("boxing") @Test public void testPerformReplaceAndFind() { TextViewer textViewer= setupTextViewer("HelloWorld!"); @@ -293,13 +291,11 @@ private void expectStatusIsCode(IFindReplaceLogic findReplaceLogic, FindStatus.S assertThat(((FindStatus) findReplaceLogic.getStatus()).getMessageCode(), equalTo(code)); } - @SuppressWarnings("boxing") private void expectStatusIsReplaceAllWithCount(IFindReplaceLogic findReplaceLogic, int count) { assertThat(findReplaceLogic.getStatus(), instanceOf(ReplaceAllStatus.class)); assertThat(((ReplaceAllStatus) findReplaceLogic.getStatus()).getReplaceCount(), equalTo(count)); } - @SuppressWarnings("boxing") private void expectStatusIsFindAllWithCount(IFindReplaceLogic findReplaceLogic, int count) { assertThat(findReplaceLogic.getStatus(), instanceOf(FindAllStatus.class)); assertThat(((FindAllStatus) findReplaceLogic.getStatus()).getSelectCount(), equalTo(count)); diff --git a/tests/org.eclipse.ui.workbench.texteditor.tests/src/org/eclipse/ui/workbench/texteditor/tests/FindReplaceDialogTest.java b/tests/org.eclipse.ui.workbench.texteditor.tests/src/org/eclipse/ui/workbench/texteditor/tests/FindReplaceDialogTest.java index 0104286c939..c9ff238231e 100644 --- a/tests/org.eclipse.ui.workbench.texteditor.tests/src/org/eclipse/ui/workbench/texteditor/tests/FindReplaceDialogTest.java +++ b/tests/org.eclipse.ui.workbench.texteditor.tests/src/org/eclipse/ui/workbench/texteditor/tests/FindReplaceDialogTest.java @@ -315,10 +315,30 @@ public void testShiftEnterReversesSearchDirection() { Button forwardRadioButton= (Button) fFindReplaceDialog.get("fForwardRadioButton"); forwardRadioButton.setSelection(false); - fFindReplaceDialog.invoke("updateButtonState", null); event.doit= true; forwardRadioButton.traverse(SWT.TRAVERSE_RETURN, event); assertEquals(5, (target.getSelection()).x); } + @Test + public void testChangeInputForIncrementalSearch() { + openTextViewerAndFindReplaceDialog(); + Button incremenalSearchButton= ((Button) fFindReplaceDialog.get("fIncrementalCheckBox")); + incremenalSearchButton.setSelection(true); + fFindReplaceDialog.invoke("updateButtonState", null); + + Combo findField= (Combo) fFindReplaceDialog.get("fFindField"); + findField.setText("lin"); + IFindReplaceTarget target= findReplaceLogic.getTarget(); + assertEquals(0, (target.getSelection()).x); + assertEquals(3, (target.getSelection()).y); + + findField.setText("line"); + assertEquals(0, (target.getSelection()).x); + assertEquals(4, (target.getSelection()).y); + + incremenalSearchButton.setSelection(false); + fFindReplaceDialog.invoke("updateButtonState", null); + } + }