IDEA-89505 Git branches: for the multi-repository case display current branch of each repository more evidently

Draw branch name near repository name, aligned to the right and grayed.

Override getListElementRenderer & createItemComponent: create a second label to display the branch name, and align it to the right.
This commit is contained in:
Kirill Likhodedov
2012-10-19 15:42:05 +04:00
parent d497e7137a
commit 1993ff8239
3 changed files with 188 additions and 101 deletions
@@ -63,24 +63,33 @@ public abstract class GroupedElementsRenderer {
myComponent.setBorder(getSelectedBorder());
setSelected(myComponent);
setSelected(myTextLabel);
if (UIUtil.isUnderNimbusLookAndFeel()) {
myTextLabel.setOpaque(true);
}
}
else {
myComponent.setBorder(getBorder());
setDeselected(myComponent);
setDeselected(myTextLabel);
if (UIUtil.isUnderGTKLookAndFeel() || UIUtil.isUnderNimbusLookAndFeel()) {
myTextLabel.setOpaque(false);
}
}
adjustOpacity(myTextLabel, isSelected);
myRendererComponent.setPrefereedWidth(preferredForcedWidth);
return myRendererComponent;
}
protected static void adjustOpacity(JComponent component, boolean selected) {
if (selected) {
if (UIUtil.isUnderNimbusLookAndFeel()) {
component.setOpaque(true);
}
}
else {
if (UIUtil.isUnderGTKLookAndFeel() || UIUtil.isUnderNimbusLookAndFeel()) {
component.setOpaque(false);
}
}
}
protected final void setSelected(JComponent aComponent) {
aComponent.setBackground(getSelectionBackground());
aComponent.setForeground(getSelectionForeground());
@@ -1,79 +1,83 @@
/*
* Copyright 2000-2009 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.ui.popup.list;
import com.intellij.openapi.ui.popup.ListItemDescriptor;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.ui.ErrorLabel;
import com.intellij.ui.GroupedElementsRenderer;
import com.intellij.ui.components.panels.OpaquePanel;
import javax.swing.*;
import java.awt.*;
public class GroupedItemsListRenderer extends GroupedElementsRenderer.List implements ListCellRenderer {
protected ListItemDescriptor myDescriptor;
protected JLabel myNextStepLabel;
public JLabel getNextStepLabel() {
return myNextStepLabel;
}
public GroupedItemsListRenderer(ListItemDescriptor descriptor) {
myDescriptor = descriptor;
}
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
String caption = myDescriptor.getCaptionAboveOf(value);
boolean hasSeparator = myDescriptor.hasSeparatorAboveOf(value);
if (index == 0 && StringUtil.isEmptyOrSpaces(caption)) hasSeparator = false;
final JComponent result = configureComponent(myDescriptor.getTextFor(value), myDescriptor.getTooltipFor(value),
myDescriptor.getIconFor(value), myDescriptor.getIconFor(value), isSelected, hasSeparator,
caption, -1);
customizeComponent(list, value, isSelected);
return result;
}
protected JComponent createItemComponent() {
JPanel result = new OpaquePanel(new BorderLayout(4, 4), Color.white);
myTextLabel = new ErrorLabel();
myTextLabel.setOpaque(true);
myTextLabel.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
myNextStepLabel = new JLabel();
myNextStepLabel.setOpaque(true);
result.add(myTextLabel, BorderLayout.CENTER);
result.add(myNextStepLabel, BorderLayout.EAST);
result.setBorder(getDefaultItemComponentBorder());
return result;
}
protected void customizeComponent(JList list, Object value, boolean isSelected) {
}
}
/*
* Copyright 2000-2009 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.ui.popup.list;
import com.intellij.openapi.ui.popup.ListItemDescriptor;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.ui.ErrorLabel;
import com.intellij.ui.GroupedElementsRenderer;
import com.intellij.ui.components.panels.OpaquePanel;
import javax.swing.*;
import java.awt.*;
public class GroupedItemsListRenderer extends GroupedElementsRenderer.List implements ListCellRenderer {
protected ListItemDescriptor myDescriptor;
protected JLabel myNextStepLabel;
public JLabel getNextStepLabel() {
return myNextStepLabel;
}
public GroupedItemsListRenderer(ListItemDescriptor descriptor) {
myDescriptor = descriptor;
}
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
String caption = myDescriptor.getCaptionAboveOf(value);
boolean hasSeparator = myDescriptor.hasSeparatorAboveOf(value);
if (index == 0 && StringUtil.isEmptyOrSpaces(caption)) hasSeparator = false;
final JComponent result = configureComponent(myDescriptor.getTextFor(value), myDescriptor.getTooltipFor(value),
myDescriptor.getIconFor(value), myDescriptor.getIconFor(value), isSelected, hasSeparator,
caption, -1);
customizeComponent(list, value, isSelected);
return result;
}
protected JComponent createItemComponent() {
myTextLabel = new ErrorLabel();
myTextLabel.setOpaque(true);
myTextLabel.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
return layoutComponent(myTextLabel);
}
protected final JComponent layoutComponent(JComponent middleItemComponent) {
JPanel result = new OpaquePanel(new BorderLayout(4, 4), Color.white);
myNextStepLabel = new JLabel();
myNextStepLabel.setOpaque(true);
result.add(middleItemComponent, BorderLayout.CENTER);
result.add(myNextStepLabel, BorderLayout.EAST);
result.setBorder(getDefaultItemComponentBorder());
return result;
}
protected void customizeComponent(JList list, Object value, boolean isSelected) {
}
}
@@ -28,10 +28,14 @@ import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.popup.ListPopup;
import com.intellij.openapi.ui.popup.PopupStep;
import com.intellij.openapi.util.Condition;
import com.intellij.ui.ErrorLabel;
import com.intellij.ui.components.panels.OpaquePanel;
import com.intellij.ui.popup.PopupFactoryImpl;
import com.intellij.ui.popup.WizardPopup;
import com.intellij.ui.popup.list.ListPopupImpl;
import com.intellij.ui.popup.list.PopupListElementRenderer;
import com.intellij.util.PlatformIcons;
import com.intellij.util.ui.UIUtil;
import git4idea.GitUtil;
import git4idea.GitVcs;
import git4idea.config.GitVcsSettings;
@@ -43,6 +47,7 @@ import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import java.awt.*;
import java.util.List;
/**
@@ -113,22 +118,8 @@ class GitBranchPopup {
return false;
}
};
myPopup = new PopupFactoryImpl.ActionGroupPopup(
title, createActions(),
SimpleDataContext.getProjectContext(project),
false, false, false, true, null, -1, preselectActionCondition, null) {
@Override
protected WizardPopup createPopup(WizardPopup parent, PopupStep step, Object parentValue) {
WizardPopup popup = super.createPopup(parent, step, parentValue);
if (parentValue instanceof PopupFactoryImpl.ActionItem) {
AnAction action = ((PopupFactoryImpl.ActionItem)parentValue).getAction();
if (action instanceof RootAction) {
popup.setAdText(((RootAction)action).getCaption());
}
}
return popup;
}
};
myPopup = new GitBranchActionGroupPopup(title, project, preselectActionCondition);
initBranchSyncPolicyIfNotInitialized();
setCurrentBranchInfo();
@@ -287,6 +278,89 @@ class GitBranchPopup {
return "Current branch in " + GitUIUtil.getShortRepositoryName(myRepository) + ": " +
GitBranchUiUtil.getDisplayableBranchText(myRepository);
}
@NotNull
public String getBranch() {
return GitBranchUiUtil.getBranchNameOrRev(myRepository);
}
}
private class GitBranchActionGroupPopup extends PopupFactoryImpl.ActionGroupPopup {
public GitBranchActionGroupPopup(@NotNull String title, @NotNull Project project,
@NotNull Condition<AnAction> preselectActionCondition) {
super(title, GitBranchPopup.this.createActions(), SimpleDataContext.getProjectContext(project), false, false, false, true, null, -1,
preselectActionCondition, null);
}
@Override
protected WizardPopup createPopup(WizardPopup parent, PopupStep step, Object parentValue) {
WizardPopup popup = super.createPopup(parent, step, parentValue);
RootAction rootAction = getRootAction(parentValue);
if (rootAction != null) {
popup.setAdText((rootAction).getCaption());
}
return popup;
}
@Nullable
private RootAction getRootAction(Object value) {
if (value instanceof PopupFactoryImpl.ActionItem) {
AnAction action = ((PopupFactoryImpl.ActionItem)value).getAction();
if (action instanceof RootAction) {
return (RootAction)action;
}
}
return null;
}
@Override
protected ListCellRenderer getListElementRenderer() {
return new PopupListElementRenderer(this) {
private ErrorLabel myBranchLabel;
@Override
protected void customizeComponent(JList list, Object value, boolean isSelected) {
super.customizeComponent(list, value, isSelected);
RootAction rootAction = getRootAction(value);
if (rootAction != null) {
myBranchLabel.setVisible(true);
myBranchLabel.setText(String.format("[%s]", rootAction.getBranch()));
if (isSelected) {
setSelected(myBranchLabel);
}
else {
myBranchLabel.setBackground(getBackground());
myBranchLabel.setForeground(Color.gray); // different foreground than for other elements
}
adjustOpacity(myBranchLabel, isSelected);
}
else {
myBranchLabel.setVisible(false);
}
}
@Override
protected JComponent createItemComponent() {
myTextLabel = new ErrorLabel();
myTextLabel.setOpaque(true);
myTextLabel.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
myBranchLabel = new ErrorLabel();
myBranchLabel.setOpaque(true);
myBranchLabel.setBorder(BorderFactory.createEmptyBorder(1, UIUtil.DEFAULT_HGAP, 1, 1));
JPanel compoundPanel = new OpaquePanel(new BorderLayout(), Color.white);
compoundPanel.add(myTextLabel, BorderLayout.CENTER);
compoundPanel.add(myBranchLabel, BorderLayout.EAST);
return layoutComponent(compoundPanel);
}
};
}
}
}