JS lookup items: use new API
[fedora-idea.git] / lang-impl / src / com / intellij / codeInsight / lookup / DefaultLookupItemRenderer.java
blob4c2f03a223bbc89b44c7332143f5a84f8e1fea38
1 /*
2 * Copyright (c) 2000-2005 by JetBrains s.r.o. All Rights Reserved.
3 * Use is subject to license terms.
4 */
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;
15 import javax.swing.*;
17 /**
18 * @author peter
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);
34 @Nullable
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;
42 if (!real) {
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);
63 return null;
67 @Nullable
68 private static String getText3(final LookupItem item) {
69 Object o = item.getObject();
70 String text;
71 if (o instanceof LookupValueWithUIHint) {
72 text = ((LookupValueWithUIHint)o).getTypeHint();
74 else {
75 text = (String)item.getAttribute(LookupItem.TYPE_TEXT_ATTR);
77 return text;
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();
92 String name = null;
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();
105 else {
106 name = String.valueOf(o);
108 if (name == null){
109 name = "";
112 return name;