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