001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.texteditor;
011:
012: import org.eclipse.swt.graphics.Color;
013: import org.eclipse.swt.graphics.Point;
014:
015: import org.eclipse.jface.text.IFindReplaceTarget;
016: import org.eclipse.jface.text.IFindReplaceTargetExtension;
017: import org.eclipse.jface.text.IFindReplaceTargetExtension3;
018: import org.eclipse.jface.text.IRegion;
019:
020: /**
021: * Internal find/replace target wrapping the editor's source viewer.
022: * @since 2.1
023: */
024: class FindReplaceTarget implements IFindReplaceTarget,
025: IFindReplaceTargetExtension, IFindReplaceTargetExtension2,
026: IFindReplaceTargetExtension3 {
027:
028: /** The editor */
029: private AbstractTextEditor fEditor;
030: /** The find/replace target */
031: private IFindReplaceTarget fTarget;
032:
033: /**
034: * Creates a new find/replace target.
035: *
036: * @param editor the editor
037: * @param target the wrapped find/replace target
038: */
039: public FindReplaceTarget(AbstractTextEditor editor,
040: IFindReplaceTarget target) {
041: fEditor = editor;
042: fTarget = target;
043: }
044:
045: /**
046: * Returns the wrapped find/replace target.
047: *
048: * @return the wrapped find/replace target
049: */
050: private IFindReplaceTarget getTarget() {
051: return fTarget;
052: }
053:
054: /**
055: * Returns the find/replace target extension.
056: *
057: * @return the find/replace target extension
058: */
059: private IFindReplaceTargetExtension getExtension() {
060: if (fTarget instanceof IFindReplaceTargetExtension)
061: return (IFindReplaceTargetExtension) fTarget;
062: return null;
063: }
064:
065: /*
066: * @see org.eclipse.jface.text.IFindReplaceTarget#canPerformFind()
067: */
068: public boolean canPerformFind() {
069: if (getTarget() != null)
070: return getTarget().canPerformFind();
071: return false;
072: }
073:
074: /*
075: * @see org.eclipse.jface.text.IFindReplaceTarget#findAndSelect(int, java.lang.String, boolean, boolean, boolean)
076: */
077: public int findAndSelect(int offset, String findString,
078: boolean searchForward, boolean caseSensitive,
079: boolean wholeWord) {
080: if (getTarget() != null)
081: return getTarget().findAndSelect(offset, findString,
082: searchForward, caseSensitive, wholeWord);
083: return -1;
084: }
085:
086: /*
087: * @see org.eclipse.jface.text.IFindReplaceTargetExtension3#findAndSelect(int, String, boolean, boolean, boolean, boolean)
088: * @since 3.0
089: */
090: public int findAndSelect(int offset, String findString,
091: boolean searchForward, boolean caseSensitive,
092: boolean wholeWord, boolean regExSearch) {
093: if (getTarget() instanceof IFindReplaceTargetExtension3)
094: return ((IFindReplaceTargetExtension3) getTarget())
095: .findAndSelect(offset, findString, searchForward,
096: caseSensitive, wholeWord, regExSearch);
097:
098: // fallback
099: if (!regExSearch && getTarget() != null)
100: return getTarget().findAndSelect(offset, findString,
101: searchForward, caseSensitive, wholeWord);
102:
103: return -1;
104: }
105:
106: /*
107: * @see org.eclipse.jface.text.IFindReplaceTarget#getSelection()
108: */
109: public Point getSelection() {
110: if (getTarget() != null)
111: return getTarget().getSelection();
112: return new Point(-1, -1);
113: }
114:
115: /*
116: * @see org.eclipse.jface.text.IFindReplaceTarget#getSelectionText()
117: */
118: public String getSelectionText() {
119: if (getTarget() != null)
120: return getTarget().getSelectionText();
121: return ""; //$NON-NLS-1$
122: }
123:
124: /*
125: * @see org.eclipse.jface.text.IFindReplaceTarget#isEditable()
126: */
127: public boolean isEditable() {
128: if (getTarget() != null) {
129: if (getTarget().isEditable())
130: return true;
131: return fEditor.isEditorInputModifiable();
132: }
133: return false;
134: }
135:
136: /*
137: * @see org.eclipse.jface.text.IFindReplaceTarget#replaceSelection(java.lang.String)
138: */
139: public void replaceSelection(String text) {
140: if (getTarget() != null)
141: getTarget().replaceSelection(text);
142: }
143:
144: /*
145: * @see org.eclipse.jface.text.IFindReplaceTargetExtension3#replaceSelection(String, boolean)
146: * @since 3.0
147: */
148: public void replaceSelection(String text, boolean regExReplace) {
149: if (getTarget() instanceof IFindReplaceTargetExtension3) {
150: ((IFindReplaceTargetExtension3) getTarget())
151: .replaceSelection(text, regExReplace);
152: return;
153: }
154:
155: // fallback
156: if (!regExReplace && getTarget() != null)
157: getTarget().replaceSelection(text);
158: }
159:
160: /*
161: * @see org.eclipse.jface.text.IFindReplaceTargetExtension#beginSession()
162: */
163: public void beginSession() {
164: if (getExtension() != null)
165: getExtension().beginSession();
166: }
167:
168: /*
169: * @see org.eclipse.jface.text.IFindReplaceTargetExtension#endSession()
170: */
171: public void endSession() {
172: if (getExtension() != null)
173: getExtension().endSession();
174: }
175:
176: /*
177: * @see org.eclipse.jface.text.IFindReplaceTargetExtension#getScope()
178: */
179: public IRegion getScope() {
180: if (getExtension() != null)
181: return getExtension().getScope();
182: return null;
183: }
184:
185: /*
186: * @see org.eclipse.jface.text.IFindReplaceTargetExtension#setScope(org.eclipse.jface.text.IRegion)
187: */
188: public void setScope(IRegion scope) {
189: if (getExtension() != null)
190: getExtension().setScope(scope);
191: }
192:
193: /*
194: * @see org.eclipse.jface.text.IFindReplaceTargetExtension#getLineSelection()
195: */
196: public Point getLineSelection() {
197: if (getExtension() != null)
198: return getExtension().getLineSelection();
199: return null;
200: }
201:
202: /*
203: * @see org.eclipse.jface.text.IFindReplaceTargetExtension#setSelection(int, int)
204: */
205: public void setSelection(int offset, int length) {
206: if (getExtension() != null)
207: getExtension().setSelection(offset, length);
208: }
209:
210: /*
211: * @see org.eclipse.jface.text.IFindReplaceTargetExtension#setScopeHighlightColor(org.eclipse.swt.graphics.Color)
212: */
213: public void setScopeHighlightColor(Color color) {
214: if (getExtension() != null)
215: getExtension().setScopeHighlightColor(color);
216: }
217:
218: /*
219: * @see org.eclipse.jface.text.IFindReplaceTargetExtension#setReplaceAllMode(boolean)
220: */
221: public void setReplaceAllMode(boolean replaceAll) {
222: if (getExtension() != null)
223: getExtension().setReplaceAllMode(replaceAll);
224: }
225:
226: /*
227: * @see org.eclipse.ui.texteditor.IFindReplaceTargetExtension2#validateTargetState()
228: */
229: public boolean validateTargetState() {
230: return fEditor.validateEditorInputState();
231: }
232: }
|