]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/ui/org/eclipse/jdt/internal/ui/util/TypeNameMatchLabelProvider.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / internal / ui / util / TypeNameMatchLabelProvider.java
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.util;
12
13 import org.eclipse.swt.graphics.Image;
14
15 import org.eclipse.jface.resource.ImageDescriptor;
16 import org.eclipse.jface.viewers.LabelProvider;
17
18 import org.eclipse.jdt.core.Flags;
19 import org.eclipse.jdt.core.IPackageFragmentRoot;
20 import org.eclipse.jdt.core.search.TypeNameMatch;
21
22 import org.eclipse.jdt.ui.JavaElementImageDescriptor;
23 import org.eclipse.jdt.ui.JavaElementLabels;
24
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;
30
31 public class TypeNameMatchLabelProvider extends LabelProvider {
32
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;
40
41         private int fFlags;
42
43         public TypeNameMatchLabelProvider(int flags) {
44                 fFlags= flags;
45         }
46
47         /* non java-doc
48          * @see ILabelProvider#getText
49          */
50         @Override
51         public String getText(Object element) {
52                 if (! (element instanceof TypeNameMatch))
53                         return super.getText(element);
54
55                 return getText((TypeNameMatch) element, fFlags);
56         }
57
58         /* non java-doc
59          * @see ILabelProvider#getImage
60          */
61         @Override
62         public Image getImage(Object element) {
63                 if (! (element instanceof TypeNameMatch))
64                         return super.getImage(element);
65                 return getImage((TypeNameMatch) element, fFlags);
66         }
67
68         private static boolean isSet(int flag, int flags) {
69                 return (flags & flag) != 0;
70         }
71
72         private static String getPackageName(String packName) {
73                 if (packName.length() == 0)
74                         return JavaUIMessages.TypeInfoLabelProvider_default_package;
75                 else
76                         return packName;
77         }
78
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));
89                 } else {
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);
98                                 }
99                         } else {
100                                 buf.append(typeRef.getTypeQualifiedName());
101                         }
102
103                         if (isSet(SHOW_PACKAGE_POSTFIX, flags)) {
104                                 buf.append(JavaElementLabels.CONCAT_STRING);
105                                 String packName= typeRef.getPackageName();
106                                 buf.append(getPackageName(packName));
107                         }
108                 }
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);
113                 }
114                 return BasicElementLabels.getJavaElementName(buf.toString());
115         }
116
117
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;
122
123                         // XXX cannot check outer type for interface efficiently (5887)
124                         return JavaPluginImages.DESC_OBJS_CLASS;
125
126                 } else if (isSet(SHOW_PACKAGE_ONLY, flags)) {
127                         return JavaPluginImages.DESC_OBJS_PACKAGE;
128                 } else {
129                         boolean isInner= typeRef.getTypeContainerName().indexOf('.') != -1;
130                         int modifiers= typeRef.getModifiers();
131
132                         ImageDescriptor desc= JavaElementImageProvider.getTypeImageDescriptor(isInner, false, modifiers, false);
133                         int adornmentFlags= 0;
134                         if (Flags.isFinal(modifiers)) {
135                                 adornmentFlags |= JavaElementImageDescriptor.FINAL;
136                         }
137                         if (Flags.isAbstract(modifiers) && !Flags.isInterface(modifiers)) {
138                                 adornmentFlags |= JavaElementImageDescriptor.ABSTRACT;
139                         }
140                         if (Flags.isStatic(modifiers)) {
141                                 adornmentFlags |= JavaElementImageDescriptor.STATIC;
142                         }
143                         if (Flags.isDeprecated(modifiers)) {
144                                 adornmentFlags |= JavaElementImageDescriptor.DEPRECATED;
145                         }
146
147                         return new JavaElementImageDescriptor(desc, adornmentFlags, JavaElementImageProvider.BIG_SIZE);
148                 }
149         }
150
151         public static Image getImage(TypeNameMatch typeRef, int flags) {
152                 return JavaPlugin.getImageDescriptorRegistry().get(getImageDescriptor(typeRef, flags));
153         }
154
155
156 }