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.jdt.internal.ui.text.java;
011:
012: import org.eclipse.core.runtime.Assert;
013:
014: import org.eclipse.swt.graphics.Image;
015:
016: import org.eclipse.jface.text.IDocument;
017:
018: import org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext;
019:
020: public class JavaCompletionProposal extends
021: AbstractJavaCompletionProposal {
022:
023: /**
024: * Creates a new completion proposal. All fields are initialized based on the provided
025: * information.
026: *
027: * @param replacementString the actual string to be inserted into the document
028: * @param replacementOffset the offset of the text to be replaced
029: * @param replacementLength the length of the text to be replaced
030: * @param image the image to display for this proposal
031: * @param displayString the string to be displayed for the proposal If set to <code>null</code>, the replacement string will be taken as display string.
032: * @param relevance the relevance
033: */
034: public JavaCompletionProposal(String replacementString,
035: int replacementOffset, int replacementLength, Image image,
036: String displayString, int relevance) {
037: this (replacementString, replacementOffset, replacementLength,
038: image, displayString, relevance, false);
039: }
040:
041: /**
042: * Creates a new completion proposal. All fields are initialized based on the provided
043: * information.
044: *
045: * @param replacementString the actual string to be inserted into the document
046: * @param replacementOffset the offset of the text to be replaced
047: * @param replacementLength the length of the text to be replaced
048: * @param image the image to display for this proposal
049: * @param displayString the string to be displayed for the proposal If set to <code>null</code>,
050: * the replacement string will be taken as display string.
051: * @param relevance the relevance
052: * @param inJavadoc <code>true</code> for a javadoc proposal
053: * @since 3.2
054: */
055: public JavaCompletionProposal(String replacementString,
056: int replacementOffset, int replacementLength, Image image,
057: String displayString, int relevance, boolean inJavadoc) {
058: this (replacementString, replacementOffset, replacementLength,
059: image, displayString, relevance, inJavadoc, null);
060: }
061:
062: /**
063: * Creates a new completion proposal. All fields are initialized based on the provided
064: * information.
065: *
066: * @param replacementString the actual string to be inserted into the document
067: * @param replacementOffset the offset of the text to be replaced
068: * @param replacementLength the length of the text to be replaced
069: * @param image the image to display for this proposal
070: * @param displayString the string to be displayed for the proposal If set to <code>null</code>,
071: * the replacement string will be taken as display string.
072: * @param relevance the relevance
073: * @param inJavadoc <code>true</code> for a javadoc proposal
074: * @param invocationContext the invocation context of this completion proposal or <code>null</code> not available
075: * @since 3.3
076: */
077: public JavaCompletionProposal(String replacementString,
078: int replacementOffset, int replacementLength, Image image,
079: String displayString, int relevance, boolean inJavadoc,
080: JavaContentAssistInvocationContext invocationContext) {
081: super (invocationContext);
082: Assert.isNotNull(replacementString);
083: Assert.isTrue(replacementOffset >= 0);
084: Assert.isTrue(replacementLength >= 0);
085:
086: setReplacementString(replacementString);
087: setReplacementOffset(replacementOffset);
088: setReplacementLength(replacementLength);
089: setImage(image);
090: setDisplayString(displayString == null ? replacementString
091: : displayString);
092: setRelevance(relevance);
093: setCursorPosition(replacementString.length());
094: setInJavadoc(inJavadoc);
095: setSortString(displayString == null ? replacementString
096: : displayString);
097: }
098:
099: /*
100: * @see org.eclipse.jdt.internal.ui.text.java.AbstractJavaCompletionProposal#isValidPrefix(java.lang.String)
101: */
102: protected boolean isValidPrefix(String prefix) {
103: String word = getDisplayString();
104: if (isInJavadoc()) {
105: int idx = word.indexOf("{@link "); //$NON-NLS-1$
106: if (idx == 0) {
107: word = word.substring(7);
108: } else {
109: idx = word.indexOf("{@value "); //$NON-NLS-1$
110: if (idx == 0) {
111: word = word.substring(8);
112: }
113: }
114: } else if (word.indexOf("this.") != -1) { //$NON-NLS-1$
115: word = word.substring(word.indexOf("this.") + 5); //$NON-NLS-1$
116: }
117: return isPrefix(prefix, word);
118: }
119:
120: /*
121: * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension3#getReplacementText()
122: */
123: public CharSequence getPrefixCompletionText(IDocument document,
124: int completionOffset) {
125: String string = getReplacementString();
126: int pos = string.indexOf('(');
127: if (pos > 0)
128: return string.subSequence(0, pos);
129: else if (string.startsWith("this.")) //$NON-NLS-1$
130: return string.substring(5);
131: else
132: return string;
133: }
134: }
|