Hierarchy providers refactored
[fedora-idea.git] / source / com / intellij / slicer / SliceUsageCellRenderer.java
blobb8ae7ec2c12bc2ba96792e8cfd1e8ba1807ca10c
1 package com.intellij.slicer;
3 import com.intellij.openapi.editor.colors.EditorColorsScheme;
4 import com.intellij.psi.*;
5 import com.intellij.psi.util.PsiFormatUtil;
6 import com.intellij.psi.util.PsiTreeUtil;
7 import com.intellij.ui.ColoredTreeCellRenderer;
8 import com.intellij.ui.SimpleTextAttributes;
9 import com.intellij.usageView.UsageTreeColors;
10 import com.intellij.usageView.UsageTreeColorsScheme;
11 import com.intellij.usages.TextChunk;
12 import com.intellij.usages.UsageInfo2UsageAdapter;
14 import javax.swing.*;
15 import javax.swing.tree.DefaultMutableTreeNode;
17 /**
18 * @author cdr
20 public class SliceUsageCellRenderer extends ColoredTreeCellRenderer {
21 private static final EditorColorsScheme ourColorsScheme = UsageTreeColorsScheme.getInstance().getScheme();
22 public static final SimpleTextAttributes ourInvalidAttributes = SimpleTextAttributes.fromTextAttributes(ourColorsScheme.getAttributes(UsageTreeColors.INVALID_PREFIX));
24 public SliceUsageCellRenderer() {
25 setOpaque(false);
28 public void customizeCellRenderer(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
29 assert value instanceof DefaultMutableTreeNode;
30 DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode)value;
31 Object userObject = treeNode.getUserObject();
32 if (userObject == null) return;
33 if (userObject instanceof MyColoredTreeCellRenderer) {
34 MyColoredTreeCellRenderer node = (MyColoredTreeCellRenderer)userObject;
35 node.customizeCellRenderer(this, tree, value, selected, expanded, leaf, row, hasFocus);
37 else {
38 append(userObject.toString(), SimpleTextAttributes.REGULAR_BOLD_ATTRIBUTES);
42 public void customizeTreeCellRenderer(UsageInfo2UsageAdapter sliceUsage) {
43 TextChunk[] text = sliceUsage.getPresentation().getText();
44 for (TextChunk textChunk : text) {
45 append(textChunk.getText(), SimpleTextAttributes.fromTextAttributes(textChunk.getAttributes()));
48 PsiElement element = sliceUsage.getElement();
49 PsiMethod method;
50 PsiClass aClass;
51 while (true) {
52 method = PsiTreeUtil.getParentOfType(element, PsiMethod.class);
53 aClass = method == null ? PsiTreeUtil.getParentOfType(element, PsiClass.class) : method.getContainingClass();
54 if (aClass instanceof PsiAnonymousClass) {
55 element = aClass;
57 else {
58 break;
61 String location = method != null ? PsiFormatUtil.formatMethod(method, PsiSubstitutor.EMPTY, PsiFormatUtil.SHOW_NAME | PsiFormatUtil.SHOW_PARAMETERS | PsiFormatUtil.SHOW_CONTAINING_CLASS, PsiFormatUtil.SHOW_TYPE, 2)
62 : aClass != null ? PsiFormatUtil.formatClass(aClass, PsiFormatUtil.SHOW_NAME)
63 : null;
64 if (location != null) {
65 append(" in " + location, SimpleTextAttributes.GRAY_ATTRIBUTES);