2 * Copyright (c) 2000-2005 by JetBrains s.r.o. All Rights Reserved.
3 * Use is subject to license terms.
5 package com
.intellij
.codeInsight
.lookup
;
7 import com
.intellij
.openapi
.util
.Iconable
;
8 import com
.intellij
.psi
.PsiElement
;
9 import com
.intellij
.psi
.meta
.PsiMetaData
;
10 import com
.intellij
.psi
.util
.PsiUtilBase
;
11 import com
.intellij
.util
.Icons
;
12 import com
.intellij
.util
.ui
.EmptyIcon
;
13 import org
.jetbrains
.annotations
.Nullable
;
20 public class DefaultLookupItemRenderer
extends LookupElementRenderer
<LookupItem
>{
21 public static final DefaultLookupItemRenderer INSTANCE
= new DefaultLookupItemRenderer();
22 private static final Icon SAMPLE_ICON
= Icons
.CLASS_ICON
;
24 public void renderElement(final LookupItem item
, final LookupElementPresentation presentation
) {
25 presentation
.setIcon(getRawIcon(item
, presentation
.isReal()));
26 final boolean bold
= item
.getAttribute(LookupItem
.HIGHLIGHTED_ATTR
) != null;
27 final boolean grayed
= item
.getAttribute(LookupItem
.TAIL_TEXT_SMALL_ATTR
) != null;
29 presentation
.setItemText(getName(item
), isToStrikeout(item
), bold
);
30 presentation
.setTailText(getText2(item
), grayed
, false, isToStrikeout(item
));
31 presentation
.setTypeText(getText3(item
), null);
35 public static Icon
getRawIcon(final LookupItem item
, boolean real
) {
36 Icon icon
= (Icon
)item
.getAttribute(LookupItem
.ICON_ATTR
);
37 if (icon
!= null) return icon
;
39 Object o
= item
.getObject();
41 int flags
= Iconable
.ICON_FLAG_VISIBILITY
;
43 if (item
.getObject() instanceof String
) {
44 return new EmptyIcon(0, 0);
47 return new EmptyIcon(SAMPLE_ICON
.getIconWidth() * 2, SAMPLE_ICON
.getIconHeight());
50 if (o
instanceof Iconable
&& !(o
instanceof PsiElement
)) {
51 return ((Iconable
)o
).getIcon(flags
);
54 if (o
instanceof LookupValueWithPsiElement
) {
55 o
= ((LookupValueWithPsiElement
)o
).getElement();
57 if (o
instanceof PsiElement
) {
58 final PsiElement element
= (PsiElement
)o
;
59 if (element
.isValid()) {
60 return element
.getIcon(flags
);
68 private static String
getText3(final LookupItem item
) {
69 Object o
= item
.getObject();
71 if (o
instanceof LookupValueWithUIHint
) {
72 text
= ((LookupValueWithUIHint
)o
).getTypeHint();
75 text
= (String
)item
.getAttribute(LookupItem
.TYPE_TEXT_ATTR
);
80 private static String
getText2(final LookupItem item
) {
81 return (String
)item
.getAttribute(LookupItem
.TAIL_TEXT_ATTR
);
84 private static boolean isToStrikeout(LookupItem item
) {
85 return item
.getAttribute(LookupItem
.DEPRECATED_ATTR
) != null;
88 private static String
getName(final LookupItem item
){
89 final String presentableText
= item
.getPresentableText();
90 if (presentableText
!= null) return presentableText
;
91 final Object o
= item
.getObject();
93 if (o
instanceof PsiElement
) {
94 final PsiElement element
= (PsiElement
)o
;
95 if (element
.isValid()) {
96 name
= PsiUtilBase
.getName(element
);
99 else if (o
instanceof PsiMetaData
) {
100 name
= ((PsiMetaData
)o
).getName();
102 else if (o
instanceof PresentableLookupValue
) {
103 name
= ((PresentableLookupValue
)o
).getPresentation();
106 name
= String
.valueOf(o
);