Skip to main content
summaryrefslogtreecommitdiffstats
blob: bbb1cd2374359d99a786140525fd1b92a6d3f3c5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
/*******************************************************************************
 * Copyright (c) 2000, 2016 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Felix Pahl (fpahl@web.de) - fixed https://bugs.eclipse.org/bugs/show_bug.cgi?id=51820
 *******************************************************************************/

package org.eclipse.ui.texteditor;

import java.util.Stack;

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.graphics.Color;
import org.eclipse.swt.graphics.Point;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IExecutionListener;
import org.eclipse.core.commands.NotHandledException;

import org.eclipse.core.runtime.Assert;

import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.jface.util.Util;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;

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.ITextSelection;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.ITextViewerExtension;
import org.eclipse.jface.text.ITextViewerExtension5;
import org.eclipse.jface.text.TextEvent;

import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.commands.ICommandService;
import org.eclipse.ui.internal.texteditor.NLSUtility;
import org.eclipse.ui.keys.IBindingService;

/**
 * An incremental find target. Replace is always disabled.
 * @since 2.0
 */
class IncrementalFindTarget implements IFindReplaceTarget, IFindReplaceTargetExtension, VerifyKeyListener, MouseListener, FocusListener, ISelectionChangedListener, ITextListener, IExecutionListener {

	/** The string representing rendered tab */
	private final static String TAB= EditorMessages.Editor_FindIncremental_render_tab;
	/**
	 * The string representing "Reverse Incremental Find"
	 * @since 3.0
	 */
	private final static String FIELD_NAME= EditorMessages.Editor_FindIncremental_name;
	/**
	 * The string representing "Incremental Find"
	 * @since 3.0
	 */
	private final static String REVERSE_FIELD_NAME= EditorMessages.Editor_FindIncremental_reverse_name;
	/**
	 * The string representing reverse
	 * @since 2.1
	 */
	private final static String REVERSE= EditorMessages.Editor_FindIncremental_reverse;
	/**
	 * The string representing wrapped
	 * @since 2.1
	 */
	private final static String WRAPPED= EditorMessages.Editor_FindIncremental_wrapped;
	/** The text viewer to operate on */
	private final ITextViewer fTextViewer;
	/** The status line manager for output */
	private final IStatusLineManager fStatusLine;
	/** The find replace target to delegate find requests */
	private final IFindReplaceTarget fTarget;
	/** The current find string */
	private StringBuffer fFindString= new StringBuffer();
	/** 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
	 * @since 2.1
	 */
	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
	 * @since 2.1
	 */
	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.
	 * @since 2.1
	 */
	private boolean fSearching;
	/** The current find stack */
	private Stack<SearchResult> fSessionStack;
	/**
	 * The previous search string
	 * @since 2.1
	 */
	private String fPrevFindString= ""; //$NON-NLS-1$
	/**
	 * The previous position of the first upper case character, -1 if none
	 * @since 3.0
	 */
	private int fPrevCasePosition= -1;
	/**
	 * The find status field.
	 * @since 3.0
	 */
	private IStatusField fStatusField;
	/**
	 * Tells whether the status field implements
	 * <code>IStatusFieldExtension</code>.
	 * @see IStatusFieldExtension
	 * @since 3.0
	 */
	private boolean fIsStatusFieldExtension;

	/**
	 * Data structure for a search result.
	 * @since 2.1
	 */
	private class SearchResult {
		int selection, length, index, findLength;
		boolean found, forward;

		/**
		 * Creates a new search result data object and fills
		 * it with the current values of this target.
		 */
		public SearchResult() {
			Point p= fTarget.getSelection();
			selection= p.x;
			length= p.y;
			index= fCurrentIndex;
			findLength= fFindString.length();
			found= fFound;
			forward= fForward;
		}

	}

	/**
	 * Stores the search result.
	 */
	private void saveState() {
		fSessionStack.push(new SearchResult());
	}

	/**
	 * Restores the search result.
	 *
	 * @since 2.1
	 */
	private void restoreState() {

		StyledText text= fTextViewer.getTextWidget();
		if (text == null || text.isDisposed())
			return;

		SearchResult searchResult= null;
		if (!fSessionStack.empty())
			searchResult= fSessionStack.pop();

		if (searchResult == null) {
			text.getDisplay().beep();
			return;
		}

		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()
	 * @since 2.1
	 */
	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();
	}

	@Override
	public boolean canPerformFind() {
		return fTarget.canPerformFind();
	}

	@Override
	public int findAndSelect(int offset, String findString, boolean searchForward, boolean caseSensitive, boolean wholeWord) {
		return fTarget.findAndSelect(offset, findString, searchForward, caseSensitive, wholeWord);
	}

	@Override
	public Point getSelection() {
		return fTarget.getSelection();
	}

	@Override
	public String getSelectionText() {
		return fTarget.getSelectionText();
	}

	@Override
	public boolean isEditable() {
		return false;
	}

	@Override
	public void replaceSelection(String text) {
	}

	@Override
	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<>();
		fCasePosition= -1;
		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();

		// Set the mark
		if (fTextViewer instanceof ITextViewerExtension) {
			int modelOffset;
			if (fTextViewer instanceof ITextViewerExtension5)
				modelOffset= fCurrentIndex == -1 ? -1 : ((ITextViewerExtension5)fTextViewer).widgetOffset2ModelOffset(fCurrentIndex);
			else
				modelOffset= fCurrentIndex;
			((ITextViewerExtension)fTextViewer).setMark(modelOffset);
		}

		updateStatus();

		if (fTarget instanceof IFindReplaceTargetExtension)
			((IFindReplaceTargetExtension) fTarget).beginSession();

		fSearching= false;
	}

	@Override
	public void endSession() {
		if (fTarget instanceof IFindReplaceTargetExtension)
			((IFindReplaceTargetExtension) fTarget).endSession();

		// will uninstall itself
	}

	@Override
	public IRegion getScope() {
		return null;
	}

	@Override
	public void setScope(IRegion scope) {
	}

	@Override
	public void setReplaceAllMode(boolean replaceAll) {
	}

	/**
	 * Installs this target. I.e. adds all required listeners.
	 */
	private void install() {

		if (fInstalled)
			return;

		StyledText text= fTextViewer.getTextWidget();
		if (text == null)
			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) fTextViewer).prependVerifyKeyListener(this);
		else
			text.addVerifyKeyListener(this);

		ICommandService commandService= PlatformUI.getWorkbench().getAdapter(ICommandService.class);
		if (commandService != null)
			commandService.addExecutionListener(this);

		fInstalled= true;
	}

	/**
	 * 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();
		if (text != null) {
			text.removeMouseListener(this);
			text.removeFocusListener(this);
		}

		if (fTextViewer instanceof ITextViewerExtension) {
			((ITextViewerExtension) fTextViewer).removeVerifyKeyListener(this);

		} else {
			if (text != null)
				text.removeVerifyKeyListener(this);
		}

		ICommandService commandService= PlatformUI.getWorkbench().getAdapter(ICommandService.class);
		if (commandService != null)
			commandService.removeExecutionListener(this);

		fInstalled= false;
	}

	/**
	 * Updates the status line.
	 * @since 2.1
	 */
	private void updateStatus() {

		if (!fInstalled)
			return;

		String string= fFindString.toString();
		String wrapPrefix= fWrapPosition == -1 ? "" : WRAPPED; //$NON-NLS-1$
		String reversePrefix= fForward ? "" : REVERSE; //$NON-NLS-1$

		if (!fFound) {
			String pattern= EditorMessages.Editor_FindIncremental_not_found_pattern;
			statusError(NLSUtility.format(pattern, new Object[] { reversePrefix, wrapPrefix, string }));

		} else if (string.length() == 0) {
			if (fForward)
				statusMessage(FIELD_NAME);
			else
				statusMessage(REVERSE_FIELD_NAME);
		} else if (!fForward || fWrapPosition > -1) {
			String pattern= EditorMessages.Editor_FindIncremental_found_pattern;
			statusMessage(NLSUtility.format(pattern, new Object[] { reversePrefix, wrapPrefix, string }));
		} else {
			statusMessage(string);
		}
	}

	@Override
	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.ARROW_LEFT:
			case SWT.ARROW_RIGHT:
			case SWT.HOME:
			case SWT.END:
			case SWT.PAGE_DOWN:
			case SWT.PAGE_UP:
				leave();
				break;

			case SWT.ARROW_DOWN:
				saveState();
				setDirection(true);
				repeatSearch(fForward);
				event.doit= false;
				break;

			case SWT.ARROW_UP:
				saveState();
				setDirection(false);
				repeatSearch(fForward);
				event.doit= false;
				break;
			}

		// event.character != 0
		} else {

			switch (event.character) {

			// ESC, CR = quit
			case 0x1B:
			case 0x0D:
				leave();
				event.doit= false;
				break;

			// backspace	and delete
			case 0x08:
			case 0x7F:
				restoreState();
				event.doit= false;
				break;

			default:
				int stateMask= event.stateMask;
				if (stateMask == 0
						|| stateMask == SWT.SHIFT
						|| !Util.isMac() && stateMask == (SWT.ALT | SWT.CTRL) // AltGr (see bug 43049)
						|| Util.isMac() && (stateMask == (SWT.ALT | SWT.SHIFT) || stateMask == SWT.ALT) ) { // special chars on Mac (bug 272994)
					saveState();
					addCharSearch(event.character);
					event.doit= false;
				}
				break;
			}
		}
		updateStatus();
		fSearching= false;
	}

	/**
	 * Repeats the last search while possibly changing the direction.
	 *
	 * @param forward <code>true</code> iff the next search should be forward
	 * @return if the search was successful
	 * @since 2.1
	 */
	private boolean repeatSearch(boolean forward) {
		if (fFindString.length() == 0) {
			fFindString= new StringBuffer(fPrevFindString);
			fCasePosition= fPrevCasePosition;
		}

		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;
	}

	/**
	 * 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
	 * @return <code>true</code> the search found a match
	 * @since 2.1
	 */
	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);
		}
		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();
			fPrevCasePosition= fCasePosition;
		}
		statusClear();
		uninstall();
		fSessionStack = null;
	}

	@Override
	public void textChanged(TextEvent event) {
		if (event.getDocumentEvent() != null)
			leave();
	}

	/*
	 * @see MouseListener##mouseDoubleClick(MouseEvent)
	 */
	@Override
	public void mouseDoubleClick(MouseEvent e) {
		leave();
	}

	@Override
	public void mouseDown(MouseEvent e) {
		leave();
	}

	@Override
	public void mouseUp(MouseEvent e) {
		leave();
	}

	@Override
	public void focusGained(FocusEvent e) {
	}

	@Override
	public void focusLost(FocusEvent e) {
		IBindingService bindingService= PlatformUI.getWorkbench().getAdapter(IBindingService.class);
		if (bindingService != null && !bindingService.isKeyFilterEnabled()) {
			/*
			 * Workaround for bug 492587: Autosave breaks Incremental Find:
			 * We don't want to leave when the Workbench Window temporarily disables controls to
			 * run an IRunnableWithProgress. There's no direct API to know that this happens, but
			 * we can rely on the implementation detail that WorkbenchWindow#run(..) disables the
			 * key filter (and is the only one who does this, except for the Keys preference page).
			 */
			return;
		}
		leave();
	}

	/**
	 * Sets the given string as status message, clears the status error message.
	 * @param string the status message
	 */
	private void statusMessage(String string) {
		if (fStatusField != null) {
			if (fIsStatusFieldExtension) {
				((IStatusFieldExtension)fStatusField).setErrorText(null);
				fStatusField.setText(escapeTabs(string));
				((IStatusFieldExtension)fStatusField).setVisible(true);
				fStatusLine.update(true);
			} else {
				fStatusLine.setErrorMessage(null);
				fStatusField.setText(escapeTabs(string));
			}
		} else {
			fStatusLine.setErrorMessage(null);
			fStatusLine.setMessage(escapeTabs(string));
		}
	}

	/**
	 * Sets the status error message, clears the status message.
	 * @param string the status error message
	 */
	private void statusError(String string) {
		if (fStatusField != null) {
			if (fIsStatusFieldExtension) {
				((IStatusFieldExtension)fStatusField).setErrorText(escapeTabs(string));
				fStatusField.setText(""); //$NON-NLS-1$
				((IStatusFieldExtension)fStatusField).setVisible(true);
				fStatusLine.update(true);
			} else {
				fStatusLine.setErrorMessage(escapeTabs(string));
				fStatusField.setText(""); //$NON-NLS-1$
			}
		} else {
			fStatusLine.setErrorMessage(escapeTabs(string));
			fStatusLine.setMessage(null);
		}
	}

	/**
	 * Clears the status message and the status error message.
	 */
	private void statusClear() {
		if (fStatusField != null) {
			if (fIsStatusFieldExtension) {
				fStatusField.setText(""); //$NON-NLS-1$
				((IStatusFieldExtension)fStatusField).setErrorText(null);
				((IStatusFieldExtension)fStatusField).setVisible(false);
				fStatusLine.update(true);
			} else {
				fStatusField.setText(""); //$NON-NLS-1$
				fStatusLine.setErrorMessage(null);
			}
		} else {
			fStatusLine.setErrorMessage(null);
			fStatusLine.setMessage(null);
		}
	}

	/**
	 * Translates all tab characters into a proper status line presentation.
	 * @param string the string in which to translate the tabs
	 * @return the given string with all tab characters replace with a proper status line presentation
	 */
	private String escapeTabs(String string) {
		StringBuffer buffer= new StringBuffer();

		int begin= 0;
		int end= string.indexOf('\t', begin);

		while (end >= 0) {
			buffer.append(string.substring(begin, end));
			buffer.append(TAB);
			begin= end + 1;
			end= string.indexOf('\t', begin);
		}
		buffer.append(string.substring(begin));

		return buffer.toString();
	}

	@Override
	public Point getLineSelection() {
		if (fTarget instanceof IFindReplaceTargetExtension)
			return ((IFindReplaceTargetExtension) fTarget).getLineSelection();

		return null; // XXX: should not return null
	}

	@Override
	public void setSelection(int offset, int length) {
		if (fTarget instanceof IFindReplaceTargetExtension)
			((IFindReplaceTargetExtension) fTarget).setSelection(offset, length);
	}

	@Override
	public void setScopeHighlightColor(Color color) {
	}

	@Override
	public void selectionChanged(SelectionChangedEvent e) {
		boolean ignore= false;
		ISelection selection= e.getSelection();
		if (selection instanceof ITextSelection) {
			ITextSelection textSelection= (ITextSelection)selection;
			Point range= getSelection();
			ignore= textSelection.getOffset() + textSelection.getLength() == range.x + range.y;
		}
		if (!fSearching && !ignore)
			leave();
	}

	/**
	 * Sets the find status field for this incremental find target.
	 *
	 * @param statusField the status field
	 * @since 3.0
	 */
	void setStatusField(IStatusField statusField) {
		fStatusField= statusField;
		fIsStatusFieldExtension= fStatusField instanceof IStatusFieldExtension;
	}

	@Override
	public void notHandled(String commandId, NotHandledException exception) {
	}

	@Override
	public void postExecuteFailure(String commandId, ExecutionException exception) {
	}

	@Override
	public void postExecuteSuccess(String commandId, Object returnValue) {
	}

	@Override
	public void preExecute(String commandId, ExecutionEvent event) {
		if (IWorkbenchActionDefinitionIds.FIND_INCREMENTAL.equals(commandId)
				|| IWorkbenchActionDefinitionIds.FIND_INCREMENTAL_REVERSE.equals(commandId))
			return;
		leave();
	}
}

Back to the top