summaryrefslogtreecommitdiff
path: root/platform/lang-impl/src/com/intellij/profile/codeInspection/ui/inspectionsTree/MultiScopeSeverityIcon.java
blob: 099dbb62226193c1a2ed473307f8c2a9c2fe80f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.profile.codeInspection.ui.inspectionsTree;


import com.intellij.codeHighlighting.HighlightDisplayLevel;
import com.intellij.codeInspection.ex.InspectionProfileImpl;
import com.intellij.lang.annotation.HighlightSeverity;
import com.intellij.profile.codeInspection.ui.ScopeOrderComparator;
import com.intellij.ui.JBColor;

import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.util.List;

/**
 * @author Dmitry Batkovich
 */
public class MultiScopeSeverityIcon implements Icon {
  private final static JBColor MIXED_SEVERITY_COLOR = JBColor.DARK_GRAY;

  private final static int SIZE = 12;

  private final LinkedHashMap<String, HighlightSeverity> myScopeToAverageSeverityMap;

  public MultiScopeSeverityIcon(final Map<String, HighlightSeverity> scopeToAverageSeverityMap,
                                final String defaultScopeName,
                                final InspectionProfileImpl inspectionProfile) {
    final List<String> sortedScopeNames = new ArrayList<String>(scopeToAverageSeverityMap.keySet());
    myScopeToAverageSeverityMap = new LinkedHashMap<String, HighlightSeverity>();
    Collections.sort(sortedScopeNames, new ScopeOrderComparator(inspectionProfile));
    sortedScopeNames.remove(defaultScopeName);
    sortedScopeNames.add(defaultScopeName);
    for (final String scopeName : sortedScopeNames) {
      myScopeToAverageSeverityMap.put(scopeName, scopeToAverageSeverityMap.get(scopeName));
    }
  }

  public LinkedHashMap<String, HighlightSeverity> getScopeToAverageSeverityMap() {
    return myScopeToAverageSeverityMap;
  }

  @Override
  public void paintIcon(final Component c, final Graphics g, final int i, final int j) {
    final int iconWidth = getIconWidth();

    final int partWidth = iconWidth / myScopeToAverageSeverityMap.size();

    final Collection<HighlightSeverity> values = myScopeToAverageSeverityMap.values();
    int idx = 0;
    for (final HighlightSeverity severity : values) {
      final Icon icon = HighlightDisplayLevel.find(severity).getIcon();
      g.setColor(icon instanceof HighlightDisplayLevel.SingleColorIconWithMask ?
                 ((HighlightDisplayLevel.SingleColorIconWithMask)icon).getColor() : MIXED_SEVERITY_COLOR);
      final int x = i + partWidth * idx;
      g.fillRect(x, j, partWidth, getIconHeight());
      idx++;
    }
    g.drawImage(HighlightDisplayLevel.ImageHolder.ourErrorMaskImage, i, j, null);
  }

  @Override
  public int getIconWidth() {
    return SIZE;
  }

  @Override
  public int getIconHeight() {
    return SIZE;
  }
}