KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > text > contentassist > CompletionProposal


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 package org.eclipse.jface.text.contentassist;
12
13 import org.eclipse.swt.graphics.Image;
14 import org.eclipse.swt.graphics.Point;
15
16 import org.eclipse.core.runtime.Assert;
17
18 import org.eclipse.jface.text.BadLocationException;
19 import org.eclipse.jface.text.IDocument;
20
21
22 /**
23  * The standard implementation of the <code>ICompletionProposal</code> interface.
24  */

25 public final class CompletionProposal implements ICompletionProposal {
26
27     /** The string to be displayed in the completion proposal popup. */
28     private String JavaDoc fDisplayString;
29     /** The replacement string. */
30     private String JavaDoc fReplacementString;
31     /** The replacement offset. */
32     private int fReplacementOffset;
33     /** The replacement length. */
34     private int fReplacementLength;
35     /** The cursor position after this proposal has been applied. */
36     private int fCursorPosition;
37     /** The image to be displayed in the completion proposal popup. */
38     private Image fImage;
39     /** The context information of this proposal. */
40     private IContextInformation fContextInformation;
41     /** The additional info of this proposal. */
42     private String JavaDoc fAdditionalProposalInfo;
43
44     /**
45      * Creates a new completion proposal based on the provided information. The replacement string is
46      * considered being the display string too. All remaining fields are set to <code>null</code>.
47      *
48      * @param replacementString the actual string to be inserted into the document
49      * @param replacementOffset the offset of the text to be replaced
50      * @param replacementLength the length of the text to be replaced
51      * @param cursorPosition the position of the cursor following the insert relative to replacementOffset
52      */

53     public CompletionProposal(String JavaDoc replacementString, int replacementOffset, int replacementLength, int cursorPosition) {
54         this(replacementString, replacementOffset, replacementLength, cursorPosition, null, null, null, null);
55     }
56
57     /**
58      * Creates a new completion proposal. All fields are initialized based on the provided information.
59      *
60      * @param replacementString the actual string to be inserted into the document
61      * @param replacementOffset the offset of the text to be replaced
62      * @param replacementLength the length of the text to be replaced
63      * @param cursorPosition the position of the cursor following the insert relative to replacementOffset
64      * @param image the image to display for this proposal
65      * @param displayString the string to be displayed for the proposal
66      * @param contextInformation the context information associated with this proposal
67      * @param additionalProposalInfo the additional information associated with this proposal
68      */

69     public CompletionProposal(String JavaDoc replacementString, int replacementOffset, int replacementLength, int cursorPosition, Image image, String JavaDoc displayString, IContextInformation contextInformation, String JavaDoc additionalProposalInfo) {
70         Assert.isNotNull(replacementString);
71         Assert.isTrue(replacementOffset >= 0);
72         Assert.isTrue(replacementLength >= 0);
73         Assert.isTrue(cursorPosition >= 0);
74
75         fReplacementString= replacementString;
76         fReplacementOffset= replacementOffset;
77         fReplacementLength= replacementLength;
78         fCursorPosition= cursorPosition;
79         fImage= image;
80         fDisplayString= displayString;
81         fContextInformation= contextInformation;
82         fAdditionalProposalInfo= additionalProposalInfo;
83     }
84
85     /*
86      * @see ICompletionProposal#apply(IDocument)
87      */

88     public void apply(IDocument document) {
89         try {
90             document.replace(fReplacementOffset, fReplacementLength, fReplacementString);
91         } catch (BadLocationException x) {
92             // ignore
93
}
94     }
95
96     /*
97      * @see ICompletionProposal#getSelection(IDocument)
98      */

99     public Point getSelection(IDocument document) {
100         return new Point(fReplacementOffset + fCursorPosition, 0);
101     }
102
103     /*
104      * @see ICompletionProposal#getContextInformation()
105      */

106     public IContextInformation getContextInformation() {
107         return fContextInformation;
108     }
109
110     /*
111      * @see ICompletionProposal#getImage()
112      */

113     public Image getImage() {
114         return fImage;
115     }
116
117     /*
118      * @see ICompletionProposal#getDisplayString()
119      */

120     public String JavaDoc getDisplayString() {
121         if (fDisplayString != null)
122             return fDisplayString;
123         return fReplacementString;
124     }
125
126     /*
127      * @see ICompletionProposal#getAdditionalProposalInfo()
128      */

129     public String JavaDoc getAdditionalProposalInfo() {
130         return fAdditionalProposalInfo;
131     }
132 }
133
Popular Tags