update copyrights
[fedora-idea.git] / platform / lang-impl / src / com / intellij / find / actions / ShowUsagesTableCellRenderer.java
blob0fc641fa10cfd4b23aa7c06fadbcc51bfdcdc581
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.find.actions;
19 import com.intellij.openapi.project.Project;
20 import com.intellij.openapi.vfs.VirtualFile;
21 import com.intellij.psi.PsiManager;
22 import com.intellij.psi.PsiFile;
23 import com.intellij.ui.SimpleColoredComponent;
24 import com.intellij.ui.SimpleTextAttributes;
25 import com.intellij.ui.FileColorManager;
26 import com.intellij.usages.TextChunk;
27 import com.intellij.usages.Usage;
28 import com.intellij.usages.UsageGroup;
29 import com.intellij.usages.UsagePresentation;
30 import com.intellij.usages.impl.GroupNode;
31 import com.intellij.usages.impl.NullUsage;
32 import com.intellij.usages.impl.UsageNode;
33 import com.intellij.usages.impl.UsageViewImpl;
34 import com.intellij.usages.rules.UsageInFile;
35 import com.intellij.util.ui.UIUtil;
37 import javax.swing.*;
38 import javax.swing.table.TableCellRenderer;
39 import java.awt.*;
41 /**
42 * @author cdr
44 class ShowUsagesTableCellRenderer implements TableCellRenderer {
45 private final UsageViewImpl myUsageView;
47 ShowUsagesTableCellRenderer(UsageViewImpl usageView) {
48 myUsageView = usageView;
51 public Component getTableCellRendererComponent(JTable list, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
52 if (!(value instanceof UsageNode)) {
53 return new JLabel("<html><body><b>" + value + "</b></body></html>", SwingConstants.CENTER);
55 UsageNode usageNode = (UsageNode)value;
57 GroupNode parent = (GroupNode)usageNode.getParent();
58 SimpleColoredComponent textChunks = new SimpleColoredComponent();
59 textChunks.setIpad(new Insets(0,0,0,0));
60 textChunks.setBorder(null);
61 Usage usage = usageNode.getUsage();
63 Color fileBgColor = getBackgroundColor(isSelected, usage);
65 JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0,0));
66 panel.setBackground(isSelected ? UIUtil.getListSelectionBackground() : fileBgColor == null ? list.getBackground() : fileBgColor);
67 panel.setForeground(isSelected ? UIUtil.getListSelectionForeground() : list.getForeground());
69 if (column == 0) {
70 appendGroupText(parent, panel, fileBgColor);
71 if (usage == NullUsage.INSTANCE) {
72 textChunks.append("...<");
73 textChunks.append("more usages", SimpleTextAttributes.REGULAR_BOLD_ATTRIBUTES);
74 textChunks.append(">...");
77 else if (usage != NullUsage.INSTANCE) {
78 UsagePresentation presentation = usage.getPresentation();
79 TextChunk[] text = presentation.getText();
81 if (column == 1) {
82 textChunks.setIcon(presentation.getIcon());
83 if (text.length != 0) {
84 SimpleTextAttributes attributes =
85 deriveAttributesWithColor(SimpleTextAttributes.fromTextAttributes(text[0].getAttributes()), fileBgColor);
86 textChunks.append(text[0].getText(), attributes);
89 else if (column == 2) {
90 for (int i = 1; i < text.length; i++) {
91 TextChunk textChunk = text[i];
92 SimpleTextAttributes attributes =
93 deriveAttributesWithColor(SimpleTextAttributes.fromTextAttributes(textChunk.getAttributes()), fileBgColor);
94 textChunks.append(textChunk.getText(), attributes);
97 else {
98 assert false : column;
101 panel.add(textChunks);
102 return panel;
105 private static SimpleTextAttributes deriveAttributesWithColor(SimpleTextAttributes attributes, Color fileBgColor) {
106 if (fileBgColor != null) {
107 attributes = attributes.derive(-1,null, fileBgColor,null);
109 return attributes;
112 private Color getBackgroundColor(boolean isSelected, Usage usage) {
113 Color fileBgColor = null;
114 if (isSelected) {
115 fileBgColor = UIUtil.getListSelectionBackground();
117 else {
118 VirtualFile virtualFile = usage instanceof UsageInFile ? ((UsageInFile)usage).getFile() : null;
119 if (virtualFile != null) {
120 Project project = myUsageView.getProject();
121 PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile);
122 if (psiFile != null && psiFile.isValid()) {
123 final FileColorManager colorManager = FileColorManager.getInstance(project);
124 if (colorManager.isEnabled()) {
125 final Color color = colorManager.getFileColor(psiFile);
126 if (color != null) fileBgColor = color;
131 return fileBgColor;
134 private void appendGroupText(final GroupNode node, JPanel panel, Color fileBgColor) {
135 UsageGroup group = node == null ? null : node.getGroup();
136 if (group == null) return;
137 GroupNode parentGroup = (GroupNode)node.getParent();
138 appendGroupText(parentGroup, panel, fileBgColor);
139 if (node.canNavigateToSource()) {
140 SimpleColoredComponent renderer = new SimpleColoredComponent();
142 renderer.setIcon(group.getIcon(false));
143 SimpleTextAttributes attributes = deriveAttributesWithColor(SimpleTextAttributes.REGULAR_ATTRIBUTES, fileBgColor);
144 renderer.append(group.getText(myUsageView), attributes);
145 renderer.append(" ", attributes);
146 renderer.setIpad(new Insets(0,0,0,0));
147 renderer.setBorder(null);
148 panel.add(renderer);