[vcs-log] change RefGroup so that is returned several colors

This should help with proper HEAD label display in the table: even for expanded groups, HEAD reference should not be shown in the list, yet the group still has to have a yellow tag.
Also, SimpleRefGroup class from Mercurial plugin is extracted and moved to the vcs-log impl. This class now can be used from git and mercurial, and it has a proper getColors method moved from LabelPainter.
This commit is contained in:
Julia Beliaeva
2016-12-05 18:19:32 +03:00
parent f8a22f70a5
commit e1469c6356
6 changed files with 100 additions and 104 deletions
@@ -31,8 +31,8 @@ public interface RefGroup {
List<VcsRef> getRefs();
/**
* Returns the background color of this ref group, which will be used to paint it on the Branches panel.
* Returns the colors of this ref group, which will be used to paint it in the table.
*/
@NotNull
Color getBgColor();
List<Color> getColors();
}
@@ -0,0 +1,83 @@
/*
* Copyright 2000-2016 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.vcs.log.impl;
import com.intellij.util.ObjectUtils;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.MultiMap;
import com.intellij.vcs.log.RefGroup;
import com.intellij.vcs.log.VcsRef;
import com.intellij.vcs.log.VcsRefType;
import org.jetbrains.annotations.NotNull;
import java.awt.*;
import java.util.*;
import java.util.List;
public class SimpleRefGroup implements RefGroup {
@NotNull private final String myName;
@NotNull private final List<VcsRef> myRefs;
public SimpleRefGroup(@NotNull String name, @NotNull List<VcsRef> refs) {
myName = name;
myRefs = refs;
}
@Override
public boolean isExpanded() {
return false;
}
@NotNull
@Override
public String getName() {
return myName;
}
@NotNull
@Override
public List<VcsRef> getRefs() {
return myRefs;
}
@NotNull
@Override
public List<Color> getColors() {
return getColors(myRefs);
}
@NotNull
public static List<Color> getColors(@NotNull Collection<VcsRef> refs) {
MultiMap<VcsRefType, VcsRef> referencesByType = ContainerUtil.groupBy(refs, VcsRef::getType);
if (referencesByType.size() == 1) {
Map.Entry<VcsRefType, Collection<VcsRef>> firstItem =
ObjectUtils.assertNotNull(ContainerUtil.getFirstItem(referencesByType.entrySet()));
boolean multiple = firstItem.getValue().size() > 1;
Color color = firstItem.getKey().getBackgroundColor();
return multiple ? Arrays.asList(color, color) : Collections.singletonList(color);
}
else {
List<Color> colorsList = ContainerUtil.newArrayList();
for (VcsRefType type : referencesByType.keySet()) {
if (referencesByType.get(type).size() > 1) {
colorsList.add(type.getBackgroundColor());
}
colorsList.add(type.getBackgroundColor());
}
return colorsList;
}
}
}
@@ -52,7 +52,7 @@ public class SingletonRefGroup implements RefGroup {
@NotNull
@Override
public Color getBgColor() {
return myRef.getType().getBackgroundColor();
public List<Color> getColors() {
return Collections.singletonList(myRef.getType().getBackgroundColor());
}
}
@@ -37,16 +37,13 @@ import com.intellij.openapi.util.text.StringUtil;
import com.intellij.ui.ColorUtil;
import com.intellij.ui.JBColor;
import com.intellij.ui.SimpleColoredComponent;
import com.intellij.util.ObjectUtils;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.MultiMap;
import com.intellij.util.ui.GraphicsUtil;
import com.intellij.util.ui.JBUI;
import com.intellij.util.ui.UIUtil;
import com.intellij.vcs.log.RefGroup;
import com.intellij.vcs.log.VcsLogRefManager;
import com.intellij.vcs.log.VcsRef;
import com.intellij.vcs.log.VcsRefType;
import com.intellij.vcs.log.data.VcsLogData;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -54,9 +51,12 @@ import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.awt.*;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import static com.intellij.vcs.log.impl.SimpleRefGroup.getColors;
public class LabelPainter implements ReferencePainter {
public static final int TOP_TEXT_PADDING = JBUI.scale(1);
public static final int BOTTOM_TEXT_PADDING = JBUI.scale(2);
@@ -131,7 +131,8 @@ public class LabelPainter implements ReferencePainter {
}
}
else {
LabelIcon labelIcon = new LabelIcon(height, background, getGroupColors(group));
List<Color> colors = group.getColors();
LabelIcon labelIcon = new LabelIcon(height, background, colors.toArray(new Color[colors.size()]));
width += labelIcon.getIconWidth() + MIDDLE_PADDING;
String text = shortenRefName(group.getName(), fontMetrics, availableWidth - width);
@@ -186,30 +187,6 @@ public class LabelPainter implements ReferencePainter {
return refName;
}
@NotNull
public static Color[] getGroupColors(@NotNull RefGroup group) {
MultiMap<VcsRefType, VcsRef> referencesByType = ContainerUtil.groupBy(group.getRefs(), VcsRef::getType);
Color[] colors;
if (referencesByType.size() == 1) {
Map.Entry<VcsRefType, Collection<VcsRef>> firstItem =
ObjectUtils.assertNotNull(ContainerUtil.getFirstItem(referencesByType.entrySet()));
boolean multiple = firstItem.getValue().size() > 1;
Color color = firstItem.getKey().getBackgroundColor();
colors = multiple ? new Color[]{color, color} : new Color[]{color};
}
else {
List<Color> colorsList = ContainerUtil.newArrayList();
for (VcsRefType type : referencesByType.keySet()) {
if (referencesByType.get(type).size() > 1) {
colorsList.add(type.getBackgroundColor());
}
colorsList.add(type.getBackgroundColor());
}
colors = colorsList.toArray(new Color[colorsList.size()]);
}
return colors;
}
public void paint(@NotNull Graphics2D g2, int x, int y, int height) {
if (myLabels.isEmpty()) return;
@@ -10,6 +10,7 @@ import com.intellij.util.ObjectUtils;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.MultiMap;
import com.intellij.vcs.log.*;
import com.intellij.vcs.log.impl.SimpleRefGroup;
import com.intellij.vcs.log.impl.SingletonRefGroup;
import com.intellij.vcs.log.impl.VcsLogUtil;
import git4idea.GitBranch;
@@ -155,7 +156,7 @@ public class GitRefManager implements VcsLogRefManager {
}
else {
if (!repository.isOnBranch()) {
groups.add(new TableRefGroup("!", Collections.singletonList(head)));
groups.add(new SimpleRefGroup("!", Collections.singletonList(head)));
sortedReferences = sortedReferences.subList(1, sortedReferences.size());
}
}
@@ -177,7 +178,7 @@ public class GitRefManager implements VcsLogRefManager {
}
}
groups.add(new TableRefGroup(name, sortedReferences));
groups.add(new SimpleRefGroup(name, sortedReferences));
return groups;
}
@@ -358,8 +359,8 @@ public class GitRefManager implements VcsLogRefManager {
@NotNull
@Override
public Color getBgColor() {
return VcsLogStandardColors.Refs.TIP;
public List<Color> getColors() {
return Collections.singletonList(VcsLogStandardColors.Refs.TIP);
}
}
@@ -391,41 +392,8 @@ public class GitRefManager implements VcsLogRefManager {
@NotNull
@Override
public Color getBgColor() {
return VcsLogStandardColors.Refs.BRANCH_REF;
}
}
private static class TableRefGroup implements RefGroup {
@NotNull private final String myName;
@NotNull private final List<VcsRef> myRefs;
private TableRefGroup(@NotNull String name, @NotNull List<VcsRef> refs) {
myName = name;
myRefs = refs;
}
@Override
public boolean isExpanded() {
return false;
}
@NotNull
@Override
public String getName() {
return myName;
}
@NotNull
@Override
public List<VcsRef> getRefs() {
return myRefs;
}
@NotNull
@Override
public Color getBgColor() {
return myRefs.get(0).getType().getBackgroundColor();
public List<Color> getColors() {
return Collections.singletonList(VcsLogStandardColors.Refs.BRANCH_REF);
}
}
@@ -20,6 +20,7 @@ import com.intellij.util.Function;
import com.intellij.util.ObjectUtils;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.vcs.log.*;
import com.intellij.vcs.log.impl.SimpleRefGroup;
import com.intellij.vcs.log.impl.SingletonRefGroup;
import com.intellij.vcs.log.impl.VcsLogUtil;
import org.jetbrains.annotations.NotNull;
@@ -193,37 +194,4 @@ public class HgRefManager implements VcsLogRefManager {
return Objects.hash(myName, myIsBranch);
}
}
private static class SimpleRefGroup implements RefGroup {
@NotNull private final String myName;
@NotNull private final List<VcsRef> myRefs;
private SimpleRefGroup(@NotNull String name, @NotNull List<VcsRef> refs) {
myName = name;
myRefs = refs;
}
@Override
public boolean isExpanded() {
return false;
}
@NotNull
@Override
public String getName() {
return myName;
}
@NotNull
@Override
public List<VcsRef> getRefs() {
return myRefs;
}
@NotNull
@Override
public Color getBgColor() {
return myRefs.get(0).getType().getBackgroundColor();
}
}
}