Merge branch 'master' of git.labs.intellij.net:idea/community
[idea/community.git] / platform / platform-impl / src / com / intellij / openapi / diff / actions / IgnoreWhiteSpacesAction.java
1 /*
2  * Copyright 2000-2009 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.util.containers.HashMap;
29 import org.jetbrains.annotations.NotNull;
30
31 import javax.swing.*;
32 import java.awt.*;
33 import java.util.Map;
34
35 public class IgnoreWhiteSpacesAction extends ComboBoxAction implements DumbAware {
36   private final Map<ComparisonPolicy, AnAction> myActions = new HashMap<ComparisonPolicy, AnAction>();
37   private static final ComparisonPolicy[] ourActionOrder = new ComparisonPolicy[]{
38     ComparisonPolicy.DEFAULT,
39     ComparisonPolicy.TRIM_SPACE,
40     ComparisonPolicy.IGNORE_SPACE};
41
42   public IgnoreWhiteSpacesAction() {
43     myActions.put(ComparisonPolicy.DEFAULT, new IgnoringPolicyAction(DiffBundle.message("diff.acton.ignore.qhitespace.policy.do.not.ignore"), ComparisonPolicy.DEFAULT));
44     myActions.put(ComparisonPolicy.TRIM_SPACE, new IgnoringPolicyAction(
45       DiffBundle.message("diff.acton.ignore.qhitespace.policy.leading.and.trailing"), ComparisonPolicy.TRIM_SPACE));
46     myActions.put(ComparisonPolicy.IGNORE_SPACE, new IgnoringPolicyAction(DiffBundle.message("diff.acton.ignore.qhitespace.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("comparison.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     Presentation presentation = e.getPresentation();
70     DiffPanelEx diffPanel = DiffPanelImpl.fromDataContext(e.getDataContext());
71     if (diffPanel != null && diffPanel.getComponent().isDisplayable()) {
72       AnAction action = myActions.get(diffPanel.getComparisonPolicy());
73       Presentation templatePresentation = action.getTemplatePresentation();
74       presentation.setIcon(templatePresentation.getIcon());
75       presentation.setText(templatePresentation.getText());
76       presentation.setEnabled(true);
77     } else {
78       presentation.setIcon(null);
79       presentation.setText(DiffBundle.message("ignore.whitespace.action.not.avaliable.action.name"));
80       presentation.setEnabled(false);
81     }
82   }
83
84   private static class IgnoringPolicyAction extends AnAction {
85     private final ComparisonPolicy myPolicy;
86
87     public IgnoringPolicyAction(String text, ComparisonPolicy policy) {
88       super(text);
89       myPolicy = policy;
90     }
91
92     public void actionPerformed(AnActionEvent e) {
93       final DiffPanelImpl diffPanel = DiffPanelImpl.fromDataContext(e.getDataContext());
94       if (diffPanel != null) {
95         diffPanel.setComparisonPolicy(myPolicy);
96       }
97     }
98   }
99 }