package org.eclipse.jface.text.contentassist; | |
/* | |
* (c) Copyright IBM Corp. 2000, 2001. | |
* All Rights Reserved. | |
*/ | |
import org.eclipse.swt.graphics.Image; | |
import org.eclipse.swt.graphics.Point; | |
import org.eclipse.jface.text.BadLocationException; | |
import org.eclipse.jface.text.IDocument; | |
import org.eclipse.jface.util.Assert; | |
/** | |
* The standard implementation of the <code>ICompletionProposal</code> interface. | |
*/ | |
public final class CompletionProposal implements ICompletionProposal { | |
private String fDisplayString; | |
private String fReplacementString; | |
private int fReplacementOffset; | |
private int fReplacementLength; | |
private int fCursorPosition; | |
private Image fImage; | |
private IContextInformation fContextInformation; | |
private String fAdditionalProposalInfo; | |
/** | |
* Creates a new completion proposal based on the provided information. The replacement string is | |
* considered being the display string too. All remaining fields are set to <code>null</code>. | |
* | |
* @param replacementString the actual string to be inserted into the document | |
* @param replacementOffset the offset of the text to be replaced | |
* @param replacementLength the length of the text to be replaced | |
* @param cursorPosition the position of the cursor following the insert relative to replacementOffset | |
*/ | |
public CompletionProposal(String replacementString, int replacementOffset, int replacementLength, int cursorPosition) { | |
this(replacementString, replacementOffset, replacementLength, cursorPosition, null, null, null, null); | |
} | |
/** | |
* Creates a new completion proposal. All fields are initialized based on the provided information. | |
* | |
* @param replacementString the actual string to be inserted into the document | |
* @param replacementOffset the offset of the text to be replaced | |
* @param replacementLength the length of the text to be replaced | |
* @param cursorPosition the position of the cursor following the insert relative to replacementOffset | |
* @param image the image to display for this proposal | |
* @param displayString the string to be displayed for the proposal | |
* @param contentInformation the context information associated with this proposal | |
* @param additionalProposalInfo the additional information associated with this proposal | |
*/ | |
public CompletionProposal(String replacementString, int replacementOffset, int replacementLength, int cursorPosition, Image image, String displayString, IContextInformation contextInformation, String additionalProposalInfo) { | |
Assert.isNotNull(replacementString); | |
Assert.isTrue(replacementOffset >= 0); | |
Assert.isTrue(replacementLength >= 0); | |
Assert.isTrue(cursorPosition >= 0); | |
fReplacementString= replacementString; | |
fReplacementOffset= replacementOffset; | |
fReplacementLength= replacementLength; | |
fCursorPosition= cursorPosition; | |
fImage= image; | |
fDisplayString= displayString; | |
fContextInformation= contextInformation; | |
fAdditionalProposalInfo= additionalProposalInfo; | |
} | |
/* | |
* @see ICompletionProposal#apply | |
*/ | |
public void apply(IDocument document) { | |
try { | |
document.replace(fReplacementOffset, fReplacementLength, fReplacementString); | |
} catch (BadLocationException x) { | |
// ignore | |
} | |
} | |
/* | |
* @see ICompletionProposal#getAdditionalProposalInfo() | |
*/ | |
public String getAdditionalProposalInfo() { | |
return fAdditionalProposalInfo; | |
} | |
/* | |
* @see ICompletionProposal#getContextInformation() | |
*/ | |
public IContextInformation getContextInformation() { | |
return fContextInformation; | |
} | |
/* | |
* @see ICompletionProposal#getDisplayString() | |
*/ | |
public String getDisplayString() { | |
if (fDisplayString != null) | |
return fDisplayString; | |
return fReplacementString; | |
} | |
/* | |
* @see ICompletionProposal#getImage() | |
*/ | |
public Image getImage() { | |
return fImage; | |
} | |
/* | |
* @see ICompletionProposal#getSelection | |
*/ | |
public Point getSelection(IDocument document) { | |
return new Point(fReplacementOffset + fCursorPosition, 0); | |
} | |
} |