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;
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;
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
35 public class PositionBasedCompletionProposal implements ICompletionProposal,
36 ICompletionProposalExtension, ICompletionProposalExtension2 {
38 /** The string to be displayed in the completion proposal popup */
39 private String fDisplayString;
41 /** The replacement string */
42 private String fReplacementString;
44 /** The replacement position. */
45 private Position fReplacementPosition;
47 /** The cursor position after this proposal has been applied */
48 private int fCursorPosition;
50 /** The image to be displayed in the completion proposal popup */
53 /** The context information of this proposal */
54 private IContextInformation fContextInformation;
56 /** The additional info of this proposal */
57 private String fAdditionalProposalInfo;
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>.
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
72 public PositionBasedCompletionProposal(String replacementString,
73 Position replacementPosition, int cursorPosition) {
74 this(replacementString, replacementPosition, cursorPosition, null,
79 * Creates a new completion proposal. All fields are initialized based on
80 * the provided information.
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
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
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);
105 fReplacementString = replacementString;
106 fReplacementPosition = replacementPosition;
107 fCursorPosition = cursorPosition;
109 fDisplayString = displayString;
110 fContextInformation = contextInformation;
111 fAdditionalProposalInfo = additionalProposalInfo;
115 * @see ICompletionProposal#apply(IDocument)
117 public void apply(IDocument document) {
119 document.replace(fReplacementPosition.getOffset(),
120 fReplacementPosition.getLength(), fReplacementString);
121 } catch (BadLocationException x) {
127 * @see ICompletionProposal#getSelection(IDocument)
129 public Point getSelection(IDocument document) {
130 return new Point(fReplacementPosition.getOffset() + fCursorPosition, 0);
134 * @see ICompletionProposal#getContextInformation()
136 public IContextInformation getContextInformation() {
137 return fContextInformation;
141 * @see ICompletionProposal#getImage()
143 public Image getImage() {
148 * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getDisplayString()
150 public String getDisplayString() {
151 if (fDisplayString != null)
152 return fDisplayString;
153 return fReplacementString;
157 * @see ICompletionProposal#getAdditionalProposalInfo()
159 public String getAdditionalProposalInfo() {
160 return fAdditionalProposalInfo;
164 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#apply(org.eclipse.jface.text.ITextViewer,
167 public void apply(ITextViewer viewer, char trigger, int stateMask,
169 apply(viewer.getDocument());
173 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#selected(org.eclipse.jface.text.ITextViewer,
176 public void selected(ITextViewer viewer, boolean smartToggle) {
180 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#unselected(org.eclipse.jface.text.ITextViewer)
182 public void unselected(ITextViewer viewer) {
186 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#validate(org.eclipse.jface.text.IDocument,
187 * int, org.eclipse.jface.text.DocumentEvent)
189 public boolean validate(IDocument document, int offset, DocumentEvent event) {
191 String content = document.get(fReplacementPosition.getOffset(),
192 fReplacementPosition.getLength());
193 if (content.startsWith(fReplacementString))
195 } catch (BadLocationException e) {
196 // ignore concurrently modified document
202 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension#apply(org.eclipse.jface.text.IDocument,
205 public void apply(IDocument document, char trigger, int offset) {
206 // not called any more
210 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension#isValidFor(org.eclipse.jface.text.IDocument,
213 public boolean isValidFor(IDocument document, int offset) {
214 // not called any more
219 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension#getTriggerCharacters()
221 public char[] getTriggerCharacters() {
226 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension#getContextInformationPosition()
228 public int getContextInformationPosition() {
229 return fReplacementPosition.getOffset();