KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > ui > text > java > CompletionProposalLabelProvider


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 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.ui.text.java;
12
13 import org.eclipse.core.runtime.Assert;
14
15 import org.eclipse.jface.resource.ImageDescriptor;
16
17 import org.eclipse.jdt.core.CompletionContext;
18 import org.eclipse.jdt.core.CompletionProposal;
19 import org.eclipse.jdt.core.Flags;
20 import org.eclipse.jdt.core.Signature;
21
22 import org.eclipse.jdt.internal.corext.template.java.SignatureUtil;
23 import org.eclipse.jdt.internal.corext.util.Messages;
24
25 import org.eclipse.jdt.ui.JavaElementImageDescriptor;
26 import org.eclipse.jdt.ui.JavaElementLabels;
27
28 import org.eclipse.jdt.internal.ui.JavaPluginImages;
29 import org.eclipse.jdt.internal.ui.viewsupport.JavaElementImageProvider;
30
31
32 /**
33  * Provides labels for java content assist proposals. The functionality is
34  * similar to the one provided by {@link org.eclipse.jdt.ui.JavaElementLabels},
35  * but based on signatures and {@link CompletionProposal}s.
36  *
37  * @see Signature
38  * @since 3.1
39  */

40 public class CompletionProposalLabelProvider {
41
42     /**
43      * The completion context.
44      *
45      * @since 3.2
46      */

47     private CompletionContext fContext;
48
49     /**
50      * Creates a new label provider.
51      */

52     public CompletionProposalLabelProvider() {
53     }
54
55     /**
56      * Creates and returns a parameter list of the given method or type proposal
57      * suitable for display. The list does not include parentheses. The lower
58      * bound of parameter types is returned.
59      * <p>
60      * Examples:
61      * <pre>
62      * &quot;void method(int i, Strings)&quot; -&gt; &quot;int i, String s&quot;
63      * &quot;? extends Number method(java.lang.String s, ? super Number n)&quot; -&gt; &quot;String s, Number n&quot;
64      * </pre>
65      * </p>
66      *
67      * @param proposal the proposal to create the parameter list
68      * for. Must be of kind {@link CompletionProposal#METHOD_REF} or
69      * {@link CompletionProposal#TYPE_REF}.
70      * @return the list of comma-separated parameters suitable for display
71      */

72     public String JavaDoc createParameterList(CompletionProposal proposal) {
73         int kind= proposal.getKind();
74         switch (kind) {
75             case CompletionProposal.METHOD_REF:
76                 return appendUnboundedParameterList(new StringBuffer JavaDoc(), proposal).toString();
77             case CompletionProposal.TYPE_REF:
78                 return appendTypeParameterList(new StringBuffer JavaDoc(), proposal).toString();
79             default:
80                 Assert.isLegal(false);
81                 return null; // dummy
82
}
83     }
84
85     /**
86      * Appends the parameter list to <code>buffer</code>.
87      *
88      * @param buffer the buffer to append to
89      * @param methodProposal the method proposal
90      * @return the modified <code>buffer</code>
91      */

92     private StringBuffer JavaDoc appendUnboundedParameterList(StringBuffer JavaDoc buffer, CompletionProposal methodProposal) {
93         // TODO remove once https://bugs.eclipse.org/bugs/show_bug.cgi?id=85293
94
// gets fixed.
95
char[] signature= SignatureUtil.fix83600(methodProposal.getSignature());
96         char[][] parameterNames= methodProposal.findParameterNames(null);
97         char[][] parameterTypes= Signature.getParameterTypes(signature);
98
99         for (int i= 0; i < parameterTypes.length; i++)
100             parameterTypes[i]= createTypeDisplayName(SignatureUtil.getLowerBound(parameterTypes[i]));
101
102         if (Flags.isVarargs(methodProposal.getFlags())) {
103             int index= parameterTypes.length - 1;
104             parameterTypes[index]= convertToVararg(parameterTypes[index]);
105         }
106         return appendParameterSignature(buffer, parameterTypes, parameterNames);
107     }
108
109     /**
110      * Appends the type parameter list to <code>buffer</code>.
111      *
112      * @param buffer the buffer to append to
113      * @param typeProposal the type proposal
114      * @return the modified <code>buffer</code>
115      * @since 3.2
116      */

117     private StringBuffer JavaDoc appendTypeParameterList(StringBuffer JavaDoc buffer, CompletionProposal typeProposal) {
118         // TODO remove once https://bugs.eclipse.org/bugs/show_bug.cgi?id=85293
119
// gets fixed.
120
char[] signature= SignatureUtil.fix83600(typeProposal.getSignature());
121         char[][] typeParameters= Signature.getTypeArguments(signature);
122         for (int i= 0; i < typeParameters.length; i++) {
123             char[] param= typeParameters[i];
124             typeParameters[i]= Signature.toCharArray(param);
125         }
126         return appendParameterSignature(buffer, typeParameters, null);
127     }
128     
129     /**
130      * Converts the display name for an array type into a variable arity display name.
131      * <p>
132      * Examples:
133      * <ul>
134      * <li> "int[]" -> "int..."</li>
135      * <li> "Object[][]" -> "Object[]..."</li>
136      * <li> "String" -> "String"</li>
137      * </ul>
138      * </p>
139      * <p>
140      * If <code>typeName</code> does not include the substring "[]", it is returned unchanged.
141      * </p>
142      *
143      * @param typeName the type name to convert
144      * @return the converted type name
145      * @since 3.2
146      */

147     private char[] convertToVararg(char[] typeName) {
148         if (typeName == null)
149             return typeName;
150         final int len= typeName.length;
151         if (len < 2)
152             return typeName;
153         
154         if (typeName[len - 1] != ']')
155             return typeName;
156         if (typeName[len - 2] != '[')
157             return typeName;
158         
159         char[] vararg= new char[len + 1];
160         System.arraycopy(typeName, 0, vararg, 0, len - 2);
161         vararg[len - 2]= '.';
162         vararg[len - 1]= '.';
163         vararg[len]= '.';
164         return vararg;
165     }
166
167     /**
168      * Returns the display string for a java type signature.
169      *
170      * @param typeSignature the type signature to create a display name for
171      * @return the display name for <code>typeSignature</code>
172      * @throws IllegalArgumentException if <code>typeSignature</code> is not a
173      * valid signature
174      * @see Signature#toCharArray(char[])
175      * @see Signature#getSimpleName(char[])
176      */

177     private char[] createTypeDisplayName(char[] typeSignature) throws IllegalArgumentException JavaDoc {
178         char[] displayName= Signature.getSimpleName(Signature.toCharArray(typeSignature));
179
180         // XXX see https://bugs.eclipse.org/bugs/show_bug.cgi?id=84675
181
boolean useShortGenerics= false;
182         if (useShortGenerics) {
183             StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
184             buf.append(displayName);
185             int pos;
186             do {
187                 pos= buf.indexOf("? extends "); //$NON-NLS-1$
188
if (pos >= 0) {
189                     buf.replace(pos, pos + 10, "+"); //$NON-NLS-1$
190
} else {
191                     pos= buf.indexOf("? super "); //$NON-NLS-1$
192
if (pos >= 0)
193                         buf.replace(pos, pos + 8, "-"); //$NON-NLS-1$
194
}
195             } while (pos >= 0);
196             return buf.toString().toCharArray();
197         }
198         return displayName;
199     }
200
201     /**
202      * Creates a display string of a parameter list (without the parentheses)
203      * for the given parameter types and names.
204      *
205      * @param buffer the string buffer
206      * @param parameterTypes the parameter types
207      * @param parameterNames the parameter names
208      * @return the display string of the parameter list defined by the passed arguments
209      */

210     private final StringBuffer JavaDoc appendParameterSignature(StringBuffer JavaDoc buffer, char[][] parameterTypes, char[][] parameterNames) {
211         if (parameterTypes != null) {
212             for (int i = 0; i < parameterTypes.length; i++) {
213                 if (i > 0) {
214                     buffer.append(',');
215                     buffer.append(' ');
216                 }
217                 buffer.append(parameterTypes[i]);
218                 if (parameterNames != null && parameterNames[i] != null) {
219                     buffer.append(' ');
220                     buffer.append(parameterNames[i]);
221                 }
222             }
223         }
224         return buffer;
225     }
226
227     /**
228      * Creates a display label for the given method proposal. The display label
229      * consists of:
230      * <ul>
231      * <li>the method name</li>
232      * <li>the parameter list (see {@link #createParameterList(CompletionProposal)})</li>
233      * <li>the upper bound of the return type (see {@link SignatureUtil#getUpperBound(String)})</li>
234      * <li>the raw simple name of the declaring type</li>
235      * </ul>
236      * <p>
237      * Examples:
238      * For the <code>get(int)</code> method of a variable of type <code>List<? extends Number></code>, the following
239      * display name is returned: <code>get(int index) Number - List</code>.<br>
240      * For the <code>add(E)</code> method of a variable of type <code>List<? super Number></code>, the following
241      * display name is returned: <code>add(Number o) void - List</code>.<br>
242      * </p>
243      *
244      * @param methodProposal the method proposal to display
245      * @return the display label for the given method proposal
246      */

247     String JavaDoc createMethodProposalLabel(CompletionProposal methodProposal) {
248         StringBuffer JavaDoc nameBuffer= new StringBuffer JavaDoc();
249
250         // method name
251
nameBuffer.append(methodProposal.getName());
252
253         // parameters
254
nameBuffer.append('(');
255         appendUnboundedParameterList(nameBuffer, methodProposal);
256         nameBuffer.append(')');
257
258         // return type
259
if (!methodProposal.isConstructor()) {
260             // TODO remove SignatureUtil.fix83600 call when bugs are fixed
261
char[] returnType= createTypeDisplayName(SignatureUtil.getUpperBound(Signature.getReturnType(SignatureUtil.fix83600(methodProposal.getSignature()))));
262             nameBuffer.append(" "); //$NON-NLS-1$
263
nameBuffer.append(returnType);
264         }
265
266         // declaring type
267
nameBuffer.append(" - "); //$NON-NLS-1$
268
String JavaDoc declaringType= extractDeclaringTypeFQN(methodProposal);
269         declaringType= Signature.getSimpleName(declaringType);
270         nameBuffer.append(declaringType);
271
272         return nameBuffer.toString();
273     }
274     
275     /**
276      * Creates a display label for the given method proposal. The display label consists of:
277      * <ul>
278      * <li>the method name</li>
279      * <li>the raw simple name of the declaring type</li>
280      * </ul>
281      * <p>
282      * Examples: For the <code>get(int)</code> method of a variable of type
283      * <code>List<? extends Number></code>, the following display name is returned <code>get(int) - List</code>.<br>
284      * For the <code>add(E)</code> method of a variable of type <code>List</code>, the
285      * following display name is returned:
286      * <code>add(Object) - List</code>.<br>
287      * </p>
288      *
289      * @param methodProposal the method proposal to display
290      * @return the display label for the given method proposal
291      * @since 3.2
292      */

293     String JavaDoc createJavadocMethodProposalLabel(CompletionProposal methodProposal) {
294         StringBuffer JavaDoc nameBuffer= new StringBuffer JavaDoc();
295         
296         // method name
297
nameBuffer.append(methodProposal.getCompletion());
298         
299         // declaring type
300
nameBuffer.append(" - "); //$NON-NLS-1$
301
String JavaDoc declaringType= extractDeclaringTypeFQN(methodProposal);
302         declaringType= Signature.getSimpleName(declaringType);
303         nameBuffer.append(declaringType);
304         
305         return nameBuffer.toString();
306     }
307     
308     String JavaDoc createOverrideMethodProposalLabel(CompletionProposal methodProposal) {
309         StringBuffer JavaDoc nameBuffer= new StringBuffer JavaDoc();
310
311         // method name
312
nameBuffer.append(methodProposal.getName());
313
314         // parameters
315
nameBuffer.append('(');
316         appendUnboundedParameterList(nameBuffer, methodProposal);
317         nameBuffer.append(") "); //$NON-NLS-1$
318

319         // return type
320
// TODO remove SignatureUtil.fix83600 call when bugs are fixed
321
char[] returnType= createTypeDisplayName(SignatureUtil.getUpperBound(Signature.getReturnType(SignatureUtil.fix83600(methodProposal.getSignature()))));
322         nameBuffer.append(returnType);
323
324         // declaring type
325
nameBuffer.append(" - "); //$NON-NLS-1$
326

327         String JavaDoc declaringType= extractDeclaringTypeFQN(methodProposal);
328         declaringType= Signature.getSimpleName(declaringType);
329         nameBuffer.append(Messages.format(JavaTextMessages.ResultCollector_overridingmethod, new String JavaDoc(declaringType)));
330
331         return nameBuffer.toString();
332     }
333
334     /**
335      * Extracts the fully qualified name of the declaring type of a method
336      * reference.
337      *
338      * @param methodProposal a proposed method
339      * @return the qualified name of the declaring type
340      */

341     private String JavaDoc extractDeclaringTypeFQN(CompletionProposal methodProposal) {
342         char[] declaringTypeSignature= methodProposal.getDeclarationSignature();
343         // special methods may not have a declaring type: methods defined on arrays etc.
344
// TODO remove when bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=84690 gets fixed
345
if (declaringTypeSignature == null)
346             return "java.lang.Object"; //$NON-NLS-1$
347
return SignatureUtil.stripSignatureToFQN(String.valueOf(declaringTypeSignature));
348     }
349
350     /**
351      * Creates a display label for a given type proposal. The display label
352      * consists of:
353      * <ul>
354      * <li>the simple type name (erased when the context is in javadoc)</li>
355      * <li>the package name</li>
356      * </ul>
357      * <p>
358      * Examples:
359      * A proposal for the generic type <code>java.util.List&lt;E&gt;</code>, the display label
360      * is: <code>List<E> - java.util</code>.
361      * </p>
362      *
363      * @param typeProposal the method proposal to display
364      * @return the display label for the given type proposal
365      */

366     String JavaDoc createTypeProposalLabel(CompletionProposal typeProposal) {
367         char[] signature;
368         if (fContext != null && fContext.isInJavadoc())
369             signature= Signature.getTypeErasure(typeProposal.getSignature());
370         else
371             signature= typeProposal.getSignature();
372         char[] fullName= Signature.toCharArray(signature);
373         return createTypeProposalLabel(fullName);
374     }
375     
376     String JavaDoc createJavadocTypeProposalLabel(CompletionProposal typeProposal) {
377         char[] fullName= Signature.toCharArray(typeProposal.getSignature());
378         return createJavadocTypeProposalLabel(fullName);
379     }
380     
381     String JavaDoc createJavadocSimpleProposalLabel(CompletionProposal proposal) {
382         // TODO get rid of this
383
return createSimpleLabel(proposal);
384     }
385     
386     String JavaDoc createTypeProposalLabel(char[] fullName) {
387         // only display innermost type name as type name, using any
388
// enclosing types as qualification
389
int qIndex= findSimpleNameStart(fullName);
390
391         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
392         buf.append(fullName, qIndex, fullName.length - qIndex);
393         if (qIndex > 0) {
394             buf.append(JavaElementLabels.CONCAT_STRING);
395             buf.append(fullName, 0, qIndex - 1);
396         }
397         return buf.toString();
398     }
399     
400     String JavaDoc createJavadocTypeProposalLabel(char[] fullName) {
401         // only display innermost type name as type name, using any
402
// enclosing types as qualification
403
int qIndex= findSimpleNameStart(fullName);
404         
405         StringBuffer JavaDoc buf= new StringBuffer JavaDoc("{@link "); //$NON-NLS-1$
406
buf.append(fullName, qIndex, fullName.length - qIndex);
407         buf.append('}');
408         if (qIndex > 0) {
409             buf.append(JavaElementLabels.CONCAT_STRING);
410             buf.append(fullName, 0, qIndex - 1);
411         }
412         return buf.toString();
413     }
414     
415     private int findSimpleNameStart(char[] array) {
416         int lastDot= 0;
417         for (int i= 0, len= array.length; i < len; i++) {
418             char ch= array[i];
419             if (ch == '<') {
420                 return lastDot;
421             } else if (ch == '.') {
422                 lastDot= i + 1;
423             }
424         }
425         return lastDot;
426     }
427
428     String JavaDoc createSimpleLabelWithType(CompletionProposal proposal) {
429         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
430         buf.append(proposal.getCompletion());
431         char[] typeName= Signature.getSignatureSimpleName(proposal.getSignature());
432         if (typeName.length > 0) {
433             buf.append(" "); //$NON-NLS-1$
434
buf.append(typeName);
435         }
436         return buf.toString();
437     }
438
439     /**
440      * Returns whether the given string starts with "this.".
441      *
442      * @param string
443      * @return <code>true</code> if the given string starts with "this."
444      * @since 3.3
445      */

446     private boolean isThisPrefix(char[] string) {
447         if (string == null || string.length < 5)
448             return false;
449         return string[0] == 't' && string[1] == 'h' && string[2] == 'i' && string[3] == 's' && string[4] == '.';
450     }
451
452     String JavaDoc createLabelWithTypeAndDeclaration(CompletionProposal proposal) {
453         char[] name= proposal.getCompletion();
454         if (!isThisPrefix(name))
455             name= proposal.getName();
456         
457         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
458         buf.append(name);
459         char[] typeName= Signature.getSignatureSimpleName(proposal.getSignature());
460         if (typeName.length > 0) {
461             buf.append(" "); //$NON-NLS-1$
462
buf.append(typeName);
463         }
464         char[] declaration= proposal.getDeclarationSignature();
465         if (declaration != null) {
466             declaration= Signature.getSignatureSimpleName(declaration);
467             if (declaration.length > 0) {
468                 buf.append(" - "); //$NON-NLS-1$
469
buf.append(declaration);
470             }
471         }
472
473         return buf.toString();
474     }
475
476     String JavaDoc createPackageProposalLabel(CompletionProposal proposal) {
477         Assert.isTrue(proposal.getKind() == CompletionProposal.PACKAGE_REF);
478         return String.valueOf(proposal.getDeclarationSignature());
479     }
480
481     String JavaDoc createSimpleLabel(CompletionProposal proposal) {
482         return String.valueOf(proposal.getCompletion());
483     }
484
485     String JavaDoc createAnonymousTypeLabel(CompletionProposal proposal) {
486         char[] declaringTypeSignature= proposal.getDeclarationSignature();
487
488         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
489         buffer.append(Signature.getSignatureSimpleName(declaringTypeSignature));
490         buffer.append('(');
491         appendUnboundedParameterList(buffer, proposal);
492         buffer.append(')');
493         buffer.append(" "); //$NON-NLS-1$
494
buffer.append(JavaTextMessages.ResultCollector_anonymous_type);
495
496         return buffer.toString();
497     }
498
499     /**
500      * Creates the display label for a given <code>CompletionProposal</code>.
501      *
502      * @param proposal the completion proposal to create the display label for
503      * @return the display label for <code>proposal</code>
504      */

505     public String JavaDoc createLabel(CompletionProposal proposal) {
506         switch (proposal.getKind()) {
507             case CompletionProposal.METHOD_NAME_REFERENCE:
508             case CompletionProposal.METHOD_REF:
509             case CompletionProposal.POTENTIAL_METHOD_DECLARATION:
510                 if (fContext != null && fContext.isInJavadoc())
511                     return createJavadocMethodProposalLabel(proposal);
512                 return createMethodProposalLabel(proposal);
513             case CompletionProposal.METHOD_DECLARATION:
514                 return createOverrideMethodProposalLabel(proposal);
515             case CompletionProposal.ANONYMOUS_CLASS_DECLARATION:
516                 return createAnonymousTypeLabel(proposal);
517             case CompletionProposal.TYPE_REF:
518                 return createTypeProposalLabel(proposal);
519             case CompletionProposal.JAVADOC_TYPE_REF:
520                 return createJavadocTypeProposalLabel(proposal);
521             case CompletionProposal.JAVADOC_FIELD_REF:
522             case CompletionProposal.JAVADOC_VALUE_REF:
523             case CompletionProposal.JAVADOC_BLOCK_TAG:
524             case CompletionProposal.JAVADOC_INLINE_TAG:
525             case CompletionProposal.JAVADOC_PARAM_REF:
526                 return createJavadocSimpleProposalLabel(proposal);
527             case CompletionProposal.JAVADOC_METHOD_REF:
528                 return createJavadocMethodProposalLabel(proposal);
529             case CompletionProposal.PACKAGE_REF:
530                 return createPackageProposalLabel(proposal);
531             case CompletionProposal.ANNOTATION_ATTRIBUTE_REF:
532             case CompletionProposal.FIELD_REF:
533                 return createLabelWithTypeAndDeclaration(proposal);
534             case CompletionProposal.LOCAL_VARIABLE_REF:
535             case CompletionProposal.VARIABLE_DECLARATION:
536                 return createSimpleLabelWithType(proposal);
537             case CompletionProposal.KEYWORD:
538             case CompletionProposal.LABEL_REF:
539                 return createSimpleLabel(proposal);
540             default:
541                 Assert.isTrue(false);
542                 return null;
543         }
544     }
545
546     /**
547      * Creates and returns a decorated image descriptor for a completion proposal.
548      *
549      * @param proposal the proposal for which to create an image descriptor
550      * @return the created image descriptor, or <code>null</code> if no image is available
551      */

552     public ImageDescriptor createImageDescriptor(CompletionProposal proposal) {
553         final int flags= proposal.getFlags();
554
555         ImageDescriptor descriptor;
556         switch (proposal.getKind()) {
557             case CompletionProposal.METHOD_DECLARATION:
558             case CompletionProposal.METHOD_NAME_REFERENCE:
559             case CompletionProposal.METHOD_REF:
560             case CompletionProposal.ANNOTATION_ATTRIBUTE_REF:
561             case CompletionProposal.POTENTIAL_METHOD_DECLARATION:
562                 descriptor= JavaElementImageProvider.getMethodImageDescriptor(false, flags);
563                 break;
564             case CompletionProposal.ANONYMOUS_CLASS_DECLARATION:
565             case CompletionProposal.TYPE_REF:
566                 switch (Signature.getTypeSignatureKind(proposal.getSignature())) {
567                     case Signature.CLASS_TYPE_SIGNATURE:
568                         descriptor= JavaElementImageProvider.getTypeImageDescriptor(false, false, flags, false);
569                         break;
570                     case Signature.TYPE_VARIABLE_SIGNATURE:
571                         descriptor= JavaPluginImages.DESC_OBJS_TYPEVARIABLE;
572                         break;
573                     default:
574                         descriptor= null;
575                 }
576                 break;
577             case CompletionProposal.FIELD_REF:
578                 descriptor= JavaElementImageProvider.getFieldImageDescriptor(false, flags);
579                 break;
580             case CompletionProposal.LOCAL_VARIABLE_REF:
581             case CompletionProposal.VARIABLE_DECLARATION:
582                 descriptor= JavaPluginImages.DESC_OBJS_LOCAL_VARIABLE;
583                 break;
584             case CompletionProposal.PACKAGE_REF:
585                 descriptor= JavaPluginImages.DESC_OBJS_PACKAGE;
586                 break;
587             case CompletionProposal.KEYWORD:
588             case CompletionProposal.LABEL_REF:
589                 descriptor= null;
590                 break;
591             case CompletionProposal.JAVADOC_METHOD_REF:
592             case CompletionProposal.JAVADOC_TYPE_REF:
593             case CompletionProposal.JAVADOC_FIELD_REF:
594             case CompletionProposal.JAVADOC_VALUE_REF:
595             case CompletionProposal.JAVADOC_BLOCK_TAG:
596             case CompletionProposal.JAVADOC_INLINE_TAG:
597             case CompletionProposal.JAVADOC_PARAM_REF:
598                 descriptor = JavaPluginImages.DESC_OBJS_JAVADOCTAG;
599                 break;
600             default:
601                 descriptor= null;
602                 Assert.isTrue(false);
603         }
604
605         if (descriptor == null)
606             return null;
607         return decorateImageDescriptor(descriptor, proposal);
608     }
609
610     ImageDescriptor createMethodImageDescriptor(CompletionProposal proposal) {
611         final int flags= proposal.getFlags();
612         return decorateImageDescriptor(JavaElementImageProvider.getMethodImageDescriptor(false, flags), proposal);
613     }
614
615     ImageDescriptor createTypeImageDescriptor(CompletionProposal proposal) {
616         final int flags= proposal.getFlags();
617         boolean isInterfaceOrAnnotation= Flags.isInterface(flags) || Flags.isAnnotation(flags);
618         return decorateImageDescriptor(JavaElementImageProvider.getTypeImageDescriptor(true /* in order to get all visibility decorations */, isInterfaceOrAnnotation, flags, false), proposal);
619     }
620
621     ImageDescriptor createFieldImageDescriptor(CompletionProposal proposal) {
622         final int flags= proposal.getFlags();
623         return decorateImageDescriptor(JavaElementImageProvider.getFieldImageDescriptor(false, flags), proposal);
624     }
625
626     ImageDescriptor createLocalImageDescriptor(CompletionProposal proposal) {
627         return decorateImageDescriptor(JavaPluginImages.DESC_OBJS_LOCAL_VARIABLE, proposal);
628     }
629
630     ImageDescriptor createPackageImageDescriptor(CompletionProposal proposal) {
631         return decorateImageDescriptor(JavaPluginImages.DESC_OBJS_PACKAGE, proposal);
632     }
633
634     /**
635      * Returns a version of <code>descriptor</code> decorated according to
636      * the passed <code>modifier</code> flags.
637      *
638      * @param descriptor the image descriptor to decorate
639      * @param proposal the proposal
640      * @return an image descriptor for a method proposal
641      * @see Flags
642      */

643     private ImageDescriptor decorateImageDescriptor(ImageDescriptor descriptor, CompletionProposal proposal) {
644         int adornments= 0;
645         int flags= proposal.getFlags();
646         int kind= proposal.getKind();
647
648         if (Flags.isDeprecated(flags))
649             adornments |= JavaElementImageDescriptor.DEPRECATED;
650
651         if (kind == CompletionProposal.FIELD_REF || kind == CompletionProposal.METHOD_DECLARATION || kind == CompletionProposal.METHOD_DECLARATION || kind == CompletionProposal.METHOD_NAME_REFERENCE || kind == CompletionProposal.METHOD_REF)
652             if (Flags.isStatic(flags))
653                 adornments |= JavaElementImageDescriptor.STATIC;
654
655         if (kind == CompletionProposal.METHOD_DECLARATION || kind == CompletionProposal.METHOD_DECLARATION || kind == CompletionProposal.METHOD_NAME_REFERENCE || kind == CompletionProposal.METHOD_REF)
656             if (Flags.isSynchronized(flags))
657                 adornments |= JavaElementImageDescriptor.SYNCHRONIZED;
658
659         if (kind == CompletionProposal.TYPE_REF && Flags.isAbstract(flags) && !Flags.isInterface(flags))
660             adornments |= JavaElementImageDescriptor.ABSTRACT;
661         
662         if (kind == CompletionProposal.FIELD_REF) {
663             if (Flags.isTransient(flags))
664                 adornments |= JavaElementImageDescriptor.TRANSIENT;
665             if (Flags.isVolatile(flags))
666                 adornments |= JavaElementImageDescriptor.VOLATILE;
667         }
668
669         return new JavaElementImageDescriptor(descriptor, adornments, JavaElementImageProvider.SMALL_SIZE);
670     }
671
672     /**
673      * Sets the completion context.
674      *
675      * @param context the completion context
676      * @since 3.2
677      */

678     void setContext(CompletionContext context) {
679         fContext= context;
680     }
681
682 }
683
Popular Tags