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.jface.text.BadLocationException;
17 import org.eclipse.jface.text.DocumentEvent;
18 import org.eclipse.jface.text.IDocument;
19 import org.eclipse.jface.text.ITextViewer;
20 import org.eclipse.jface.text.Position;
21 import org.eclipse.jface.text.contentassist.ICompletionProposal;
22 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension;
23 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension2;
24 import org.eclipse.jface.text.contentassist.IContextInformation;
25 import org.eclipse.swt.graphics.Image;
26 import org.eclipse.swt.graphics.Point;
30 * An enhanced implementation of the <code>ICompletionProposal</code> interface implementing all the extension interfaces.
31 * It uses a position to track its replacement offset and length. The position must be set up externally.
33 public class PositionBasedCompletionProposal implements ICompletionProposal, ICompletionProposalExtension, ICompletionProposalExtension2 {
35 /** The string to be displayed in the completion proposal popup */
36 private String fDisplayString;
37 /** The replacement string */
38 private String fReplacementString;
39 /** The replacement position. */
40 private Position fReplacementPosition;
41 /** The cursor position after this proposal has been applied */
42 private int fCursorPosition;
43 /** The image to be displayed in the completion proposal popup */
45 /** The context information of this proposal */
46 private IContextInformation fContextInformation;
47 /** The additional info of this proposal */
48 private String fAdditionalProposalInfo;
51 * Creates a new completion proposal based on the provided information. The replacement string is
52 * considered being the display string too. All remaining fields are set to <code>null</code>.
54 * @param replacementString the actual string to be inserted into the document
55 * @param replacementPosition the position of the text to be replaced
56 * @param cursorPosition the position of the cursor following the insert relative to replacementOffset
58 public PositionBasedCompletionProposal(String replacementString, Position replacementPosition, int cursorPosition) {
59 this(replacementString, replacementPosition, cursorPosition, null, null, null, null);
63 * Creates a new completion proposal. All fields are initialized based on the provided information.
65 * @param replacementString the actual string to be inserted into the document
66 * @param replacementPosition the position of the text to be replaced
67 * @param cursorPosition the position of the cursor following the insert relative to replacementOffset
68 * @param image the image to display for this proposal
69 * @param displayString the string to be displayed for the proposal
70 * @param contextInformation the context information associated with this proposal
71 * @param additionalProposalInfo the additional information associated with this proposal
73 public PositionBasedCompletionProposal(String replacementString, Position replacementPosition, int cursorPosition, Image image, String displayString, IContextInformation contextInformation, String additionalProposalInfo) {
74 Assert.isNotNull(replacementString);
75 Assert.isTrue(replacementPosition != null);
77 fReplacementString= replacementString;
78 fReplacementPosition= replacementPosition;
79 fCursorPosition= cursorPosition;
81 fDisplayString= displayString;
82 fContextInformation= contextInformation;
83 fAdditionalProposalInfo= additionalProposalInfo;
87 * @see ICompletionProposal#apply(IDocument)
89 public void apply(IDocument document) {
91 document.replace(fReplacementPosition.getOffset(), fReplacementPosition.getLength(), fReplacementString);
92 } catch (BadLocationException x) {
98 * @see ICompletionProposal#getSelection(IDocument)
100 public Point getSelection(IDocument document) {
101 return new Point(fReplacementPosition.getOffset() + fCursorPosition, 0);
105 * @see ICompletionProposal#getContextInformation()
107 public IContextInformation getContextInformation() {
108 return fContextInformation;
112 * @see ICompletionProposal#getImage()
114 public Image getImage() {
119 * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getDisplayString()
121 public String getDisplayString() {
122 if (fDisplayString != null)
123 return fDisplayString;
124 return fReplacementString;
128 * @see ICompletionProposal#getAdditionalProposalInfo()
130 public String getAdditionalProposalInfo() {
131 return fAdditionalProposalInfo;
135 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#apply(org.eclipse.jface.text.ITextViewer, char, int, int)
137 public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
138 apply(viewer.getDocument());
142 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#selected(org.eclipse.jface.text.ITextViewer, boolean)
144 public void selected(ITextViewer viewer, boolean smartToggle) {
148 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#unselected(org.eclipse.jface.text.ITextViewer)
150 public void unselected(ITextViewer viewer) {
154 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#validate(org.eclipse.jface.text.IDocument, int, org.eclipse.jface.text.DocumentEvent)
156 public boolean validate(IDocument document, int offset, DocumentEvent event) {
158 String content= document.get(fReplacementPosition.getOffset(), fReplacementPosition.getLength());
159 if (content.startsWith(fReplacementString))
161 } catch (BadLocationException e) {
162 // ignore concurrently modified document
168 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension#apply(org.eclipse.jface.text.IDocument, char, int)
170 public void apply(IDocument document, char trigger, int offset) {
171 // not called any more
175 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension#isValidFor(org.eclipse.jface.text.IDocument, int)
177 public boolean isValidFor(IDocument document, int offset) {
178 // not called any more
183 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension#getTriggerCharacters()
185 public char[] getTriggerCharacters() {
190 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension#getContextInformationPosition()
192 public int getContextInformationPosition() {
193 return fReplacementPosition.getOffset();