KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 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  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.text.javadoc;
12
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IConfigurationElement;
20 import org.eclipse.core.runtime.IExtensionRegistry;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.Platform;
23
24 import org.eclipse.swt.graphics.Point;
25
26
27 import org.eclipse.jface.text.contentassist.IContextInformation;
28
29 import org.eclipse.jdt.core.ICompilationUnit;
30
31 import org.eclipse.jdt.ui.JavaUI;
32 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposalComputer;
33 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal;
34 import org.eclipse.jdt.ui.text.java.IJavadocCompletionProcessor;
35 import org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext;
36 import org.eclipse.jdt.ui.text.java.ContentAssistInvocationContext;
37
38 import org.eclipse.jdt.internal.ui.JavaPlugin;
39
40 /**
41  * Java doc completion processor using contributed IJavaDocCompletionProcessor's
42  * to evaluate proposals.
43  *
44  * @since 3.2
45  */

46 public class LegacyJavadocCompletionProposalComputer implements IJavaCompletionProposalComputer {
47
48     private static final String JavaDoc PROCESSOR_CONTRIBUTION_ID= "javadocCompletionProcessor"; //$NON-NLS-1$
49

50     private IJavadocCompletionProcessor[] fSubProcessors;
51
52     private String JavaDoc fErrorMessage;
53
54     public LegacyJavadocCompletionProposalComputer() {
55         fSubProcessors= null;
56     }
57
58
59     private IJavadocCompletionProcessor[] getContributedProcessors() {
60         if (fSubProcessors == null) {
61             try {
62                 IExtensionRegistry registry= Platform.getExtensionRegistry();
63                 IConfigurationElement[] elements= registry.getConfigurationElementsFor(JavaUI.ID_PLUGIN, PROCESSOR_CONTRIBUTION_ID);
64                 IJavadocCompletionProcessor[] result= new IJavadocCompletionProcessor[elements.length];
65                 for (int i= 0; i < elements.length; i++) {
66                     result[i]= (IJavadocCompletionProcessor) elements[i].createExecutableExtension("class"); //$NON-NLS-1$
67
}
68                 fSubProcessors= result;
69             } catch (CoreException e) {
70                 JavaPlugin.log(e);
71                 fSubProcessors= new IJavadocCompletionProcessor[0];
72             }
73         }
74         return fSubProcessors;
75     }
76
77     /*
78      * @see org.eclipse.jface.text.contentassist.ICompletionProposalComputer#computeContextInformation(org.eclipse.jface.text.contentassist.TextContentAssistInvocationContext, org.eclipse.core.runtime.IProgressMonitor)
79      */

80     public List JavaDoc computeContextInformation(ContentAssistInvocationContext context, IProgressMonitor monitor) {
81         if (context instanceof JavaContentAssistInvocationContext) {
82             JavaContentAssistInvocationContext javaContext= (JavaContentAssistInvocationContext) context;
83             
84             ICompilationUnit cu= javaContext.getCompilationUnit();
85             int offset= javaContext.getInvocationOffset();
86             
87             ArrayList JavaDoc result= new ArrayList JavaDoc();
88             
89             IJavadocCompletionProcessor[] processors= getContributedProcessors();
90             String JavaDoc error= null;
91             for (int i= 0; i < processors.length; i++) {
92                 IJavadocCompletionProcessor curr= processors[i];
93                 IContextInformation[] contextInfos= curr.computeContextInformation(cu, offset);
94                 if (contextInfos != null) {
95                     for (int k= 0; k < contextInfos.length; k++) {
96                         result.add(contextInfos[k]);
97                     }
98                 } else if (error == null) {
99                     error= curr.getErrorMessage();
100                 }
101             }
102             fErrorMessage= error;
103             return result;
104         }
105         return Collections.EMPTY_LIST;
106     }
107
108     /*
109      * @see org.eclipse.jface.text.contentassist.ICompletionProposalComputer#computeCompletionProposals(org.eclipse.jface.text.contentassist.TextContentAssistInvocationContext, org.eclipse.core.runtime.IProgressMonitor)
110      */

111     public List JavaDoc computeCompletionProposals(ContentAssistInvocationContext context, IProgressMonitor monitor) {
112         if (context instanceof JavadocContentAssistInvocationContext) {
113             JavadocContentAssistInvocationContext javaContext= (JavadocContentAssistInvocationContext) context;
114             
115             ICompilationUnit cu= javaContext.getCompilationUnit();
116             int offset= javaContext.getInvocationOffset();
117             int length= javaContext.getSelectionLength();
118             Point selection= javaContext.getViewer().getSelectedRange();
119             if (selection.y > 0) {
120                 offset= selection.x;
121                 length= selection.y;
122             }
123             
124             ArrayList JavaDoc result= new ArrayList JavaDoc();
125             
126             IJavadocCompletionProcessor[] processors= getContributedProcessors();
127             for (int i= 0; i < processors.length; i++) {
128                 IJavadocCompletionProcessor curr= processors[i];
129                 IJavaCompletionProposal[] proposals= curr.computeCompletionProposals(cu, offset, length, javaContext.getFlags());
130                 if (proposals != null) {
131                     for (int k= 0; k < proposals.length; k++) {
132                         result.add(proposals[k]);
133                     }
134                 }
135             }
136             return result;
137         }
138         return Collections.EMPTY_LIST;
139     }
140
141
142     /*
143      * @see org.eclipse.jface.text.contentassist.ICompletionProposalComputer#getErrorMessage()
144      */

145     public String JavaDoc getErrorMessage() {
146         return fErrorMessage;
147     }
148
149
150     /*
151      * @see org.eclipse.jdt.ui.text.java.IJavaCompletionProposalComputer#sessionStarted()
152      */

153     public void sessionStarted() {
154     }
155
156
157     /*
158      * @see org.eclipse.jdt.ui.text.java.IJavaCompletionProposalComputer#sessionEnded()
159      */

160     public void sessionEnded() {
161         fErrorMessage= null;
162     }
163 }
164
Popular Tags