ComponentWithBrowseButton - optional remove listener on hide
[fedora-idea.git] / platform / lang-impl / src / com / intellij / ide / util / PsiElementListCellRenderer.java
blob278cd1f1e4e2a844a20e104c1522c0e3fdccab51
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.intellij.ide.util;
19 import com.intellij.ide.ui.UISettings;
20 import com.intellij.navigation.ItemPresentation;
21 import com.intellij.navigation.NavigationItem;
22 import com.intellij.openapi.editor.colors.EditorColorsManager;
23 import com.intellij.openapi.editor.colors.TextAttributesKey;
24 import com.intellij.openapi.editor.markup.EffectType;
25 import com.intellij.openapi.editor.markup.TextAttributes;
26 import com.intellij.openapi.ui.popup.PopupChooserBuilder;
27 import com.intellij.openapi.vcs.FileStatus;
28 import com.intellij.openapi.vcs.FileStatusManager;
29 import com.intellij.openapi.vfs.VirtualFile;
30 import com.intellij.problems.WolfTheProblemSolver;
31 import com.intellij.psi.PsiElement;
32 import com.intellij.psi.PsiFile;
33 import com.intellij.ui.ColoredListCellRenderer;
34 import com.intellij.ui.FileColorManager;
35 import com.intellij.ui.ListSpeedSearch;
36 import com.intellij.ui.SimpleTextAttributes;
37 import com.intellij.util.Function;
38 import com.intellij.util.IconUtil;
39 import com.intellij.util.ui.UIUtil;
40 import org.jetbrains.annotations.Nullable;
42 import javax.swing.*;
43 import java.awt.*;
44 import java.util.Comparator;
46 public abstract class PsiElementListCellRenderer<T extends PsiElement> extends JPanel implements ListCellRenderer {
47 protected PsiElementListCellRenderer() {
48 super(new BorderLayout());
51 private class LeftRenderer extends ColoredListCellRenderer {
52 private final String myModuleName;
54 public LeftRenderer(final String moduleName) {
55 myModuleName = moduleName;
58 protected void customizeCellRenderer(
59 JList list,
60 Object value,
61 int index,
62 boolean selected,
63 boolean hasFocus
64 ) {
65 Color bgColor = UIUtil.getListBackground();
66 if (value instanceof PsiElement) {
67 T element = (T)value;
68 String name = getElementText((T)element);
69 Color color = list.getForeground();
70 PsiFile psiFile = element.getContainingFile();
71 boolean isProblemFile = false;
73 if (psiFile != null) {
74 VirtualFile vFile = psiFile.getVirtualFile();
75 if (vFile != null) {
76 if (WolfTheProblemSolver.getInstance(psiFile.getProject()).isProblemFile(vFile)) {
77 isProblemFile = true;
79 FileStatus status = FileStatusManager.getInstance(psiFile.getProject()).getStatus(vFile);
80 color = status.getColor();
82 final FileColorManager colorManager = FileColorManager.getInstance(psiFile.getProject());
83 if (colorManager.isEnabled()) {
84 final Color fileBgColor = colorManager.getRendererBackground(psiFile);
85 bgColor = fileBgColor == null ? bgColor : fileBgColor;
90 TextAttributes attributes = null;
92 if (value instanceof NavigationItem) {
93 TextAttributesKey attributesKey = null;
94 final ItemPresentation presentation = ((NavigationItem)value).getPresentation();
95 if (presentation != null) attributesKey = presentation.getTextAttributesKey();
97 if (attributesKey != null) {
98 attributes = EditorColorsManager.getInstance().getGlobalScheme().getAttributes(attributesKey);
102 SimpleTextAttributes nameAttributes;
103 if (isProblemFile) {
104 attributes = TextAttributes.merge(new TextAttributes(color, null, Color.red, EffectType.WAVE_UNDERSCORE, Font.PLAIN),attributes);
107 nameAttributes = attributes != null ? SimpleTextAttributes.fromTextAttributes(attributes):null;
109 if (nameAttributes == null) nameAttributes = new SimpleTextAttributes(Font.PLAIN, color);
111 assert name != null: "Null name for PSI element " + element;
112 append(name, nameAttributes);
113 setIcon(PsiElementListCellRenderer.this.getIcon(element));
115 String containerText = getContainerText(element, name + (myModuleName != null ? myModuleName + " " : ""));
116 if (containerText != null) {
117 append(" " + containerText, new SimpleTextAttributes(Font.PLAIN, Color.GRAY));
120 else {
121 setIcon(IconUtil.getEmptyIcon(false));
122 append(value == null ? "" : value.toString(), new SimpleTextAttributes(Font.PLAIN, list.getForeground()));
124 setPaintFocusBorder(false);
125 setBackground(selected ? UIUtil.getListSelectionBackground() : bgColor);
129 public Component getListCellRendererComponent(JList list,
130 Object value,
131 int index,
132 boolean isSelected,
133 boolean cellHasFocus) {
134 removeAll();
135 String moduleName = null;
136 DefaultListCellRenderer rightRenderer = getRightCellRenderer();
137 final Component leftCellRendererComponent =
138 new LeftRenderer(moduleName).getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
139 if (rightRenderer != null) {
140 final Component rightCellRendererComponent =
141 rightRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
142 rightCellRendererComponent.setBackground(isSelected ? UIUtil.getListSelectionBackground() : leftCellRendererComponent.getBackground());
143 add(rightCellRendererComponent, BorderLayout.EAST);
144 moduleName = rightRenderer.getText();
145 final JPanel spacer = new JPanel();
146 spacer.setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 2));
147 spacer.setBackground(isSelected ? UIUtil.getListSelectionBackground() : leftCellRendererComponent.getBackground());
148 add(spacer, BorderLayout.CENTER);
150 add(leftCellRendererComponent, BorderLayout.WEST);
151 setBackground(isSelected ? UIUtil.getListSelectionBackground() : leftCellRendererComponent.getBackground());
152 return this;
155 @Nullable
156 protected DefaultListCellRenderer getRightCellRenderer() {
157 if (UISettings.getInstance().SHOW_ICONS_IN_QUICK_NAVIGATION) {
158 return ModuleRendererFactory.getInstance().getModuleRenderer();
160 return null;
163 public abstract String getElementText(T element);
165 @Nullable
166 protected abstract String getContainerText(T element, final String name);
168 protected abstract int getIconFlags();
170 protected Icon getIcon(PsiElement element) {
171 return element.getIcon(getIconFlags());
174 public Comparator<T> getComparator() {
175 return new Comparator<T>() {
176 public int compare(T o1, T o2) {
177 return getText(o1).compareTo(getText(o2));
180 private String getText(T element) {
181 String elementText = getElementText(element);
182 String containerText = getContainerText(element, elementText);
183 return containerText != null ? elementText + " " + containerText : elementText;
188 public void installSpeedSearch(PopupChooserBuilder builder) {
189 builder.setFilteringEnabled(new Function<Object, String>() {
190 public String fun(Object o) {
191 if (o instanceof PsiElement) {
192 return PsiElementListCellRenderer.this.getElementText((T)o);
194 else {
195 return o.toString();
202 * User {@link #installSpeedSearch(com.intellij.openapi.ui.popup.PopupChooserBuilder)} instead
204 @Deprecated
205 public void installSpeedSearch(JList list) {
206 new ListSpeedSearch(list) {
207 protected String getElementText(Object o) {
208 if (o instanceof PsiElement) {
209 return PsiElementListCellRenderer.this.getElementText((T)o);
211 else {
212 return o.toString();