cleanup for dataflow renderer
[idea/community.git] / java / java-impl / src / com / intellij / slicer / SliceUsageCellRenderer.java
1 /*
2  * Copyright 2000-2015 JetBrains s.r.o.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package com.intellij.slicer;
17
18 import com.intellij.openapi.util.Segment;
19 import com.intellij.openapi.util.TextRange;
20 import com.intellij.psi.*;
21 import com.intellij.psi.util.PsiFormatUtil;
22 import com.intellij.psi.util.PsiFormatUtilBase;
23 import com.intellij.psi.util.PsiTreeUtil;
24 import com.intellij.ui.JBColor;
25 import com.intellij.ui.SimpleTextAttributes;
26 import com.intellij.usages.TextChunk;
27 import com.intellij.util.FontUtil;
28 import com.intellij.util.Processor;
29 import com.intellij.util.SmartList;
30 import org.jetbrains.annotations.NotNull;
31
32 import java.awt.*;
33 import java.util.List;
34
35 /**
36  * @author cdr
37  */
38 public class SliceUsageCellRenderer extends SliceUsageCellRendererBase {
39
40   @Override
41   public void customizeCellRendererFor(@NotNull SliceUsage sliceUsage) {
42     boolean isForcedLeaf = sliceUsage instanceof JavaSliceDereferenceUsage;
43     //might come SliceTooComplexDFAUsage
44     JavaSliceUsage javaSliceUsage = sliceUsage instanceof JavaSliceUsage ? (JavaSliceUsage)sliceUsage : null;
45
46     TextChunk[] text = sliceUsage.getText();
47     final List<TextRange> usageRanges = new SmartList<TextRange>();
48     sliceUsage.processRangeMarkers(new Processor<Segment>() {
49       @Override
50       public boolean process(Segment segment) {
51         usageRanges.add(TextRange.create(segment));
52         return true;
53       }
54     });
55     boolean isInsideContainer = javaSliceUsage != null && javaSliceUsage.indexNesting != 0;
56     for (int i = 0, length = text.length; i < length; i++) {
57       TextChunk textChunk = text[i];
58       SimpleTextAttributes attributes = textChunk.getSimpleAttributesIgnoreBackground();
59       if (isForcedLeaf) {
60         attributes = attributes.derive(attributes.getStyle(), JBColor.LIGHT_GRAY, attributes.getBgColor(), attributes.getWaveColor());
61       }
62       boolean inUsage = (attributes.getFontStyle() & Font.BOLD) != 0;
63       if (isInsideContainer && inUsage) {
64         //Color darker = Color.BLACK;//attributes.getBgColor() == null ? Color.BLACK : attributes.getBgColor().darker();
65         //attributes = attributes.derive(SimpleTextAttributes.STYLE_OPAQUE, attributes.getFgColor(), UIUtil.getTreeBackground().brighter(), attributes.getWaveColor());
66         //setMyBorder(IdeBorderFactory.createRoundedBorder(10, 3));
67         //setPaintFocusBorder(true);
68       }
69       append(textChunk.getText(), attributes);
70       if (i == 0) {
71         append(FontUtil.spaceAndThinSpace());
72       }
73     }
74
75     if (javaSliceUsage != null) {
76       for (int i = 0; i < javaSliceUsage.indexNesting; i++) {
77         append(
78           " (Tracking container contents" +
79           (javaSliceUsage.syntheticField.isEmpty() ? "" : " '" + javaSliceUsage.syntheticField + "'") +
80           ")",
81           SimpleTextAttributes.REGULAR_BOLD_ATTRIBUTES);
82       }
83     }
84
85     PsiElement element = sliceUsage.getElement();
86     PsiMethod method;
87     PsiClass aClass;
88     while (true) {
89       method = PsiTreeUtil.getParentOfType(element, PsiMethod.class);
90       aClass = method == null ? PsiTreeUtil.getParentOfType(element, PsiClass.class) : method.getContainingClass();
91       if (aClass instanceof PsiAnonymousClass) {
92         element = aClass;
93       }
94       else {
95         break;
96       }
97     }
98     int methodOptions = PsiFormatUtilBase.SHOW_NAME | PsiFormatUtilBase.SHOW_PARAMETERS | PsiFormatUtilBase.SHOW_CONTAINING_CLASS;
99     String location = method != null
100                       ? PsiFormatUtil.formatMethod(method, PsiSubstitutor.EMPTY, methodOptions, PsiFormatUtilBase.SHOW_TYPE, 2)
101                       : aClass != null ? PsiFormatUtil.formatClass(aClass, PsiFormatUtilBase.SHOW_NAME) : null;
102     if (location != null) {
103       SimpleTextAttributes attributes = SimpleTextAttributes.GRAY_ATTRIBUTES;
104       append(" in " + location, attributes);
105     }
106   }
107 }
108