Fix temp path calculation
[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     panel.add(new JLabel(DiffBundle.message("comparison.ignore.whitespace.acton.name")), BorderLayout.WEST);
53     panel.add(super.createCustomComponent(presentation), BorderLayout.CENTER);
54     return panel;
55   }
56
57   @NotNull
58   protected DefaultActionGroup createPopupActionGroup(JComponent button) {
59     DefaultActionGroup actionGroup = new DefaultActionGroup();
60     for (ComparisonPolicy comparisonPolicy : ourActionOrder) {
61       actionGroup.add(myActions.get(comparisonPolicy));
62     }
63     return actionGroup;
64   }
65
66   public void update(AnActionEvent e) {
67     Presentation presentation = e.getPresentation();
68     DiffPanelEx diffPanel = DiffPanelImpl.fromDataContext(e.getDataContext());
69     if (diffPanel != null && diffPanel.getComponent().isDisplayable()) {
70       AnAction actoin = myActions.get(diffPanel.getComparisonPolicy());
71       Presentation templatePresentation = actoin.getTemplatePresentation();
72       presentation.setIcon(templatePresentation.getIcon());
73       presentation.setText(templatePresentation.getText());
74       presentation.setEnabled(true);
75     } else {
76       presentation.setIcon(null);
77       presentation.setText(DiffBundle.message("ignore.whitespace.action.not.avaliable.action.name"));
78       presentation.setEnabled(false);
79     }
80   }
81
82   private static class IgnoringPolicyAction extends AnAction {
83     private final ComparisonPolicy myPolicy;
84
85     public IgnoringPolicyAction(String text, ComparisonPolicy policy) {
86       super(text);
87       myPolicy = policy;
88     }
89
90     public void actionPerformed(AnActionEvent e) {
91       final DiffPanelImpl diffPanel = DiffPanelImpl.fromDataContext(e.getDataContext());
92       if (diffPanel != null) {
93         diffPanel.setComparisonPolicy(myPolicy);
94       }
95     }
96   }
97 }