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 * Copyright 2000-2015 JetBrains s.r.o.
20 * Licensed under the Apache License, Version 2.0 (the "License");
21 * you may not use this file except in compliance with the License.
22 * You may obtain a copy of the License at
24 * http://www.apache.org/licenses/LICENSE-2.0
26 * Unless required by applicable law or agreed to in writing, software
27 * distributed under the License is distributed on an "AS IS" BASIS,
28 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29 * See the License for the specific language governing permissions and
30 * limitations under the License.
33 import com.intellij.openapi.ui.GraphicsConfig;
34 import com.intellij.openapi.util.Pair;
35 import com.intellij.openapi.util.text.StringUtil;
36 import com.intellij.ui.ColorUtil;
37 import com.intellij.ui.JBColor;
38 import com.intellij.ui.SimpleColoredComponent;
39 import com.intellij.util.ObjectUtils;
40 import com.intellij.util.containers.ContainerUtil;
41 import com.intellij.util.containers.MultiMap;
42 import com.intellij.util.ui.GraphicsUtil;
43 import com.intellij.util.ui.JBUI;
44 import com.intellij.util.ui.UIUtil;
45 import com.intellij.vcs.log.RefGroup;
46 import com.intellij.vcs.log.VcsLogRefManager;
47 import com.intellij.vcs.log.VcsRef;
48 import com.intellij.vcs.log.VcsRefType;
49 import com.intellij.vcs.log.data.VcsLogData;
50 import org.jetbrains.annotations.NotNull;
51 import org.jetbrains.annotations.Nullable;
55 import java.util.Collection;
56 import java.util.List;
59 public class LabelPainter implements ReferencePainter {
60 public static final int TOP_TEXT_PADDING = JBUI.scale(1);
61 public static final int BOTTOM_TEXT_PADDING = JBUI.scale(2);
62 public static final int RIGHT_PADDING = JBUI.scale(2);
63 public static final int LEFT_PADDING = JBUI.scale(2);
64 public static final int MIDDLE_PADDING = JBUI.scale(2);
65 private static final int MAX_LENGTH = 22;
66 private static final String THREE_DOTS = "...";
67 private static final String TWO_DOTS = "..";
68 private static final String SEPARATOR = "/";
69 @SuppressWarnings("UseJBColor") private static final JBColor BACKGROUND = new JBColor(Color.BLACK, Color.WHITE);
70 private static final float BALANCE = 0.08f;
72 @NotNull private final VcsLogData myLogData;
74 @NotNull private List<Pair<String, LabelIcon>> myLabels = ContainerUtil.newArrayList();
75 private int myHeight = JBUI.scale(22);
76 private int myWidth = 0;
77 @NotNull private Color myBackground = UIUtil.getTableBackground();
78 @Nullable private Color myGreyBackground = null;
79 @NotNull private Color myForeground = UIUtil.getTableForeground();
81 public LabelPainter(@NotNull VcsLogData data) {
85 public void customizePainter(@NotNull JComponent component,
86 @NotNull Collection<VcsRef> references,
87 @NotNull Color background,
88 @NotNull Color foreground,
91 myBackground = background;
92 myForeground = foreground;
94 FontMetrics metrics = component.getFontMetrics(getReferenceFont());
95 myHeight = metrics.getHeight() + TOP_TEXT_PADDING + BOTTOM_TEXT_PADDING;
97 VcsLogRefManager manager = ReferencePainter.getRefManager(myLogData, references);
98 List<RefGroup> refGroups = manager == null ? ContainerUtil.emptyList() : manager.groupForTable(references);
100 myGreyBackground = calculateGreyBackground(refGroups, background, isSelected);
101 Pair<List<Pair<String, LabelIcon>>, Integer> presentation =
102 calculatePresentation(refGroups, metrics, myHeight, myGreyBackground != null ? myGreyBackground : myBackground, false);
103 if (presentation.second > availableWidth) {
104 presentation = calculatePresentation(refGroups, metrics, myHeight, myGreyBackground != null ? myGreyBackground : myBackground, true);
107 myLabels = presentation.first;
108 myWidth = presentation.second;
112 private static Pair<List<Pair<String, LabelIcon>>, Integer> calculatePresentation(@NotNull List<RefGroup> refGroups,
113 @NotNull FontMetrics fontMetrics,
115 @NotNull Color background,
117 int width = LEFT_PADDING + RIGHT_PADDING;
119 List<Pair<String, LabelIcon>> labels = ContainerUtil.newArrayList();
120 if (refGroups.isEmpty()) return Pair.create(labels, width);
122 for (RefGroup group : refGroups) {
123 if (group.isExpanded()) {
124 for (VcsRef ref : group.getRefs()) {
125 LabelIcon labelIcon = new LabelIcon(height, background, ref.getType().getBackgroundColor());
126 String text = shorten ? shortenRefName(ref.getName()) : ref.getName();
128 labels.add(Pair.create(text, labelIcon));
129 width += labelIcon.getIconWidth() + fontMetrics.stringWidth(text) + MIDDLE_PADDING;
133 LabelIcon labelIcon = new LabelIcon(height, background, getGroupColors(group));
134 String text = shorten ? shortenRefName(group.getName()) : group.getName();
136 labels.add(Pair.create(text, labelIcon));
137 width += labelIcon.getIconWidth() + fontMetrics.stringWidth(text) + MIDDLE_PADDING;
141 return Pair.create(labels, width);
145 private static Color calculateGreyBackground(@NotNull List<RefGroup> refGroups, @NotNull Color background, boolean isSelected) {
146 if (isSelected) return null;
148 boolean paintGreyBackground;
149 for (RefGroup group : refGroups) {
150 if (group.isExpanded()) {
151 paintGreyBackground = ContainerUtil.find(group.getRefs(), ref -> !ref.getName().isEmpty()) != null;
154 paintGreyBackground = !group.getName().isEmpty();
157 if (paintGreyBackground) return ColorUtil.mix(background, BACKGROUND, BALANCE);
164 private static String shortenRefName(@NotNull String refName) {
165 int textLength = refName.length();
166 if (textLength > MAX_LENGTH) {
167 int separatorIndex = refName.indexOf(SEPARATOR);
168 if (separatorIndex > TWO_DOTS.length()) {
169 refName = TWO_DOTS + refName.substring(separatorIndex);
171 return StringUtil.shortenTextWithEllipsis(refName, MAX_LENGTH, 0, THREE_DOTS);
177 public static Color[] getGroupColors(@NotNull RefGroup group) {
178 MultiMap<VcsRefType, VcsRef> referencesByType = ContainerUtil.groupBy(group.getRefs(), VcsRef::getType);
180 if (referencesByType.size() == 1) {
181 Map.Entry<VcsRefType, Collection<VcsRef>> firstItem =
182 ObjectUtils.assertNotNull(ContainerUtil.getFirstItem(referencesByType.entrySet()));
183 boolean multiple = firstItem.getValue().size() > 1;
184 Color color = firstItem.getKey().getBackgroundColor();
185 colors = multiple ? new Color[]{color, color} : new Color[]{color};
188 List<Color> colorsList = ContainerUtil.newArrayList();
189 for (VcsRefType type : referencesByType.keySet()) {
190 if (referencesByType.get(type).size() > 1) {
191 colorsList.add(type.getBackgroundColor());
193 colorsList.add(type.getBackgroundColor());
195 colors = colorsList.toArray(new Color[colorsList.size()]);
200 public void paint(@NotNull Graphics2D g2, int x, int y, int height) {
201 if (myLabels.isEmpty()) return;
203 GraphicsConfig config = GraphicsUtil.setupAAPainting(g2);
204 g2.setFont(getReferenceFont());
205 g2.setStroke(new BasicStroke(1.5f));
207 FontMetrics fontMetrics = g2.getFontMetrics();
208 int baseLine = SimpleColoredComponent.getTextBaseLine(fontMetrics, height);
210 g2.setColor(myBackground);
211 g2.fillRect(x, y, myWidth, height);
213 if (myGreyBackground != null) {
214 g2.setColor(myGreyBackground);
215 g2.fillRect(x, y + baseLine - fontMetrics.getAscent() - TOP_TEXT_PADDING,
216 myWidth - RIGHT_PADDING + LEFT_PADDING,
217 fontMetrics.getHeight() + TOP_TEXT_PADDING + BOTTOM_TEXT_PADDING);
222 for (Pair<String, LabelIcon> label : myLabels) {
223 LabelIcon icon = label.second;
224 String text = label.first;
226 icon.paintIcon(null, g2, x, y + (height - icon.getIconHeight()) / 2);
227 x += icon.getIconWidth();
229 g2.setColor(myForeground);
230 g2.drawString(text, x, y + baseLine);
231 x += fontMetrics.stringWidth(text) + MIDDLE_PADDING;
237 public Dimension getSize() {
238 if (myLabels.isEmpty()) return new Dimension();
239 return new Dimension(myWidth, myHeight);
243 public boolean isLeftAligned() {
248 public Font getReferenceFont() {
249 Font font = RectanglePainter.getFont();
250 return font.deriveFont(font.getSize() - 1f);