KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > texteditor > HippieProposalProcessor


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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  * Genady Beryozkin, me@genady.org - #getSuggestions implementation copied from HippieCompleteAction
11  *******************************************************************************/

12 package org.eclipse.ui.texteditor;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.swt.graphics.Image;
19 import org.eclipse.swt.graphics.Point;
20
21 import org.eclipse.jface.text.BadLocationException;
22 import org.eclipse.jface.text.DocumentEvent;
23 import org.eclipse.jface.text.IDocument;
24 import org.eclipse.jface.text.IInformationControlCreator;
25 import org.eclipse.jface.text.ITextViewer;
26 import org.eclipse.jface.text.contentassist.ICompletionProposal;
27 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension;
28 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension2;
29 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension3;
30 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension4;
31 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
32 import org.eclipse.jface.text.contentassist.IContextInformation;
33 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
34
35 import org.eclipse.ui.IEditorInput;
36 import org.eclipse.ui.IEditorPart;
37 import org.eclipse.ui.IEditorReference;
38 import org.eclipse.ui.IWorkbenchWindow;
39 import org.eclipse.ui.PlatformUI;
40 import org.eclipse.ui.internal.texteditor.HippieCompletionEngine;
41
42 /**
43  * A completion proposal computer for hippie word completions.
44  * <p>
45  * Clients may instantiate.
46  * </p>
47  *
48  * @since 3.2
49  */

50 public final class HippieProposalProcessor implements IContentAssistProcessor {
51
52     private static final ICompletionProposal[] NO_PROPOSALS= new ICompletionProposal[0];
53     private static final IContextInformation[] NO_CONTEXTS= new IContextInformation[0];
54
55     private static final class Proposal implements ICompletionProposal, ICompletionProposalExtension, ICompletionProposalExtension2, ICompletionProposalExtension3, ICompletionProposalExtension4 {
56
57         private final String JavaDoc fString;
58         private final String JavaDoc fPrefix;
59         private final int fOffset;
60
61         public Proposal(String JavaDoc string, String JavaDoc prefix, int offset) {
62             fString= string;
63             fPrefix= prefix;
64             fOffset= offset;
65         }
66
67         public void apply(IDocument document) {
68             apply(null, '\0', 0, fOffset);
69         }
70
71         public Point getSelection(IDocument document) {
72             return new Point(fOffset + fString.length(), 0);
73         }
74
75         public String JavaDoc getAdditionalProposalInfo() {
76             return null;
77         }
78
79         public String JavaDoc getDisplayString() {
80             return fPrefix + fString;
81         }
82
83         public Image getImage() {
84             return null;
85         }
86
87         public IContextInformation getContextInformation() {
88             return null;
89         }
90
91         public void apply(IDocument document, char trigger, int offset) {
92             try {
93                 String JavaDoc replacement= fString.substring(offset - fOffset);
94                 document.replace(offset, 0, replacement);
95             } catch (BadLocationException x) {
96                 // TODO Auto-generated catch block
97
x.printStackTrace();
98             }
99         }
100
101         public boolean isValidFor(IDocument document, int offset) {
102             return validate(document, offset, null);
103         }
104
105         public char[] getTriggerCharacters() {
106             return null;
107         }
108
109         public int getContextInformationPosition() {
110             return 0;
111         }
112
113         public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
114             apply(viewer.getDocument(), trigger, offset);
115         }
116
117         public void selected(ITextViewer viewer, boolean smartToggle) {
118         }
119
120         public void unselected(ITextViewer viewer) {
121         }
122
123         public boolean validate(IDocument document, int offset, DocumentEvent event) {
124             try {
125                 int prefixStart= fOffset - fPrefix.length();
126                 return offset >= fOffset && offset < fOffset + fString.length() && document.get(prefixStart, offset - (prefixStart)).equals((fPrefix + fString).substring(0, offset - prefixStart));
127             } catch (BadLocationException x) {
128                 return false;
129             }
130         }
131
132         public IInformationControlCreator getInformationControlCreator() {
133             return null;
134         }
135
136         public CharSequence JavaDoc getPrefixCompletionText(IDocument document, int completionOffset) {
137             return fPrefix + fString;
138         }
139
140         public int getPrefixCompletionStart(IDocument document, int completionOffset) {
141             return fOffset - fPrefix.length();
142         }
143
144         public boolean isAutoInsertable() {
145             return true;
146         }
147
148     }
149
150     private final HippieCompletionEngine fEngine= new HippieCompletionEngine();
151
152     /**
153      * Creates a new hippie completion proposal computer.
154      */

155     public HippieProposalProcessor() {
156     }
157
158     /*
159      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeCompletionProposals(org.eclipse.jface.text.ITextViewer, int)
160      */

161     public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
162         try {
163             String JavaDoc prefix= getPrefix(viewer, offset);
164             if (prefix == null || prefix.length() == 0)
165                 return NO_PROPOSALS;
166             
167             List JavaDoc suggestions= getSuggestions(viewer, offset, prefix);
168             
169             List JavaDoc result= new ArrayList JavaDoc();
170             for (Iterator JavaDoc it= suggestions.iterator(); it.hasNext();) {
171                 String JavaDoc string= (String JavaDoc) it.next();
172                 if (string.length() > 0)
173                     result.add(createProposal(string, prefix, offset));
174             }
175             
176             return (ICompletionProposal[]) result.toArray(new ICompletionProposal[result.size()]);
177             
178         } catch (BadLocationException x) {
179             // ignore and return no proposals
180
return NO_PROPOSALS;
181         }
182     }
183
184     private String JavaDoc getPrefix(ITextViewer viewer, int offset) throws BadLocationException {
185         IDocument doc= viewer.getDocument();
186         if (doc == null || offset > doc.getLength())
187             return null;
188         
189         int length= 0;
190         while (--offset >= 0 && Character.isJavaIdentifierPart(doc.getChar(offset)))
191             length++;
192         
193         return doc.get(offset + 1, length);
194     }
195
196     private ICompletionProposal createProposal(String JavaDoc string, String JavaDoc prefix, int offset) {
197         return new Proposal(string, prefix, offset);
198     }
199
200     /*
201      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeContextInformation(org.eclipse.jface.text.ITextViewer, int)
202      */

203     public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
204         // no context informations for hippie completions
205
return NO_CONTEXTS;
206     }
207     
208     /*
209      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
210      */

211     public char[] getCompletionProposalAutoActivationCharacters() {
212         return null;
213     }
214     
215     /*
216      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getContextInformationAutoActivationCharacters()
217      */

218     public char[] getContextInformationAutoActivationCharacters() {
219         return null;
220     }
221     
222     /*
223      * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getContextInformationValidator()
224      */

225     public IContextInformationValidator getContextInformationValidator() {
226         return null;
227     }
228
229     /**
230      * Return the list of suggestions from the current document. First the
231      * document is searched backwards from the caret position and then forwards.
232      *
233      * @param offset
234      * @param viewer
235      * @param prefix the completion prefix
236      * @return all possible completions that were found in the current document
237      * @throws BadLocationException if accessing the document fails
238      */

239     private ArrayList JavaDoc createSuggestionsFromOpenDocument(ITextViewer viewer, int offset, String JavaDoc prefix) throws BadLocationException {
240         IDocument document= viewer.getDocument();
241         ArrayList JavaDoc completions= new ArrayList JavaDoc();
242         completions.addAll(fEngine.getCompletionsBackwards(document, prefix, offset));
243         completions.addAll(fEngine.getCompletionsForward(document, prefix, offset - prefix.length(), true));
244
245         return completions;
246     }
247
248     /**
249      * Create the array of suggestions. It scans all open text editors and
250      * prefers suggestions from the currently open editor. It also adds the
251      * empty suggestion at the end.
252      *
253      * @param viewer
254      * @param offset
255      * @param prefix the prefix to search for
256      * @return the list of all possible suggestions in the currently open
257      * editors
258      * @throws BadLocationException if accessing the current document fails
259      */

260     private List JavaDoc getSuggestions(ITextViewer viewer, int offset, String JavaDoc prefix) throws BadLocationException {
261
262         ArrayList JavaDoc suggestions= createSuggestionsFromOpenDocument(viewer, offset, prefix);
263         IDocument currentDocument= viewer.getDocument();
264
265         IWorkbenchWindow window= PlatformUI.getWorkbench().getActiveWorkbenchWindow();
266         IEditorReference editorReferences[]= window.getActivePage().getEditorReferences();
267
268         for (int i= 0; i < editorReferences.length; i++) {
269             IEditorPart editor= editorReferences[i].getEditor(false); // don't create!
270
if (editor instanceof ITextEditor) {
271                 ITextEditor textEditor= (ITextEditor) editor;
272                 IEditorInput input= textEditor.getEditorInput();
273                 IDocument doc= textEditor.getDocumentProvider().getDocument(input);
274                 if (!currentDocument.equals(doc))
275                     suggestions.addAll(fEngine.getCompletionsForward(doc, prefix, 0, false));
276             }
277         }
278         // add the empty suggestion
279
suggestions.add(""); //$NON-NLS-1$
280

281         List JavaDoc uniqueSuggestions= fEngine.makeUnique(suggestions);
282
283         return uniqueSuggestions;
284     }
285
286     /*
287      * @see org.eclipse.jface.text.contentassist.ICompletionProposalComputer#getErrorMessage()
288      */

289     public String JavaDoc getErrorMessage() {
290         return null; // no custom error message
291
}
292 }
293
Popular Tags