diff: move HighlightingMode logic to TextCompareProcessor
[idea/community.git] / platform / platform-impl / src / com / intellij / openapi / diff / actions / IgnoreWhiteSpacesAction.java
1 /*
2  * Copyright 2000-2012 JetBrains s.r.o.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.intellij.openapi.diff.actions;
17
18 import com.intellij.openapi.actionSystem.AnAction;
19 import com.intellij.openapi.actionSystem.AnActionEvent;
20 import com.intellij.openapi.actionSystem.DefaultActionGroup;
21 import com.intellij.openapi.actionSystem.Presentation;
22 import com.intellij.openapi.actionSystem.ex.ComboBoxAction;
23 import com.intellij.openapi.diff.DiffBundle;
24 import com.intellij.openapi.diff.ex.DiffPanelEx;
25 import com.intellij.openapi.diff.impl.ComparisonPolicy;
26 import com.intellij.openapi.diff.impl.DiffPanelImpl;
27 import com.intellij.openapi.project.DumbAware;
28 import com.intellij.openapi.project.DumbAwareAction;
29 import com.intellij.util.containers.HashMap;
30 import org.jetbrains.annotations.NotNull;
31
32 import javax.swing.*;
33 import java.awt.*;
34 import java.util.Map;
35
36 public class IgnoreWhiteSpacesAction extends ComboBoxAction implements DumbAware {
37   private final Map<ComparisonPolicy, AnAction> myActions = new HashMap<ComparisonPolicy, AnAction>();
38   private static final ComparisonPolicy[] ourActionOrder = new ComparisonPolicy[]{
39     ComparisonPolicy.DEFAULT,
40     ComparisonPolicy.TRIM_SPACE,
41     ComparisonPolicy.IGNORE_SPACE};
42
43   public IgnoreWhiteSpacesAction() {
44     myActions.put(ComparisonPolicy.DEFAULT, new IgnoringPolicyAction(DiffBundle.message("diff.acton.ignore.whitespace.policy.do.not.ignore"), ComparisonPolicy.DEFAULT));
45     myActions.put(ComparisonPolicy.TRIM_SPACE, new IgnoringPolicyAction(DiffBundle.message("diff.acton.ignore.whitespace.policy.leading.and.trailing"), ComparisonPolicy.TRIM_SPACE));
46     myActions.put(ComparisonPolicy.IGNORE_SPACE, new IgnoringPolicyAction(DiffBundle.message("diff.acton.ignore.whitespace.policy.all"), ComparisonPolicy.IGNORE_SPACE));
47   }
48
49   @Override
50   public JComponent createCustomComponent(final Presentation presentation) {
51     JPanel panel = new JPanel(new BorderLayout());
52     final JLabel label = new JLabel(DiffBundle.message("ignore.whitespace.acton.name"));
53     label.setBorder(BorderFactory.createEmptyBorder(0, 4, 0, 4));
54     panel.add(label, BorderLayout.WEST);
55     panel.add(super.createCustomComponent(presentation), BorderLayout.CENTER);
56     return panel;
57   }
58
59   @NotNull
60   protected DefaultActionGroup createPopupActionGroup(JComponent button) {
61     DefaultActionGroup actionGroup = new DefaultActionGroup();
62     for (ComparisonPolicy comparisonPolicy : ourActionOrder) {
63       actionGroup.add(myActions.get(comparisonPolicy));
64     }
65     return actionGroup;
66   }
67
68   public void update(AnActionEvent e) {
69     super.update(e);
70     Presentation presentation = e.getPresentation();
71     DiffPanelEx diffPanel = DiffPanelImpl.fromDataContext(e.getDataContext());
72     if (diffPanel != null && diffPanel.getComponent().isDisplayable()) {
73       AnAction action = myActions.get(diffPanel.getComparisonPolicy());
74       Presentation templatePresentation = action.getTemplatePresentation();
75       presentation.setIcon(templatePresentation.getIcon());
76       presentation.setText(templatePresentation.getText());
77       presentation.setEnabled(true);
78     } else {
79       presentation.setIcon(null);
80       presentation.setText(DiffBundle.message("ignore.whitespace.action.not.available.action.name"));
81       presentation.setEnabled(false);
82     }
83   }
84
85   private static class IgnoringPolicyAction extends DumbAwareAction {
86     private final ComparisonPolicy myPolicy;
87
88     public IgnoringPolicyAction(String text, ComparisonPolicy policy) {
89       super(text);
90       myPolicy = policy;
91     }
92
93     public void actionPerformed(AnActionEvent e) {
94       final DiffPanelImpl diffPanel = DiffPanelImpl.fromDataContext(e.getDataContext());
95       if (diffPanel != null) {
96         diffPanel.setComparisonPolicy(myPolicy);
97       }
98     }
99   }
100 }