KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > java > CompletionProposalCategory


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  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.text.java;
12
13 import java.net.URL JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.runtime.FileLocator;
19 import org.eclipse.core.runtime.IConfigurationElement;
20 import org.eclipse.core.runtime.IExtension;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.InvalidRegistryObjectException;
23 import org.eclipse.core.runtime.Path;
24 import org.eclipse.core.runtime.Platform;
25 import org.eclipse.core.runtime.Status;
26 import org.eclipse.core.runtime.SubProgressMonitor;
27
28 import org.eclipse.jface.action.LegacyActionTools;
29 import org.eclipse.jface.resource.ImageDescriptor;
30
31 import org.eclipse.jdt.internal.corext.util.Messages;
32
33 import org.eclipse.jdt.ui.text.java.ContentAssistInvocationContext;
34 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposalComputer;
35
36 import org.eclipse.jdt.internal.ui.JavaPlugin;
37
38 import org.osgi.framework.Bundle;
39
40 /**
41  * Describes a category extension to the "javaCompletionProposalComputer" extension point.
42  *
43  * @since 3.2
44  */

45 public final class CompletionProposalCategory {
46     /** The extension schema name of the icon attribute. */
47     private static final String JavaDoc ICON= "icon"; //$NON-NLS-1$
48

49     private final String JavaDoc fId;
50     private final String JavaDoc fName;
51     private final IConfigurationElement fElement;
52     /** The image descriptor for this category, or <code>null</code> if none specified. */
53     private final ImageDescriptor fImage;
54     
55     private boolean fIsSeparateCommand= true;
56     private boolean fIsEnabled= true;
57     private boolean fIsIncluded= true;
58     private final CompletionProposalComputerRegistry fRegistry;
59     
60     private int fSortOrder= 0x10000;
61     private String JavaDoc fLastError= null;
62
63     CompletionProposalCategory(IConfigurationElement element, CompletionProposalComputerRegistry registry) {
64         fElement= element;
65         fRegistry= registry;
66         IExtension parent= (IExtension) element.getParent();
67         fId= parent.getUniqueIdentifier();
68         checkNotNull(fId, "id"); //$NON-NLS-1$
69
String JavaDoc name= parent.getLabel();
70         if (name == null)
71             fName= fId;
72         else
73             fName= name;
74         
75         String JavaDoc icon= element.getAttribute(ICON);
76         ImageDescriptor img= null;
77         if (icon != null) {
78             Bundle bundle= getBundle();
79             if (bundle != null) {
80                 Path path= new Path(icon);
81                 URL JavaDoc url= FileLocator.find(bundle, path, null);
82                 img= ImageDescriptor.createFromURL(url);
83             }
84         }
85         fImage= img;
86
87     }
88
89     CompletionProposalCategory(String JavaDoc id, String JavaDoc name, CompletionProposalComputerRegistry registry) {
90         fRegistry= registry;
91         fId= id;
92         fName= name;
93         fElement= null;
94         fImage= null;
95     }
96
97     private Bundle getBundle() {
98         String JavaDoc namespace= fElement.getDeclaringExtension().getContributor().getName();
99         Bundle bundle= Platform.getBundle(namespace);
100         return bundle;
101     }
102
103     /**
104      * Checks an element that must be defined according to the extension
105      * point schema. Throws an
106      * <code>InvalidRegistryObjectException</code> if <code>obj</code>
107      * is <code>null</code>.
108      */

109     private void checkNotNull(Object JavaDoc obj, String JavaDoc attribute) throws InvalidRegistryObjectException {
110         if (obj == null) {
111             Object JavaDoc[] args= { getId(), fElement.getContributor().getName(), attribute };
112             String JavaDoc message= Messages.format(JavaTextMessages.CompletionProposalComputerDescriptor_illegal_attribute_message, args);
113             IStatus status= new Status(IStatus.WARNING, JavaPlugin.getPluginId(), IStatus.OK, message, null);
114             JavaPlugin.log(status);
115             throw new InvalidRegistryObjectException();
116         }
117     }
118
119     /**
120      * Returns the identifier of the described extension.
121      *
122      * @return Returns the id
123      */

124     public String JavaDoc getId() {
125         return fId;
126     }
127
128     /**
129      * Returns the name of the described extension.
130      *
131      * @return Returns the name
132      */

133     public String JavaDoc getName() {
134         return fName;
135     }
136     
137     /**
138      * Returns the name of the described extension
139      * without mnemonic hint in order to be displayed
140      * in a message.
141      *
142      * @return Returns the name
143      */

144     public String JavaDoc getDisplayName() {
145         return LegacyActionTools.removeMnemonics(fName);
146     }
147     
148     /**
149      * Returns the image descriptor of the described category.
150      *
151      * @return the image descriptor of the described category
152      */

153     public ImageDescriptor getImageDescriptor() {
154         return fImage;
155     }
156     
157     /**
158      * Sets the separate command state of the category.
159      *
160      * @param enabled the new enabled state.
161      */

162     public void setSeparateCommand(boolean enabled) {
163         fIsSeparateCommand= enabled;
164     }
165     
166     /**
167      * Returns the enablement state of the category.
168      *
169      * @return the enablement state of the category
170      */

171     public boolean isSeparateCommand() {
172         return fIsSeparateCommand;
173     }
174     
175     /**
176      * @param included the included
177      */

178     public void setIncluded(boolean included) {
179         fIsIncluded= included;
180     }
181     
182     /**
183      * @return included
184      */

185     public boolean isIncluded() {
186         return fIsIncluded;
187     }
188
189     public boolean isEnabled() {
190         return fIsEnabled;
191     }
192
193     public void setEnabled(boolean isEnabled) {
194         fIsEnabled= isEnabled;
195     }
196
197     /**
198      * Returns <code>true</code> if the category contains any computers, <code>false</code>
199      * otherwise.
200      *
201      * @return <code>true</code> if the category contains any computers, <code>false</code>
202      * otherwise
203      */

204     public boolean hasComputers() {
205         List JavaDoc descriptors= fRegistry.getProposalComputerDescriptors();
206         for (Iterator JavaDoc it= descriptors.iterator(); it.hasNext();) {
207             CompletionProposalComputerDescriptor desc= (CompletionProposalComputerDescriptor) it.next();
208             if (desc.getCategory() == this)
209                 return true;
210         }
211         return false;
212     }
213     
214     /**
215      * Returns <code>true</code> if the category contains any computers in the given partition, <code>false</code>
216      * otherwise.
217      *
218      * @param partition the partition
219      * @return <code>true</code> if the category contains any computers, <code>false</code>
220      * otherwise
221      */

222     public boolean hasComputers(String JavaDoc partition) {
223         List JavaDoc descriptors= fRegistry.getProposalComputerDescriptors(partition);
224         for (Iterator JavaDoc it= descriptors.iterator(); it.hasNext();) {
225             CompletionProposalComputerDescriptor desc= (CompletionProposalComputerDescriptor) it.next();
226             if (desc.getCategory() == this)
227                 return true;
228         }
229         return false;
230     }
231     
232     /**
233      * @return sortOrder
234      */

235     public int getSortOrder() {
236         return fSortOrder;
237     }
238     
239     /**
240      * @param sortOrder the sortOrder
241      */

242     public void setSortOrder(int sortOrder) {
243         fSortOrder= sortOrder;
244     }
245
246     /**
247      * Safely computes completion proposals of all computers of this category through their
248      * extension. If an extension is disabled, throws an exception or otherwise does not adhere to
249      * the contract described in {@link IJavaCompletionProposalComputer}, it is disabled.
250      *
251      * @param context the invocation context passed on to the extension
252      * @param partition the partition type where to invocation occurred
253      * @param monitor the progress monitor passed on to the extension
254      * @return the list of computed completion proposals (element type:
255      * {@link org.eclipse.jface.text.contentassist.ICompletionProposal})
256      */

257     public List JavaDoc computeCompletionProposals(ContentAssistInvocationContext context, String JavaDoc partition, SubProgressMonitor monitor) {
258         fLastError= null;
259         List JavaDoc result= new ArrayList JavaDoc();
260         List JavaDoc descriptors= new ArrayList JavaDoc(fRegistry.getProposalComputerDescriptors(partition));
261         for (Iterator JavaDoc it= descriptors.iterator(); it.hasNext();) {
262             CompletionProposalComputerDescriptor desc= (CompletionProposalComputerDescriptor) it.next();
263             if (desc.getCategory() == this)
264                 result.addAll(desc.computeCompletionProposals(context, monitor));
265             if (fLastError == null)
266                 fLastError= desc.getErrorMessage();
267         }
268         return result;
269     }
270
271     /**
272      * Safely computes context information objects of all computers of this category through their
273      * extension. If an extension is disabled, throws an exception or otherwise does not adhere to
274      * the contract described in {@link IJavaCompletionProposalComputer}, it is disabled.
275      *
276      * @param context the invocation context passed on to the extension
277      * @param partition the partition type where to invocation occurred
278      * @param monitor the progress monitor passed on to the extension
279      * @return the list of computed context information objects (element type:
280      * {@link org.eclipse.jface.text.contentassist.IContextInformation})
281      */

282     public List JavaDoc computeContextInformation(ContentAssistInvocationContext context, String JavaDoc partition, SubProgressMonitor monitor) {
283         fLastError= null;
284         List JavaDoc result= new ArrayList JavaDoc();
285         List JavaDoc descriptors= new ArrayList JavaDoc(fRegistry.getProposalComputerDescriptors(partition));
286         for (Iterator JavaDoc it= descriptors.iterator(); it.hasNext();) {
287             CompletionProposalComputerDescriptor desc= (CompletionProposalComputerDescriptor) it.next();
288             if (desc.getCategory() == this)
289                 result.addAll(desc.computeContextInformation(context, monitor));
290             if (fLastError == null)
291                 fLastError= desc.getErrorMessage();
292         }
293         return result;
294     }
295
296     /**
297      * Returns the error message from the computers in this category.
298      *
299      * @return the error message from the computers in this category
300      */

301     public String JavaDoc getErrorMessage() {
302         return fLastError;
303     }
304
305     /**
306      * Notifies the computers in this category of a proposal computation session start.
307      */

308     public void sessionStarted() {
309         List JavaDoc descriptors= new ArrayList JavaDoc(fRegistry.getProposalComputerDescriptors());
310         for (Iterator JavaDoc it= descriptors.iterator(); it.hasNext();) {
311             CompletionProposalComputerDescriptor desc= (CompletionProposalComputerDescriptor) it.next();
312             if (desc.getCategory() == this)
313                 desc.sessionStarted();
314             if (fLastError == null)
315                 fLastError= desc.getErrorMessage();
316         }
317     }
318     
319     /**
320      * Notifies the computers in this category of a proposal computation session end.
321      */

322     public void sessionEnded() {
323         List JavaDoc descriptors= new ArrayList JavaDoc(fRegistry.getProposalComputerDescriptors());
324         for (Iterator JavaDoc it= descriptors.iterator(); it.hasNext();) {
325             CompletionProposalComputerDescriptor desc= (CompletionProposalComputerDescriptor) it.next();
326             if (desc.getCategory() == this)
327                 desc.sessionEnded();
328             if (fLastError == null)
329                 fLastError= desc.getErrorMessage();
330         }
331     }
332     
333 }
334
Popular Tags