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