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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
12 package net.sourceforge.phpdt.internal.ui.text.template.contentassist;
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;
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
33 public class PositionBasedCompletionProposal implements ICompletionProposal,
34 ICompletionProposalExtension, ICompletionProposalExtension2 {
36 /** The string to be displayed in the completion proposal popup */
37 private String fDisplayString;
39 /** The replacement string */
40 private String fReplacementString;
42 /** The replacement position. */
43 private Position fReplacementPosition;
45 /** The cursor position after this proposal has been applied */
46 private int fCursorPosition;
48 /** The image to be displayed in the completion proposal popup */
51 /** The context information of this proposal */
52 private IContextInformation fContextInformation;
54 /** The additional info of this proposal */
55 private String fAdditionalProposalInfo;
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>.
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
70 public PositionBasedCompletionProposal(String replacementString,
71 Position replacementPosition, int cursorPosition) {
72 this(replacementString, replacementPosition, cursorPosition, null,
77 * Creates a new completion proposal. All fields are initialized based on
78 * the provided information.
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
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
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);
103 fReplacementString = replacementString;
104 fReplacementPosition = replacementPosition;
105 fCursorPosition = cursorPosition;
107 fDisplayString = displayString;
108 fContextInformation = contextInformation;
109 fAdditionalProposalInfo = additionalProposalInfo;
113 * @see ICompletionProposal#apply(IDocument)
115 public void apply(IDocument document) {
117 document.replace(fReplacementPosition.getOffset(),
118 fReplacementPosition.getLength(), fReplacementString);
119 } catch (BadLocationException x) {
125 * @see ICompletionProposal#getSelection(IDocument)
127 public Point getSelection(IDocument document) {
128 return new Point(fReplacementPosition.getOffset() + fCursorPosition, 0);
132 * @see ICompletionProposal#getContextInformation()
134 public IContextInformation getContextInformation() {
135 return fContextInformation;
139 * @see ICompletionProposal#getImage()
141 public Image getImage() {
146 * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getDisplayString()
148 public String getDisplayString() {
149 if (fDisplayString != null)
150 return fDisplayString;
151 return fReplacementString;
155 * @see ICompletionProposal#getAdditionalProposalInfo()
157 public String getAdditionalProposalInfo() {
158 return fAdditionalProposalInfo;
162 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#apply(org.eclipse.jface.text.ITextViewer,
165 public void apply(ITextViewer viewer, char trigger, int stateMask,
167 apply(viewer.getDocument());
171 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#selected(org.eclipse.jface.text.ITextViewer,
174 public void selected(ITextViewer viewer, boolean smartToggle) {
178 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#unselected(org.eclipse.jface.text.ITextViewer)
180 public void unselected(ITextViewer viewer) {
184 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#validate(org.eclipse.jface.text.IDocument,
185 * int, org.eclipse.jface.text.DocumentEvent)
187 public boolean validate(IDocument document, int offset, DocumentEvent event) {
189 String content = document.get(fReplacementPosition.getOffset(),
190 fReplacementPosition.getLength());
191 if (content.startsWith(fReplacementString))
193 } catch (BadLocationException e) {
194 // ignore concurrently modified document
200 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension#apply(org.eclipse.jface.text.IDocument,
203 public void apply(IDocument document, char trigger, int offset) {
204 // not called any more
208 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension#isValidFor(org.eclipse.jface.text.IDocument,
211 public boolean isValidFor(IDocument document, int offset) {
212 // not called any more
217 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension#getTriggerCharacters()
219 public char[] getTriggerCharacters() {
224 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension#getContextInformationPosition()
226 public int getContextInformationPosition() {
227 return fReplacementPosition.getOffset();