1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.util;
13 import org.eclipse.swt.graphics.Image;
15 import org.eclipse.jface.resource.ImageDescriptor;
16 import org.eclipse.jface.viewers.LabelProvider;
18 import org.eclipse.jdt.core.Flags;
19 import org.eclipse.jdt.core.IPackageFragmentRoot;
20 import org.eclipse.jdt.core.search.TypeNameMatch;
22 import org.eclipse.jdt.ui.JavaElementImageDescriptor;
23 import org.eclipse.jdt.ui.JavaElementLabels;
25 import org.eclipse.jdt.internal.ui.JavaPlugin;
26 import org.eclipse.jdt.internal.ui.JavaPluginImages;
27 import org.eclipse.jdt.internal.ui.JavaUIMessages;
28 import org.eclipse.jdt.internal.ui.viewsupport.BasicElementLabels;
29 import org.eclipse.jdt.internal.ui.viewsupport.JavaElementImageProvider;
31 public class TypeNameMatchLabelProvider extends LabelProvider {
33 public static final int SHOW_FULLYQUALIFIED= 0x01;
34 public static final int SHOW_PACKAGE_POSTFIX= 0x02;
35 public static final int SHOW_PACKAGE_ONLY= 0x04;
36 public static final int SHOW_ROOT_POSTFIX= 0x08;
37 public static final int SHOW_TYPE_ONLY= 0x10;
38 public static final int SHOW_TYPE_CONTAINER_ONLY= 0x20;
39 public static final int SHOW_POST_QUALIFIED= 0x40;
43 public TypeNameMatchLabelProvider(int flags) {
48 * @see ILabelProvider#getText
51 public String getText(Object element) {
52 if (! (element instanceof TypeNameMatch))
53 return super.getText(element);
55 return getText((TypeNameMatch) element, fFlags);
59 * @see ILabelProvider#getImage
62 public Image getImage(Object element) {
63 if (! (element instanceof TypeNameMatch))
64 return super.getImage(element);
65 return getImage((TypeNameMatch) element, fFlags);
68 private static boolean isSet(int flag, int flags) {
69 return (flags & flag) != 0;
72 private static String getPackageName(String packName) {
73 if (packName.length() == 0)
74 return JavaUIMessages.TypeInfoLabelProvider_default_package;
79 public static String getText(TypeNameMatch typeRef, int flags) {
80 StringBuffer buf= new StringBuffer();
81 if (isSet(SHOW_TYPE_ONLY, flags)) {
82 buf.append(typeRef.getSimpleTypeName());
83 } else if (isSet(SHOW_TYPE_CONTAINER_ONLY, flags)) {
84 String containerName= typeRef.getTypeContainerName();
85 buf.append(getPackageName(containerName));
86 } else if (isSet(SHOW_PACKAGE_ONLY, flags)) {
87 String packName= typeRef.getPackageName();
88 buf.append(getPackageName(packName));
90 if (isSet(SHOW_FULLYQUALIFIED, flags)) {
91 buf.append(typeRef.getFullyQualifiedName());
92 } else if (isSet(SHOW_POST_QUALIFIED, flags)) {
93 buf.append(typeRef.getSimpleTypeName());
94 String containerName= typeRef.getTypeContainerName();
95 if (containerName != null && containerName.length() > 0) {
96 buf.append(JavaElementLabels.CONCAT_STRING);
97 buf.append(containerName);
100 buf.append(typeRef.getTypeQualifiedName());
103 if (isSet(SHOW_PACKAGE_POSTFIX, flags)) {
104 buf.append(JavaElementLabels.CONCAT_STRING);
105 String packName= typeRef.getPackageName();
106 buf.append(getPackageName(packName));
109 if (isSet(SHOW_ROOT_POSTFIX, flags)) {
110 buf.append(JavaElementLabels.CONCAT_STRING);
111 IPackageFragmentRoot root= typeRef.getPackageFragmentRoot();
112 JavaElementLabels.getPackageFragmentRootLabel(root, JavaElementLabels.ROOT_QUALIFIED, buf);
114 return BasicElementLabels.getJavaElementName(buf.toString());
118 public static ImageDescriptor getImageDescriptor(TypeNameMatch typeRef, int flags) {
119 if (isSet(SHOW_TYPE_CONTAINER_ONLY, flags)) {
120 if (typeRef.getPackageName().equals(typeRef.getTypeContainerName()))
121 return JavaPluginImages.DESC_OBJS_PACKAGE;
123 // XXX cannot check outer type for interface efficiently (5887)
124 return JavaPluginImages.DESC_OBJS_CLASS;
126 } else if (isSet(SHOW_PACKAGE_ONLY, flags)) {
127 return JavaPluginImages.DESC_OBJS_PACKAGE;
129 boolean isInner= typeRef.getTypeContainerName().indexOf('.') != -1;
130 int modifiers= typeRef.getModifiers();
132 ImageDescriptor desc= JavaElementImageProvider.getTypeImageDescriptor(isInner, false, modifiers, false);
133 int adornmentFlags= 0;
134 if (Flags.isFinal(modifiers)) {
135 adornmentFlags |= JavaElementImageDescriptor.FINAL;
137 if (Flags.isAbstract(modifiers) && !Flags.isInterface(modifiers)) {
138 adornmentFlags |= JavaElementImageDescriptor.ABSTRACT;
140 if (Flags.isStatic(modifiers)) {
141 adornmentFlags |= JavaElementImageDescriptor.STATIC;
143 if (Flags.isDeprecated(modifiers)) {
144 adornmentFlags |= JavaElementImageDescriptor.DEPRECATED;
147 return new JavaElementImageDescriptor(desc, adornmentFlags, JavaElementImageProvider.BIG_SIZE);
151 public static Image getImage(TypeNameMatch typeRef, int flags) {
152 return JavaPlugin.getImageDescriptorRegistry().get(getImageDescriptor(typeRef, flags));