001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.util;
011:
012: import org.eclipse.swt.graphics.Image;
013:
014: import org.eclipse.jface.resource.ImageDescriptor;
015: import org.eclipse.jface.viewers.LabelProvider;
016:
017: import org.eclipse.jdt.core.Flags;
018: import org.eclipse.jdt.core.IPackageFragmentRoot;
019: import org.eclipse.jdt.core.search.TypeNameMatch;
020:
021: import org.eclipse.jdt.ui.JavaElementImageDescriptor;
022: import org.eclipse.jdt.ui.JavaElementLabels;
023:
024: import org.eclipse.jdt.internal.ui.JavaPlugin;
025: import org.eclipse.jdt.internal.ui.JavaPluginImages;
026: import org.eclipse.jdt.internal.ui.JavaUIMessages;
027: import org.eclipse.jdt.internal.ui.viewsupport.JavaElementImageProvider;
028:
029: public class TypeNameMatchLabelProvider extends LabelProvider {
030:
031: public static final int SHOW_FULLYQUALIFIED = 0x01;
032: public static final int SHOW_PACKAGE_POSTFIX = 0x02;
033: public static final int SHOW_PACKAGE_ONLY = 0x04;
034: public static final int SHOW_ROOT_POSTFIX = 0x08;
035: public static final int SHOW_TYPE_ONLY = 0x10;
036: public static final int SHOW_TYPE_CONTAINER_ONLY = 0x20;
037: public static final int SHOW_POST_QUALIFIED = 0x40;
038:
039: private int fFlags;
040:
041: public TypeNameMatchLabelProvider(int flags) {
042: fFlags = flags;
043: }
044:
045: /* non java-doc
046: * @see ILabelProvider#getText
047: */
048: public String getText(Object element) {
049: if (!(element instanceof TypeNameMatch))
050: return super .getText(element);
051:
052: return getText((TypeNameMatch) element, fFlags);
053: }
054:
055: /* non java-doc
056: * @see ILabelProvider#getImage
057: */
058: public Image getImage(Object element) {
059: if (!(element instanceof TypeNameMatch))
060: return super .getImage(element);
061: return getImage((TypeNameMatch) element, fFlags);
062: }
063:
064: private static boolean isSet(int flag, int flags) {
065: return (flags & flag) != 0;
066: }
067:
068: private static String getPackageName(String packName) {
069: if (packName.length() == 0)
070: return JavaUIMessages.TypeInfoLabelProvider_default_package;
071: else
072: return packName;
073: }
074:
075: public static String getText(TypeNameMatch typeRef, int flags) {
076: StringBuffer buf = new StringBuffer();
077: if (isSet(SHOW_TYPE_ONLY, flags)) {
078: buf.append(typeRef.getSimpleTypeName());
079: } else if (isSet(SHOW_TYPE_CONTAINER_ONLY, flags)) {
080: String containerName = typeRef.getTypeContainerName();
081: buf.append(getPackageName(containerName));
082: } else if (isSet(SHOW_PACKAGE_ONLY, flags)) {
083: String packName = typeRef.getPackageName();
084: buf.append(getPackageName(packName));
085: } else {
086: if (isSet(SHOW_FULLYQUALIFIED, flags)) {
087: buf.append(typeRef.getFullyQualifiedName());
088: } else if (isSet(SHOW_POST_QUALIFIED, flags)) {
089: buf.append(typeRef.getSimpleTypeName());
090: String containerName = typeRef.getTypeContainerName();
091: if (containerName != null && containerName.length() > 0) {
092: buf.append(JavaElementLabels.CONCAT_STRING);
093: buf.append(containerName);
094: }
095: } else {
096: buf.append(typeRef.getTypeQualifiedName());
097: }
098:
099: if (isSet(SHOW_PACKAGE_POSTFIX, flags)) {
100: buf.append(JavaElementLabels.CONCAT_STRING);
101: String packName = typeRef.getPackageName();
102: buf.append(getPackageName(packName));
103: }
104: }
105: if (isSet(SHOW_ROOT_POSTFIX, flags)) {
106: buf.append(JavaElementLabels.CONCAT_STRING);
107: IPackageFragmentRoot root = typeRef
108: .getPackageFragmentRoot();
109: JavaElementLabels.getPackageFragmentRootLabel(root,
110: JavaElementLabels.ROOT_QUALIFIED, buf);
111: }
112: return buf.toString();
113: }
114:
115: public static ImageDescriptor getImageDescriptor(
116: TypeNameMatch typeRef, int flags) {
117: if (isSet(SHOW_TYPE_CONTAINER_ONLY, flags)) {
118: if (typeRef.getPackageName().equals(
119: typeRef.getTypeContainerName()))
120: return JavaPluginImages.DESC_OBJS_PACKAGE;
121:
122: // XXX cannot check outer type for interface efficiently (5887)
123: return JavaPluginImages.DESC_OBJS_CLASS;
124:
125: } else if (isSet(SHOW_PACKAGE_ONLY, flags)) {
126: return JavaPluginImages.DESC_OBJS_PACKAGE;
127: } else {
128: boolean isInner = typeRef.getTypeContainerName().indexOf(
129: '.') != -1;
130: int modifiers = typeRef.getModifiers();
131:
132: ImageDescriptor desc = JavaElementImageProvider
133: .getTypeImageDescriptor(isInner, false, modifiers,
134: false);
135: int adornmentFlags = 0;
136: if (Flags.isFinal(modifiers)) {
137: adornmentFlags |= JavaElementImageDescriptor.FINAL;
138: }
139: if (Flags.isAbstract(modifiers)
140: && !Flags.isInterface(modifiers)) {
141: adornmentFlags |= JavaElementImageDescriptor.ABSTRACT;
142: }
143: if (Flags.isStatic(modifiers)) {
144: adornmentFlags |= JavaElementImageDescriptor.STATIC;
145: }
146: if (Flags.isDeprecated(modifiers)) {
147: adornmentFlags |= JavaElementImageDescriptor.DEPRECATED;
148: }
149:
150: return new JavaElementImageDescriptor(desc, adornmentFlags,
151: JavaElementImageProvider.BIG_SIZE);
152: }
153: }
154:
155: public static Image getImage(TypeNameMatch typeRef, int flags) {
156: return JavaPlugin.getImageDescriptorRegistry().get(
157: getImageDescriptor(typeRef, flags));
158: }
159:
160: }
|