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