1/*
2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25package sun.tools.jconsole;
26
27import java.util.HashMap;
28import java.util.Map;
29
30import javax.swing.JOptionPane;
31import javax.swing.JPanel;
32import javax.swing.SwingWorker;
33
34import com.sun.tools.jconsole.JConsolePlugin;
35
36/**
37 * Proxy that shields GUI from plug-in exceptions.
38 *
39 */
40final class ExceptionSafePlugin extends JConsolePlugin {
41
42    private static boolean ignoreExceptions;
43    private final JConsolePlugin plugin;
44
45    public ExceptionSafePlugin(JConsolePlugin plugin) {
46        this.plugin = plugin;
47    }
48
49    @Override
50    public Map<String, JPanel> getTabs() {
51        try {
52            return plugin.getTabs();
53        } catch (RuntimeException e) {
54            handleException(e);
55        }
56        return new HashMap<>();
57    }
58
59    @Override
60    public SwingWorker<?, ?> newSwingWorker() {
61        try {
62            return plugin.newSwingWorker();
63        } catch (RuntimeException e) {
64            handleException(e);
65        }
66        return null;
67    }
68
69    @Override
70    public void dispose() {
71        try {
72            plugin.dispose();
73        } catch (RuntimeException e) {
74            handleException(e);
75        }
76    }
77
78    public void executeSwingWorker(SwingWorker<?, ?> sw) {
79        try {
80            sw.execute();
81        } catch (RuntimeException e) {
82            handleException(e);
83        }
84    }
85
86    private void handleException(Exception e) {
87        if (JConsole.isDebug()) {
88            System.err.println("Plug-in exception:");
89            e.printStackTrace();
90        } else {
91            if (!ignoreExceptions) {
92                showExceptionDialog(e);
93            }
94        }
95    }
96
97    private void showExceptionDialog(Exception e) {
98        Object[] buttonTexts = {
99            Messages.PLUGIN_EXCEPTION_DIALOG_BUTTON_OK,
100            Messages.PLUGIN_EXCEPTION_DIALOG_BUTTON_EXIT,
101            Messages.PLUGIN_EXCEPTION_DIALOG_BUTTON_IGNORE
102        };
103
104        String message = String.format(
105            Messages.PLUGIN_EXCEPTION_DIALOG_MESSAGE,
106            plugin.getClass().getSimpleName(),
107            String.valueOf(e.getMessage())
108        );
109
110        int buttonIndex = JOptionPane.showOptionDialog(
111            null,
112            message,
113            Messages.PLUGIN_EXCEPTION_DIALOG_TITLE,
114            JOptionPane.YES_NO_CANCEL_OPTION,
115            JOptionPane.ERROR_MESSAGE,
116            null,
117            buttonTexts,
118            buttonTexts[0]
119        );
120
121        if (buttonIndex == 1) {
122            System.exit(0);
123        }
124        ignoreExceptions = buttonIndex == 2;
125    }
126}
127