1 package com.intellij.openapi.diff.actions;
3 import com.intellij.openapi.actionSystem.*;
4 import com.intellij.openapi.actionSystem.ex.ComboBoxAction;
5 import com.intellij.openapi.diff.DiffBundle;
6 import com.intellij.openapi.diff.ex.DiffPanelEx;
7 import com.intellij.openapi.diff.impl.DiffPanelImpl;
8 import com.intellij.openapi.project.DumbAware;
9 import com.intellij.openapi.project.DumbAwareAction;
10 import com.intellij.util.containers.HashMap;
11 import org.jetbrains.annotations.NotNull;
12 import org.jetbrains.annotations.Nullable;
18 public abstract class DiffPanelComboBoxAction<T> extends ComboBoxAction implements DumbAware {
19 @NotNull private final Map<T, AnAction> myActions = new HashMap<T, AnAction>();
20 @NotNull private final T[] myActionOrder;
22 protected DiffPanelComboBoxAction(@NotNull T[] actionOrder) {
23 myActionOrder = actionOrder;
27 protected abstract String getActionName();
30 protected abstract T getCurrentOption(@NotNull DiffPanelEx diffPanel);
33 private static DiffPanelEx getDiffPanel(@NotNull DataContext context) {
34 return DiffPanelImpl.fromDataContext(context);
37 protected void addAction(T key, @NotNull AnAction action) {
38 myActions.put(key, action);
42 public JComponent createCustomComponent(final Presentation presentation) {
43 JPanel panel = new JPanel(new BorderLayout());
44 final JLabel label = new JLabel(getActionName());
45 label.setBorder(BorderFactory.createEmptyBorder(0, 4, 0, 4));
46 panel.add(label, BorderLayout.WEST);
47 panel.add(super.createCustomComponent(presentation), BorderLayout.CENTER);
53 protected DefaultActionGroup createPopupActionGroup(JComponent button) {
54 DefaultActionGroup actionGroup = new DefaultActionGroup();
55 for (T option : myActionOrder) {
56 actionGroup.add(myActions.get(option));
62 public void update(AnActionEvent e) {
64 Presentation presentation = e.getPresentation();
65 DiffPanelEx diffPanel = getDiffPanel(e.getDataContext());
66 if (diffPanel != null && diffPanel.getComponent().isDisplayable()) {
67 AnAction action = myActions.get(getCurrentOption(diffPanel));
68 Presentation templatePresentation = action.getTemplatePresentation();
69 presentation.setIcon(templatePresentation.getIcon());
70 presentation.setText(templatePresentation.getText());
71 presentation.setEnabled(true);
74 presentation.setIcon(null);
75 presentation.setText(DiffBundle.message("diff.panel.combo.box.action.not.available.action.name"));
76 presentation.setEnabled(false);
80 protected static abstract class DiffPanelAction extends DumbAwareAction {
81 public DiffPanelAction(@NotNull String text) {
85 public void actionPerformed(AnActionEvent e) {
86 final DiffPanelEx diffPanel = getDiffPanel(e.getDataContext());
87 if (diffPanel != null) {
92 protected abstract void perform(@NotNull DiffPanelEx diffPanel);