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.swt.graphics.Image;
16 import org.eclipse.swt.graphics.Point;
18 import org.eclipse.jface.text.Assert;
19 import org.eclipse.jface.text.BadLocationException;
20 import org.eclipse.jface.text.DocumentEvent;
21 import org.eclipse.jface.text.IDocument;
22 import org.eclipse.jface.text.ITextViewer;
23 import org.eclipse.jface.text.Position;
24 import org.eclipse.jface.text.contentassist.ICompletionProposal;
25 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension;
26 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension2;
27 import org.eclipse.jface.text.contentassist.IContextInformation;
31 * An enhanced implementation of the <code>ICompletionProposal</code> interface implementing all the extension interfaces.
32 * It uses a position to track its replacement offset and length. The position must be set up externally.
34 public class PositionBasedCompletionProposal implements ICompletionProposal, ICompletionProposalExtension, ICompletionProposalExtension2 {
36 /** The string to be displayed in the completion proposal popup */
37 private String fDisplayString;
38 /** The replacement string */
39 private String fReplacementString;
40 /** The replacement position. */
41 private Position fReplacementPosition;
42 /** The cursor position after this proposal has been applied */
43 private int fCursorPosition;
44 /** The image to be displayed in the completion proposal popup */
46 /** The context information of this proposal */
47 private IContextInformation fContextInformation;
48 /** The additional info of this proposal */
49 private String fAdditionalProposalInfo;
52 * Creates a new completion proposal based on the provided information. The replacement string is
53 * considered being the display string too. All remaining fields are set to <code>null</code>.
55 * @param replacementString the actual string to be inserted into the document
56 * @param replacementPosition the position of the text to be replaced
57 * @param cursorPosition the position of the cursor following the insert relative to replacementOffset
59 public PositionBasedCompletionProposal(String replacementString, Position replacementPosition, int cursorPosition) {
60 this(replacementString, replacementPosition, cursorPosition, null, null, null, null);
64 * Creates a new completion proposal. All fields are initialized based on the provided information.
66 * @param replacementString the actual string to be inserted into the document
67 * @param replacementPosition the position of the text to be replaced
68 * @param cursorPosition the position of the cursor following the insert relative to replacementOffset
69 * @param image the image to display for this proposal
70 * @param displayString the string to be displayed for the proposal
71 * @param contextInformation the context information associated with this proposal
72 * @param additionalProposalInfo the additional information associated with this proposal
74 public PositionBasedCompletionProposal(String replacementString, Position replacementPosition, int cursorPosition, Image image, String displayString, IContextInformation contextInformation, String additionalProposalInfo) {
75 Assert.isNotNull(replacementString);
76 Assert.isTrue(replacementPosition != null);
78 fReplacementString= replacementString;
79 fReplacementPosition= replacementPosition;
80 fCursorPosition= cursorPosition;
82 fDisplayString= displayString;
83 fContextInformation= contextInformation;
84 fAdditionalProposalInfo= additionalProposalInfo;
88 * @see ICompletionProposal#apply(IDocument)
90 public void apply(IDocument document) {
92 document.replace(fReplacementPosition.getOffset(), fReplacementPosition.getLength(), fReplacementString);
93 } catch (BadLocationException x) {
99 * @see ICompletionProposal#getSelection(IDocument)
101 public Point getSelection(IDocument document) {
102 return new Point(fReplacementPosition.getOffset() + fCursorPosition, 0);
106 * @see ICompletionProposal#getContextInformation()
108 public IContextInformation getContextInformation() {
109 return fContextInformation;
113 * @see ICompletionProposal#getImage()
115 public Image getImage() {
120 * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getDisplayString()
122 public String getDisplayString() {
123 if (fDisplayString != null)
124 return fDisplayString;
125 return fReplacementString;
129 * @see ICompletionProposal#getAdditionalProposalInfo()
131 public String getAdditionalProposalInfo() {
132 return fAdditionalProposalInfo;
136 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#apply(org.eclipse.jface.text.ITextViewer, char, int, int)
138 public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
139 apply(viewer.getDocument());
143 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#selected(org.eclipse.jface.text.ITextViewer, boolean)
145 public void selected(ITextViewer viewer, boolean smartToggle) {
149 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#unselected(org.eclipse.jface.text.ITextViewer)
151 public void unselected(ITextViewer viewer) {
155 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#validate(org.eclipse.jface.text.IDocument, int, org.eclipse.jface.text.DocumentEvent)
157 public boolean validate(IDocument document, int offset, DocumentEvent event) {
159 String content= document.get(fReplacementPosition.getOffset(), fReplacementPosition.getLength());
160 if (content.startsWith(fReplacementString))
162 } catch (BadLocationException e) {
163 // ignore concurrently modified document
169 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension#apply(org.eclipse.jface.text.IDocument, char, int)
171 public void apply(IDocument document, char trigger, int offset) {
172 // not called any more
176 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension#isValidFor(org.eclipse.jface.text.IDocument, int)
178 public boolean isValidFor(IDocument document, int offset) {
179 // not called any more
184 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension#getTriggerCharacters()
186 public char[] getTriggerCharacters() {
191 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension#getContextInformationPosition()
193 public int getContextInformationPosition() {
194 return fReplacementPosition.getOffset();