From e1469c63566f4ce006dcae139b603aaabe99ee7c Mon Sep 17 00:00:00 2001 From: Julia Beliaeva Date: Mon, 14 Nov 2016 15:52:19 +0300 Subject: [PATCH] [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. --- .../src/com/intellij/vcs/log/RefGroup.java | 4 +- .../intellij/vcs/log/impl/SimpleRefGroup.java | 83 +++++++++++++++++++ .../vcs/log/impl/SingletonRefGroup.java | 4 +- .../vcs/log/ui/render/LabelPainter.java | 33 ++------ .../src/git4idea/log/GitRefManager.java | 46 ++-------- .../org/zmlx/hg4idea/log/HgRefManager.java | 34 +------- 6 files changed, 100 insertions(+), 104 deletions(-) create mode 100644 platform/vcs-log/impl/src/com/intellij/vcs/log/impl/SimpleRefGroup.java diff --git a/platform/vcs-log/api/src/com/intellij/vcs/log/RefGroup.java b/platform/vcs-log/api/src/com/intellij/vcs/log/RefGroup.java index 3f54510a1060..143cefe413f0 100644 --- a/platform/vcs-log/api/src/com/intellij/vcs/log/RefGroup.java +++ b/platform/vcs-log/api/src/com/intellij/vcs/log/RefGroup.java @@ -31,8 +31,8 @@ public interface RefGroup { List 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 getColors(); } diff --git a/platform/vcs-log/impl/src/com/intellij/vcs/log/impl/SimpleRefGroup.java b/platform/vcs-log/impl/src/com/intellij/vcs/log/impl/SimpleRefGroup.java new file mode 100644 index 000000000000..38dc14b96af4 --- /dev/null +++ b/platform/vcs-log/impl/src/com/intellij/vcs/log/impl/SimpleRefGroup.java @@ -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 myRefs; + + public SimpleRefGroup(@NotNull String name, @NotNull List refs) { + myName = name; + myRefs = refs; + } + + @Override + public boolean isExpanded() { + return false; + } + + @NotNull + @Override + public String getName() { + return myName; + } + + @NotNull + @Override + public List getRefs() { + return myRefs; + } + + @NotNull + @Override + public List getColors() { + return getColors(myRefs); + } + + @NotNull + public static List getColors(@NotNull Collection refs) { + MultiMap referencesByType = ContainerUtil.groupBy(refs, VcsRef::getType); + if (referencesByType.size() == 1) { + Map.Entry> 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 colorsList = ContainerUtil.newArrayList(); + for (VcsRefType type : referencesByType.keySet()) { + if (referencesByType.get(type).size() > 1) { + colorsList.add(type.getBackgroundColor()); + } + colorsList.add(type.getBackgroundColor()); + } + return colorsList; + } + } +} diff --git a/platform/vcs-log/impl/src/com/intellij/vcs/log/impl/SingletonRefGroup.java b/platform/vcs-log/impl/src/com/intellij/vcs/log/impl/SingletonRefGroup.java index 9a7738aa6829..5fa6e35c09b5 100644 --- a/platform/vcs-log/impl/src/com/intellij/vcs/log/impl/SingletonRefGroup.java +++ b/platform/vcs-log/impl/src/com/intellij/vcs/log/impl/SingletonRefGroup.java @@ -52,7 +52,7 @@ public class SingletonRefGroup implements RefGroup { @NotNull @Override - public Color getBgColor() { - return myRef.getType().getBackgroundColor(); + public List getColors() { + return Collections.singletonList(myRef.getType().getBackgroundColor()); } } diff --git a/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/render/LabelPainter.java b/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/render/LabelPainter.java index 50c193b806fa..aab368a32118 100644 --- a/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/render/LabelPainter.java +++ b/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/render/LabelPainter.java @@ -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 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 referencesByType = ContainerUtil.groupBy(group.getRefs(), VcsRef::getType); - Color[] colors; - if (referencesByType.size() == 1) { - Map.Entry> 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 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; diff --git a/plugins/git4idea/src/git4idea/log/GitRefManager.java b/plugins/git4idea/src/git4idea/log/GitRefManager.java index 0179e77cd463..fd9e8e8af0d4 100644 --- a/plugins/git4idea/src/git4idea/log/GitRefManager.java +++ b/plugins/git4idea/src/git4idea/log/GitRefManager.java @@ -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 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 myRefs; - - private TableRefGroup(@NotNull String name, @NotNull List refs) { - myName = name; - myRefs = refs; - } - - @Override - public boolean isExpanded() { - return false; - } - - @NotNull - @Override - public String getName() { - return myName; - } - - @NotNull - @Override - public List getRefs() { - return myRefs; - } - - @NotNull - @Override - public Color getBgColor() { - return myRefs.get(0).getType().getBackgroundColor(); + public List getColors() { + return Collections.singletonList(VcsLogStandardColors.Refs.BRANCH_REF); } } diff --git a/plugins/hg4idea/src/org/zmlx/hg4idea/log/HgRefManager.java b/plugins/hg4idea/src/org/zmlx/hg4idea/log/HgRefManager.java index ae96baf9b2cd..da53654bc4b8 100644 --- a/plugins/hg4idea/src/org/zmlx/hg4idea/log/HgRefManager.java +++ b/plugins/hg4idea/src/org/zmlx/hg4idea/log/HgRefManager.java @@ -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 myRefs; - - private SimpleRefGroup(@NotNull String name, @NotNull List refs) { - myName = name; - myRefs = refs; - } - - @Override - public boolean isExpanded() { - return false; - } - - @NotNull - @Override - public String getName() { - return myName; - } - - @NotNull - @Override - public List getRefs() { - return myRefs; - } - - @NotNull - @Override - public Color getBgColor() { - return myRefs.get(0).getType().getBackgroundColor(); - } - } }