KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > javadoc > JavaDocCompletionProcessor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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 package org.eclipse.jdt.internal.ui.text.javadoc;
12
13
14 import java.util.ArrayList;
15 import java.util.Arrays;
16
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IConfigurationElement;
19 import org.eclipse.core.runtime.IExtensionRegistry;
20 import org.eclipse.core.runtime.Platform;
21
22 import org.eclipse.swt.graphics.Point;
23
24 import org.eclipse.jface.text.ITextViewer;
25 import org.eclipse.jface.text.contentassist.ICompletionProposal;
26 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
27 import org.eclipse.jface.text.contentassist.IContextInformation;
28 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
29 import org.eclipse.jface.text.templates.TemplateContextType;
30
31 import org.eclipse.ui.IEditorPart;
32
33 import org.eclipse.jdt.core.ICompilationUnit;
34
35 import org.eclipse.jdt.internal.corext.template.java.JavaDocContextType;
36
37 import org.eclipse.jdt.ui.JavaUI;
38 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal;
39 import org.eclipse.jdt.ui.text.java.IJavadocCompletionProcessor;
40 import org.eclipse.jdt.ui.text.java.CompletionProposalComparator;
41
42 import org.eclipse.jdt.internal.ui.JavaPlugin;
43 import org.eclipse.jdt.internal.ui.text.template.contentassist.TemplateEngine;
44
45 /**
46  * Java doc completion processor using contributed IJavaDocCompletionProcessor's
47  * to evaluate proposals.
48  */

49 public class JavaDocCompletionProcessor implements IContentAssistProcessor {
50
51     private static final String PROCESSOR_CONTRIBUTION_ID= "javadocCompletionProcessor"; //$NON-NLS-1$
52

53     private IEditorPart fEditor;
54     private char[] fProposalAutoActivationSet;
55     private CompletionProposalComparator fComparator;
56     private TemplateEngine fTemplateEngine;
57     private int fSubProcessorFlags;
58     private String fErrorMessage;
59
60     private IJavadocCompletionProcessor[] fSubProcessors;
61
62     public JavaDocCompletionProcessor(IEditorPart editor) {
63         fEditor= editor;
64         TemplateContextType contextType= JavaPlugin.getDefault().getTemplateContextRegistry().getContextType("javadoc"); //$NON-NLS-1$
65
if (contextType == null) {
66             contextType= new JavaDocContextType();
67             JavaPlugin.getDefault().getTemplateContextRegistry().addContextType(contextType);
68         }
69         if (contextType != null)
70             fTemplateEngine= new TemplateEngine(contextType);
71         fSubProcessorFlags= 0;
72         fComparator= new CompletionProposalComparator();
73         fSubProcessors= null;
74         fErrorMessage= null;
75     }
76
77
78     private IJavadocCompletionProcessor[] getContributedProcessors() {
79         if (fSubProcessors == null) {
80             try {
81                 IExtensionRegistry registry= Platform.getExtensionRegistry();
82                 IConfigurationElement[] elements= registry.getConfigurationElementsFor(JavaUI.ID_PLUGIN, PROCESSOR_CONTRIBUTION_ID);
83                 IJavadocCompletionProcessor[] result= new IJavadocCompletionProcessor[elements.length];
84                 for (int i= 0; i < elements.length; i++) {
85                     result[i]= (IJavadocCompletionProcessor) elements[i].createExecutableExtension("class"); //$NON-NLS-1$
86
}
87                 fSubProcessors= result;
88             } catch (CoreException e) {
89                 JavaPlugin.log(e);
90                 fSubProcessors= new IJavadocCompletionProcessor[] { new JavaDocCompletionEvaluator() };
91             }
92         }
93         return fSubProcessors;
94     }
95
96
97     /**
98      * Tells this processor to order the proposals alphabetically.
99      *
100      * @param order <code>true</code> if proposals should be ordered.
101      */

102     public void orderProposalsAlphabetically(boolean order) {
103         fComparator.setOrderAlphabetically(order);
104     }
105
106     /**
107      * Tells this processor to restrict is proposals to those
108      * starting with matching cases.
109      *
110      * @param restrict <code>true</code> if proposals should be restricted
111      */

112     public void restrictProposalsToMatchingCases(boolean restrict) {
113         fSubProcessorFlags= restrict ? IJavadocCompletionProcessor.RESTRICT_TO_MATCHING_CASE : 0;
114     }
115
116     /**
117      * @see IContentAssistProcessor#getErrorMessage()
118      */

119     public String getErrorMessage() {
120         return fErrorMessage;
121     }
122
123     /**
124      * @see IContentAssistProcessor#getContextInformationValidator()
125      */

126     public IContextInformationValidator getContextInformationValidator() {
127         return null;
128     }
129
130     /**
131      * @see IContentAssistProcessor#getContextInformationAutoActivationCharacters()
132      */

133     public char[] getContextInformationAutoActivationCharacters() {
134         return null;
135     }
136
137     /**
138      * @see IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
139      */

140     public char[] getCompletionProposalAutoActivationCharacters() {
141         return fProposalAutoActivationSet;
142     }
143
144     /**
145      * Sets this processor's set of characters triggering the activation of the
146      * completion proposal computation.
147      *
148      * @param activationSet the activation set
149      */

150     public void setCompletionProposalAutoActivationCharacters(char[] activationSet) {
151         fProposalAutoActivationSet= activationSet;
152     }
153
154     /**
155      * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
156      */

157     public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
158         ICompilationUnit cu= JavaUI.getWorkingCopyManager().getWorkingCopy(fEditor.getEditorInput());
159
160         ArrayList result= new ArrayList();
161         String errorMessage= null;
162
163         IJavadocCompletionProcessor[] processors= getContributedProcessors();
164         for (int i= 0; i < processors.length; i++) {
165             IJavadocCompletionProcessor curr= processors[i];
166             IContextInformation[] contextInfos= curr.computeContextInformation(cu, offset);
167             if (contextInfos != null) {
168                 for (int k= 0; k < contextInfos.length; k++) {
169                     result.add(contextInfos[k]);
170                 }
171             }
172             if (curr.getErrorMessage() != null) {
173                 errorMessage= curr.getErrorMessage();
174             }
175         }
176         fErrorMessage= result.isEmpty() ? errorMessage : null;
177         return (IContextInformation[]) result.toArray(new IContextInformation[result.size()]);
178     }
179
180     /**
181      * @see IContentAssistProcessor#computeCompletionProposals(ITextViewer, int)
182      */

183     public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
184         ICompilationUnit cu= JavaUI.getWorkingCopyManager().getWorkingCopy(fEditor.getEditorInput());
185
186         int offset= documentOffset;
187         int length= 0;
188         Point selection= viewer.getSelectedRange();
189         if (selection.y > 0) {
190             offset= selection.x;
191             length= selection.y;
192         }
193
194         ArrayList result= new ArrayList();
195         String errorMessage= null;
196
197         IJavadocCompletionProcessor[] processors= getContributedProcessors();
198         for (int i= 0; i < processors.length; i++) {
199             IJavadocCompletionProcessor curr= processors[i];
200             IJavaCompletionProposal[] proposals= curr.computeCompletionProposals(cu, offset, length, fSubProcessorFlags);
201             if (proposals != null) {
202                 for (int k= 0; k < proposals.length; k++) {
203                     result.add(proposals[k]);
204                 }
205             }
206             if (curr.getErrorMessage() != null) {
207                 errorMessage= curr.getErrorMessage();
208             }
209         }
210         if (fTemplateEngine != null) {
211             fTemplateEngine.reset();
212             fTemplateEngine.complete(viewer, offset, cu);
213
214             ICompletionProposal[] templateResults= fTemplateEngine.getResults();
215             for (int k= 0; k < templateResults.length; k++) {
216                 result.add(templateResults[k]);
217             }
218         }
219         fErrorMessage= result.isEmpty() ? errorMessage : null;
220
221         IJavaCompletionProposal[] total= (IJavaCompletionProposal[]) result.toArray(new IJavaCompletionProposal[result.size()]);
222         return order(total);
223     }
224
225     /**
226      * Order the given proposals.
227      */

228     private IJavaCompletionProposal[] order(IJavaCompletionProposal[] proposals) {
229         Arrays.sort(proposals, fComparator);
230         return proposals;
231     }
232 }
233
Popular Tags