KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > texteditor > FindReplaceTarget


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ui.texteditor;
13
14 import org.eclipse.swt.graphics.Color;
15 import org.eclipse.swt.graphics.Point;
16
17 import org.eclipse.jface.text.IFindReplaceTarget;
18 import org.eclipse.jface.text.IFindReplaceTargetExtension;
19 import org.eclipse.jface.text.IFindReplaceTargetExtension3;
20 import org.eclipse.jface.text.IRegion;
21
22 /**
23  * Internal find/replace target wrapping the editor's source viewer.
24  * @since 2.1
25  */

26 class FindReplaceTarget implements IFindReplaceTarget, IFindReplaceTargetExtension, IFindReplaceTargetExtension2, IFindReplaceTargetExtension3 {
27
28     /** The editor */
29     private AbstractTextEditor fEditor;
30     /** The find/replace target */
31     private IFindReplaceTarget fTarget;
32
33     /**
34      * Creates a new find/replace target.
35      *
36      * @param editor the editor
37      * @param target the wrapped find/replace target
38      */

39     public FindReplaceTarget(AbstractTextEditor editor, IFindReplaceTarget target) {
40         fEditor= editor;
41         fTarget= target;
42     }
43
44     /**
45      * Returns the wrapped find/replace target.
46      *
47      * @return the wrapped find/replace target
48      */

49     private IFindReplaceTarget getTarget() {
50         return fTarget;
51     }
52
53     /**
54      * Returns the find/replace target extension.
55      *
56      * @return the find/replace target extension
57      */

58     private IFindReplaceTargetExtension getExtension() {
59         if (fTarget instanceof IFindReplaceTargetExtension)
60             return (IFindReplaceTargetExtension) fTarget;
61         return null;
62     }
63
64     /*
65      * @see org.eclipse.jface.text.IFindReplaceTarget#canPerformFind()
66      */

67     public boolean canPerformFind() {
68         if (getTarget() != null)
69             return getTarget().canPerformFind();
70         return false;
71     }
72
73     /*
74      * @see org.eclipse.jface.text.IFindReplaceTarget#findAndSelect(int, java.lang.String, boolean, boolean, boolean)
75      */

76     public int findAndSelect(int offset, String JavaDoc findString, boolean searchForward, boolean caseSensitive, boolean wholeWord) {
77         if (getTarget() != null)
78             return getTarget().findAndSelect(offset, findString, searchForward, caseSensitive, wholeWord);
79         return -1;
80     }
81
82     /*
83      * @see org.eclipse.jface.text.IFindReplaceTargetExtension3#findAndSelect(int, String, boolean, boolean, boolean, boolean)
84      * @since 3.0
85      */

86     public int findAndSelect(int offset, String JavaDoc findString, boolean searchForward, boolean caseSensitive, boolean wholeWord, boolean regExSearch) {
87         if (getTarget() instanceof IFindReplaceTargetExtension3)
88             return ((IFindReplaceTargetExtension3)getTarget()).findAndSelect(offset, findString, searchForward, caseSensitive, wholeWord, regExSearch);
89
90         // fallback
91
if (!regExSearch && getTarget() != null)
92             return getTarget().findAndSelect(offset, findString, searchForward, caseSensitive, wholeWord);
93
94         return -1;
95     }
96
97     /*
98      * @see org.eclipse.jface.text.IFindReplaceTarget#getSelection()
99      */

100     public Point getSelection() {
101         if (getTarget() != null)
102             return getTarget().getSelection();
103         return null;
104     }
105
106     /*
107      * @see org.eclipse.jface.text.IFindReplaceTarget#getSelectionText()
108      */

109     public String JavaDoc getSelectionText() {
110         if (getTarget() != null)
111             return getTarget().getSelectionText();
112         return null;
113     }
114
115     /*
116      * @see org.eclipse.jface.text.IFindReplaceTarget#isEditable()
117      */

118     public boolean isEditable() {
119         if (getTarget() != null) {
120             if (getTarget().isEditable())
121                 return true;
122             return fEditor.isEditorInputModifiable();
123         }
124         return false;
125     }
126
127     /*
128      * @see org.eclipse.jface.text.IFindReplaceTarget#replaceSelection(java.lang.String)
129      */

130     public void replaceSelection(String JavaDoc text) {
131         if (getTarget() != null)
132             getTarget().replaceSelection(text);
133     }
134
135     /*
136      * @see org.eclipse.jface.text.IFindReplaceTargetExtension3#replaceSelection(String, boolean)
137      * @since 3.0
138      */

139     public void replaceSelection(String JavaDoc text, boolean regExReplace) {
140         if (getTarget() instanceof IFindReplaceTargetExtension3) {
141             ((IFindReplaceTargetExtension3)getTarget()).replaceSelection(text, regExReplace);
142             return;
143         }
144
145         // fallback
146
if (!regExReplace && getTarget() != null)
147             getTarget().replaceSelection(text);
148     }
149
150     /*
151      * @see org.eclipse.jface.text.IFindReplaceTargetExtension#beginSession()
152      */

153     public void beginSession() {
154         if (getExtension() != null)
155             getExtension().beginSession();
156     }
157
158     /*
159      * @see org.eclipse.jface.text.IFindReplaceTargetExtension#endSession()
160      */

161     public void endSession() {
162         if (getExtension() != null)
163             getExtension().endSession();
164     }
165
166     /*
167      * @see org.eclipse.jface.text.IFindReplaceTargetExtension#getScope()
168      */

169     public IRegion getScope() {
170         if (getExtension() != null)
171             return getExtension().getScope();
172         return null;
173     }
174
175     /*
176      * @see org.eclipse.jface.text.IFindReplaceTargetExtension#setScope(org.eclipse.jface.text.IRegion)
177      */

178     public void setScope(IRegion scope) {
179         if (getExtension() != null)
180             getExtension().setScope(scope);
181     }
182
183     /*
184      * @see org.eclipse.jface.text.IFindReplaceTargetExtension#getLineSelection()
185      */

186     public Point getLineSelection() {
187         if (getExtension() != null)
188             return getExtension().getLineSelection();
189         return null;
190     }
191
192     /*
193      * @see org.eclipse.jface.text.IFindReplaceTargetExtension#setSelection(int, int)
194      */

195     public void setSelection(int offset, int length) {
196         if (getExtension() != null)
197             getExtension().setSelection(offset, length);
198     }
199
200     /*
201      * @see org.eclipse.jface.text.IFindReplaceTargetExtension#setScopeHighlightColor(org.eclipse.swt.graphics.Color)
202      */

203     public void setScopeHighlightColor(Color color) {
204         if (getExtension() != null)
205             getExtension().setScopeHighlightColor(color);
206     }
207
208     /*
209      * @see org.eclipse.jface.text.IFindReplaceTargetExtension#setReplaceAllMode(boolean)
210      */

211     public void setReplaceAllMode(boolean replaceAll) {
212         if (getExtension() != null)
213             getExtension().setReplaceAllMode(replaceAll);
214     }
215
216     /*
217      * @see org.eclipse.ui.texteditor.IFindReplaceTargetExtension2#validateTargetState()
218      */

219     public boolean validateTargetState() {
220         return fEditor.validateEditorInputState();
221     }
222 }
223
Popular Tags