2 * Copyright 2000-2016 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.vcs.log.ui.render;
18 import com.intellij.openapi.ui.GraphicsConfig;
19 import com.intellij.openapi.util.Pair;
20 import com.intellij.ui.ColorUtil;
21 import com.intellij.util.ObjectUtils;
22 import com.intellij.util.containers.ContainerUtil;
23 import com.intellij.util.ui.GraphicsUtil;
24 import com.intellij.util.ui.JBUI;
25 import com.intellij.util.ui.UIUtil;
26 import com.intellij.vcs.log.VcsLogRefManager;
27 import com.intellij.vcs.log.VcsRef;
28 import com.intellij.vcs.log.VcsRefType;
29 import com.intellij.vcs.log.paint.PaintParameters;
30 import org.jetbrains.annotations.NotNull;
31 import org.jetbrains.annotations.Nullable;
35 import java.util.Collection;
36 import java.util.List;
39 public class RectangleReferencePainter implements ReferencePainter {
41 private List<Pair<String, Color>> myLabels = ContainerUtil.newArrayList();
42 private int myHeight = JBUI.scale(22);
43 private int myWidth = 0;
45 private final RectanglePainter myLabelPainter = new RectanglePainter(false) {
47 protected Font getLabelFont() {
48 return getReferenceFont();
53 public void customizePainter(@NotNull JComponent component,
54 @NotNull Collection<VcsRef> references,
55 @Nullable VcsLogRefManager manager,
56 @NotNull Color background,
57 @NotNull Color foreground) {
58 FontMetrics metrics = component.getFontMetrics(getReferenceFont());
59 myHeight = metrics.getHeight() + RectanglePainter.TOP_TEXT_PADDING + RectanglePainter.BOTTOM_TEXT_PADDING;
60 myWidth = 2 * PaintParameters.LABEL_PADDING;
62 myLabels = ContainerUtil.newArrayList();
63 if (manager == null) return;
65 List<VcsRef> sorted = ContainerUtil.sorted(references, manager.getLabelsOrderComparator());
67 for (Map.Entry<VcsRefType, Collection<VcsRef>> entry : ContainerUtil.groupBy(sorted, VcsRef::getType).entrySet()) {
68 VcsRef ref = ObjectUtils.assertNotNull(ContainerUtil.getFirstItem(entry.getValue()));
69 String text = ref.getName() + (entry.getValue().size() > 1 ? " +" : "");
70 myLabels.add(Pair.create(text, entry.getKey().getBackgroundColor()));
72 myWidth += myLabelPainter.calculateSize(text, metrics).getWidth() + PaintParameters.LABEL_PADDING;
76 public void paint(@NotNull Graphics2D g2, int x, int y, int height) {
77 if (myLabels.isEmpty()) return;
79 GraphicsConfig config = GraphicsUtil.setupAAPainting(g2);
80 g2.setFont(getReferenceFont());
81 g2.setStroke(new BasicStroke(1.5f));
83 FontMetrics fontMetrics = g2.getFontMetrics();
85 x += PaintParameters.LABEL_PADDING;
86 for (Pair<String, Color> label : myLabels) {
87 Dimension size = myLabelPainter.calculateSize(label.first, fontMetrics);
88 int paddingY = y + (height - size.height) / 2;
89 myLabelPainter.paint(g2, label.first, x, paddingY, getLabelColor(label.second));
90 x += size.width + PaintParameters.LABEL_PADDING;
97 public static Color getLabelColor(@NotNull Color color) {
98 if (UIUtil.isUnderDarcula()) {
99 color = ColorUtil.darker(color, 6);
102 color = ColorUtil.brighter(color, 6);
104 return ColorUtil.desaturate(color, 3);
107 public Dimension getSize() {
108 if (myLabels.isEmpty()) return new Dimension();
109 return new Dimension(myWidth, myHeight);
113 public boolean isLeftAligned() {