IDEA-50121
[fedora-idea.git] / java / java-impl / src / com / intellij / slicer / SliceUsageCellRenderer.java
blobf755a06d3023306e1e631927a57c020db50204c8
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.
16 package com.intellij.slicer;
18 import com.intellij.openapi.editor.colors.EditorColorsScheme;
19 import com.intellij.psi.*;
20 import com.intellij.psi.util.PsiFormatUtil;
21 import com.intellij.psi.util.PsiTreeUtil;
22 import com.intellij.ui.ColoredTreeCellRenderer;
23 import com.intellij.ui.SimpleTextAttributes;
24 import com.intellij.usageView.UsageTreeColors;
25 import com.intellij.usageView.UsageTreeColorsScheme;
26 import com.intellij.usages.TextChunk;
28 import javax.swing.*;
29 import javax.swing.tree.DefaultMutableTreeNode;
30 import java.awt.*;
32 /**
33 * @author cdr
35 public class SliceUsageCellRenderer extends ColoredTreeCellRenderer {
36 private static final EditorColorsScheme ourColorsScheme = UsageTreeColorsScheme.getInstance().getScheme();
37 public static final SimpleTextAttributes ourInvalidAttributes = SimpleTextAttributes.fromTextAttributes(ourColorsScheme.getAttributes(UsageTreeColors.INVALID_PREFIX));
39 public SliceUsageCellRenderer() {
40 setOpaque(false);
43 public void customizeCellRenderer(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
44 assert value instanceof DefaultMutableTreeNode;
45 DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode)value;
46 Object userObject = treeNode.getUserObject();
47 if (userObject == null) return;
48 if (userObject instanceof MyColoredTreeCellRenderer) {
49 MyColoredTreeCellRenderer node = (MyColoredTreeCellRenderer)userObject;
50 node.customizeCellRenderer(this, tree, value, selected, expanded, leaf, row, hasFocus);
51 if (node instanceof SliceNode) {
52 setToolTipText(((SliceNode)node).getPresentation().getTooltip());
55 else {
56 append(userObject.toString(), SimpleTextAttributes.REGULAR_BOLD_ATTRIBUTES);
60 public void customizeCellRendererFor(SliceUsage sliceUsage) {
61 boolean isForcedLeaf = sliceUsage instanceof SliceDereferenceUsage;
63 TextChunk[] text = sliceUsage.getPresentation().getText();
64 for (TextChunk textChunk : text) {
65 SimpleTextAttributes attributes = SimpleTextAttributes.fromTextAttributes(textChunk.getAttributes());
66 if (isForcedLeaf) {
67 attributes = attributes.derive(attributes.getStyle(), Color.LIGHT_GRAY, attributes.getBgColor(), attributes.getWaveColor());
68 //attributes = attributes.derive(SimpleTextAttributes.STYLE_UNDERLINE, attributes.getBgColor(), attributes.getBgColor(), attributes.getWaveColor());
70 append(textChunk.getText(), attributes);
73 PsiElement element = sliceUsage.getElement();
74 PsiMethod method;
75 PsiClass aClass;
76 while (true) {
77 method = PsiTreeUtil.getParentOfType(element, PsiMethod.class);
78 aClass = method == null ? PsiTreeUtil.getParentOfType(element, PsiClass.class) : method.getContainingClass();
79 if (aClass instanceof PsiAnonymousClass) {
80 element = aClass;
82 else {
83 break;
86 String location = method != null
87 ? PsiFormatUtil.formatMethod(method, PsiSubstitutor.EMPTY, PsiFormatUtil.SHOW_NAME |
88 PsiFormatUtil.SHOW_PARAMETERS |
89 PsiFormatUtil.SHOW_CONTAINING_CLASS,
90 PsiFormatUtil.SHOW_TYPE, 2)
91 : aClass != null ? PsiFormatUtil.formatClass(aClass, PsiFormatUtil.SHOW_NAME) : null;
92 if (location != null) {
93 SimpleTextAttributes attributes = SimpleTextAttributes.GRAY_ATTRIBUTES;
94 append(" in " + location, attributes);