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
.PsiFile
;
22 import com
.intellij
.psi
.PsiManager
;
23 import com
.intellij
.ui
.FileColorManager
;
24 import com
.intellij
.ui
.SimpleColoredComponent
;
25 import com
.intellij
.ui
.SimpleTextAttributes
;
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
;
38 import javax
.swing
.table
.TableCellRenderer
;
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());
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();
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 final SimpleTextAttributes attrs
= SimpleTextAttributes
.fromTextAttributes(textChunk
.getAttributes());
93 final Color bg
= UIUtil
.getListSelectionBackground();
94 final Color fg
= UIUtil
.getListSelectionForeground();
95 SimpleTextAttributes attributes
= isSelected ?
96 new SimpleTextAttributes(bg
, fg
, fg
, attrs
.getStyle()) : deriveAttributesWithColor(attrs
, fileBgColor
);
97 textChunks
.append(textChunk
.getText(), attributes
);
101 assert false : column
;
104 panel
.add(textChunks
);
108 private static SimpleTextAttributes
deriveAttributesWithColor(SimpleTextAttributes attributes
, Color fileBgColor
) {
109 if (fileBgColor
!= null) {
110 attributes
= attributes
.derive(-1,null, fileBgColor
,null);
115 private Color
getBackgroundColor(boolean isSelected
, Usage usage
) {
116 Color fileBgColor
= null;
118 fileBgColor
= UIUtil
.getListSelectionBackground();
121 VirtualFile virtualFile
= usage
instanceof UsageInFile ?
((UsageInFile
)usage
).getFile() : null;
122 if (virtualFile
!= null) {
123 Project project
= myUsageView
.getProject();
124 PsiFile psiFile
= PsiManager
.getInstance(project
).findFile(virtualFile
);
125 if (psiFile
!= null && psiFile
.isValid()) {
126 final Color color
= FileColorManager
.getInstance(project
).getRendererBackground(psiFile
);
127 if (color
!= null) fileBgColor
= color
;
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);