dd9d5c19b180f6f4861dd33a24071867fceb9896
[phpeclipse.git] /
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11
12 package net.sourceforge.phpdt.internal.ui.text.template.contentassist;
13
14
15 import org.eclipse.swt.graphics.Image;
16 import org.eclipse.swt.graphics.Point;
17
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;
28
29
30 /**
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.
33  */
34 public class PositionBasedCompletionProposal implements ICompletionProposal, ICompletionProposalExtension, ICompletionProposalExtension2 {
35         
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 */
45         private Image fImage;
46         /** The context information of this proposal */
47         private IContextInformation fContextInformation;
48         /** The additional info of this proposal */
49         private String fAdditionalProposalInfo;
50         
51         /**
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>.
54          *
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
58          */
59         public PositionBasedCompletionProposal(String replacementString, Position replacementPosition, int cursorPosition) {
60                 this(replacementString, replacementPosition, cursorPosition, null, null, null, null);
61         }
62         
63         /**
64          * Creates a new completion proposal. All fields are initialized based on the provided information.
65          *
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
73          */
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);
77                 
78                 fReplacementString= replacementString;
79                 fReplacementPosition= replacementPosition;
80                 fCursorPosition= cursorPosition;
81                 fImage= image;
82                 fDisplayString= displayString;
83                 fContextInformation= contextInformation;
84                 fAdditionalProposalInfo= additionalProposalInfo;
85         }
86         
87         /*
88          * @see ICompletionProposal#apply(IDocument)
89          */
90         public void apply(IDocument document) {
91                 try {
92                         document.replace(fReplacementPosition.getOffset(), fReplacementPosition.getLength(), fReplacementString);
93                 } catch (BadLocationException x) {
94                         // ignore
95                 }
96         }
97         
98         /*
99          * @see ICompletionProposal#getSelection(IDocument)
100          */
101         public Point getSelection(IDocument document) {
102                 return new Point(fReplacementPosition.getOffset() + fCursorPosition, 0);
103         }
104         
105         /*
106          * @see ICompletionProposal#getContextInformation()
107          */
108         public IContextInformation getContextInformation() {
109                 return fContextInformation;
110         }
111         
112         /*
113          * @see ICompletionProposal#getImage()
114          */
115         public Image getImage() {
116                 return fImage;
117         }
118         
119         /*
120          * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getDisplayString()
121          */
122         public String getDisplayString() {
123                 if (fDisplayString != null)
124                         return fDisplayString;
125                 return fReplacementString;
126         }
127         
128         /*
129          * @see ICompletionProposal#getAdditionalProposalInfo()
130          */
131         public String getAdditionalProposalInfo() {
132                 return fAdditionalProposalInfo;
133         }
134         
135         /*
136          * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#apply(org.eclipse.jface.text.ITextViewer, char, int, int)
137          */
138         public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
139                 apply(viewer.getDocument());
140         }
141         
142         /*
143          * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#selected(org.eclipse.jface.text.ITextViewer, boolean)
144          */
145         public void selected(ITextViewer viewer, boolean smartToggle) {
146         }
147         
148         /*
149          * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#unselected(org.eclipse.jface.text.ITextViewer)
150          */
151         public void unselected(ITextViewer viewer) {
152         }
153         
154         /*
155          * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#validate(org.eclipse.jface.text.IDocument, int, org.eclipse.jface.text.DocumentEvent)
156          */
157         public boolean validate(IDocument document, int offset, DocumentEvent event) {
158                 try {
159                         String content= document.get(fReplacementPosition.getOffset(), fReplacementPosition.getLength());
160                         if (content.startsWith(fReplacementString))
161                                 return true;
162                 } catch (BadLocationException e) {
163                         // ignore concurrently modified document
164                 }
165                 return false;
166         }
167
168         /*
169          * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension#apply(org.eclipse.jface.text.IDocument, char, int)
170          */
171         public void apply(IDocument document, char trigger, int offset) {
172                 // not called any more
173         }
174
175         /*
176          * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension#isValidFor(org.eclipse.jface.text.IDocument, int)
177          */
178         public boolean isValidFor(IDocument document, int offset) {
179                 // not called any more
180                 return false;
181         }
182
183         /*
184          * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension#getTriggerCharacters()
185          */
186         public char[] getTriggerCharacters() {
187                 return null;
188         }
189
190         /*
191          * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension#getContextInformationPosition()
192          */
193         public int getContextInformationPosition() {
194                 return fReplacementPosition.getOffset();
195         }
196         
197 }
198