Hierarchy providers refactored
[fedora-idea.git] / lang-impl / src / com / intellij / ide / util / NavigationItemListCellRenderer.java
blob9adeee0834f927334e22fa3b6aa1425b705fb0f0
1 /*
2 * Copyright (c) 2004 JetBrains s.r.o. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
8 * -Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * -Redistribution in binary form must reproduct the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the distribution.
15 * Neither the name of JetBrains or IntelliJ IDEA
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * This software is provided "AS IS," without a warranty of any kind. ALL
20 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
21 * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
22 * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. JETBRAINS AND ITS LICENSORS SHALL NOT
23 * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
24 * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
25 * DERIVATIVES. IN NO EVENT WILL JETBRAINS OR ITS LICENSORS BE LIABLE FOR ANY LOST
26 * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
27 * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
28 * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
29 * IF JETBRAINS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 package com.intellij.ide.util;
34 import com.intellij.ide.ui.UISettings;
35 import com.intellij.ide.util.treeView.NodeRenderer;
36 import com.intellij.navigation.ItemPresentation;
37 import com.intellij.navigation.NavigationItem;
38 import com.intellij.openapi.editor.colors.EditorColorsManager;
39 import com.intellij.openapi.editor.colors.EditorColorsScheme;
40 import com.intellij.openapi.editor.markup.EffectType;
41 import com.intellij.openapi.editor.markup.TextAttributes;
42 import com.intellij.openapi.vcs.FileStatus;
43 import com.intellij.openapi.vfs.VirtualFile;
44 import com.intellij.openapi.project.Project;
45 import com.intellij.problems.WolfTheProblemSolver;
46 import com.intellij.psi.PsiElement;
47 import com.intellij.psi.util.PsiUtilBase;
48 import com.intellij.ui.ColoredListCellRenderer;
49 import com.intellij.ui.SimpleTextAttributes;
50 import com.intellij.ui.FileColorManager;
51 import com.intellij.util.IconUtil;
52 import com.intellij.util.ui.UIUtil;
54 import javax.swing.*;
55 import java.awt.*;
57 public class NavigationItemListCellRenderer extends JPanel implements ListCellRenderer {
58 public NavigationItemListCellRenderer() {
59 super(new BorderLayout());
62 public Component getListCellRendererComponent(
63 JList list,
64 Object value,
65 int index,
66 boolean isSelected,
67 boolean cellHasFocus) {
68 EditorColorsScheme scheme = EditorColorsManager.getInstance().getGlobalScheme();
69 Font editorFont = new Font(scheme.getEditorFontName(), Font.PLAIN, scheme.getEditorFontSize());
70 setFont(editorFont);
71 removeAll();
72 final Component leftCellRendererComponent =
73 new LeftRenderer().getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
74 final Color listBg = leftCellRendererComponent.getBackground();
75 add(leftCellRendererComponent, BorderLayout.WEST);
76 if (UISettings.getInstance().SHOW_ICONS_IN_QUICK_NAVIGATION){
77 final DefaultListCellRenderer moduleRenderer = ModuleRendererFactory.getInstance().getModuleRenderer();
78 final Component rightCellRendererComponent =
79 moduleRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
80 rightCellRendererComponent.setBackground(listBg);
81 add(rightCellRendererComponent, BorderLayout.EAST);
82 final JPanel spacer = new JPanel();
83 final Dimension size = rightCellRendererComponent.getSize();
84 spacer.setSize(new Dimension((int)(size.width * 0.015 + leftCellRendererComponent.getSize().width * 0.015), size.height));
85 spacer.setBackground(isSelected ? UIUtil.getListSelectionBackground() : listBg);
86 add(spacer, BorderLayout.CENTER);
88 setBackground(isSelected ? UIUtil.getListSelectionBackground() : UIUtil.getListBackground());
89 return this;
92 private static class LeftRenderer extends ColoredListCellRenderer {
93 protected void customizeCellRenderer(
94 JList list,
95 Object value,
96 int index,
97 boolean selected,
98 boolean hasFocus
99 ) {
100 Color bgColor = UIUtil.getListBackground();
102 if (value instanceof NavigationItem) {
103 NavigationItem element = (NavigationItem)value;
104 ItemPresentation presentation = element.getPresentation();
105 assert presentation != null: "PSI elements displayed in choose by name lists must return a non-null value from getPresentation(): element " +
106 element.toString() + ", class " + element.getClass().getName();
107 String name = presentation.getPresentableText();
108 Color color = list.getForeground();
109 boolean isProblemFile = element instanceof PsiElement
110 && WolfTheProblemSolver.getInstance(((PsiElement)element).getProject())
111 .isProblemFile(PsiUtilBase.getVirtualFile((PsiElement)element));
113 if (element instanceof PsiElement) {
114 final PsiElement psiElement = (PsiElement)element;
115 final Project project = psiElement.getProject();
117 final VirtualFile virtualFile = PsiUtilBase.getVirtualFile(psiElement);
118 isProblemFile = WolfTheProblemSolver.getInstance(project).isProblemFile(virtualFile);
120 final FileColorManager fileColorManager = FileColorManager.getInstance(project);
121 if (virtualFile != null && fileColorManager.isEnabled()) {
122 final Color fileColor = fileColorManager.getFileColor(psiElement.getContainingFile());
123 if (fileColor != null) {
124 bgColor = fileColor;
129 FileStatus status = element.getFileStatus();
130 if (status != FileStatus.NOT_CHANGED) {
131 color = status.getColor();
134 final TextAttributes textAttributes = NodeRenderer.getSimpleTextAttributes(presentation).toTextAttributes();
135 if (isProblemFile) {
136 textAttributes.setEffectType(EffectType.WAVE_UNDERSCORE);
137 textAttributes.setEffectColor(Color.red);
139 textAttributes.setForegroundColor(color);
140 SimpleTextAttributes nameAttributes = SimpleTextAttributes.fromTextAttributes(textAttributes);
141 append(name, nameAttributes);
142 setIcon(presentation.getIcon(false));
144 String containerText = presentation.getLocationString();
146 if (containerText != null && containerText.length() > 0) {
147 append(" " + containerText, new SimpleTextAttributes(Font.PLAIN, Color.GRAY));
150 else {
151 setIcon(IconUtil.getEmptyIcon(false));
152 append(value == null ? "" : value.toString(), new SimpleTextAttributes(Font.PLAIN, list.getForeground()));
154 setPaintFocusBorder(false);
155 setBackground(selected ? UIUtil.getListSelectionBackground() : bgColor);