KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > text > templates > PositionBasedCompletionProposal


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
12 package org.eclipse.jface.text.templates;
13
14
15 import org.eclipse.swt.graphics.Image;
16 import org.eclipse.swt.graphics.Point;
17
18 import org.eclipse.core.runtime.Assert;
19
20 import org.eclipse.jface.text.BadLocationException;
21 import org.eclipse.jface.text.DocumentEvent;
22 import org.eclipse.jface.text.IDocument;
23 import org.eclipse.jface.text.ITextViewer;
24 import org.eclipse.jface.text.Position;
25 import org.eclipse.jface.text.contentassist.ICompletionProposal;
26 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension2;
27 import org.eclipse.jface.text.contentassist.IContextInformation;
28
29
30 /**
31  * An enhanced implementation of the <code>ICompletionProposal</code> interface implementing all the extension interfaces.
32  *
33  * @since 3.0
34  */

35 final class PositionBasedCompletionProposal implements ICompletionProposal, ICompletionProposalExtension2 {
36
37     /** The string to be displayed in the completion proposal popup */
38     private String JavaDoc fDisplayString;
39     /** The replacement string */
40     private String JavaDoc fReplacementString;
41     /** The replacement position. */
42     private Position fReplacementPosition;
43     /** The cursor position after this proposal has been applied */
44     private int fCursorPosition;
45     /** The image to be displayed in the completion proposal popup */
46     private Image fImage;
47     /** The context information of this proposal */
48     private IContextInformation fContextInformation;
49     /** The additional info of this proposal */
50     private String JavaDoc fAdditionalProposalInfo;
51
52     /**
53      * Creates a new completion proposal based on the provided information. The replacement string is
54      * considered being the display string too. All remaining fields are set to <code>null</code>.
55      *
56      * @param replacementString the actual string to be inserted into the document
57      * @param replacementPosition the position of the text to be replaced
58      * @param cursorPosition the position of the cursor following the insert relative to replacementOffset
59      */

60     public PositionBasedCompletionProposal(String JavaDoc replacementString, Position replacementPosition, int cursorPosition) {
61         this(replacementString, replacementPosition, cursorPosition, null, null, null, null);
62     }
63
64     /**
65      * Creates a new completion proposal. All fields are initialized based on the provided information.
66      *
67      * @param replacementString the actual string to be inserted into the document
68      * @param replacementPosition the position of the text to be replaced
69      * @param cursorPosition the position of the cursor following the insert relative to replacementOffset
70      * @param image the image to display for this proposal
71      * @param displayString the string to be displayed for the proposal
72      * @param contextInformation the context information associated with this proposal
73      * @param additionalProposalInfo the additional information associated with this proposal
74      */

75     public PositionBasedCompletionProposal(String JavaDoc replacementString, Position replacementPosition, int cursorPosition, Image image, String JavaDoc displayString, IContextInformation contextInformation, String JavaDoc additionalProposalInfo) {
76         Assert.isNotNull(replacementString);
77         Assert.isTrue(replacementPosition != null);
78
79         fReplacementString= replacementString;
80         fReplacementPosition= replacementPosition;
81         fCursorPosition= cursorPosition;
82         fImage= image;
83         fDisplayString= displayString;
84         fContextInformation= contextInformation;
85         fAdditionalProposalInfo= additionalProposalInfo;
86     }
87
88     /*
89      * @see ICompletionProposal#apply(IDocument)
90      */

91     public void apply(IDocument document) {
92         try {
93             document.replace(fReplacementPosition.getOffset(), fReplacementPosition.getLength(), fReplacementString);
94         } catch (BadLocationException x) {
95             // ignore
96
}
97     }
98
99     /*
100      * @see ICompletionProposal#getSelection(IDocument)
101      */

102     public Point getSelection(IDocument document) {
103         return new Point(fReplacementPosition.getOffset() + fCursorPosition, 0);
104     }
105
106     /*
107      * @see ICompletionProposal#getContextInformation()
108      */

109     public IContextInformation getContextInformation() {
110         return fContextInformation;
111     }
112
113     /*
114      * @see ICompletionProposal#getImage()
115      */

116     public Image getImage() {
117         return fImage;
118     }
119
120     /*
121      * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getDisplayString()
122      */

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

132     public String JavaDoc getAdditionalProposalInfo() {
133         return fAdditionalProposalInfo;
134     }
135
136     /*
137      * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#apply(org.eclipse.jface.text.ITextViewer, char, int, int)
138      */

139     public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
140         apply(viewer.getDocument());
141     }
142
143     /*
144      * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#selected(org.eclipse.jface.text.ITextViewer, boolean)
145      */

146     public void selected(ITextViewer viewer, boolean smartToggle) {
147     }
148
149     /*
150      * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#unselected(org.eclipse.jface.text.ITextViewer)
151      */

152     public void unselected(ITextViewer viewer) {
153     }
154
155     /*
156      * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#validate(org.eclipse.jface.text.IDocument, int, org.eclipse.jface.text.DocumentEvent)
157      */

158     public boolean validate(IDocument document, int offset, DocumentEvent event) {
159         try {
160             String JavaDoc content= document.get(fReplacementPosition.getOffset(), offset - fReplacementPosition.getOffset());
161             if (fReplacementString.startsWith(content))
162                 return true;
163         } catch (BadLocationException e) {
164             // ignore concurrently modified document
165
}
166         return false;
167     }
168
169 }
170
Popular Tags