From 0455efe2e0849591bd21ab2034714bc2c385ea9c Mon Sep 17 00:00:00 2001
From: Maximilian Wittmer
* This class may be instantiated; it is not intended to be subclassed.
@@ -53,17 +52,24 @@
* @noextend This class is not intended to be subclassed by clients.
*/
public class FindReplaceAction extends ResourceAction implements IUpdate {
+ private static boolean MW_FIND_REPLACE_OVERLAY_SHOULD_ABSOLUTELY_REPLACE_WITH_A_PROPERTY_LATER = true;
+
+ private static boolean shouldShowModernOverlay() {
+ return MW_FIND_REPLACE_OVERLAY_SHOULD_ABSOLUTELY_REPLACE_WITH_A_PROPERTY_LATER;
+ }
/**
- * Represents the "global" find/replace dialog. It tracks the active
- * part and retargets the find/replace dialog accordingly. The find/replace
- * target is retrieved from the active part using
+ * Represents the "global" find/replace dialog. It tracks the active part and
+ * retargets the find/replace dialog accordingly. The find/replace target is
+ * retrieved from the active part using
* getAdapter(IFindReplaceTarget.class)
.
*
- * The stub has the same life cycle as the find/replace dialog.
+ * The stub has the same life cycle as the find/replace dialog. + * *- * If no IWorkbenchPart is available a Shell must be provided - * In this case the IFindReplaceTarget will never change.
+ * If no IWorkbenchPart is available a Shell must be provided In this case the + * IFindReplaceTarget will never change. + * */ static class FindReplaceDialogStub implements IPartListener2, IPageChangedListener, DisposeListener { @@ -86,8 +92,8 @@ static class FindReplaceDialogStub implements IPartListener2, IPageChangedListen */ public FindReplaceDialogStub(IWorkbenchPartSite site) { this(site.getShell()); - fWindow= site.getWorkbenchWindow(); - IPartService service= fWindow.getPartService(); + fWindow = site.getWorkbenchWindow(); + IPartService service = fWindow.getPartService(); service.addPartListener(this); partActivated(service.getActivePart()); } @@ -99,7 +105,7 @@ public FindReplaceDialogStub(IWorkbenchPartSite site) { * @since 3.3 */ public FindReplaceDialogStub(Shell shell) { - fDialog= new FindReplaceDialog(shell); + fDialog = new FindReplaceDialog(shell); fDialog.create(); fDialog.getShell().addDisposeListener(this); } @@ -114,19 +120,19 @@ public FindReplaceDialog getDialog() { } private void partActivated(IWorkbenchPart part) { - IFindReplaceTarget target= part == null ? null : part.getAdapter(IFindReplaceTarget.class); - fPreviousPart= fPart; - fPart= target == null ? null : part; + IFindReplaceTarget target = part == null ? null : part.getAdapter(IFindReplaceTarget.class); + fPreviousPart = fPart; + fPart = target == null ? null : part; if (fPreviousTarget != target) { - fPreviousTarget= target; + fPreviousTarget = target; if (fDialog != null) { - boolean isEditable= false; + boolean isEditable = false; if (fPart instanceof ITextEditorExtension2) { - ITextEditorExtension2 extension= (ITextEditorExtension2) fPart; - isEditable= extension.isEditorInputModifiable(); + ITextEditorExtension2 extension = (ITextEditorExtension2) fPart; + isEditable = extension.isEditorInputModifiable(); } else if (target != null) - isEditable= target.isEditable(); + isEditable = target.isEditable(); fDialog.updateTarget(target, isEditable, false); } } @@ -140,38 +146,38 @@ public void partActivated(IWorkbenchPartReference partRef) { @Override public void pageChanged(PageChangedEvent event) { if (event.getSource() instanceof IWorkbenchPart) - partActivated((IWorkbenchPart)event.getSource()); + partActivated((IWorkbenchPart) event.getSource()); } @Override public void partClosed(IWorkbenchPartReference partRef) { - IWorkbenchPart part= partRef.getPart(true); + IWorkbenchPart part = partRef.getPart(true); if (part == fPreviousPart) { - fPreviousPart= null; - fPreviousTarget= null; + fPreviousPart = null; + fPreviousTarget = null; } if (part == fPart) - partActivated((IWorkbenchPart)null); + partActivated((IWorkbenchPart) null); } @Override public void widgetDisposed(DisposeEvent event) { if (fgFindReplaceDialogStub == this) - fgFindReplaceDialogStub= null; + fgFindReplaceDialogStub = null; - if(fgFindReplaceDialogStubShell == this) - fgFindReplaceDialogStubShell= null; + if (fgFindReplaceDialogStubShell == this) + fgFindReplaceDialogStubShell = null; if (fWindow != null) { fWindow.getPartService().removePartListener(this); - fWindow= null; + fWindow = null; } - fDialog= null; - fPart= null; - fPreviousPart= null; - fPreviousTarget= null; + fDialog = null; + fPart = null; + fPreviousPart = null; + fPreviousTarget = null; } @Override @@ -199,8 +205,8 @@ public void partVisible(IWorkbenchPartReference partRef) { } /** - * Checks if the dialogs shell is the same as the givenshell
and if not clears
- * the stub and closes the dialog.
+ * Checks if the dialogs shell is the same as the given shell
and
+ * if not clears the stub and closes the dialog.
*
* @param shell the shell check
* @since 3.3
@@ -208,10 +214,10 @@ public void partVisible(IWorkbenchPartReference partRef) {
public void checkShell(Shell shell) {
if (fDialog != null && shell != fDialog.getParentShell()) {
if (fgFindReplaceDialogStub == this)
- fgFindReplaceDialogStub= null;
+ fgFindReplaceDialogStub = null;
if (fgFindReplaceDialogStubShell == this)
- fgFindReplaceDialogStubShell= null;
+ fgFindReplaceDialogStubShell = null;
fDialog.close();
}
@@ -219,17 +225,20 @@ public void checkShell(Shell shell) {
}
-
/**
* Listener for disabling the dialog on shell close.
*
- * This stub is shared amongst IWorkbenchPart
s.
IWorkbenchPart
s.
+ *
*/
private static FindReplaceDialogStub fgFindReplaceDialogStub;
- /** Listener for disabling the dialog on shell close.
+ /**
+ * Listener for disabling the dialog on shell close.
*
- * This stub is shared amongst Shell
s.
Shell
s.
+ *
+ *
* @since 3.3
*/
private static FindReplaceDialogStub fgFindReplaceDialogStubShell;
@@ -242,45 +251,51 @@ public void checkShell(Shell shell) {
private IWorkbenchWindow fWorkbenchWindow;
/**
* The shell to use if the action is created with a shell.
+ *
* @since 3.3
*/
private Shell fShell;
+ private FindReplaceOverlay overlay;
+
/**
* Creates a new find/replace action for the given workbench part.
* - * The action configures its visual representation from the given - * resource bundle.
+ * The action configures its visual representation from the given resource + * bundle. + * * - * @param bundle the resource bundle - * @param prefix a prefix to be prepended to the various resource keys - * (described inResourceAction
constructor), or
- * null
if none
- * @param workbenchPart the workbench part
+ * @param bundle the resource bundle
+ * @param prefix a prefix to be prepended to the various resource keys
+ * (described in ResourceAction
constructor),
+ * or null
if none
+ * @param workbenchPart the workbench part
* @see ResourceAction#ResourceAction(ResourceBundle, String)
*/
public FindReplaceAction(ResourceBundle bundle, String prefix, IWorkbenchPart workbenchPart) {
super(bundle, prefix);
Assert.isLegal(workbenchPart != null);
- fWorkbenchPart= workbenchPart;
+ fWorkbenchPart = workbenchPart;
update();
}
/**
* Creates a new find/replace action for the given target and shell.
* - * This can be used without having an IWorkbenchPart e.g. for - * dialogs or wizards.
+ * This can be used without having an IWorkbenchPart e.g. for dialogs or + * wizards. + * *- * The action configures its visual representation from the given - * resource bundle.
+ * The action configures its visual representation from the given resource + * bundle. + * * * @param bundle the resource bundle * @param prefix a prefix to be prepended to the various resource keys - * (described inResourceAction
constructor), or
- * null
if none
+ * (described in ResourceAction
constructor), or
+ * null
if none
* @param target the IFindReplaceTarget to use
- * @param shell the shell
+ * @param shell the shell
* @see ResourceAction#ResourceAction(ResourceBundle, String)
*
* @since 3.3
@@ -288,81 +303,117 @@ public FindReplaceAction(ResourceBundle bundle, String prefix, IWorkbenchPart wo
public FindReplaceAction(ResourceBundle bundle, String prefix, Shell shell, IFindReplaceTarget target) {
super(bundle, prefix);
Assert.isLegal(target != null && shell != null);
- fTarget= target;
- fShell= shell;
+ fTarget = target;
+ fShell = shell;
update();
}
/**
- * Creates a new find/replace action for the given workbench window.
- * The action configures its visual representation from the given
- * resource bundle.
+ * Creates a new find/replace action for the given workbench window. The action
+ * configures its visual representation from the given resource bundle.
*
- * @param bundle the resource bundle
- * @param prefix a prefix to be prepended to the various resource keys
- * (described in ResourceAction
constructor), or
- * null
if none
+ * @param bundle the resource bundle
+ * @param prefix a prefix to be prepended to the various resource keys
+ * (described in ResourceAction
+ * constructor), or null
if none
* @param workbenchWindow the workbench window
* @see ResourceAction#ResourceAction(ResourceBundle, String)
*
- * @deprecated use FindReplaceAction(ResourceBundle, String, IWorkbenchPart) instead
+ * @deprecated use FindReplaceAction(ResourceBundle, String, IWorkbenchPart)
+ * instead
*/
@Deprecated
public FindReplaceAction(ResourceBundle bundle, String prefix, IWorkbenchWindow workbenchWindow) {
super(bundle, prefix);
- fWorkbenchWindow= workbenchWindow;
+ fWorkbenchWindow = workbenchWindow;
update();
}
@Override
public void run() {
- if (fTarget == null)
- return;
+ if (shouldShowModernOverlay()) {
+ showModernOverlay();
+ } else {
+ if (fTarget == null)
+ return;
+ showClassicDialog();
+ }
+ }
+
+ /**
+ * @since 3.18.0
+ */
+ public void showClassicDialog() {
final FindReplaceDialog dialog;
final boolean isEditable;
- if(fShell == null) {
+ if (fShell == null) {
if (fgFindReplaceDialogStub != null) {
- Shell shell= fWorkbenchPart.getSite().getShell();
+ Shell shell = fWorkbenchPart.getSite().getShell();
fgFindReplaceDialogStub.checkShell(shell);
}
if (fgFindReplaceDialogStub == null)
- fgFindReplaceDialogStub= new FindReplaceDialogStub(fWorkbenchPart.getSite());
+ fgFindReplaceDialogStub = new FindReplaceDialogStub(fWorkbenchPart.getSite());
if (fWorkbenchPart instanceof ITextEditorExtension2)
- isEditable= ((ITextEditorExtension2) fWorkbenchPart).isEditorInputModifiable();
+ isEditable = ((ITextEditorExtension2) fWorkbenchPart).isEditorInputModifiable();
else
- isEditable= fTarget.isEditable();
+ isEditable = fTarget.isEditable();
- dialog= fgFindReplaceDialogStub.getDialog();
+ dialog = fgFindReplaceDialogStub.getDialog();
} else {
if (fgFindReplaceDialogStubShell != null) {
fgFindReplaceDialogStubShell.checkShell(fShell);
}
if (fgFindReplaceDialogStubShell == null)
- fgFindReplaceDialogStubShell= new FindReplaceDialogStub(fShell);
+ fgFindReplaceDialogStubShell = new FindReplaceDialogStub(fShell);
- isEditable= fTarget.isEditable();
- dialog= fgFindReplaceDialogStubShell.getDialog();
+ isEditable = fTarget.isEditable();
+ dialog = fgFindReplaceDialogStubShell.getDialog();
}
dialog.updateTarget(fTarget, isEditable, true);
dialog.open();
}
+ private void showModernOverlay() {
+ if (overlay == null) {
+ Shell shellToUse;
+ if (fShell == null) {
+ shellToUse = fWorkbenchPart.getSite().getShell();
+ } else {
+ shellToUse = fShell;
+ }
+ overlay = new FindReplaceOverlay(shellToUse, fWorkbenchPart, fTarget, this);
+ }
+ overlay.create();
+ overlay.open();
+ }
+
+ /**
+ * Closes the "modern" overlay. Typically called by the overlay itself.
+ *
+ * @since 3.18
+ */
+ public void closeModernOverlay() {
+ if (overlay != null) {
+ overlay.close();
+ }
+ }
+
@Override
public void update() {
- if(fShell == null){
+ if (fShell == null) {
if (fWorkbenchPart == null && fWorkbenchWindow != null)
- fWorkbenchPart= fWorkbenchWindow.getPartService().getActivePart();
+ fWorkbenchPart = fWorkbenchWindow.getPartService().getActivePart();
if (fWorkbenchPart != null)
- fTarget= fWorkbenchPart.getAdapter(IFindReplaceTarget.class);
+ fTarget = fWorkbenchPart.getAdapter(IFindReplaceTarget.class);
else
- fTarget= null;
+ fTarget = null;
}
setEnabled(fTarget != null && fTarget.canPerformFind());
}
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 fae3682279a..6eaac2a3299 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
@@ -21,12 +21,10 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
-import java.util.regex.PatternSyntaxException;
import org.osgi.framework.FrameworkUtil;
import org.eclipse.swt.SWT;
-import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
@@ -62,19 +60,10 @@
import org.eclipse.jface.text.FindReplaceDocumentAdapter;
import org.eclipse.jface.text.FindReplaceDocumentAdapterContentProposalProvider;
import org.eclipse.jface.text.IFindReplaceTarget;
-import org.eclipse.jface.text.IFindReplaceTargetExtension;
-import org.eclipse.jface.text.IFindReplaceTargetExtension3;
-import org.eclipse.jface.text.IFindReplaceTargetExtension4;
-import org.eclipse.jface.text.IRegion;
-import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.TextUtilities;
-import org.eclipse.ui.IEditorPart;
-import org.eclipse.ui.IWorkbenchPage;
-import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.fieldassist.ContentAssistCommandAdapter;
-import org.eclipse.ui.internal.texteditor.NLSUtility;
import org.eclipse.ui.internal.texteditor.SWTUtil;
@@ -85,6 +74,7 @@
class FindReplaceDialog extends Dialog {
private static final int CLOSE_BUTTON_ID = 101;
+ private FindReplacer findReplacer;
/**
* Updates the find replace dialog on activation changes.
@@ -108,18 +98,17 @@ public void shellDeactivated(ShellEvent e) {
fGlobalRadioButton.setSelection(true);
fSelectedRangeRadioButton.setSelection(false);
- fUseSelectedLines= false;
+ findReplacer.setGlobalSearch(false);
- if (fTarget != null && (fTarget instanceof IFindReplaceTargetExtension))
- ((IFindReplaceTargetExtension) fTarget).setScope(null);
-
- fOldScope= null;
+ findReplacer.deactivateScope();
fActiveShell= null;
updateButtonState();
}
}
+ private final FindModifyListener fFindModifyListener = new FindModifyListener();
+
/**
* Modify listener to update the search result in case of incremental search.
* @since 2.0
@@ -129,7 +118,7 @@ private class FindModifyListener implements ModifyListener {
// XXX: Workaround for Combo bug on Linux (see bug 404202 and bug 410603)
private boolean fIgnoreNextEvent;
private void ignoreNextEvent() {
- fIgnoreNextEvent= true;
+ fIgnoreNextEvent = true;
}
@Override
@@ -137,57 +126,26 @@ public void modifyText(ModifyEvent e) {
// XXX: Workaround for Combo bug on Linux (see bug 404202 and bug 410603)
if (fIgnoreNextEvent) {
- fIgnoreNextEvent= false;
+ fIgnoreNextEvent = false;
return;
}
- if (isIncrementalSearch() && !isRegExSearchAvailableAndChecked()) {
- if (fFindField.getText().equals("") && fTarget != null) { //$NON-NLS-1$
- // empty selection at base location
- int offset= fIncrementalBaseLocation.x;
-
- if (isForwardSearch() && !fNeedsInitialFindBeforeReplace || !isForwardSearch() && fNeedsInitialFindBeforeReplace)
- offset= offset + fIncrementalBaseLocation.y;
-
- fNeedsInitialFindBeforeReplace= false;
- findAndSelect(offset, "", isForwardSearch(), isCaseSensitiveSearch(), isWholeWordSearch(), isRegExSearchAvailableAndChecked()); //$NON-NLS-1$
- } else {
- performSearch(false, false, isForwardSearch());
- }
- }
+ findReplacer.updateSearchResultAfterTextWasModified(getFindString());
- updateButtonState(!isIncrementalSearch());
+ updateButtonState(!findReplacer.isIncrementalSearch());
}
}
/** The size of the dialogs search history. */
private static final int HISTORY_SIZE= 15;
- private Point fIncrementalBaseLocation;
- private boolean fWrapInit, fCaseInit, fWholeWordInit, fForwardInit, fGlobalInit, fIncrementalInit;
- /**
- * Tells whether an initial find operation is needed
- * before the replace operation.
- * @since 3.0
- */
- private boolean fNeedsInitialFindBeforeReplace;
- /**
- * Initial value for telling whether the search string is a regular expression.
- * @since 3.0
- */
- boolean fIsRegExInit;
-
private Listtrue
if the target supports regular expressions
- * @since 3.0
- */
- private boolean fIsTargetSupportingRegEx;
- /**
- * Tells whether fUseSelectedLines radio is checked.
- * @since 3.0
- */
- private boolean fUseSelectedLines;
- /**
- * true
if the find field should receive focus the next time
- * the dialog is activated, false
otherwise.
+ * true
if the find field should receive focus the next time the
+ * dialog is activated, false
otherwise.
+ *
* @since 3.0
*/
private boolean fGiveFocusToFindField= true;
@@ -242,23 +190,16 @@ public void modifyText(ModifyEvent e) {
*/
public FindReplaceDialog(Shell parentShell) {
super(parentShell);
+ findReplacer = new FindReplacer();
fParentShell= null;
- fTarget= null;
fDialogPositionInit= null;
fFindHistory= new ArrayList<>(HISTORY_SIZE);
fReplaceHistory= new ArrayList<>(HISTORY_SIZE);
- fWrapInit= false;
- fCaseInit= false;
- fIsRegExInit= false;
- fWholeWordInit= false;
- fIncrementalInit= false;
- fGlobalInit= true;
- fForwardInit= true;
-
readConfiguration();
+ updateButtonState();
setShellStyle(getShellStyle() ^ SWT.APPLICATION_MODAL | SWT.MODELESS);
setBlockOnOpen(false);
@@ -333,12 +274,15 @@ private Composite createButtonSection(Composite parent) {
fFindNextButton= makeButton(panel, EditorMessages.FindReplace_FindNextButton_label, 102, true, new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
- if (isIncrementalSearch() && !isRegExSearchAvailableAndChecked())
- initIncrementalBaseLocation();
+ if (findReplacer.isIncrementalSearch() && !findReplacer.isRegExSearchAvailableAndChecked())
+ findReplacer.initIncrementalBaseLocation();
- fNeedsInitialFindBeforeReplace= false;
- performSearch(((e.stateMask & SWT.MODIFIER_MASK) == SWT.SHIFT) ^ isForwardSearch());
+ findReplacer.setNeedsInitialFindBeforeReplace(false);
+ boolean somethingFound = findReplacer.performSearch(getFindString());
+ writeSelection();
+ updateButtonState(!somethingFound);
updateFindHistory();
+ evaluateFindReplacerStatus();
}
});
setGridData(fFindNextButton, SWT.FILL, true, SWT.FILL, false);
@@ -347,8 +291,11 @@ public void widgetSelected(SelectionEvent e) {
new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
- performSelectAll();
+ findReplacer.performSelectAll(getFindString(), fActiveShell.getDisplay());
+ writeSelection();
+ updateButtonState();
updateFindAndReplaceHistory();
+ evaluateFindReplacerStatus();
}
});
setGridData(fSelectAllButton, SWT.FILL, true, SWT.FILL, false);
@@ -358,11 +305,12 @@ public void widgetSelected(SelectionEvent e) {
fReplaceFindButton= makeButton(panel, EditorMessages.FindReplace_ReplaceFindButton_label, 103, false, new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
- if (fNeedsInitialFindBeforeReplace)
- performSearch(((e.stateMask & SWT.MODIFIER_MASK) == SWT.SHIFT) ^ isForwardSearch());
- if (performReplaceSelection())
- performSearch(((e.stateMask & SWT.MODIFIER_MASK) == SWT.SHIFT) ^ isForwardSearch());
+ if (findReplacer.performFindFirstThenReplaceInASecondStep(getFindString(), getReplaceString())) {
+ writeSelection();
+ }
+ updateButtonState();
updateFindAndReplaceHistory();
+ evaluateFindReplacerStatus();
}
});
setGridData(fReplaceFindButton, SWT.FILL, false, SWT.FILL, false);
@@ -370,11 +318,12 @@ public void widgetSelected(SelectionEvent e) {
fReplaceSelectionButton= makeButton(panel, EditorMessages.FindReplace_ReplaceSelectionButton_label, 104, false, new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
- if (fNeedsInitialFindBeforeReplace)
- performSearch();
- performReplaceSelection();
+ if (findReplacer.performSelectAndReplace(getFindString(), getReplaceString())) {
+ writeSelection();
+ }
updateButtonState();
updateFindAndReplaceHistory();
+ evaluateFindReplacerStatus();
}
});
setGridData(fReplaceSelectionButton, SWT.FILL, false, SWT.FILL, false);
@@ -382,14 +331,17 @@ public void widgetSelected(SelectionEvent e) {
fReplaceAllButton= makeButton(panel, EditorMessages.FindReplace_ReplaceAllButton_label, 105, false, new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
- performReplaceAll();
+ findReplacer.performReplaceAll(getFindString(), getReplaceString(), fActiveShell.getDisplay());
+ writeSelection();
+ updateButtonState();
updateFindAndReplaceHistory();
+ evaluateFindReplacerStatus();
}
});
setGridData(fReplaceAllButton, SWT.FILL, true, SWT.FILL, false);
// Make the all the buttons the same size as the Remove Selection button.
- fReplaceAllButton.setEnabled(isEditable());
+ fReplaceAllButton.setEnabled(findReplacer.isEditable());
return panel;
}
@@ -535,12 +487,15 @@ private Composite createDirectionGroup(Composite parent) {
SelectionListener selectionListener= new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
- if (isIncrementalSearch() && !isRegExSearchAvailableAndChecked())
- initIncrementalBaseLocation();
+ if (findReplacer.isIncrementalSearch() && !findReplacer.isRegExSearchAvailableAndChecked())
+ findReplacer.initIncrementalBaseLocation();
+
+ findReplacer.setForwardSearch(fForwardRadioButton.getSelection());
}
@Override
public void widgetDefaultSelected(SelectionEvent e) {
+ // Do nothing
}
};
@@ -556,8 +511,9 @@ public void widgetDefaultSelected(SelectionEvent e) {
backwardRadioButton.addSelectionListener(selectionListener);
storeButtonWithMnemonicInMap(backwardRadioButton);
- backwardRadioButton.setSelection(!fForwardInit);
- fForwardRadioButton.setSelection(fForwardInit);
+ findReplacer.setForwardSearch(true); // search forward by default
+ backwardRadioButton.setSelection(!findReplacer.isForwardSearch());
+ fForwardRadioButton.setSelection(findReplacer.isForwardSearch());
return panel;
}
@@ -586,14 +542,14 @@ private Composite createScopeGroup(Composite parent) {
fGlobalRadioButton= new Button(group, SWT.RADIO | SWT.LEFT);
fGlobalRadioButton.setText(EditorMessages.FindReplace_GlobalRadioButton_label);
setGridData(fGlobalRadioButton, SWT.LEFT, false, SWT.CENTER, false);
- fGlobalRadioButton.setSelection(fGlobalInit);
+ fGlobalRadioButton.setSelection(findReplacer.isGlobalSearch());
fGlobalRadioButton.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
- if (!fGlobalRadioButton.getSelection() || !fUseSelectedLines)
+ if (!fGlobalRadioButton.getSelection() || findReplacer.isGlobalSearch())
return;
- fUseSelectedLines= false;
- useSelectedLines(false);
+ findReplacer.setGlobalSearch(true);
+ findReplacer.useSelectedLines(false);
}
@Override
@@ -605,15 +561,14 @@ public void widgetDefaultSelected(SelectionEvent e) {
fSelectedRangeRadioButton= new Button(group, SWT.RADIO | SWT.LEFT);
fSelectedRangeRadioButton.setText(EditorMessages.FindReplace_SelectedRangeRadioButton_label);
setGridData(fSelectedRangeRadioButton, SWT.LEFT, false, SWT.CENTER, false);
- fSelectedRangeRadioButton.setSelection(!fGlobalInit);
- fUseSelectedLines= !fGlobalInit;
+ fSelectedRangeRadioButton.setSelection(!findReplacer.isGlobalSearch());
fSelectedRangeRadioButton.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
- if (!fSelectedRangeRadioButton.getSelection() || fUseSelectedLines)
+ if (!fSelectedRangeRadioButton.getSelection() || !findReplacer.isGlobalSearch())
return;
- fUseSelectedLines= true;
- useSelectedLines(true);
+ findReplacer.setGlobalSearch(false);
+ findReplacer.useSelectedLines(true);
}
@Override
@@ -625,42 +580,6 @@ public void widgetDefaultSelected(SelectionEvent e) {
return panel;
}
- /**
- * Tells the dialog to perform searches only in the scope given by the actually selected lines.
- * @param selectedLines true
if selected lines should be used
- * @since 2.0
- */
- private void useSelectedLines(boolean selectedLines) {
- if (isIncrementalSearch() && !isRegExSearchAvailableAndChecked())
- initIncrementalBaseLocation();
-
- if (fTarget == null || !(fTarget instanceof IFindReplaceTargetExtension))
- return;
-
- IFindReplaceTargetExtension extensionTarget= (IFindReplaceTargetExtension) fTarget;
-
- if (selectedLines) {
-
- IRegion scope;
- if (fOldScope == null) {
- Point lineSelection= extensionTarget.getLineSelection();
- scope= new Region(lineSelection.x, lineSelection.y);
- } else {
- scope= fOldScope;
- fOldScope= null;
- }
-
- int offset= isForwardSearch()
- ? scope.getOffset()
- : scope.getOffset() + scope.getLength();
-
- extensionTarget.setSelection(offset, 0);
- extensionTarget.setScope(scope);
- } else {
- fOldScope= extensionTarget.getScope();
- extensionTarget.setScope(null);
- }
- }
/**
* Creates the panel where the user specifies the text to search
@@ -743,6 +662,7 @@ private Composite createOptionsGroup(Composite parent) {
SelectionListener selectionListener= new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
+ setupFindReplacer();
storeSettings();
}
@@ -754,34 +674,35 @@ public void widgetDefaultSelected(SelectionEvent e) {
fCaseCheckBox= new Button(group, SWT.CHECK | SWT.LEFT);
fCaseCheckBox.setText(EditorMessages.FindReplace_CaseCheckBox_label);
setGridData(fCaseCheckBox, SWT.LEFT, false, SWT.CENTER, false);
- fCaseCheckBox.setSelection(fCaseInit);
+ fCaseCheckBox.setSelection(findReplacer.isCaseSensitiveSearch());
fCaseCheckBox.addSelectionListener(selectionListener);
storeButtonWithMnemonicInMap(fCaseCheckBox);
fWrapCheckBox= new Button(group, SWT.CHECK | SWT.LEFT);
fWrapCheckBox.setText(EditorMessages.FindReplace_WrapCheckBox_label);
setGridData(fWrapCheckBox, SWT.LEFT, false, SWT.CENTER, false);
- fWrapCheckBox.setSelection(fWrapInit);
+ fWrapCheckBox.setSelection(findReplacer.isWrapSearch());
fWrapCheckBox.addSelectionListener(selectionListener);
storeButtonWithMnemonicInMap(fWrapCheckBox);
fWholeWordCheckBox= new Button(group, SWT.CHECK | SWT.LEFT);
fWholeWordCheckBox.setText(EditorMessages.FindReplace_WholeWordCheckBox_label);
setGridData(fWholeWordCheckBox, SWT.LEFT, false, SWT.CENTER, false);
- fWholeWordCheckBox.setSelection(fWholeWordInit);
+ fWholeWordCheckBox.setSelection(findReplacer.isWholeWordSearchSetting());
fWholeWordCheckBox.addSelectionListener(selectionListener);
storeButtonWithMnemonicInMap(fWholeWordCheckBox);
fIncrementalCheckBox= new Button(group, SWT.CHECK | SWT.LEFT);
fIncrementalCheckBox.setText(EditorMessages.FindReplace_IncrementalCheckBox_label);
setGridData(fIncrementalCheckBox, SWT.LEFT, false, SWT.CENTER, false);
- fIncrementalCheckBox.setSelection(fIncrementalInit);
+ fIncrementalCheckBox.setSelection(findReplacer.isIncrementalSearch());
fIncrementalCheckBox.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
- if (isIncrementalSearch() && !isRegExSearch())
- initIncrementalBaseLocation();
+ if (findReplacer.isIncrementalSearch() && !findReplacer.isRegexSearch())
+ findReplacer.initIncrementalBaseLocation();
+ setupFindReplacer();
storeSettings();
}
@@ -795,26 +716,27 @@ public void widgetDefaultSelected(SelectionEvent e) {
fIsRegExCheckBox.setText(EditorMessages.FindReplace_RegExCheckbox_label);
setGridData(fIsRegExCheckBox, SWT.LEFT, false, SWT.CENTER, false);
((GridData)fIsRegExCheckBox.getLayoutData()).horizontalSpan= 2;
- fIsRegExCheckBox.setSelection(fIsRegExInit);
+ fIsRegExCheckBox.setSelection(findReplacer.isRegexSearch());
fIsRegExCheckBox.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
boolean newState= fIsRegExCheckBox.getSelection();
fIncrementalCheckBox.setEnabled(!newState);
- updateButtonState();
+ setupFindReplacer();
storeSettings();
+ updateButtonState();
setContentAssistsEnablement(newState);
}
});
storeButtonWithMnemonicInMap(fIsRegExCheckBox);
- fWholeWordCheckBox.setEnabled(!isRegExSearchAvailableAndChecked());
+ fWholeWordCheckBox.setEnabled(!findReplacer.isRegExSearchAvailableAndChecked());
fWholeWordCheckBox.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
updateButtonState();
}
});
- fIncrementalCheckBox.setEnabled(!isRegExSearchAvailableAndChecked());
+ fIncrementalCheckBox.setEnabled(!findReplacer.isRegExSearchAvailableAndChecked());
return panel;
}
@@ -856,142 +778,6 @@ protected void buttonPressed(int buttonID) {
// ------- action invocation ---------------------------------------
- /**
- * Returns the position of the specified search string, or -1
if the string can not
- * be found when searching using the given options.
- *
- * @param findString the string to search for
- * @param startPosition the position at which to start the search
- * @param forwardSearch the direction of the search
- * @param caseSensitive should the search be case sensitive
- * @param wrapSearch should the search wrap to the start/end if arrived at the end/start
- * @param wholeWord does the search string represent a complete word
- * @param regExSearch if true
findString represents a regular expression
- * @param beep if true
beeps when search does not find a match or needs to wrap
- * @return the occurrence of the find string following the options or -1
if nothing
- * found
- * @since 3.0
- */
- private int findIndex(String findString, int startPosition, boolean forwardSearch, boolean caseSensitive, boolean wrapSearch, boolean wholeWord, boolean regExSearch, boolean beep) {
-
- if (forwardSearch) {
- int index= findAndSelect(startPosition, findString, true, caseSensitive, wholeWord, regExSearch);
- if (index == -1) {
-
- if (beep && okToUse(getShell()))
- getShell().getDisplay().beep();
-
- if (wrapSearch) {
- statusMessage(EditorMessages.FindReplace_Status_wrapped_label);
- index= findAndSelect(-1, findString, true, caseSensitive, wholeWord, regExSearch);
- }
- }
- return index;
- }
-
- // backward
- int index= startPosition == 0 ? -1 : findAndSelect(startPosition - 1, findString, false, caseSensitive, wholeWord, regExSearch);
- if (index == -1) {
-
- if (beep && okToUse(getShell()))
- getShell().getDisplay().beep();
-
- if (wrapSearch) {
- statusMessage(EditorMessages.FindReplace_Status_wrapped_label);
- index= findAndSelect(-1, findString, false, caseSensitive, wholeWord, regExSearch);
- }
- }
- return index;
- }
-
- /**
- * Searches for a string starting at the given offset and using the specified search
- * directives. If a string has been found it is selected and its start offset is
- * returned.
- *
- * @param offset the offset at which searching starts
- * @param findString the string which should be found
- * @param forwardSearch the direction of the search
- * @param caseSensitive true
performs a case sensitive search, false
an insensitive search
- * @param wholeWord if true
only occurrences are reported in which the findString stands as a word by itself
- * @param regExSearch if true
findString represents a regular expression
- * @return the position of the specified string, or -1 if the string has not been found
- * @since 3.0
- */
- private int findAndSelect(int offset, String findString, boolean forwardSearch, boolean caseSensitive, boolean wholeWord, boolean regExSearch) {
- if (fTarget instanceof IFindReplaceTargetExtension3)
- return ((IFindReplaceTargetExtension3)fTarget).findAndSelect(offset, findString, forwardSearch, caseSensitive, wholeWord, regExSearch);
- return fTarget.findAndSelect(offset, findString, forwardSearch, caseSensitive, wholeWord);
- }
-
- /**
- * Replaces the selection with replaceString
. If
- * regExReplace
is true
,
- * replaceString
is a regex replace pattern which will get
- * expanded if the underlying target supports it. Returns the region of the
- * inserted text; note that the returned selection covers the expanded
- * pattern in case of regex replace.
- *
- * @param replaceString the replace string (or a regex pattern)
- * @param regExReplace true
if replaceString
- * is a pattern
- * @return the selection after replacing, i.e. the inserted text
- * @since 3.0
- */
- Point replaceSelection(String replaceString, boolean regExReplace) {
- if (fTarget instanceof IFindReplaceTargetExtension3)
- ((IFindReplaceTargetExtension3)fTarget).replaceSelection(replaceString, regExReplace);
- else
- fTarget.replaceSelection(replaceString);
-
- return fTarget.getSelection();
- }
-
- /**
- * Returns whether the specified search string can be found using the given options.
- *
- * @param findString the string to search for
- * @param forwardSearch the direction of the search
- * @param caseSensitive should the search be case sensitive
- * @param wrapSearch should the search wrap to the start/end if arrived at the end/start
- * @param wholeWord does the search string represent a complete word
- * @param incremental is this an incremental search
- * @param regExSearch if true
findString represents a regular expression
- * @param beep if true
beeps when search does not find a match or needs to wrap
- * @return true
if the search string can be found using the given options
- *
- * @since 3.0
- */
- private boolean findNext(String findString, boolean forwardSearch, boolean caseSensitive, boolean wrapSearch, boolean wholeWord, boolean incremental, boolean regExSearch, boolean beep) {
-
- if (fTarget == null)
- return false;
-
- Point r= null;
- if (incremental)
- r= fIncrementalBaseLocation;
- else
- r= fTarget.getSelection();
-
- int findReplacePosition= r.x;
- if (forwardSearch && !fNeedsInitialFindBeforeReplace || !forwardSearch && fNeedsInitialFindBeforeReplace)
- findReplacePosition += r.y;
-
- fNeedsInitialFindBeforeReplace= false;
-
- int index= findIndex(findString, findReplacePosition, forwardSearch, caseSensitive, wrapSearch, wholeWord, regExSearch, beep);
-
- if (index == -1) {
- String msg= NLSUtility.format(EditorMessages.FindReplace_Status_noMatchWithValue_label, findString);
- statusMessage(false, EditorMessages.FindReplace_Status_noMatch_label, msg);
- return false;
- }
-
- if (forwardSearch && index >= findReplacePosition || !forwardSearch && index <= findReplacePosition)
- statusMessage(""); //$NON-NLS-1$
-
- return true;
- }
/**
* Returns the dialog's boundaries.
@@ -1091,13 +877,10 @@ private void handleDialogClose() {
// store current settings in case of re-open
storeSettings();
- if (fTarget != null && fTarget instanceof IFindReplaceTargetExtension)
- ((IFindReplaceTargetExtension) fTarget).endSession();
+ findReplacer.endSession();
// prevent leaks
fActiveShell= null;
- fTarget= null;
-
}
/**
@@ -1105,11 +888,12 @@ private void handleDialogClose() {
* @since 3.0
*/
private void writeSelection() {
- if (fTarget == null)
+ String selection = findReplacer.getCurrentSelection();
+ if (selection == null)
return;
IDialogSettings s= getDialogSettings();
- s.put("selection", fTarget.getSelectionText()); //$NON-NLS-1$
+ s.put("selection", selection); //$NON-NLS-1$
}
/**
@@ -1118,12 +902,6 @@ private void writeSelection() {
*/
private void storeSettings() {
fDialogPositionInit= getDialogBoundaries();
- fWrapInit= isWrapSearch();
- fWholeWordInit= isWholeWordSetting();
- fCaseInit= isCaseSensitiveSearch();
- fIsRegExInit= isRegExSearch();
- fIncrementalInit= isIncrementalSearch();
- fForwardInit= isForwardSearch();
writeConfiguration();
}
@@ -1134,9 +912,9 @@ private void storeSettings() {
* action's target.
*/
private void initFindStringFromSelection() {
- if (fTarget != null && okToUse(fFindField)) {
- String fullSelection= fTarget.getSelectionText();
- boolean isRegEx= isRegExSearchAvailableAndChecked();
+ String fullSelection = findReplacer.getCurrentSelection();
+ if (fullSelection != null && okToUse(fFindField)) {
+ boolean isRegEx = findReplacer.isRegExSearchAvailableAndChecked();
fFindField.removeModifyListener(fFindModifyListener);
if (!fullSelection.isEmpty()) {
String firstLine= getFirstLine(fullSelection);
@@ -1144,10 +922,9 @@ private void initFindStringFromSelection() {
fFindField.setText(pattern);
if (!firstLine.equals(fullSelection)) {
// multiple lines selected
- useSelectedLines(true);
+ findReplacer.useSelectedLines(true);
fGlobalRadioButton.setSelection(false);
fSelectedRangeRadioButton.setSelection(true);
- fUseSelectedLines= true;
}
} else {
if ("".equals(fFindField.getText())) { //$NON-NLS-1$
@@ -1162,114 +939,6 @@ private void initFindStringFromSelection() {
}
}
- /**
- * Initializes the anchor used as starting point for incremental searching.
- * @since 2.0
- */
- private void initIncrementalBaseLocation() {
- if (fTarget != null && isIncrementalSearch() && !isRegExSearchAvailableAndChecked()) {
- fIncrementalBaseLocation= fTarget.getSelection();
- } else {
- fIncrementalBaseLocation= new Point(0, 0);
- }
- }
-
- // ------- history ---------------------------------------
-
- /**
- * Retrieves and returns the option case sensitivity from the appropriate check box.
- * @return true
if case sensitive
- */
- private boolean isCaseSensitiveSearch() {
- if (okToUse(fCaseCheckBox)) {
- return fCaseCheckBox.getSelection();
- }
- return fCaseInit;
- }
-
- /**
- * Retrieves and returns the regEx option from the appropriate check box.
- *
- * @return true
if case sensitive
- * @since 3.0
- */
- private boolean isRegExSearch() {
- if (okToUse(fIsRegExCheckBox)) {
- return fIsRegExCheckBox.getSelection();
- }
- return fIsRegExInit;
- }
-
- /**
- * If the target supports regular expressions search retrieves and returns
- * regEx option from appropriate check box.
- *
- * @return true
if regEx is available and checked
- * @since 3.0
- */
- private boolean isRegExSearchAvailableAndChecked() {
- if (okToUse(fIsRegExCheckBox)) {
- return fIsTargetSupportingRegEx && fIsRegExCheckBox.getSelection();
- }
- return fIsRegExInit;
- }
-
- /**
- * Retrieves and returns the option search direction from the appropriate check box.
- * @return true
if searching forward
- */
- private boolean isForwardSearch() {
- if (okToUse(fForwardRadioButton)) {
- return fForwardRadioButton.getSelection();
- }
- return fForwardInit;
- }
-
- /**
- * Retrieves and returns the option search whole words from the appropriate check box.
- * @return true
if searching for whole words
- */
- private boolean isWholeWordSetting() {
- if (okToUse(fWholeWordCheckBox)) {
- return fWholeWordCheckBox.getSelection();
- }
- return fWholeWordInit;
- }
-
- /**
- * Returns true
if searching should be restricted to entire
- * words, false
if not. This is the case if the respective
- * checkbox is turned on, regex is off, and the checkbox is enabled, i.e.
- * the current find string is an entire word.
- *
- * @return true
if the search is restricted to whole words
- */
- private boolean isWholeWordSearch() {
- return isWholeWordSetting() && !isRegExSearchAvailableAndChecked() && (okToUse(fWholeWordCheckBox) ? fWholeWordCheckBox.isEnabled() : true);
- }
-
- /**
- * Retrieves and returns the option wrap search from the appropriate check box.
- * @return true
if wrapping while searching
- */
- private boolean isWrapSearch() {
- if (okToUse(fWrapCheckBox)) {
- return fWrapCheckBox.getSelection();
- }
- return fWrapInit;
- }
-
- /**
- * Retrieves and returns the option incremental search from the appropriate check box.
- * @return true
if incremental search
- * @since 2.0
- */
- private boolean isIncrementalSearch() {
- if (okToUse(fIncrementalCheckBox)) {
- return fIncrementalCheckBox.getSelection();
- }
- return fIncrementalInit;
- }
/**
* Creates a button.
@@ -1299,331 +968,6 @@ private void storeButtonWithMnemonicInMap(Button button) {
fMnemonicButtonMap.put(Character.valueOf(Character.toLowerCase(mnemonic)), button);
}
- /**
- * Returns the status line manager of the active editor or null
if there is no such editor.
- * @return the status line manager of the active editor
- */
- private IEditorStatusLine getStatusLineManager() {
- IWorkbenchWindow window= PlatformUI.getWorkbench().getActiveWorkbenchWindow();
- if (window == null)
- return null;
-
- IWorkbenchPage page= window.getActivePage();
- if (page == null)
- return null;
-
- IEditorPart editor= page.getActiveEditor();
- if (editor == null)
- return null;
-
- return editor.getAdapter(IEditorStatusLine.class);
- }
-
- /**
- * Sets the given status message in the status line.
- *
- * @param error true
if it is an error
- * @param dialogMessage the message to display in the dialog's status line
- * @param editorMessage the message to display in the editor's status line
- */
- private void statusMessage(boolean error, String dialogMessage, String editorMessage) {
- fStatusLabel.setText(dialogMessage);
-
- if (error)
- fStatusLabel.setForeground(JFaceColors.getErrorText(fStatusLabel.getDisplay()));
- else
- fStatusLabel.setForeground(null);
-
- IEditorStatusLine statusLine= getStatusLineManager();
- if (statusLine != null)
- statusLine.setMessage(error, editorMessage, null);
-
- if (error)
- getShell().getDisplay().beep();
- }
-
- /**
- * Sets the given error message in the status line.
- * @param message the message
- */
- private void statusError(String message) {
- statusMessage(true, message, message);
- }
-
- /**
- * Sets the given message in the status line.
- * @param message the message
- */
- private void statusMessage(String message) {
- statusMessage(false, message, message);
- }
-
- /**
- * Replaces all occurrences of the user's findString with
- * the replace string. Indicate to the user the number of replacements
- * that occur.
- */
- private void performReplaceAll() {
-
- int replaceCount= 0;
- final String replaceString= getReplaceString();
- final String findString= getFindString();
-
- if (findString != null && !findString.isEmpty()) {
-
- class ReplaceAllRunnable implements Runnable {
- public int numberOfOccurrences;
- @Override
- public void run() {
- numberOfOccurrences = replaceAll(findString, replaceString == null ? "" : replaceString, //$NON-NLS-1$
- isCaseSensitiveSearch(), isWholeWordSearch(), isRegExSearchAvailableAndChecked());
- }
- }
-
- try {
- ReplaceAllRunnable runnable= new ReplaceAllRunnable();
- BusyIndicator.showWhile(fActiveShell.getDisplay(), runnable);
- replaceCount= runnable.numberOfOccurrences;
-
- if (replaceCount != 0) {
- if (replaceCount == 1) { // not plural
- statusMessage(EditorMessages.FindReplace_Status_replacement_label);
- } else {
- String msg= EditorMessages.FindReplace_Status_replacements_label;
- msg= NLSUtility.format(msg, String.valueOf(replaceCount));
- statusMessage(msg);
- }
- } else {
- String msg= NLSUtility.format(EditorMessages.FindReplace_Status_noMatchWithValue_label, findString);
- statusMessage(false, EditorMessages.FindReplace_Status_noMatch_label, msg);
- }
- } catch (PatternSyntaxException ex) {
- statusError(ex.getLocalizedMessage());
- } catch (IllegalStateException ex) {
- // we don't keep state in this dialog
- }
- }
- writeSelection();
- updateButtonState();
- }
-
- /**
- * Replaces all occurrences of the user's findString with the replace string.
- * Indicate to the user the number of replacements that occur.
- */
- private void performSelectAll() {
-
- int selectCount = 0;
- final String findString = getFindString();
-
- if (findString != null && !findString.isEmpty()) {
-
- class SelectAllRunnable implements Runnable {
- public int numberOfOccurrences;
-
- @Override
- public void run() {
- numberOfOccurrences = selectAll(findString, isCaseSensitiveSearch(), isWholeWordSearch(),
- isRegExSearchAvailableAndChecked());
- }
- }
-
- try {
- SelectAllRunnable runnable = new SelectAllRunnable();
- BusyIndicator.showWhile(fActiveShell.getDisplay(), runnable);
- selectCount = runnable.numberOfOccurrences;
-
- if (selectCount != 0) {
- if (selectCount == 1) { // not plural
- statusMessage(EditorMessages.FindReplace_Status_selection_label);
- } else {
- String msg = EditorMessages.FindReplace_Status_selections_label;
- msg = NLSUtility.format(msg, String.valueOf(selectCount));
- statusMessage(msg);
- }
- } else {
- String msg = NLSUtility.format(EditorMessages.FindReplace_Status_noMatchWithValue_label,
- findString);
- statusMessage(false, EditorMessages.FindReplace_Status_noMatch_label, msg);
- }
- } catch (PatternSyntaxException ex) {
- statusError(ex.getLocalizedMessage());
- } catch (IllegalStateException ex) {
- // we don't keep state in this dialog
- }
- }
- writeSelection();
- updateButtonState();
- }
-
- /**
- * Validates the state of the find/replace target.
- *
- * @return true
if target can be changed, false
- * otherwise
- * @since 2.1
- */
- private boolean validateTargetState() {
-
- if (fTarget instanceof IFindReplaceTargetExtension2) {
- IFindReplaceTargetExtension2 extension= (IFindReplaceTargetExtension2) fTarget;
- if (!extension.validateTargetState()) {
- statusError(EditorMessages.FindReplaceDialog_read_only);
- updateButtonState();
- return false;
- }
- }
- return isEditable();
- }
-
- /**
- * Replaces the current selection of the target with the user's
- * replace string.
- *
- * @return true
if the operation was successful
- */
- private boolean performReplaceSelection() {
-
- if (!validateTargetState())
- return false;
-
- String replaceString= getReplaceString();
- if (replaceString == null)
- replaceString= ""; //$NON-NLS-1$
-
- boolean replaced;
- try {
- replaceSelection(replaceString, isRegExSearchAvailableAndChecked());
- replaced= true;
- writeSelection();
- } catch (PatternSyntaxException ex) {
- statusError(ex.getLocalizedMessage());
- replaced= false;
- } catch (IllegalStateException ex) {
- replaced= false;
- }
-
- return replaced;
- }
-
- /**
- * Locates the user's findString in the text of the target.
- */
- private void performSearch() {
- performSearch(isForwardSearch());
- }
-
- /**
- * Locates the user's findString in the text of the target.
- *
- * @param forwardSearch true
if searching forwards, false
otherwise
- * @since 3.7
- */
- private void performSearch(boolean forwardSearch) {
- performSearch(isIncrementalSearch() && !isRegExSearchAvailableAndChecked(), true, forwardSearch);
- }
-
- /**
- * Locates the user's findString in the text of the target.
- *
- * @param mustInitIncrementalBaseLocation true
if base location must be initialized
- * @param beep if true
beeps when search does not find a match or needs to wrap
- * @param forwardSearch the search direction
- * @since 3.0
- */
- private void performSearch(boolean mustInitIncrementalBaseLocation, boolean beep, boolean forwardSearch) {
-
- if (mustInitIncrementalBaseLocation)
- initIncrementalBaseLocation();
-
- String findString= getFindString();
- boolean somethingFound= false;
-
- if (findString != null && !findString.isEmpty()) {
-
- try {
- somethingFound= findNext(findString, forwardSearch, isCaseSensitiveSearch(), isWrapSearch(), isWholeWordSearch(), isIncrementalSearch() && !isRegExSearchAvailableAndChecked(), isRegExSearchAvailableAndChecked(), beep);
- } catch (PatternSyntaxException ex) {
- statusError(ex.getLocalizedMessage());
- } catch (IllegalStateException ex) {
- // we don't keep state in this dialog
- }
- }
- writeSelection();
- updateButtonState(!somethingFound);
- }
-
- /**
- * Replaces all occurrences of the user's findString with
- * the replace string. Returns the number of replacements
- * that occur.
- *
- * @param findString the string to search for
- * @param replaceString the replacement string
- * @param caseSensitive should the search be case sensitive
- * @param wholeWord does the search string represent a complete word
- * @param regExSearch if true
findString represents a regular expression
- * @return the number of occurrences
- *
- * @since 3.0
- */
- private int replaceAll(String findString, String replaceString, boolean caseSensitive, boolean wholeWord,
- boolean regExSearch) {
-
- int replaceCount= 0;
- int findReplacePosition= 0;
-
- findReplacePosition= 0;
-
- if (!validateTargetState())
- return replaceCount;
-
- if (fTarget instanceof IFindReplaceTargetExtension)
- ((IFindReplaceTargetExtension) fTarget).setReplaceAllMode(true);
-
- try {
- int index= 0;
- while (index != -1) {
- index = findAndSelect(findReplacePosition, findString, true, caseSensitive, wholeWord, regExSearch);
- if (index != -1) { // substring not contained from current position
- Point selection= replaceSelection(replaceString, regExSearch);
- replaceCount++;
- findReplacePosition = selection.x + selection.y;
- }
- }
- } finally {
- if (fTarget instanceof IFindReplaceTargetExtension)
- ((IFindReplaceTargetExtension) fTarget).setReplaceAllMode(false);
- }
-
- return replaceCount;
- }
-
- private int selectAll(String findString, boolean caseSensitive, boolean wholeWord, boolean regExSearch) {
-
- int replaceCount = 0;
- int position = 0;
-
- if (!validateTargetState())
- return replaceCount;
-
- Listtrue
if target is editable
- */
- private boolean isEditable() {
- boolean isEditable= (fTarget == null ? false : fTarget.isEditable());
- return fIsTargetEditable && isEditable;
- }
-
/**
* Updates this dialog because of a different target.
* @param target the new target
@@ -1796,49 +1139,35 @@ private boolean isEditable() {
* @since 2.0
*/
public void updateTarget(IFindReplaceTarget target, boolean isTargetEditable, boolean initializeFindString) {
+ findReplacer.updateTarget(target, isTargetEditable);
- fIsTargetEditable= isTargetEditable;
- fNeedsInitialFindBeforeReplace= true;
-
- if (target != fTarget) {
- if (fTarget != null && fTarget instanceof IFindReplaceTargetExtension)
- ((IFindReplaceTargetExtension) fTarget).endSession();
-
- fTarget= target;
- if (fTarget != null)
- fIsTargetSupportingRegEx= fTarget instanceof IFindReplaceTargetExtension3;
-
- if (fTarget instanceof IFindReplaceTargetExtension) {
- ((IFindReplaceTargetExtension) fTarget).beginSession();
-
- fGlobalInit= true;
- fGlobalRadioButton.setSelection(fGlobalInit);
- fSelectedRangeRadioButton.setSelection(!fGlobalInit);
- fUseSelectedLines= !fGlobalInit;
- }
- }
+ boolean globalSearch = findReplacer.isGlobalSearch();
+ fGlobalRadioButton.setSelection(globalSearch);
+ boolean useSelectedLines = !globalSearch;
+ fSelectedRangeRadioButton.setSelection(useSelectedLines);
+ findReplacer.useSelectedLines(useSelectedLines);
if (okToUse(fIsRegExCheckBox))
- fIsRegExCheckBox.setEnabled(fIsTargetSupportingRegEx);
+ fIsRegExCheckBox.setEnabled(findReplacer.isTargetSupportingRegEx());
if (okToUse(fWholeWordCheckBox))
- fWholeWordCheckBox.setEnabled(!isRegExSearchAvailableAndChecked());
+ fWholeWordCheckBox.setEnabled(!findReplacer.isRegExSearchAvailableAndChecked());
if (okToUse(fIncrementalCheckBox))
- fIncrementalCheckBox.setEnabled(!isRegExSearchAvailableAndChecked());
+ fIncrementalCheckBox.setEnabled(!findReplacer.isRegExSearchAvailableAndChecked());
if (okToUse(fReplaceLabel)) {
- fReplaceLabel.setEnabled(isEditable());
- fReplaceField.setEnabled(isEditable());
+ fReplaceLabel.setEnabled(findReplacer.isEditable());
+ fReplaceField.setEnabled(findReplacer.isEditable());
if (initializeFindString) {
initFindStringFromSelection();
- fGiveFocusToFindField= true;
+ fGiveFocusToFindField = true;
}
- initIncrementalBaseLocation();
}
+
updateButtonState();
- setContentAssistsEnablement(isRegExSearchAvailableAndChecked());
+ setContentAssistsEnablement(findReplacer.isRegExSearchAvailableAndChecked());
}
/**
@@ -1901,11 +1230,11 @@ protected int getDialogBoundsStrategy() {
private void readConfiguration() {
IDialogSettings s= getDialogSettings();
- fWrapInit= s.get("wrap") == null || s.getBoolean("wrap"); //$NON-NLS-1$ //$NON-NLS-2$
- fCaseInit= s.getBoolean("casesensitive"); //$NON-NLS-1$
- fWholeWordInit= s.getBoolean("wholeword"); //$NON-NLS-1$
- fIncrementalInit= s.getBoolean("incremental"); //$NON-NLS-1$
- fIsRegExInit= s.getBoolean("isRegEx"); //$NON-NLS-1$
+ findReplacer.setWrapSearch(s.get("wrap") == null || s.getBoolean("wrap")); //$NON-NLS-1$ //$NON-NLS-2$
+ findReplacer.setCaseSensitiveSearch(s.getBoolean("casesensitive")); //$NON-NLS-1$
+ findReplacer.setWholeWordSearchSetting(s.getBoolean("wholeword")); //$NON-NLS-1$
+ findReplacer.setIncrementalSearch(s.getBoolean("incremental")); //$NON-NLS-1$
+ findReplacer.setRegexSearch(s.getBoolean("isRegEx")); //$NON-NLS-1$
String[] findHistory= s.getArray("findhistory"); //$NON-NLS-1$
if (findHistory != null) {
@@ -1922,17 +1251,25 @@ private void readConfiguration() {
}
}
+ private void setupFindReplacer() {
+ findReplacer.setWrapSearch(fWrapCheckBox.getSelection());
+ findReplacer.setCaseSensitiveSearch(fCaseCheckBox.getSelection());
+ findReplacer.setWholeWordSearchSetting(fWholeWordCheckBox.getSelection());
+ findReplacer.setIncrementalSearch(fIncrementalCheckBox.getSelection());
+ findReplacer.setRegexSearch(fIsRegExCheckBox.getSelection());
+ }
+
/**
* Stores its current configuration in the dialog store.
*/
private void writeConfiguration() {
IDialogSettings s= getDialogSettings();
- s.put("wrap", fWrapInit); //$NON-NLS-1$
- s.put("casesensitive", fCaseInit); //$NON-NLS-1$
- s.put("wholeword", fWholeWordInit); //$NON-NLS-1$
- s.put("incremental", fIncrementalInit); //$NON-NLS-1$
- s.put("isRegEx", fIsRegExInit); //$NON-NLS-1$
+ s.put("wrap", findReplacer.isWrapSearch()); //$NON-NLS-1$
+ s.put("casesensitive", findReplacer.isCaseSensitiveSearch()); //$NON-NLS-1$
+ s.put("wholeword", findReplacer.isWholeWordSearchSetting()); //$NON-NLS-1$
+ s.put("incremental", findReplacer.isIncrementalSearch()); //$NON-NLS-1$
+ s.put("isRegEx", findReplacer.isRegexSearch()); //$NON-NLS-1$
Listtrue
if
+ * the target supports regular expressions
+ */
+ private boolean fIsTargetSupportingRegEx;
+ boolean fRegexSearch;
+ boolean fForwardSearch;
+ boolean fWholeWordSearch;
+ boolean fWrapSearch;
+ boolean fCaseSensitiveSearch;
+ boolean fGlobalSearch;
+ private boolean fTargetEditable;
+
+ public Point getIncrementalBaseLocation() {
+ return fIncrementalBaseLocation;
+ }
+
+ public void setIncrementalBaseLocation(Point incrementalBaseLocation) {
+ fIncrementalBaseLocation = incrementalBaseLocation;
+ }
+
+ public boolean isGlobalSearch() {
+ return fGlobalSearch;
+ }
+
+ public void setGlobalSearch(boolean globalSearch) {
+ fGlobalSearch = globalSearch;
+ }
+
+ public boolean needsInitialFindBeforeReplace() {
+ return fNeedsInitialFindBeforeReplace;
+ }
+
+ public void setNeedsInitialFindBeforeReplace(boolean needsInitialFindBeforeReplace) {
+ fNeedsInitialFindBeforeReplace = needsInitialFindBeforeReplace;
+ }
+
+ public boolean isCaseSensitiveSearch() {
+ return fCaseSensitiveSearch;
+ }
+
+ public void setCaseSensitiveSearch(boolean caseSensitiveSearch) {
+ fCaseSensitiveSearch = caseSensitiveSearch;
+ }
+
+ public boolean isWrapSearch() {
+ return fWrapSearch;
+ }
+
+ public void setWrapSearch(boolean wrapSearch) {
+ fWrapSearch = wrapSearch;
+ }
+
+ public boolean isWholeWordSearchSetting() {
+ return fWholeWordSearch;
+ }
+
+ public void setWholeWordSearchSetting(boolean wholeWordSearch) {
+ fWholeWordSearch = wholeWordSearch;
+ }
+
+ public FindReplaceStatus getStatus() {
+ return status;
+ }
+
+ public void setStatus(FindReplaceStatus status) {
+ this.status = status;
+ }
+
+ public void resetStatus() {
+ status.resetStatus();
+ }
+
+ /**
+ * Returns true
if searching should be restricted to entire words,
+ * false
if not. This is the case if the respective checkbox is
+ * turned on, regex is off, and the checkbox is enabled, i.e. the current find
+ * string is an entire word.
+ *
+ * @return true
if the search is restricted to whole words
+ */
+ private boolean isWholeWordSearch() {
+ return isWholeWordSearchSetting() && !isRegExSearchAvailableAndChecked();
+ }
+
+ public boolean isForwardSearch() {
+ return fForwardSearch;
+ }
+
+ public void setForwardSearch(boolean forwardSearch) {
+ fForwardSearch = forwardSearch;
+ }
+
+ public boolean isTargetSupportingRegEx() {
+ return fIsTargetSupportingRegEx;
+ }
+
+ public void setIsTargetSupportingRegEx(boolean isTargetSupportingRegEx) {
+ fIsTargetSupportingRegEx = isTargetSupportingRegEx;
+ }
+
+ public boolean isIncrementalSearch() {
+ return fIncrementalSearch;
+ }
+
+ public void setIncrementalSearch(boolean incrementalSearch) {
+ fIncrementalSearch = incrementalSearch;
+ }
+
+ public boolean isRegexSearch() {
+ return fRegexSearch;
+ }
+
+ public void setRegexSearch(boolean regexSearch) {
+ fRegexSearch = regexSearch;
+ }
+
+
+ public boolean isRegExSearchAvailableAndChecked() {
+ return isRegexSearch() && fIsTargetSupportingRegEx;
+ }
+
+
+ /**
+ * initializes the anchor used as starting point for incremental searching.
+ *
+ * @since 2.0
+ */
+ public void initIncrementalBaseLocation() {
+ if (fTarget != null && isIncrementalSearch() && !isRegExSearchAvailableAndChecked()) {
+ fIncrementalBaseLocation = fTarget.getSelection();
+ } else {
+ fIncrementalBaseLocation = new Point(0, 0);
+ }
+ }
+
+ /**
+ * Tells the dialog to perform searches only in the scope given by the actually
+ * selected lines.
+ *
+ * @param selectedLines true
if selected lines should be used
+ * @since 2.0
+ */
+ public void useSelectedLines(boolean selectedLines) {
+ if (isIncrementalSearch() && !isRegExSearchAvailableAndChecked())
+ initIncrementalBaseLocation();
+
+ if (fTarget == null || !(fTarget instanceof IFindReplaceTargetExtension))
+ return;
+
+ IFindReplaceTargetExtension extensionTarget = (IFindReplaceTargetExtension) fTarget;
+
+ if (selectedLines) {
+
+ IRegion scope;
+ if (fOldScope == null) {
+ Point lineSelection = extensionTarget.getLineSelection();
+ scope = new Region(lineSelection.x, lineSelection.y);
+ } else {
+ scope = fOldScope;
+ fOldScope = null;
+ }
+
+ int offset = isForwardSearch() ? scope.getOffset() : scope.getOffset() + scope.getLength();
+
+ extensionTarget.setSelection(offset, 0);
+ extensionTarget.setScope(scope);
+ } else {
+ fOldScope = extensionTarget.getScope();
+ extensionTarget.setScope(null);
+ }
+ }
+
+ /**
+ * Returns the status line manager of the active editor or null
if
+ * there is no such editor.
+ *
+ * @return the status line manager of the active editor
+ */
+ private IEditorStatusLine getStatusLineManager() {
+ IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
+ if (window == null)
+ return null;
+
+ IWorkbenchPage page = window.getActivePage();
+ if (page == null)
+ return null;
+
+ IEditorPart editor = page.getActiveEditor();
+ if (editor == null)
+ return null;
+
+ return editor.getAdapter(IEditorStatusLine.class);
+ }
+
+
+ /**
+ * Replaces all occurrences of the user's findString with the replace string.
+ * Indicate to the user the number of replacements that occur.
+ *
+ * @param findString
+ * @param replaceString
+ * @param display
+ */
+ public void performReplaceAll(String findString, String replaceString, Display display) {
+
+ int replaceCount = 0;
+
+ if (findString != null && !findString.isEmpty()) {
+
+ class ReplaceAllRunnable implements Runnable {
+ public int numberOfOccurrences;
+
+ @Override
+ public void run() {
+ numberOfOccurrences = replaceAll(findString, replaceString == null ? "" : replaceString, //$NON-NLS-1$
+ isCaseSensitiveSearch(), isWholeWordSearch(), isRegExSearchAvailableAndChecked());
+ }
+ }
+
+ try {
+ ReplaceAllRunnable runnable = new ReplaceAllRunnable();
+ BusyIndicator.showWhile(display, runnable);
+ replaceCount = runnable.numberOfOccurrences;
+
+ if (replaceCount != 0) {
+ if (replaceCount == 1) { // not plural
+ statusMessage(EditorMessages.FindReplace_Status_replacement_label);
+ } else {
+ String msg = EditorMessages.FindReplace_Status_replacements_label;
+ msg = NLSUtility.format(msg, String.valueOf(replaceCount));
+ statusMessage(msg);
+ }
+ } else {
+ String msg = NLSUtility.format(EditorMessages.FindReplace_Status_noMatchWithValue_label,
+ findString);
+ statusMessage(false, EditorMessages.FindReplace_Status_noMatch_label, msg);
+ }
+ } catch (PatternSyntaxException ex) {
+ statusError(ex.getLocalizedMessage());
+ } catch (IllegalStateException ex) {
+ // we don't keep state in this dialog
+ }
+ }
+ }
+
+ /**
+ * Replaces all occurrences of the user's findString with the replace string.
+ * Indicate to the user the number of replacements that occur.
+ */
+ public void performSelectAll(String findString, Display display) {
+
+ int selectCount = 0;
+
+ if (findString != null && !findString.isEmpty()) {
+
+ class SelectAllRunnable implements Runnable {
+ public int numberOfOccurrences;
+
+ @Override
+ public void run() {
+ numberOfOccurrences = selectAll(findString, isCaseSensitiveSearch(), isWholeWordSearch(),
+ isRegExSearchAvailableAndChecked());
+ }
+ }
+
+ try {
+ SelectAllRunnable runnable = new SelectAllRunnable();
+ BusyIndicator.showWhile(display, runnable);
+ selectCount = runnable.numberOfOccurrences;
+
+ if (selectCount != 0) {
+ if (selectCount == 1) { // not plural
+ statusMessage(EditorMessages.FindReplace_Status_selection_label);
+ } else {
+ String msg = EditorMessages.FindReplace_Status_selections_label;
+ msg = NLSUtility.format(msg, String.valueOf(selectCount));
+ statusMessage(msg);
+ }
+ } else {
+ String msg = NLSUtility.format(EditorMessages.FindReplace_Status_noMatchWithValue_label,
+ findString);
+ statusMessage(false, EditorMessages.FindReplace_Status_noMatch_label, msg);
+ }
+ } catch (PatternSyntaxException ex) {
+ statusError(ex.getLocalizedMessage());
+ } catch (IllegalStateException ex) {
+ // we don't keep state in this dialog
+ }
+ }
+ }
+
+ /**
+ * Validates the state of the find/replace target.
+ *
+ * @return true
if target can be changed, false
+ * otherwise
+ * @since 2.1
+ */
+ public boolean validateTargetState() {
+
+ if (fTarget instanceof IFindReplaceTargetExtension2) {
+ IFindReplaceTargetExtension2 extension = (IFindReplaceTargetExtension2) fTarget;
+ if (!extension.validateTargetState()) {
+ statusError(EditorMessages.FindReplaceDialog_read_only);
+ return false;
+ }
+ }
+ return isEditable();
+ }
+
+ /**
+ * Replaces the current selection of the target with the user's replace string.
+ *
+ * @return true
if the operation was successful
+ */
+ public boolean performReplaceSelection(String replaceString) {
+
+ if (!validateTargetState())
+ return false;
+
+ if (replaceString == null)
+ replaceString = ""; //$NON-NLS-1$
+
+ boolean replaced;
+ try {
+ replaceSelection(replaceString, isRegExSearchAvailableAndChecked());
+ replaced = true;
+ } catch (PatternSyntaxException ex) {
+ statusError(ex.getLocalizedMessage());
+ replaced = false;
+ } catch (IllegalStateException ex) {
+ replaced = false;
+ }
+
+ return replaced;
+ }
+
+ /**
+ * Locates the user's findString in the text of the target.
+ *
+ * @param searchString
+ * @return
+ *
+ * @since 3.7
+ */
+ public boolean performSearch(String searchString) {
+ return performSearch(isIncrementalSearch() && !isRegExSearchAvailableAndChecked(), searchString);
+ }
+
+ /**
+ * Locates the user's findString in the text of the target.
+ *
+ * @param mustInitIncrementalBaseLocation true
if base location
+ * must be initialized
+ * @param findString
+ * @return
+ * @since 3.0
+ */
+ public boolean performSearch(boolean mustInitIncrementalBaseLocation, String findString) {
+
+ if (mustInitIncrementalBaseLocation)
+ initIncrementalBaseLocation();
+
+ boolean somethingFound = false;
+
+ if (findString != null && !findString.isEmpty()) {
+
+ try {
+ somethingFound = findNext(findString, isForwardSearch());
+ } catch (PatternSyntaxException ex) {
+ statusError(ex.getLocalizedMessage());
+ } catch (IllegalStateException ex) {
+ // we don't keep state in this dialog
+ }
+ }
+ return somethingFound;
+ }
+
+ /**
+ * Replaces all occurrences of the user's findString with the replace string.
+ * Returns the number of replacements that occur.
+ *
+ * @param findString the string to search for
+ * @param replaceString the replacement string
+ * @param caseSensitive should the search be case sensitive
+ * @param wholeWord does the search string represent a complete word
+ * @param regExSearch if true
findString represents a regular
+ * expression
+ * @return the number of occurrences
+ *
+ * @since 3.0
+ */
+ public int replaceAll(String findString, String replaceString, boolean caseSensitive, boolean wholeWord,
+ boolean regExSearch) {
+
+ int replaceCount = 0;
+ int findReplacePosition = 0;
+
+ findReplacePosition = 0;
+
+ if (!validateTargetState())
+ return replaceCount;
+
+ if (fTarget instanceof IFindReplaceTargetExtension)
+ ((IFindReplaceTargetExtension) fTarget).setReplaceAllMode(true);
+
+ try {
+ int index = 0;
+ while (index != -1) {
+ index = findAndSelect(findReplacePosition, findString);
+ if (index != -1) { // substring not contained from current position
+ Point selection = replaceSelection(replaceString, regExSearch);
+ replaceCount++;
+ findReplacePosition = selection.x + selection.y;
+ }
+ }
+ } finally {
+ if (fTarget instanceof IFindReplaceTargetExtension)
+ ((IFindReplaceTargetExtension) fTarget).setReplaceAllMode(false);
+ }
+
+ return replaceCount;
+ }
+
+ public int selectAll(String findString, boolean caseSensitive, boolean wholeWord, boolean regExSearch) {
+
+ int replaceCount = 0;
+ int position = 0;
+
+ if (!validateTargetState())
+ return replaceCount;
+
+ List-1
if
+ * the string can not be found when searching using the given options.
+ *
+ * @param findString the string to search for
+ * @param startPosition the position at which to start the search
+ * @param forwardSearch the direction of the search
+ * @param caseSensitive should the search be case sensitive
+ * @param wrapSearch should the search wrap to the start/end if arrived at
+ * the end/start
+ * @param wholeWord does the search string represent a complete word
+ * @param regExSearch if true
findString represents a regular
+ * expression
+ * @param beep if true
beeps when search does not find a
+ * match or needs to wrap
+ * @return the occurrence of the find string following the options or
+ * -1
if nothing found
+ * @since 3.0
+ */
+ public int findIndex(String findString, int startPosition) {
+
+ if (isForwardSearch()) {
+ int index = findAndSelect(startPosition, findString);
+ if (index == -1) {
+
+ status.doBeep();
+
+ if (isWrapSearch()) {
+ statusMessage(EditorMessages.FindReplace_Status_wrapped_label);
+ index = findAndSelect(-1, findString);
+ }
+ }
+ return index;
+ }
+
+ // backward
+ int index = startPosition == 0 ? -1
+ : findAndSelect(startPosition - 1, findString);
+ if (index == -1) {
+
+ status.doBeep();
+
+ if (isWrapSearch()) {
+ statusMessage(EditorMessages.FindReplace_Status_wrapped_label);
+ index = findAndSelect(-1, findString);
+ }
+ }
+ return index;
+ }
+
+ /**
+ * Searches for a string starting at the given offset and using the specified
+ * search directives. If a string has been found it is selected and its start
+ * offset is returned.
+ *
+ * @param offset the offset at which searching starts
+ * @param findString the string which should be found
+ * @param forwardSearch the direction of the search
+ * @param caseSensitive true
performs a case sensitive search,
+ * false
an insensitive search
+ * @param wholeWord if true
only occurrences are reported in
+ * which the findString stands as a word by itself
+ * @param regExSearch if true
findString represents a regular
+ * expression
+ * @return the position of the specified string, or -1 if the string has not
+ * been found
+ * @since 3.0
+ */
+ public int findAndSelect(int offset, String findString) {
+ if (fTarget instanceof IFindReplaceTargetExtension3)
+ return ((IFindReplaceTargetExtension3) fTarget).findAndSelect(offset, findString, isForwardSearch(),
+ isCaseSensitiveSearch(), isWholeWordSearch(), isRegexSearch());
+ return fTarget.findAndSelect(offset, findString, isForwardSearch(), isCaseSensitiveSearch(),
+ isWholeWordSearch());
+ }
+
+ /**
+ * Replaces the selection with replaceString
. If
+ * regExReplace
is true
, replaceString
is
+ * a regex replace pattern which will get expanded if the underlying target
+ * supports it. Returns the region of the inserted text; note that the returned
+ * selection covers the expanded pattern in case of regex replace.
+ *
+ * @param replaceString the replace string (or a regex pattern)
+ * @param regExReplace true
if replaceString
is a
+ * pattern
+ * @return the selection after replacing, i.e. the inserted text
+ * @since 3.0
+ */
+ public Point replaceSelection(String replaceString, boolean regExReplace) {
+ if (fTarget instanceof IFindReplaceTargetExtension3)
+ ((IFindReplaceTargetExtension3) fTarget).replaceSelection(replaceString, regExReplace);
+ else
+ fTarget.replaceSelection(replaceString);
+
+ return fTarget.getSelection();
+ }
+
+ /**
+ * Returns whether the specified search string can be found using the given
+ * options.
+ *
+ * @param findString the string to search for
+ * @param forwardSearch the direction of the search
+ * @param caseSensitive should the search be case sensitive
+ * @param wrapSearch should the search wrap to the start/end if arrived at
+ * the end/start
+ * @param wholeWord does the search string represent a complete word
+ * @param incremental is this an incremental search
+ * @param regExSearch if true
findString represents a regular
+ * expression
+ * @param beep if true
beeps when search does not find a
+ * match or needs to wrap
+ * @return true
if the search string can be found using the given
+ * options
+ *
+ * @since 3.0
+ */
+ public boolean findNext(String findString, boolean forwardSearch) {
+
+ if (fTarget == null)
+ return false;
+
+ Point r = null;
+ if (isIncrementalSearch())
+ r = fIncrementalBaseLocation;
+ else
+ r = fTarget.getSelection();
+
+ int findReplacePosition = r.x;
+ if (forwardSearch && !fNeedsInitialFindBeforeReplace || !forwardSearch && fNeedsInitialFindBeforeReplace)
+ findReplacePosition += r.y;
+
+ fNeedsInitialFindBeforeReplace = false;
+
+ int index = findIndex(findString, findReplacePosition);
+
+ if (index == -1) {
+ String msg = NLSUtility.format(EditorMessages.FindReplace_Status_noMatchWithValue_label, findString);
+ statusMessage(false, EditorMessages.FindReplace_Status_noMatch_label, msg);
+ return false;
+ }
+
+ if (forwardSearch && index >= findReplacePosition || !forwardSearch && index <= findReplacePosition)
+ statusMessage(""); //$NON-NLS-1$
+
+ return true;
+ }
+
+ public boolean performFindFirstThenReplaceInASecondStep(String findString, String replaceString) {
+ if (fNeedsInitialFindBeforeReplace) {
+ performSearch(findString);
+ }
+ if (performReplaceSelection(replaceString)) {
+ performSearch(findString);
+ return true;
+ }
+ return false;
+ }
+
+ public boolean performSelectAndReplace(String findString, String replaceString) {
+ if (fNeedsInitialFindBeforeReplace)
+ performSearch(findString);
+ return performReplaceSelection(replaceString);
+ }
+
+ public void updateTarget(IFindReplaceTarget target, boolean isTargetEditable) {
+ fTargetEditable = isTargetEditable;
+ fNeedsInitialFindBeforeReplace = true;
+
+ if (target != fTarget) {
+ if (fTarget != null && fTarget instanceof IFindReplaceTargetExtension)
+ ((IFindReplaceTargetExtension) fTarget).endSession();
+
+ fTarget = target;
+ if (fTarget != null)
+ fIsTargetSupportingRegEx = fTarget instanceof IFindReplaceTargetExtension3;
+
+ if (fTarget instanceof IFindReplaceTargetExtension) {
+ ((IFindReplaceTargetExtension) fTarget).beginSession();
+
+ setGlobalSearch(true);
+ }
+ }
+
+ initIncrementalBaseLocation();
+ }
+
+ public void endSession() {
+ if (fTarget != null && fTarget instanceof IFindReplaceTargetExtension)
+ ((IFindReplaceTargetExtension) fTarget).endSession();
+
+ fTarget = null;
+ }
+
+
+ public void deactivateScope() {
+ if (fTarget != null && (fTarget instanceof IFindReplaceTargetExtension))
+ ((IFindReplaceTargetExtension) fTarget).setScope(null);
+
+ fOldScope = null;
+ }
+
+ public String getCurrentSelection() {
+ if (fTarget == null) {
+ return null;
+ }
+
+ return fTarget.getSelectionText();
+ }
+
+ /**
+ * Returns whether the target is editable.
+ *
+ * @return true
if target is editable
+ */
+ public boolean isEditable() {
+ boolean isEditable = (fTarget == null ? false : fTarget.isEditable());
+ return fTargetEditable && isEditable;
+ }
+
+ public boolean supportsMultiSelection() {
+ return fTarget instanceof IFindReplaceTargetExtension4;
+ }
+
+ public boolean isTargetAvailable() {
+ return fTarget != null;
+ }
+
+ /**
+ * Sets the given status message in the status line.
+ *
+ * @param error true
if it is an error
+ * @param dialogMessage the message to display in the dialog's status line
+ * @param editorMessage the message to display in the editor's status line
+ */
+ private void statusMessage(boolean error, String dialogMessage, String editorMessage) {
+ status.setError(error);
+ status.setMessage(dialogMessage);
+
+ IEditorStatusLine statusLine = getStatusLineManager();
+ if (statusLine != null)
+ statusLine.setMessage(error, editorMessage, null);
+ }
+
+ /**
+ * Sets the given error message in the status line.
+ *
+ * @param message the message
+ */
+ private void statusError(String message) {
+ statusMessage(true, message, message);
+ }
+
+ /**
+ * Sets the given message in the status line.
+ *
+ * @param message the message
+ */
+ private void statusMessage(String message) {
+ statusMessage(false, message, message);
+ }
+
+ public void updateSearchResultAfterTextWasModified(String searchString) {
+ if (isIncrementalSearch() && !isRegExSearchAvailableAndChecked()) {
+ if (searchString.equals("") && fTarget != null) { //$NON-NLS-1$
+ // empty selection at base location
+ int offset = fIncrementalBaseLocation.x;
+
+ if (isForwardSearch() && !fNeedsInitialFindBeforeReplace
+ || !isForwardSearch() && fNeedsInitialFindBeforeReplace)
+ offset = offset + fIncrementalBaseLocation.y;
+
+ fNeedsInitialFindBeforeReplace = false;
+ findAndSelect(offset, ""); //$NON-NLS-1$
+ } else {
+ performSearch(false, searchString);
+ }
+ }
+ }
+
+ public boolean isTargetEditable() {
+ return fTargetEditable;
+ }
+
+}