summaryrefslogtreecommitdiff
path: root/platform/dvcs/src/com/intellij/dvcs/ui/BranchActionGroupPopup.java
blob: 3b935745a212c44dbed4f57d107f1eaa0377c118 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * Copyright 2000-2013 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.dvcs.ui;

import com.intellij.openapi.actionSystem.ActionGroup;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.impl.SimpleDataContext;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.popup.PopupStep;
import com.intellij.openapi.util.Condition;
import com.intellij.ui.ErrorLabel;
import com.intellij.ui.JBColor;
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.PopupListElementRenderer;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;

/**
 * @author Nadya Zabrodina
 */
public class BranchActionGroupPopup extends PopupFactoryImpl.ActionGroupPopup {
  public BranchActionGroupPopup(@NotNull String title, @NotNull Project project,
                                @NotNull Condition<AnAction> preselectActionCondition, @NotNull ActionGroup actions) {
    super(title, actions, SimpleDataContext.getProjectContext(project), false, false, false, false, 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 static 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.getDisplayableBranchText()));

          if (isSelected) {
            setSelected(myBranchLabel);
          }
          else {
            myBranchLabel.setBackground(getBackground());
            myBranchLabel.setForeground(JBColor.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(), JBColor.WHITE);
        compoundPanel.add(myTextLabel, BorderLayout.CENTER);
        compoundPanel.add(myBranchLabel, BorderLayout.EAST);

        return layoutComponent(compoundPanel);
      }
    };
  }
}