001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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.jdt.internal.ui.text.template.contentassist;
011:
012: import org.eclipse.core.runtime.Assert;
013:
014: import org.eclipse.swt.graphics.Image;
015: import org.eclipse.swt.graphics.Point;
016:
017: import org.eclipse.jface.text.BadLocationException;
018: import org.eclipse.jface.text.DocumentEvent;
019: import org.eclipse.jface.text.IDocument;
020: import org.eclipse.jface.text.ITextViewer;
021: import org.eclipse.jface.text.Position;
022: import org.eclipse.jface.text.contentassist.ICompletionProposal;
023: import org.eclipse.jface.text.contentassist.ICompletionProposalExtension;
024: import org.eclipse.jface.text.contentassist.ICompletionProposalExtension2;
025: import org.eclipse.jface.text.contentassist.IContextInformation;
026:
027: /**
028: * An enhanced implementation of the <code>ICompletionProposal</code> interface implementing all the extension interfaces.
029: * It uses a position to track its replacement offset and length. The position must be set up externally.
030: */
031: public class PositionBasedCompletionProposal implements
032: ICompletionProposal, ICompletionProposalExtension,
033: ICompletionProposalExtension2 {
034:
035: /** The string to be displayed in the completion proposal popup */
036: private String fDisplayString;
037: /** The replacement string */
038: private String fReplacementString;
039: /** The replacement position. */
040: private Position fReplacementPosition;
041: /** The cursor position after this proposal has been applied */
042: private int fCursorPosition;
043: /** The image to be displayed in the completion proposal popup */
044: private Image fImage;
045: /** The context information of this proposal */
046: private IContextInformation fContextInformation;
047: /** The additional info of this proposal */
048: private String fAdditionalProposalInfo;
049:
050: /**
051: * Creates a new completion proposal based on the provided information. The replacement string is
052: * considered being the display string too. All remaining fields are set to <code>null</code>.
053: *
054: * @param replacementString the actual string to be inserted into the document
055: * @param replacementPosition the position of the text to be replaced
056: * @param cursorPosition the position of the cursor following the insert relative to replacementOffset
057: */
058: public PositionBasedCompletionProposal(String replacementString,
059: Position replacementPosition, int cursorPosition) {
060: this (replacementString, replacementPosition, cursorPosition,
061: null, null, null, null);
062: }
063:
064: /**
065: * Creates a new completion proposal. All fields are initialized based on the provided information.
066: *
067: * @param replacementString the actual string to be inserted into the document
068: * @param replacementPosition the position of the text to be replaced
069: * @param cursorPosition the position of the cursor following the insert relative to replacementOffset
070: * @param image the image to display for this proposal
071: * @param displayString the string to be displayed for the proposal
072: * @param contextInformation the context information associated with this proposal
073: * @param additionalProposalInfo the additional information associated with this proposal
074: */
075: public PositionBasedCompletionProposal(String replacementString,
076: Position replacementPosition, int cursorPosition,
077: Image image, String displayString,
078: IContextInformation contextInformation,
079: String additionalProposalInfo) {
080: Assert.isNotNull(replacementString);
081: Assert.isTrue(replacementPosition != null);
082:
083: fReplacementString = replacementString;
084: fReplacementPosition = replacementPosition;
085: fCursorPosition = cursorPosition;
086: fImage = image;
087: fDisplayString = displayString;
088: fContextInformation = contextInformation;
089: fAdditionalProposalInfo = additionalProposalInfo;
090: }
091:
092: /*
093: * @see ICompletionProposal#apply(IDocument)
094: */
095: public void apply(IDocument document) {
096: try {
097: document.replace(fReplacementPosition.getOffset(),
098: fReplacementPosition.getLength(),
099: fReplacementString);
100: } catch (BadLocationException x) {
101: // ignore
102: }
103: }
104:
105: /*
106: * @see ICompletionProposal#getSelection(IDocument)
107: */
108: public Point getSelection(IDocument document) {
109: return new Point(fReplacementPosition.getOffset()
110: + fCursorPosition, 0);
111: }
112:
113: /*
114: * @see ICompletionProposal#getContextInformation()
115: */
116: public IContextInformation getContextInformation() {
117: return fContextInformation;
118: }
119:
120: /*
121: * @see ICompletionProposal#getImage()
122: */
123: public Image getImage() {
124: return fImage;
125: }
126:
127: /*
128: * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getDisplayString()
129: */
130: public String getDisplayString() {
131: if (fDisplayString != null)
132: return fDisplayString;
133: return fReplacementString;
134: }
135:
136: /*
137: * @see ICompletionProposal#getAdditionalProposalInfo()
138: */
139: public String getAdditionalProposalInfo() {
140: return fAdditionalProposalInfo;
141: }
142:
143: /*
144: * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#apply(org.eclipse.jface.text.ITextViewer, char, int, int)
145: */
146: public void apply(ITextViewer viewer, char trigger, int stateMask,
147: int offset) {
148: apply(viewer.getDocument());
149: }
150:
151: /*
152: * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#selected(org.eclipse.jface.text.ITextViewer, boolean)
153: */
154: public void selected(ITextViewer viewer, boolean smartToggle) {
155: }
156:
157: /*
158: * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#unselected(org.eclipse.jface.text.ITextViewer)
159: */
160: public void unselected(ITextViewer viewer) {
161: }
162:
163: /*
164: * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#validate(org.eclipse.jface.text.IDocument, int, org.eclipse.jface.text.DocumentEvent)
165: */
166: public boolean validate(IDocument document, int offset,
167: DocumentEvent event) {
168: try {
169: String content = document.get(fReplacementPosition
170: .getOffset(), offset
171: - fReplacementPosition.getOffset());
172: if (fReplacementString.startsWith(content))
173: return true;
174: } catch (BadLocationException e) {
175: // ignore concurrently modified document
176: }
177: return false;
178: }
179:
180: /*
181: * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension#apply(org.eclipse.jface.text.IDocument, char, int)
182: */
183: public void apply(IDocument document, char trigger, int offset) {
184: // not called any more
185: }
186:
187: /*
188: * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension#isValidFor(org.eclipse.jface.text.IDocument, int)
189: */
190: public boolean isValidFor(IDocument document, int offset) {
191: // not called any more
192: return false;
193: }
194:
195: /*
196: * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension#getTriggerCharacters()
197: */
198: public char[] getTriggerCharacters() {
199: return null;
200: }
201:
202: /*
203: * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension#getContextInformationPosition()
204: */
205: public int getContextInformationPosition() {
206: return fReplacementPosition.getOffset();
207: }
208:
209: }
|