Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorcknaus2002-09-25 14:11:24 +0000
committercknaus2002-09-25 14:11:24 +0000
commitbb7cb18e9a9932095bd4fb1c51fb303eb27c4c72 (patch)
treef909dce180f4f1c8ede7c6282a9123698d57cb1a
parentb80aa9d4a6ddcd13e5b7e0a633fde44859479254 (diff)
downloadeclipse.platform.text-bb7cb18e9a9932095bd4fb1c51fb303eb27c4c72.tar.gz
eclipse.platform.text-bb7cb18e9a9932095bd4fb1c51fb303eb27c4c72.tar.xz
eclipse.platform.text-bb7cb18e9a9932095bd4fb1c51fb303eb27c4c72.zip
fixed 19819, 19668, 21207
-rw-r--r--org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/AbstractTextEditor.java8
-rw-r--r--org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/BasicTextEditorActionContributor.java22
-rw-r--r--org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/EditorMessages.properties19
-rw-r--r--org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/IAbstractTextEditorHelpContextIds.java12
-rw-r--r--org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/ITextEditorActionConstants.java6
-rw-r--r--org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/IWorkbenchActionDefinitionIds.java7
-rw-r--r--org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/IncrementalFindAction.java23
-rw-r--r--org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/IncrementalFindTarget.java460
8 files changed, 340 insertions, 217 deletions
diff --git a/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/AbstractTextEditor.java b/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/AbstractTextEditor.java
index 3cc4686f7..0283a8467 100644
--- a/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/AbstractTextEditor.java
+++ b/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/AbstractTextEditor.java
@@ -2925,11 +2925,16 @@ public abstract class AbstractTextEditor extends EditorPart implements ITextEdit
action.setActionDefinitionId(ITextEditorActionDefinitionIds.FIND_PREVIOUS);
setAction(ITextEditorActionConstants.FIND_PREVIOUS, action);
- action= new IncrementalFindAction(EditorMessages.getResourceBundle(), "Editor.FindIncremental.", this); //$NON-NLS-1$
+ action= new IncrementalFindAction(EditorMessages.getResourceBundle(), "Editor.FindIncremental.", this, true); //$NON-NLS-1$
action.setHelpContextId(IAbstractTextEditorHelpContextIds.FIND_INCREMENTAL_ACTION);
action.setActionDefinitionId(ITextEditorActionDefinitionIds.FIND_INCREMENTAL);
setAction(ITextEditorActionConstants.FIND_INCREMENTAL, action);
+ action= new IncrementalFindAction(EditorMessages.getResourceBundle(), "Editor.FindIncrementalReverse.", this, false); //$NON-NLS-1$
+ action.setHelpContextId(IAbstractTextEditorHelpContextIds.FIND_INCREMENTAL_REVERSE_ACTION);
+ action.setActionDefinitionId(ITextEditorActionDefinitionIds.FIND_INCREMENTAL_REVERSE);
+ setAction(ITextEditorActionConstants.FIND_INCREMENTAL_REVERSE, action);
+
action= new AddMarkerAction(EditorMessages.getResourceBundle(), "Editor.AddBookmark.", this, IMarker.BOOKMARK, true); //$NON-NLS-1$
action.setHelpContextId(IAbstractTextEditorHelpContextIds.BOOKMARK_ACTION);
action.setActionDefinitionId(ITextEditorActionDefinitionIds.ADD_BOOKMARK);
@@ -2962,6 +2967,7 @@ public abstract class AbstractTextEditor extends EditorPart implements ITextEdit
markAsContentDependentAction(ITextEditorActionConstants.FIND_NEXT, true);
markAsContentDependentAction(ITextEditorActionConstants.FIND_PREVIOUS, true);
markAsContentDependentAction(ITextEditorActionConstants.FIND_INCREMENTAL, true);
+ markAsContentDependentAction(ITextEditorActionConstants.FIND_INCREMENTAL_REVERSE, true);
markAsSelectionDependentAction(ITextEditorActionConstants.CUT, true);
markAsSelectionDependentAction(ITextEditorActionConstants.COPY, true);
diff --git a/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/BasicTextEditorActionContributor.java b/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/BasicTextEditorActionContributor.java
index a544ae147..d4a62e84c 100644
--- a/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/BasicTextEditorActionContributor.java
+++ b/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/BasicTextEditorActionContributor.java
@@ -14,11 +14,17 @@ package org.eclipse.ui.texteditor;
import java.util.HashMap;
import java.util.Map;
-import java.util.ResourceBundle; import org.eclipse.jface.action.IAction; import org.eclipse.jface.action.IContributionItem;
-import org.eclipse.jface.action.IMenuManager; import org.eclipse.jface.action.MenuManager;
- import org.eclipse.jface.action.IStatusLineManager;
-import org.eclipse.ui.IActionBars; import org.eclipse.ui.IEditorPart; import org.eclipse.ui.IWorkbenchActionConstants; import org.eclipse.ui.part.EditorActionBarContributor;
+import org.eclipse.jface.action.IAction;
+import org.eclipse.jface.action.IContributionItem;
+import org.eclipse.jface.action.IMenuManager;
+
+import org.eclipse.jface.action.IStatusLineManager;
+
+import org.eclipse.ui.IActionBars;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IWorkbenchActionConstants;
+import org.eclipse.ui.part.EditorActionBarContributor;
@@ -85,6 +91,11 @@ public class BasicTextEditorActionContributor extends EditorActionBarContributor
* @since 2.0
*/
private RetargetTextEditorAction fIncrementalFind;
+ /**
+ * The reverse incremental find action
+ * @since 2.1
+ */
+ private RetargetTextEditorAction fIncrementalFindReverse;
/** The go to line action */
private RetargetTextEditorAction fGotoLine;
/**
@@ -105,6 +116,7 @@ public class BasicTextEditorActionContributor extends EditorActionBarContributor
fFindNext= new RetargetTextEditorAction(EditorMessages.getResourceBundle(), "Editor.FindNext."); //$NON-NLS-1$
fFindPrevious= new RetargetTextEditorAction(EditorMessages.getResourceBundle(), "Editor.FindPrevious."); //$NON-NLS-1$
fIncrementalFind= new RetargetTextEditorAction(EditorMessages.getResourceBundle(), "Editor.FindIncremental."); //$NON-NLS-1$
+ fIncrementalFindReverse= new RetargetTextEditorAction(EditorMessages.getResourceBundle(), "Editor.FindIncrementalReverse."); //$NON-NLS-1$
fGotoLine= new RetargetTextEditorAction(EditorMessages.getResourceBundle(), "Editor.GotoLine."); //$NON-NLS-1$
fStatusFields= new HashMap(3);
@@ -160,6 +172,7 @@ public class BasicTextEditorActionContributor extends EditorActionBarContributor
fFindNext.setAction(getAction(editor, ITextEditorActionConstants.FIND_NEXT));
fFindPrevious.setAction(getAction(editor, ITextEditorActionConstants.FIND_PREVIOUS));
fIncrementalFind.setAction(getAction(editor, ITextEditorActionConstants.FIND_INCREMENTAL));
+ fIncrementalFindReverse.setAction(getAction(editor, ITextEditorActionConstants.FIND_INCREMENTAL_REVERSE));
fGotoLine.setAction(getAction(editor, ITextEditorActionConstants.GOTO_LINE));
if (fActiveEditorPart instanceof ITextEditorExtension) {
@@ -189,6 +202,7 @@ public class BasicTextEditorActionContributor extends EditorActionBarContributor
editMenu.appendToGroup(IWorkbenchActionConstants.FIND_EXT, fFindNext);
editMenu.appendToGroup(IWorkbenchActionConstants.FIND_EXT,fFindPrevious);
editMenu.appendToGroup(IWorkbenchActionConstants.FIND_EXT,fIncrementalFind);
+ editMenu.appendToGroup(IWorkbenchActionConstants.FIND_EXT,fIncrementalFindReverse);
editMenu.appendToGroup(IWorkbenchActionConstants.FIND_EXT,fGotoLine);
}
}
diff --git a/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/EditorMessages.properties b/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/EditorMessages.properties
index 743474dfd..123617b0f 100644
--- a/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/EditorMessages.properties
+++ b/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/EditorMessages.properties
@@ -153,14 +153,21 @@ Editor.FindPrevious.tooltip=Find Previous
Editor.FindPrevious.image=
Editor.FindPrevious.description=Find Previous
-Editor.FindIncremental.label=&Incremental Find@Ctrl+J
-Editor.FindIncremental.tooltip=Incremental Find
+Editor.FindIncremental.label=&Incremental Find Next@Ctrl+J
+Editor.FindIncremental.tooltip=Incremental Find Next
Editor.FindIncremental.image=
-Editor.FindIncremental.description=Incremental Find
-Editor.FindIncremental.not_found.pattern={0} Incremental Find: {1} not found
-Editor.FindIncremental.found.pattern={0} Incremental Find: {1}
+Editor.FindIncremental.description=Incremental Find Next
+Editor.FindIncremental.not_found.pattern={0}{1}Incremental Find: {2} not found
+Editor.FindIncremental.found.pattern={0}{1}Incremental Find: {2}
Editor.FindIncremental.render.tab=<TAB>
-Editor.FindIncremental.wrapped =Wrapped
+# The following two properties must end in a space
+Editor.FindIncremental.wrapped=Wrapped
+Editor.FindIncremental.reverse=Reverse
+
+Editor.FindIncrementalReverse.label=Incre&mental Find Previous@Ctrl+Shift+J
+Editor.FindIncrementalReverse.tooltip=Incremental Find Previous
+Editor.FindIncrementalReverse.image=
+Editor.FindIncrementalReverse.description=Incremental Find Previous
Editor.AddBookmark.label=Boo&kmark...
Editor.AddBookmark.tooltip=Add Bookmark
diff --git a/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/IAbstractTextEditorHelpContextIds.java b/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/IAbstractTextEditorHelpContextIds.java
index 02cf4fbbe..2e5ba97b6 100644
--- a/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/IAbstractTextEditorHelpContextIds.java
+++ b/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/IAbstractTextEditorHelpContextIds.java
@@ -146,21 +146,27 @@ public interface IAbstractTextEditorHelpContextIds {
* Value: <code>"org.eclipse.ui.FindNext_action_context"</code>
* @since 2.0
*/
- public static final String FIND_NEXT_ACTION = PREFIX + ITextEditorActionConstants.FIND_NEXT + ACTION_POSTFIX;
+ public static final String FIND_NEXT_ACTION= PREFIX + ITextEditorActionConstants.FIND_NEXT + ACTION_POSTFIX;
/**
* Help context id for the action.
* Value: <code>"org.eclipse.ui.FindPrevious_action_context"</code>
* @since 2.0
*/
- public static final String FIND_PREVIOUS_ACTION = PREFIX + ITextEditorActionConstants.FIND_PREVIOUS + ACTION_POSTFIX;
+ public static final String FIND_PREVIOUS_ACTION= PREFIX + ITextEditorActionConstants.FIND_PREVIOUS + ACTION_POSTFIX;
/**
* Help context id for the action.
* Value: <code>"org.eclipse.ui.FindIncremental_action_context"</code>
* @since 2.0
*/
- public static final String FIND_INCREMENTAL_ACTION = PREFIX + ITextEditorActionConstants.FIND_INCREMENTAL + ACTION_POSTFIX;
+ public static final String FIND_INCREMENTAL_ACTION= PREFIX + ITextEditorActionConstants.FIND_INCREMENTAL + ACTION_POSTFIX;
+ /**
+ * Help context id for the action.
+ * Value: <code>"org.eclipse.ui.FindIncrementalReverse_action_context"</code>
+ * @since 2.1
+ */
+ public static final String FIND_INCREMENTAL_REVERSE_ACTION= PREFIX + ITextEditorActionConstants.FIND_INCREMENTAL_REVERSE + ACTION_POSTFIX;
/**
* Help context id for the action.
diff --git a/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/ITextEditorActionConstants.java b/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/ITextEditorActionConstants.java
index 9cd3eb2df..0427ddcbc 100644
--- a/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/ITextEditorActionConstants.java
+++ b/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/ITextEditorActionConstants.java
@@ -156,6 +156,12 @@ public interface ITextEditorActionConstants extends IWorkbenchActionConstants {
* @since 2.0
*/
static final String FIND_INCREMENTAL= "FindIncremental"; //$NON-NLS-1$
+ /**
+ * Name of the action to incremental find reverse.
+ * Value: <code>"FindIncrementalReverse"</code>
+ * @since 2.1
+ */
+ static final String FIND_INCREMENTAL_REVERSE= "FindIncrementalReverse"; //$NON-NLS-1$
/**
* Name of the action to convert line delimiters to Windows.
diff --git a/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/IWorkbenchActionDefinitionIds.java b/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/IWorkbenchActionDefinitionIds.java
index 231fb4c38..c4b1ee453 100644
--- a/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/IWorkbenchActionDefinitionIds.java
+++ b/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/IWorkbenchActionDefinitionIds.java
@@ -121,4 +121,11 @@ public interface IWorkbenchActionDefinitionIds {
* Value: <code>"org.eclipse.ui.edit.findIncremental"</code>
*/
public static final String FIND_INCREMENTAL= "org.eclipse.ui.edit.findIncremental"; //$NON-NLS-1$
+ /**
+ * Action definition id of the edit incremental find reverse action.
+ * Value: <code>"org.eclipse.ui.edit.findIncrementalReverse"</code>
+ *
+ * @since 2.1
+ */
+ public static final String FIND_INCREMENTAL_REVERSE= "org.eclipse.ui.edit.findIncrementalReverse"; //$NON-NLS-1$
}
diff --git a/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/IncrementalFindAction.java b/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/IncrementalFindAction.java
index 354a65298..88c5d8fbc 100644
--- a/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/IncrementalFindAction.java
+++ b/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/IncrementalFindAction.java
@@ -35,6 +35,8 @@ public class IncrementalFindAction extends ResourceAction implements IUpdate {
private IWorkbenchPart fWorkbenchPart;
/** The workbench window */
private IWorkbenchWindow fWorkbenchWindow;
+ /** The direction to run the incremental find */
+ private boolean fForward;
/**
* Creates a new incremental find action for the given text editor.
@@ -46,11 +48,13 @@ public class IncrementalFindAction extends ResourceAction implements IUpdate {
* (described in <code>ResourceAction</code> constructor), or
* <code>null</code> if none
* @param editor the text editor
+ * @param forward <code>true</code> if the search direction is forward
* @see ResourceAction#ResourceAction
*/
- public IncrementalFindAction(ResourceBundle bundle, String prefix, IWorkbenchPart workbenchPart) {
+ public IncrementalFindAction(ResourceBundle bundle, String prefix, IWorkbenchPart workbenchPart, boolean forward) {
super(bundle, prefix);
fWorkbenchPart= workbenchPart;
+ fForward= forward;
update();
}
@@ -64,13 +68,15 @@ public class IncrementalFindAction extends ResourceAction implements IUpdate {
* (described in <code>ResourceAction</code> constructor), or
* <code>null</code> if none
* @param workbenchWindow the workbench window
+ * @param forward <code>true</code> if the search direction is forward
* @see ResourceAction#ResourceAction
*
- * @deprecated use FindReplaceAction(ResourceBundle, String, IWorkbenchPart) instead
+ * @deprecated use FindReplaceAction(ResourceBundle, String, IWorkbenchPart, boolean) instead
*/
- public IncrementalFindAction(ResourceBundle bundle, String prefix, IWorkbenchWindow workbenchWindow) {
+ public IncrementalFindAction(ResourceBundle bundle, String prefix, IWorkbenchWindow workbenchWindow, boolean forward) {
super(bundle, prefix);
fWorkbenchWindow= workbenchWindow;
+ fForward= forward;
update();
}
@@ -78,7 +84,14 @@ public class IncrementalFindAction extends ResourceAction implements IUpdate {
* @see IAction#run
*/
public void run() {
- if (fTarget != null && fTarget instanceof IFindReplaceTargetExtension)
+
+ if (fTarget == null)
+ return;
+
+ if (fTarget instanceof IncrementalFindTarget)
+ ((IncrementalFindTarget) fTarget).setDirection(fForward);
+
+ if (fTarget instanceof IFindReplaceTargetExtension)
((IFindReplaceTargetExtension) fTarget).beginSession();
}
@@ -98,4 +111,4 @@ public class IncrementalFindAction extends ResourceAction implements IUpdate {
setEnabled(fTarget != null && fTarget.canPerformFind());
}
-}
+} \ No newline at end of file
diff --git a/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/IncrementalFindTarget.java b/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/IncrementalFindTarget.java
index 45ff4a807..36e1960c4 100644
--- a/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/IncrementalFindTarget.java
+++ b/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/IncrementalFindTarget.java
@@ -15,21 +15,13 @@ import java.text.MessageFormat;
import java.util.Stack;
import org.eclipse.jface.action.IStatusLineManager;
-import org.eclipse.jface.text.IFindReplaceTarget;
-import org.eclipse.jface.text.IFindReplaceTargetExtension;
-import org.eclipse.jface.text.IRegion;
-import org.eclipse.jface.text.ITextListener;
-import org.eclipse.jface.text.ITextViewer;
-import org.eclipse.jface.text.ITextViewerExtension;
-import org.eclipse.jface.text.TextEvent;
+import org.eclipse.jface.text.*;
+import org.eclipse.jface.util.Assert;
+import org.eclipse.jface.viewers.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.custom.VerifyKeyListener;
-import org.eclipse.swt.events.FocusEvent;
-import org.eclipse.swt.events.FocusListener;
-import org.eclipse.swt.events.MouseEvent;
-import org.eclipse.swt.events.MouseListener;
-import org.eclipse.swt.events.VerifyEvent;
+import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
@@ -37,10 +29,12 @@ import org.eclipse.swt.graphics.Point;
* An incremental find target. Replace is always disabled.
* @since 2.0
*/
-class IncrementalFindTarget implements IFindReplaceTarget, IFindReplaceTargetExtension, VerifyKeyListener, MouseListener, FocusListener, ITextListener {
+class IncrementalFindTarget implements IFindReplaceTarget, IFindReplaceTargetExtension, VerifyKeyListener, MouseListener, FocusListener, ISelectionChangedListener, ITextListener {
/** The string representing rendered tab */
private final static String TAB= EditorMessages.getString("Editor.FindIncremental.render.tab"); //$NON-NLS-1$
+ private final static String REVERSE= EditorMessages.getString("Editor.FindIncremental.reverse"); //$NON-NLS-1$
+ private final static String WRAPPED= EditorMessages.getString("Editor.FindIncremental.wrapped"); //$NON-NLS-1$
/** The text viewer to operate on */
private final ITextViewer fTextViewer;
@@ -51,34 +45,98 @@ class IncrementalFindTarget implements IFindReplaceTarget, IFindReplaceTargetExt
/** The current find string */
private StringBuffer fFindString= new StringBuffer();
- /** The position to start the find from */
- private int fBasePosition;
/** The position of the first upper case character, -1 if none */
private int fCasePosition;
+ /** The position in the stack of the first wrap search, -1 if none */
+ private int fWrapPosition;
/** The position of the last successful find */
private int fCurrentIndex;
/** A flag indicating if last find was successful */
private boolean fFound;
+ /** A flag indicating if the last search was forward */
+ private boolean fForward= true;
/** A flag indicating listeners are installed. */
private boolean fInstalled;
+ /**
+ * A flag indicating that a search is currently active.
+ * Used to ignore selection callbacks generated by the incremental search itself.
+ */
+ private boolean fSearching;
/** The current find stack */
private Stack fSessionStack;
- /** A constant representing a find-next operation */
- private static final Object NEXT = new Object();
- /** A constant representing a find-previous operation */
- private static final Object PREVIOUS = new Object();
- /** A constant representing adding a character to the find pattern */
- private static final Object CHAR = new Object();
- /** A constant representing a wrap operation */
- private static final Object WRAPPED = new Object();
+ /** The previous search string */
+ private String fPrevFindString= ""; //$NON-NLS-1$
+
+ private class SearchResult {
+ int selection, length, index, findLength;
+ boolean found, forward;
+
+ public SearchResult() {
+ Point p= fTarget.getSelection();
+ selection= p.x;
+ length= p.y;
+ index= fCurrentIndex;
+ findLength= fFindString.length();
+ found= fFound;
+ forward= fForward;
+ }
+
+ }
+
+ private void saveState() {
+ fSessionStack.push(new SearchResult());
+ }
+
+ private void restoreState() {
+
+ StyledText text= fTextViewer.getTextWidget();
+ if (text == null || text.isDisposed())
+ return;
+
+ SearchResult searchResult= null;
+ if (!fSessionStack.empty())
+ searchResult= (SearchResult) fSessionStack.pop();
+
+ if (searchResult == null) {
+ text.getDisplay().beep();
+ return;
+ }
+
+ // TODO: reverse searches should put the caret at the start of the selection, instead of the end
+ text.setSelectionRange(searchResult.selection, searchResult.length);
+ text.showSelection();
+
+ // relies on the contents of the StringBuffer
+ fFindString.setLength(searchResult.findLength);
+ fCurrentIndex= searchResult.index;
+ fFound= searchResult.found;
+ fForward= searchResult.forward;
+
+ // Recalculate the indices
+ if (fFindString.length() < fCasePosition)
+ fCasePosition= -1;
+ if (fSessionStack.size() < fWrapPosition)
+ fWrapPosition= -1;
+ }
+
+ /**
+ * Sets the direction for the next search.
+ * This can be called before <code>beginSession</code> to set the initial search direction.
+ * @param forward <code>true</code> if the next search should be forward
+ * @see beginSession
+ */
+ public void setDirection(boolean forward) {
+ fForward= forward;
+ }
/**
* Creates an instance of an incremental find target.
- *
* @param viewer the text viewer to operate on
* @param manager the status line manager for output
*/
public IncrementalFindTarget(ITextViewer viewer, IStatusLineManager manager) {
+ Assert.isNotNull(viewer);
+ Assert.isNotNull(manager);
fTextViewer= viewer;
fStatusLine= manager;
fTarget= viewer.getFindReplaceTarget();
@@ -129,19 +187,44 @@ class IncrementalFindTarget implements IFindReplaceTarget, IFindReplaceTargetExt
* @see IFindReplaceTargetExtension#beginSession()
*/
public void beginSession() {
+ fSearching= true;
+
+ // Workaround since some accelerators get handled directly by the OS
+ if (fInstalled) {
+ saveState();
+ repeatSearch(fForward);
+ updateStatus();
+ fSearching= false;
+ return;
+ }
+
fFindString.setLength(0);
- fSessionStack = new Stack();
+ fSessionStack= new Stack();
fCasePosition= -1;
- fBasePosition= fTarget.getSelection().x;
- fCurrentIndex= fBasePosition;
+ fWrapPosition= -1;
fFound= true;
+
+ // clear initial selection
+ StyledText text= fTextViewer.getTextWidget();
+ if (text != null && !text.isDisposed()) {
+ fCurrentIndex= text.getCaretOffset();
+ text.setSelection(fCurrentIndex);
+ } else {
+ fCurrentIndex= 0;
+ }
install();
- performFindNext(true, fBasePosition, false, false);
- updateStatus(fFound);
+
+ // Set the mark
+ if (fTextViewer instanceof ITextViewerExtension)
+ ((ITextViewerExtension) fTextViewer).setMark(fCurrentIndex);
+
+ updateStatus();
if (fTarget instanceof IFindReplaceTargetExtension)
- ((IFindReplaceTargetExtension) fTarget).beginSession();
+ ((IFindReplaceTargetExtension) fTarget).beginSession();
+
+ fSearching= false;
}
/*
@@ -183,6 +266,7 @@ class IncrementalFindTarget implements IFindReplaceTarget, IFindReplaceTargetExt
* Installs this target. I.e. adds all required listeners.
*/
private void install() {
+
if (fInstalled)
return;
@@ -191,14 +275,17 @@ class IncrementalFindTarget implements IFindReplaceTarget, IFindReplaceTargetExt
return;
text.addMouseListener(this);
+ text.addFocusListener(this);
fTextViewer.addTextListener(this);
+
+ ISelectionProvider selectionProvider= fTextViewer.getSelectionProvider();
+ if (selectionProvider != null)
+ selectionProvider.addSelectionChangedListener(this);
- if (fTextViewer instanceof ITextViewerExtension) {
- ITextViewerExtension e= (ITextViewerExtension) fTextViewer;
- e.prependVerifyKeyListener(this);
- } else {
+ if (fTextViewer instanceof ITextViewerExtension)
+ ((ITextViewerExtension) fTextViewer).prependVerifyKeyListener(this);
+ else
text.addVerifyKeyListener(this);
- }
fInstalled= true;
}
@@ -207,164 +294,93 @@ class IncrementalFindTarget implements IFindReplaceTarget, IFindReplaceTargetExt
* Uninstalls itself. I.e. removes all listeners installed in <code>install</code>.
*/
private void uninstall() {
+
fTextViewer.removeTextListener(this);
+ ISelectionProvider selectionProvider= fTextViewer.getSelectionProvider();
+ if (selectionProvider != null)
+ selectionProvider.removeSelectionChangedListener(this);
+
StyledText text= fTextViewer.getTextWidget();
- text.removeMouseListener(this);
-
+ if (text != null) {
+ text.removeMouseListener(this);
+ text.removeFocusListener(this);
+ }
+
if (fTextViewer instanceof ITextViewerExtension) {
- ITextViewerExtension e= (ITextViewerExtension) fTextViewer;
- e.removeVerifyKeyListener(this);
+ ((ITextViewerExtension) fTextViewer).removeVerifyKeyListener(this);
+
} else {
- text.removeVerifyKeyListener(this);
+ if (text != null)
+ text.removeVerifyKeyListener(this);
}
fInstalled= false;
}
/**
- * Returns whether the find string can be found using the given options.
- *
- * @param forward the search direction
- * @param position the start offset
- * @param wrapSearch should the search wrap to start/end if end/start is reached
- * @param takeBack is the find string shortend
- * @return <code>true</code> if find string can be found using the options
- */
- private boolean performFindNext(boolean forward, int position, boolean wrapSearch, boolean takeBack) {
- String string= fFindString.toString();
-
- int index;
- if (string.length() == 0) {
-
- // workaround for empty selection in target
- fTextViewer.setSelectedRange(fBasePosition + fTextViewer.getVisibleRegion().getOffset(), 0);
- index= fBasePosition;
-
- } else {
-
- if (!forward)
- position--;
-
- index= findIndex(string, position, forward, fCasePosition != -1, wrapSearch, takeBack);
- }
-
- boolean found = (index != -1);
- if (found) fCurrentIndex = index;
-
- return found;
- }
-
- /**
- * Updates the status line appropriate for the indicated success.
- * @param found the success
+ * Updates the status line.
*/
- private void updateStatus(boolean found) {
- if (fSessionStack == null) return;
+ private void updateStatus() {
+
+ if (!fInstalled)
+ return;
String string= fFindString.toString();
- String prefix = ""; //$NON-NLS-1$
- if (fSessionStack.search(WRAPPED) != -1) {
- prefix = EditorMessages.getString("Editor.FindIncremental.wrapped"); //$NON-NLS-1$
- }
- if (!found) {
+ String wrapPrefix= fWrapPosition == -1 ? "" : WRAPPED; //$NON-NLS-1$
+ String reversePrefix= fForward ? "" : REVERSE; //$NON-NLS-1$
+
+ if (!fFound) {
String pattern= EditorMessages.getString("Editor.FindIncremental.not_found.pattern"); //$NON-NLS-1$
- statusError(MessageFormat.format(pattern, new Object[] { prefix, string }));
+ statusError(MessageFormat.format(pattern, new Object[] { reversePrefix, wrapPrefix, string }));
} else {
String pattern= EditorMessages.getString("Editor.FindIncremental.found.pattern"); //$NON-NLS-1$
- statusMessage(MessageFormat.format(pattern, new Object[] { prefix, string }));
+ statusMessage(MessageFormat.format(pattern, new Object[] { reversePrefix, wrapPrefix, string }));
}
}
- /**
- * Retuns the offset at which the search string is found next using the given options
- * @param findString the string to search for
- * @param startPosition the start offset
- * @param forward the search direction
- * @param caseSensitive is the search case sensitive
- * @param wrapSearch should the search wrap to start/end if end/start is reached
- * @param takeBack is the find string shortend
- * @return the offset of the next match or <code>-1</code>
- */
- private int findIndex(String findString, int startPosition, boolean forwardSearch, boolean caseSensitive, boolean wrapSearch, boolean takeBack) {
-
- if (fTarget == null)
- return -1;
-
- int startIndex = (forwardSearch) ? startPosition : startPosition - 1;
- int index= fTarget.findAndSelect(startIndex, findString, forwardSearch, caseSensitive, false);
-
- if (index != -1) {
- fFound = true;
- return index;
- }
-
- if (fFound) {
- // beep once
- StyledText text= fTextViewer.getTextWidget();
- if (!takeBack && text != null && !text.isDisposed())
- text.getDisplay().beep();
- }
-
- if (!wrapSearch || (fFound && !takeBack)) {
- fFound = false;
- return index;
- }
-
- index = fTarget.findAndSelect(-1, findString, forwardSearch, caseSensitive, false);
- fFound = (index != -1);
- if (!takeBack && fFound) wrap();
- return index;
- }
-
- /**
- * Remembers wrapping in the session stack.
- */
- private void wrap() {
- fSessionStack.push(WRAPPED);
- }
-
/*
* @see VerifyKeyListener#verifyKey(VerifyEvent)
*/
public void verifyKey(VerifyEvent event) {
+
if (!event.doit)
return;
+ fSearching= true;
if (event.character == 0) {
switch (event.keyCode) {
// ALT, CTRL, ARROW_LEFT, ARROW_RIGHT == leave
- case SWT.ALT:
- case SWT.CTRL:
case SWT.ARROW_LEFT:
case SWT.ARROW_RIGHT:
+ case SWT.ARROW_UP:
+ case SWT.ARROW_DOWN:
case SWT.HOME:
case SWT.END:
case SWT.PAGE_DOWN:
case SWT.PAGE_UP:
leave();
break;
-
- // find next
+/*
case SWT.ARROW_DOWN:
- if (performFindNext(true, fTarget.getSelection().x + fTarget.getSelection().y, true, false))
- fSessionStack.push(NEXT);
- event.doit= false;
+ saveState();
+ setDirection(true);
+ repeatSearch(fForward);
+ updateStatus();
+ event.doit= false;
break;
-
- // find previous
- case SWT.ARROW_UP:
- if (performFindNext(false, fTarget.getSelection().x, true, false))
- fSessionStack.push(PREVIOUS);
- if (fCurrentIndex != -1 && fCurrentIndex < fBasePosition)
- fBasePosition= fCurrentIndex;
-
- event.doit= false;
+ case SWT.ARROW_UP:
+ saveState();
+ setDirection(false);
+ repeatSearch(fForward);
+ updateStatus();
+ event.doit= false;
break;
+*/
}
// event.character != 0
@@ -382,79 +398,120 @@ class IncrementalFindTarget implements IFindReplaceTarget, IFindReplaceTargetExt
// backspace and delete
case 0x08:
case 0x7F:
- {
- if (fSessionStack.empty()) {
- StyledText text= fTextViewer.getTextWidget();
- if (text != null && !text.isDisposed())
- text.getDisplay().beep();
- event.doit= false;
- break;
- }
-
- Object last = popSessionStack();
- while (!fSessionStack.empty() && fSessionStack.peek() == WRAPPED)
- popSessionStack();
-
- // Last event repeated a search
- if (last == PREVIOUS) {
- performFindNext(true, fTarget.getSelection().x + fTarget.getSelection().y, true, true);
- } else if (last == NEXT) {
- performFindNext(false, fTarget.getSelection().x, true, true);
-
- if (fCurrentIndex != -1 && fCurrentIndex < fBasePosition)
- fBasePosition= fCurrentIndex;
- } else if (last == CHAR) {
- // Last event added a character
- performFindNext(true, fBasePosition, true, true);
- }
- event.doit= false;
- }
+ restoreState();
+ event.doit= false;
break;
default:
if (event.stateMask == 0 || event.stateMask == SWT.SHIFT) {
-
- pushChar(event.character);
- performFindNext(true, fBasePosition, false, false);
+ saveState();
+ addCharSearch(event.character);
event.doit= false;
}
break;
}
}
- updateStatus(fFound);
+ updateStatus();
+ fSearching= false;
}
-
/**
- * Extends the incremental search by the given character.
- * @param character the character to append to the searhc string
+ * Repeats the last search while possibly changing the direciton.
+ * @param forward <code>true</code> iff the next search should be forward
*/
- private void pushChar(char character) {
- if (fCasePosition == -1 && Character.isUpperCase(character) && Character.toLowerCase(character) != character)
- fCasePosition= fFindString.length();
- fFindString.append(character);
- fSessionStack.push(CHAR);
+ private boolean repeatSearch(boolean forward) {
+ if (fFindString.length() == 0)
+ fFindString= new StringBuffer(fPrevFindString);
+
+ String string= fFindString.toString();
+ if (string.length() == 0) {
+ fFound= true;
+ return true;
+ }
+
+ StyledText text= fTextViewer.getTextWidget();
+ // Cannot use fTarget.getSelection since that does not return which side of the
+ // selection the caret is on.
+ int startIndex= text.getCaretOffset();
+ if (!forward)
+ startIndex -= 1;
+
+ // Check to see if a wrap is necessary
+ if (!fFound && (fForward == forward)) {
+ startIndex= -1;
+ if (fWrapPosition == -1)
+ fWrapPosition= fSessionStack.size();
+ }
+ fForward = forward;
+
+ // Find the string
+ text.setRedraw(false);
+ int index= fTarget.findAndSelect(startIndex, string, fForward, fCasePosition != -1, false);
+
+ // Set the caret on the left if the search is reversed
+ if (!forward) {
+ Point p= fTarget.getSelection();
+ text.setSelectionRange(p.x + p.y, -p.y);
+ p= null;
+ }
+ text.setRedraw(true);
+
+ // Take appropriate action
+ boolean found = (index != -1);
+ if (!found && fFound) {
+ text= fTextViewer.getTextWidget();
+ if (text != null && !text.isDisposed())
+ text.getDisplay().beep();
+ }
+
+ if (found)
+ fCurrentIndex= startIndex;
+
+ fFound= found;
+ return found;
}
/**
- * Pops the top element from the session stack.
- * @return the top element from the session stack
- */
- private Object popSessionStack() {
- Object o = fSessionStack.pop();
- if (o == CHAR) {
- int newLength = fFindString.length() -1;
- fFindString.deleteCharAt(newLength);
- if (fCasePosition == newLength)
- fCasePosition= -1;
+ * Adds the given character to the search string and repeats the search with the last parameters.
+ * @param c the character to append to the search pattern
+ */
+ private boolean addCharSearch(char c) {
+ // Add char to pattern
+ if (fCasePosition == -1 && Character.isUpperCase(c) && Character.toLowerCase(c) != c)
+ fCasePosition= fFindString.length();
+
+ fFindString.append(c);
+ String string= fFindString.toString();
+ StyledText text= fTextViewer.getTextWidget();
+
+ text.setRedraw(false);
+ int index= fTarget.findAndSelect(fCurrentIndex, string, fForward, fCasePosition != -1, false);
+
+ // Set the caret on the left if the search is reversed
+ if (!fForward) {
+ Point p= fTarget.getSelection();
+ text.setSelectionRange(p.x + p.y, -p.y);
}
- return o;
+ text.setRedraw(true);
+
+ // Take appropriate action
+ boolean found = (index != -1);
+ if (!found && fFound) {
+ text= fTextViewer.getTextWidget();
+ if (text != null && !text.isDisposed())
+ text.getDisplay().beep();
+ }
+
+ fFound= found;
+ return found;
}
/**
* Leaves this incremental search session.
*/
private void leave() {
+ if (fFindString.length() != 0)
+ fPrevFindString= fFindString.toString();
statusClear();
uninstall();
fSessionStack = null;
@@ -574,4 +631,11 @@ class IncrementalFindTarget implements IFindReplaceTarget, IFindReplaceTargetExt
public void setScopeHighlightColor(Color color) {
}
+ /*
+ * @see ISelectionChangedListener#selectionChanged(SelectionChangedEvent)
+ */
+ public void selectionChanged(SelectionChangedEvent e) {
+ if (!fSearching)
+ leave();
+ }
}

Back to the top