1 /*******************************************************************************
2 * Copyright (c) 2000, 2008 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
12 package org.eclipse.jdt.internal.ui.text.template.contentassist;
15 import org.eclipse.swt.graphics.Image;
16 import org.eclipse.swt.graphics.Point;
18 import org.eclipse.core.runtime.Assert;
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.ICompletionProposalExtension;
27 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension2;
28 import org.eclipse.jface.text.contentassist.IContextInformation;
32 * An enhanced implementation of the <code>ICompletionProposal</code> interface implementing all the extension interfaces.
33 * It uses a position to track its replacement offset and length. The position must be set up externally.
35 public class PositionBasedCompletionProposal implements ICompletionProposal, ICompletionProposalExtension, ICompletionProposalExtension2 {
37 /** The string to be displayed in the completion proposal popup */
38 private String fDisplayString;
39 /** The replacement string */
40 private String 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 */
47 /** The context information of this proposal */
48 private IContextInformation fContextInformation;
49 /** The additional info of this proposal */
50 private String fAdditionalProposalInfo;
51 /** The trigger characters */
52 private char[] fTriggerCharacters;
55 * Creates a new completion proposal based on the provided information. The replacement string is
56 * considered being the display string too. All remaining fields are set to <code>null</code>.
58 * @param replacementString the actual string to be inserted into the document
59 * @param replacementPosition the position of the text to be replaced
60 * @param cursorPosition the position of the cursor following the insert relative to replacementOffset
62 public PositionBasedCompletionProposal(String replacementString, Position replacementPosition, int cursorPosition) {
63 this(replacementString, replacementPosition, cursorPosition, null, null, null, null, null);
67 * Creates a new completion proposal. All fields are initialized based on the provided information.
69 * @param replacementString the actual string to be inserted into the document
70 * @param replacementPosition the position of the text to be replaced
71 * @param cursorPosition the position of the cursor following the insert relative to replacementOffset
72 * @param image the image to display for this proposal
73 * @param displayString the string to be displayed for the proposal
74 * @param contextInformation the context information associated with this proposal
75 * @param additionalProposalInfo the additional information associated with this proposal
76 * @param triggers the trigger characters
78 public PositionBasedCompletionProposal(String replacementString, Position replacementPosition, int cursorPosition, Image image, String displayString, IContextInformation contextInformation, String additionalProposalInfo, char[] triggers) {
79 Assert.isNotNull(replacementString);
80 Assert.isTrue(replacementPosition != null);
82 fReplacementString= replacementString;
83 fReplacementPosition= replacementPosition;
84 fCursorPosition= cursorPosition;
86 fDisplayString= displayString;
87 fContextInformation= contextInformation;
88 fAdditionalProposalInfo= additionalProposalInfo;
89 fTriggerCharacters= triggers;
93 * @see ICompletionProposal#apply(IDocument)
95 public void apply(IDocument document) {
97 document.replace(fReplacementPosition.getOffset(), fReplacementPosition.getLength(), fReplacementString);
98 } catch (BadLocationException x) {
104 * @see ICompletionProposal#getSelection(IDocument)
106 public Point getSelection(IDocument document) {
107 return new Point(fReplacementPosition.getOffset() + fCursorPosition, 0);
111 * @see ICompletionProposal#getContextInformation()
113 public IContextInformation getContextInformation() {
114 return fContextInformation;
118 * @see ICompletionProposal#getImage()
120 public Image getImage() {
125 * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getDisplayString()
127 public String getDisplayString() {
128 if (fDisplayString != null)
129 return fDisplayString;
130 return fReplacementString;
134 * @see ICompletionProposal#getAdditionalProposalInfo()
136 public String getAdditionalProposalInfo() {
137 return fAdditionalProposalInfo;
141 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#apply(org.eclipse.jface.text.ITextViewer, char, int, int)
143 public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
144 apply(viewer.getDocument());
148 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#selected(org.eclipse.jface.text.ITextViewer, boolean)
150 public void selected(ITextViewer viewer, boolean smartToggle) {
154 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#unselected(org.eclipse.jface.text.ITextViewer)
156 public void unselected(ITextViewer viewer) {
160 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#validate(org.eclipse.jface.text.IDocument, int, org.eclipse.jface.text.DocumentEvent)
162 public boolean validate(IDocument document, int offset, DocumentEvent event) {
164 String content= document.get(fReplacementPosition.getOffset(), offset - fReplacementPosition.getOffset());
165 if (fReplacementString.startsWith(content))
167 } catch (BadLocationException e) {
168 // ignore concurrently modified document
174 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension#apply(org.eclipse.jface.text.IDocument, char, int)
176 public void apply(IDocument document, char trigger, int offset) {
177 // not called any more
181 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension#isValidFor(org.eclipse.jface.text.IDocument, int)
183 public boolean isValidFor(IDocument document, int offset) {
184 // not called any more
189 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension#getTriggerCharacters()
191 public char[] getTriggerCharacters() {
192 return fTriggerCharacters;
196 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension#getContextInformationPosition()
198 public int getContextInformationPosition() {
199 return fReplacementPosition.getOffset();