KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > java > JavaCompletionProposal


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.jdt.internal.ui.text.java;
13
14
15 import org.eclipse.core.runtime.Assert;
16
17 import org.eclipse.swt.graphics.Image;
18
19 import org.eclipse.jface.text.IDocument;
20
21 import org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext;
22
23
24 public class JavaCompletionProposal extends AbstractJavaCompletionProposal {
25
26     /**
27      * Creates a new completion proposal. All fields are initialized based on the provided
28      * information.
29      *
30      * @param replacementString the actual string to be inserted into the document
31      * @param replacementOffset the offset of the text to be replaced
32      * @param replacementLength the length of the text to be replaced
33      * @param image the image to display for this proposal
34      * @param displayString the string to be displayed for the proposal If set to <code>null</code>,
35      * the replacement string will be taken as display string.
36      */

37     public JavaCompletionProposal(String JavaDoc replacementString, int replacementOffset, int replacementLength, Image image, String JavaDoc displayString, int relevance) {
38         this(replacementString, replacementOffset, replacementLength, image, displayString, relevance, false);
39     }
40     
41     /**
42      * Creates a new completion proposal. All fields are initialized based on the provided
43      * information.
44      *
45      * @param replacementString the actual string to be inserted into the document
46      * @param replacementOffset the offset of the text to be replaced
47      * @param replacementLength the length of the text to be replaced
48      * @param image the image to display for this proposal
49      * @param displayString the string to be displayed for the proposal If set to <code>null</code>,
50      * the replacement string will be taken as display string.
51      * @param relevance the relevance
52      * @param inJavadoc <code>true</code> for a javadoc proposal
53      * @since 3.2
54      */

55     public JavaCompletionProposal(String JavaDoc replacementString, int replacementOffset, int replacementLength, Image image, String JavaDoc displayString, int relevance, boolean inJavadoc) {
56         this(replacementString, replacementOffset, replacementLength, image, displayString, relevance, inJavadoc, null);
57     }
58     
59     /**
60      * Creates a new completion proposal. All fields are initialized based on the provided
61      * information.
62      *
63      * @param replacementString the actual string to be inserted into the document
64      * @param replacementOffset the offset of the text to be replaced
65      * @param replacementLength the length of the text to be replaced
66      * @param image the image to display for this proposal
67      * @param displayString the string to be displayed for the proposal If set to <code>null</code>,
68      * the replacement string will be taken as display string.
69      * @param relevance the relevance
70      * @param inJavadoc <code>true</code> for a javadoc proposal
71      * @param invocationContext the invocation context of this completion proposal or <code>null</code> not available
72      * @since 3.3
73      */

74     public JavaCompletionProposal(String JavaDoc replacementString, int replacementOffset, int replacementLength, Image image, String JavaDoc displayString, int relevance, boolean inJavadoc, JavaContentAssistInvocationContext invocationContext) {
75         super(invocationContext);
76         Assert.isNotNull(replacementString);
77         Assert.isTrue(replacementOffset >= 0);
78         Assert.isTrue(replacementLength >= 0);
79
80         setReplacementString(replacementString);
81         setReplacementOffset(replacementOffset);
82         setReplacementLength(replacementLength);
83         setImage(image);
84         setDisplayString(displayString == null ? replacementString : displayString);
85         setRelevance(relevance);
86         setCursorPosition(replacementString.length());
87         setInJavadoc(inJavadoc);
88         setSortString(displayString == null ? replacementString : displayString);
89     }
90
91     /*
92      * @see org.eclipse.jdt.internal.ui.text.java.AbstractJavaCompletionProposal#isValidPrefix(java.lang.String)
93      */

94     protected boolean isValidPrefix(String JavaDoc prefix) {
95         String JavaDoc word= getDisplayString();
96         if (isInJavadoc()) {
97             int idx = word.indexOf("{@link "); //$NON-NLS-1$
98
if (idx==0) {
99                 word = word.substring(7);
100             } else {
101                 idx = word.indexOf("{@value "); //$NON-NLS-1$
102
if (idx==0) {
103                     word = word.substring(8);
104                 }
105             }
106         } else if (word.indexOf("this.") != -1) { //$NON-NLS-1$
107
word= word.substring(word.indexOf("this.") + 5); //$NON-NLS-1$
108
}
109         return isPrefix(prefix, word);
110     }
111     
112     /*
113      * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension3#getReplacementText()
114      */

115     public CharSequence JavaDoc getPrefixCompletionText(IDocument document, int completionOffset) {
116         String JavaDoc string= getReplacementString();
117         int pos= string.indexOf('(');
118         if (pos > 0)
119             return string.subSequence(0, pos);
120         else if (string.startsWith("this.")) //$NON-NLS-1$
121
return string.substring(5);
122         else
123             return string;
124     }
125 }
126
Popular Tags