c9870f12a2b530a8dc6dc217b0647d0885c0941b
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpdt / internal / ui / text / template / contentassist / PositionBasedCompletionProposal.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11
12 package net.sourceforge.phpdt.internal.ui.text.template.contentassist;
13
14 //incastrix
15 //import org.eclipse.jface.text.Assert;
16 import org.eclipse.core.runtime.Assert;
17 import org.eclipse.jface.text.BadLocationException;
18 import org.eclipse.jface.text.DocumentEvent;
19 import org.eclipse.jface.text.IDocument;
20 import org.eclipse.jface.text.ITextViewer;
21 import org.eclipse.jface.text.Position;
22 import org.eclipse.jface.text.contentassist.ICompletionProposal;
23 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension;
24 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension2;
25 import org.eclipse.jface.text.contentassist.IContextInformation;
26 import org.eclipse.swt.graphics.Image;
27 import org.eclipse.swt.graphics.Point;
28
29 /**
30  * An enhanced implementation of the <code>ICompletionProposal</code>
31  * interface implementing all the extension interfaces. It uses a position to
32  * track its replacement offset and length. The position must be set up
33  * externally.
34  */
35 public class PositionBasedCompletionProposal implements ICompletionProposal,
36                 ICompletionProposalExtension, ICompletionProposalExtension2 {
37
38         /** The string to be displayed in the completion proposal popup */
39         private String fDisplayString;
40
41         /** The replacement string */
42         private String fReplacementString;
43
44         /** The replacement position. */
45         private Position fReplacementPosition;
46
47         /** The cursor position after this proposal has been applied */
48         private int fCursorPosition;
49
50         /** The image to be displayed in the completion proposal popup */
51         private Image fImage;
52
53         /** The context information of this proposal */
54         private IContextInformation fContextInformation;
55
56         /** The additional info of this proposal */
57         private String fAdditionalProposalInfo;
58
59         /**
60          * Creates a new completion proposal based on the provided information. The
61          * replacement string is considered being the display string too. All
62          * remaining fields are set to <code>null</code>.
63          * 
64          * @param replacementString
65          *            the actual string to be inserted into the document
66          * @param replacementPosition
67          *            the position of the text to be replaced
68          * @param cursorPosition
69          *            the position of the cursor following the insert relative to
70          *            replacementOffset
71          */
72         public PositionBasedCompletionProposal(String replacementString,
73                         Position replacementPosition, int cursorPosition) {
74                 this(replacementString, replacementPosition, cursorPosition, null,
75                                 null, null, null);
76         }
77
78         /**
79          * Creates a new completion proposal. All fields are initialized based on
80          * the provided information.
81          * 
82          * @param replacementString
83          *            the actual string to be inserted into the document
84          * @param replacementPosition
85          *            the position of the text to be replaced
86          * @param cursorPosition
87          *            the position of the cursor following the insert relative to
88          *            replacementOffset
89          * @param image
90          *            the image to display for this proposal
91          * @param displayString
92          *            the string to be displayed for the proposal
93          * @param contextInformation
94          *            the context information associated with this proposal
95          * @param additionalProposalInfo
96          *            the additional information associated with this proposal
97          */
98         public PositionBasedCompletionProposal(String replacementString,
99                         Position replacementPosition, int cursorPosition, Image image,
100                         String displayString, IContextInformation contextInformation,
101                         String additionalProposalInfo) {
102                 Assert.isNotNull(replacementString);
103                 Assert.isTrue(replacementPosition != null);
104
105                 fReplacementString = replacementString;
106                 fReplacementPosition = replacementPosition;
107                 fCursorPosition = cursorPosition;
108                 fImage = image;
109                 fDisplayString = displayString;
110                 fContextInformation = contextInformation;
111                 fAdditionalProposalInfo = additionalProposalInfo;
112         }
113
114         /*
115          * @see ICompletionProposal#apply(IDocument)
116          */
117         public void apply(IDocument document) {
118                 try {
119                         document.replace(fReplacementPosition.getOffset(),
120                                         fReplacementPosition.getLength(), fReplacementString);
121                 } catch (BadLocationException x) {
122                         // ignore
123                 }
124         }
125
126         /*
127          * @see ICompletionProposal#getSelection(IDocument)
128          */
129         public Point getSelection(IDocument document) {
130                 return new Point(fReplacementPosition.getOffset() + fCursorPosition, 0);
131         }
132
133         /*
134          * @see ICompletionProposal#getContextInformation()
135          */
136         public IContextInformation getContextInformation() {
137                 return fContextInformation;
138         }
139
140         /*
141          * @see ICompletionProposal#getImage()
142          */
143         public Image getImage() {
144                 return fImage;
145         }
146
147         /*
148          * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getDisplayString()
149          */
150         public String getDisplayString() {
151                 if (fDisplayString != null)
152                         return fDisplayString;
153                 return fReplacementString;
154         }
155
156         /*
157          * @see ICompletionProposal#getAdditionalProposalInfo()
158          */
159         public String getAdditionalProposalInfo() {
160                 return fAdditionalProposalInfo;
161         }
162
163         /*
164          * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#apply(org.eclipse.jface.text.ITextViewer,
165          *      char, int, int)
166          */
167         public void apply(ITextViewer viewer, char trigger, int stateMask,
168                         int offset) {
169                 apply(viewer.getDocument());
170         }
171
172         /*
173          * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#selected(org.eclipse.jface.text.ITextViewer,
174          *      boolean)
175          */
176         public void selected(ITextViewer viewer, boolean smartToggle) {
177         }
178
179         /*
180          * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#unselected(org.eclipse.jface.text.ITextViewer)
181          */
182         public void unselected(ITextViewer viewer) {
183         }
184
185         /*
186          * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#validate(org.eclipse.jface.text.IDocument,
187          *      int, org.eclipse.jface.text.DocumentEvent)
188          */
189         public boolean validate(IDocument document, int offset, DocumentEvent event) {
190                 try {
191                         String content = document.get(fReplacementPosition.getOffset(),
192                                         fReplacementPosition.getLength());
193                         if (content.startsWith(fReplacementString))
194                                 return true;
195                 } catch (BadLocationException e) {
196                         // ignore concurrently modified document
197                 }
198                 return false;
199         }
200
201         /*
202          * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension#apply(org.eclipse.jface.text.IDocument,
203          *      char, int)
204          */
205         public void apply(IDocument document, char trigger, int offset) {
206                 // not called any more
207         }
208
209         /*
210          * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension#isValidFor(org.eclipse.jface.text.IDocument,
211          *      int)
212          */
213         public boolean isValidFor(IDocument document, int offset) {
214                 // not called any more
215                 return false;
216         }
217
218         /*
219          * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension#getTriggerCharacters()
220          */
221         public char[] getTriggerCharacters() {
222                 return null;
223         }
224
225         /*
226          * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension#getContextInformationPosition()
227          */
228         public int getContextInformationPosition() {
229                 return fReplacementPosition.getOffset();
230         }
231
232 }