extracted public method to show warning dialog and disable plugin
[idea/community.git] / platform / platform-impl / src / com / intellij / diagnostic / errordialog / DisablePluginWarningDialog.java
1 package com.intellij.diagnostic.errordialog;
2
3 import com.intellij.diagnostic.DiagnosticBundle;
4 import com.intellij.ide.plugins.IdeaPluginDescriptor;
5 import com.intellij.ide.plugins.PluginManager;
6 import com.intellij.ide.plugins.PluginManagerCore;
7 import com.intellij.openapi.application.Application;
8 import com.intellij.openapi.application.ApplicationManager;
9 import com.intellij.openapi.application.ApplicationNamesInfo;
10 import com.intellij.openapi.extensions.PluginId;
11 import com.intellij.openapi.ui.DialogWrapper;
12 import com.intellij.openapi.util.Condition;
13 import com.intellij.openapi.util.Ref;
14 import com.intellij.openapi.util.SystemInfo;
15 import com.intellij.util.Function;
16 import org.jetbrains.annotations.NotNull;
17
18 import javax.swing.*;
19 import java.awt.*;
20 import java.awt.event.ActionEvent;
21
22 /**
23  * @author ksafonov
24  */
25 public class DisablePluginWarningDialog extends DialogWrapper {
26   private JLabel myPromptLabel;
27   private JLabel myRestartLabel;
28   private JPanel myContentPane;
29
30   private static final int DISABLE_EXIT_CODE = OK_EXIT_CODE;
31   private static final int DISABLE_AND_RESTART_EXIT_CODE = NEXT_USER_EXIT_CODE;
32   private final boolean myRestartCapable;
33
34   private DisablePluginWarningDialog(@NotNull Component parent, String pluginName, boolean hasDependants, boolean restartCapable) {
35     super(parent, false);
36     myRestartCapable = restartCapable;
37     myPromptLabel.setText(
38       DiagnosticBundle.message(hasDependants ? "error.dialog.disable.plugin.prompt.dependants" : "error.dialog.disable.plugin.prompt",
39                                pluginName));
40     myRestartLabel
41       .setText(DiagnosticBundle.message(restartCapable ? "error.dialog.disable.plugin.restart" : "error.dialog.disable.plugin.norestart",
42                                         ApplicationNamesInfo.getInstance().getFullProductName()));
43
44     setTitle(DiagnosticBundle.message("error.dialog.disable.plugin.title"));
45     init();
46   }
47
48   public static void disablePlugin(@NotNull PluginId pluginId, @NotNull JComponent parentComponent) {
49     IdeaPluginDescriptor plugin = PluginManager.getPlugin(pluginId);
50     final Ref<Boolean> hasDependants = new Ref<Boolean>(false);
51     PluginManagerCore.checkDependants(plugin, new Function<PluginId, IdeaPluginDescriptor>() {
52                                         @Override
53                                         public IdeaPluginDescriptor fun(PluginId pluginId) {
54                                           return PluginManager.getPlugin(pluginId);
55                                         }
56                                       }, new Condition<PluginId>() {
57                                         @Override
58                                         public boolean value(PluginId pluginId) {
59                                           if (PluginManagerCore.CORE_PLUGIN_ID.equals(pluginId.getIdString())) {
60                                             return true;
61                                           }
62                                           hasDependants.set(true);
63                                           return false;
64                                         }
65                                       }
66     );
67
68     Application app = ApplicationManager.getApplication();
69     DisablePluginWarningDialog d = new DisablePluginWarningDialog(parentComponent, plugin.getName(), hasDependants.get(), app.isRestartCapable());
70     d.show();
71     switch (d.getExitCode()) {
72       case CANCEL_EXIT_CODE:
73         return;
74       case DISABLE_EXIT_CODE:
75         PluginManagerCore.disablePlugin(pluginId.getIdString());
76         break;
77       case DISABLE_AND_RESTART_EXIT_CODE:
78         PluginManagerCore.disablePlugin(pluginId.getIdString());
79         app.restart();
80         break;
81     }
82   }
83
84   @Override
85   protected JComponent createCenterPanel() {
86     return myContentPane;
87   }
88
89   @NotNull
90   @Override
91   protected Action[] createActions() {
92     if (SystemInfo.isMac) {
93       if (myRestartCapable) {
94         return new Action[]{getCancelAction(), new DisableAction(), new DisableAndRestartAction()};
95       }
96       else {
97         return new Action[]{getCancelAction(), new DisableAction()};
98       }
99     }
100     else {
101       if (myRestartCapable) {
102         return new Action[]{new DisableAction(), new DisableAndRestartAction(), getCancelAction()};
103       }
104       else {
105         return new Action[]{new DisableAction(), getCancelAction()};
106       }
107     }
108   }
109
110   private class DisableAction extends DialogWrapperAction {
111     protected DisableAction() {
112       super(DiagnosticBundle.message("error.dialog.disable.plugin.action.disable"));
113     }
114
115     @Override
116     protected void doAction(ActionEvent e) {
117       close(DISABLE_EXIT_CODE);
118     }
119   }
120
121   private class DisableAndRestartAction extends DialogWrapperAction {
122     protected DisableAndRestartAction() {
123       super(DiagnosticBundle.message("error.dialog.disable.plugin.action.disableAndRestart"));
124     }
125
126     @Override
127     protected void doAction(ActionEvent e) {
128       close(DISABLE_AND_RESTART_EXIT_CODE);
129     }
130   }
131 }