summaryrefslogtreecommitdiff
path: root/platform/lang-impl/src/com/intellij/profile/codeInspection/ui/AdvancedSettingsAction.java
blob: b0746769d83cb65ac36f830aedd3f8883fbeabf1 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
 * Copyright 2000-2014 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.profile.codeInspection.ui;

import com.intellij.codeInspection.ex.InspectionProfileImpl;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.application.ApplicationNamesInfo;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.ui.popup.PopupStep;
import com.intellij.openapi.ui.popup.util.BaseListPopupStep;
import com.intellij.profile.codeInspection.ui.inspectionsTree.InspectionConfigTreeNode;
import com.intellij.ui.LayeredIcon;
import com.intellij.ui.awt.RelativePoint;
import com.intellij.ui.components.JBLabel;
import com.intellij.ui.popup.list.ListPopupImpl;
import com.intellij.util.Consumer;
import com.intellij.util.PlatformIcons;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.ui.EmptyIcon;
import com.intellij.util.ui.UIUtil;

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

/**
 * @author Dmitry Batkovich
 */
public abstract class AdvancedSettingsAction extends AnAction {
  private final int myCheckBoxIndent;
  private Project myProject;
  private InspectionConfigTreeNode myRoot;

  public AdvancedSettingsAction(final Project project, InspectionConfigTreeNode root) {
    super("Advanced Settings");
    getTemplatePresentation().setIcon(AllIcons.General.Gear);
    myProject = project;
    myRoot = root;
    myCheckBoxIndent = calculateCheckBoxIndent();
  }

  @Override
  public void update(AnActionEvent e) {
    super.update(e);
    final InspectionProfileImpl inspectionProfile = getInspectionProfile();
    final Icon icon = AllIcons.General.Gear;
    e.getPresentation().setIcon(
      (inspectionProfile != null && inspectionProfile.isProfileLocked()) ? LayeredIcon.create(icon, PlatformIcons.LOCKED_ICON) : icon);
  }

  @Override
  public void actionPerformed(AnActionEvent e) {
    final ListPopupImpl actionGroupPopup = (ListPopupImpl)JBPopupFactory.getInstance().createListPopup(
      new BaseListPopupStep<MyAction>(null, ContainerUtil.list(new MyDisableNewInspectionsAction(), new MyResetAction())) {
        @Override
        public PopupStep onChosen(MyAction selectedValue, boolean finalChoice) {
          if (selectedValue.enabled()) {
            selectedValue.actionPerformed();
          }
          return FINAL_CHOICE;
        }
      });
    actionGroupPopup.getList().setCellRenderer(new ListCellRenderer() {
      @Override
      public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        return ((MyAction)value).createCustomComponent(isSelected);
      }
    });
    final Component component = e.getInputEvent().getComponent();
    actionGroupPopup.show(new RelativePoint(component, new Point(component.getWidth() - 1, 0)));
  }

  private JLabel installLeftIndentToLabel(final JLabel label) {
    label.setBorder(BorderFactory.createEmptyBorder(0, myCheckBoxIndent, 0, 0));
    return label;
  }

  private class MyResetAction extends MyAction {

    protected MyResetAction() {
      super("All your changes will be lost");
    }

    @Override
    protected JComponent createBaseComponent() {
      return installLeftIndentToLabel(new JLabel("Reset to Defaults Settings"));
    }

    @Override
    public void actionPerformed() {
      final InspectionProfileImpl inspectionProfile = getInspectionProfile();
      if (inspectionProfile == null) {
        return;
      }
      inspectionProfile.resetToBase(myProject);
      postProcessModification();
    }

    @Override
    protected boolean enabled() {
      return myRoot.isProperSetting();
    }
  }

  private class MyDisableNewInspectionsAction extends MyAction {
    public MyDisableNewInspectionsAction() {
      super("New inspections may appear when " + ApplicationNamesInfo.getInstance().getFullProductName() + " is updated");
    }

    @Override
    protected JComponent createBaseComponent() {
      final JCheckBox checkBox = new JCheckBox("Disable new inspections by default");
      final InspectionProfileImpl profile = getInspectionProfile();
      checkBox.setEnabled(profile != null);
      if (profile != null) {
        checkBox.setSelected(profile.isProfileLocked());
      }
      checkBox.setOpaque(false);
      return checkBox;
    }

    @Override
    public void actionPerformed() {
      final InspectionProfileImpl profile = getInspectionProfile();
      if (profile != null) {
        profile.lockProfile(!profile.isProfileLocked());
      }
    }


    @Override
    protected boolean enabled() {
      return true;
    }
  }

  private abstract class MyAction {
    private final String myDescription;

    protected MyAction(String description) {
      myDescription = description;
    }

    protected abstract JComponent createBaseComponent();

    protected abstract void actionPerformed();

    protected abstract boolean enabled();

    public JComponent createCustomComponent(final boolean selected) {
      JPanel panel = new JPanel();
      panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
      panel.add(createBaseComponent());
      panel.add(installLeftIndentToLabel(new JBLabel(myDescription, UIUtil.ComponentStyle.MINI)));
      panel.setBackground(selected ? UIUtil.getListSelectionBackground() : UIUtil.getListBackground());
      panel.setForeground(selected ? UIUtil.getListSelectionForeground() : UIUtil.getListForeground());
      UIUtil.setEnabled(panel, enabled(), true);
      return panel;
    }
  }

  protected abstract InspectionProfileImpl getInspectionProfile();

  protected abstract void postProcessModification();

  private static int calculateCheckBoxIndent() {
    JCheckBox checkBox = new JCheckBox();
    Icon icon = checkBox.getIcon();
    int indent = 0;
    if (icon == null) {
      icon = UIManager.getIcon("CheckBox.icon");
    }
    if (UIUtil.isUnderDarcula() || UIUtil.isUnderIntelliJLaF()) {
      icon = EmptyIcon.create(20, 18);
    }
    if (icon != null) {
      final Insets i = checkBox.getInsets();
      final Rectangle r = checkBox.getBounds();
      final Rectangle r1 = new Rectangle();
      r1.x = i.left;
      r1.y = i.top;
      r1.width = r.width - (i.right + r1.x);
      r1.height = r.height - (i.bottom + r1.y);
      final Rectangle iconRect = new Rectangle();
      SwingUtilities.layoutCompoundLabel(
        checkBox, checkBox.getFontMetrics(checkBox.getFont()), checkBox.getText(), icon,
        checkBox.getVerticalAlignment(), checkBox.getHorizontalAlignment(),
        checkBox.getVerticalTextPosition(), checkBox.getHorizontalTextPosition(),
        r1, new Rectangle(), iconRect,
        checkBox.getText() == null ? 0 : checkBox.getIconTextGap());
      indent = iconRect.x;
    }
    return indent + checkBox.getIconTextGap();
  }
}