Changeset 2859 in josm for trunk/src


Ignore:
Timestamp:
2010-01-14T20:28:40+01:00 (14 years ago)
Author:
Gubaer
Message:

minor clean up

Location:
trunk/src/org/openstreetmap/josm
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/plugins/PluginHandler.java

    r2858 r2859  
    706706    }
    707707
    708     private static boolean confirmDisablingPluginAfterException(PluginProxy plugin) {
     708    private static boolean confirmDeactivatingPluginAfterException(PluginProxy plugin) {
    709709        ButtonSpec [] options = new ButtonSpec[] {
    710710                new ButtonSpec(
     
    748748    }
    749749
     750    /**
     751     * Replies the plugin which most likely threw the exception <code>ex</code>.
     752     *
     753     * @param ex the exception
     754     * @return the plugin; null, if the exception proably wasn't thrown from a plugin
     755     */
    750756    private static PluginProxy getPluginCausingException(Throwable ex) {
    751757        for (PluginProxy p : pluginList) {
     
    762768    }
    763769
    764     public static boolean checkException(Throwable e) {
     770    /**
     771     * Checks whether the exception <code>e</code> was thrown by a plugin. If so,
     772     * conditionally deactivates the plugin, but asks the user first.
     773     *
     774     * @param e the exception
     775     */
     776    public static void disablePluginAfterException(Throwable e) {
    765777        PluginProxy plugin = null;
    766 
    767778        // Check for an explicit problem when calling a plugin function
    768779        if (e instanceof PluginException) {
    769780            plugin = ((PluginException) e).plugin;
    770781        }
    771 
    772782        if (plugin == null) {
    773783            plugin = getPluginCausingException(e);
    774784        }
    775 
    776         if (plugin != null && confirmDisablingPluginAfterException(plugin)) {
    777             List<String> plugins = new ArrayList<String>(Main.pref.getCollection("plugins", Collections
    778                     .<String> emptyList()));
    779             if (plugins.contains(plugin.getPluginInformation().name)) {
    780                 while (plugins.remove(plugin.getPluginInformation().name)) {
    781                 }
    782                 Main.pref.putCollection("plugins", plugins);
    783                 JOptionPane
    784                 .showMessageDialog(
    785                         Main.parent,
    786                         tr("The plugin has been removed from the configuration. Please restart JOSM to unload the plugin."),
    787                         tr("Information"), JOptionPane.INFORMATION_MESSAGE);
    788             } else {
    789                 JOptionPane.showMessageDialog(Main.parent,
    790                         tr("The plugin could not be removed. Probably it was already disabled"), tr("Error"),
    791                         JOptionPane.ERROR_MESSAGE);
    792             }
    793             return true;
    794         }
    795         return false;
     785        if (plugin == null)
     786            // don't know what plugin threw the exception
     787            return;
     788
     789        Set<String> plugins = new HashSet<String>(
     790                Main.pref.getCollection("plugins",Collections.<String> emptySet())
     791        );
     792        if (! plugins.contains(plugin.getPluginInformation().name))
     793            // plugin not activated ? strange in this context but anyway, don't bother
     794            // the user with dialogs, skip condiational deactivation
     795            return;
     796
     797        if (!confirmDeactivatingPluginAfterException(plugin))
     798            // user doesn't want to deactivate the plugin
     799            return;
     800
     801        // deactivate the plugin
     802        plugins.remove(plugin.getPluginInformation().name);
     803        Main.pref.putCollection("plugins", plugins);
     804        JOptionPane.showMessageDialog(
     805                Main.parent,
     806                tr("The plugin has been removed from the configuration. Please restart JOSM to unload the plugin."),
     807                tr("Information"),
     808                JOptionPane.INFORMATION_MESSAGE
     809        );
     810        return;
    796811    }
    797812
  • trunk/src/org/openstreetmap/josm/tools/BugReportExceptionHandler.java

    r2853 r2859  
    5151            }
    5252
    53             if(PluginHandler.checkException(e))
    54                 return;
     53            // Give the user a chance to deactivate the plugin which threw the exception (if it
     54            // was thrown from a plugin)
     55            //
     56            PluginHandler.disablePluginAfterException(e);
    5557
     58            // Then ask for submitting a bug report, for exceptions thrown from a plugin too
     59            //
    5660            Object[] options = new String[]{tr("Do nothing"), tr("Report Bug")};
    57             int answer = JOptionPane.showOptionDialog(Main.parent, tr("An unexpected exception occurred.\n\n" +
    58                     "This is always a coding error. If you are running the latest\n" +
    59             "version of JOSM, please consider being kind and file a bug report."),
    60             tr("Unexpected Exception"), JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE,null,
    61             options, options[0]);
    62             if (answer == 1) {
     61            int answer = JOptionPane.showOptionDialog(
     62                    Main.parent,
     63                    "<html>"
     64                    + tr("An unexpected exception occurred.<br>" +
     65                            "This is always a coding error. If you are running the latest<br>" +
     66                            "version of JOSM, please consider being kind and file a bug report."
     67                    )
     68                    + "</html>",
     69                    tr("Unexpected Exception"),
     70                    JOptionPane.YES_NO_OPTION,
     71                    JOptionPane.ERROR_MESSAGE,
     72                    null,
     73                    options, options[0]
     74            );
     75            if (answer != 1)  return;
     76
     77            try {
     78                final int maxlen = 7000;
     79                StringWriter stack = new StringWriter();
     80                e.printStackTrace(new PrintWriter(stack));
     81
     82                String text = ShowStatusReportAction.getReportHeader()
     83                + stack.getBuffer().toString();
     84                String urltext = text.replaceAll("\r",""); /* strip useless return chars */
     85                if(urltext.length() > maxlen)
     86                {
     87                    urltext = urltext.substring(0,maxlen);
     88                    int idx = urltext.lastIndexOf("\n");
     89                    /* cut whole line when not loosing too much */
     90                    if(maxlen-idx < 200) {
     91                        urltext = urltext.substring(0,idx+1);
     92                    }
     93                    urltext += "...<snip>...\n";
     94                }
     95
     96                URL url = new URL("http://josm.openstreetmap.de/josmticket?" +
     97                        "data="+
     98                        Base64.encode(
     99                                // To note that it came from this code
     100                                "keywords=template_report&" +
     101                                "description=" + java.net.URLEncoder.encode(
     102                                        // Note: This doesn't use tr() intentionally, we want bug reports in English
     103                                        "What steps will reproduce the problem?\n"
     104                                        + " 1. \n"
     105                                        + " 2. \n"
     106                                        + " 3. \n"
     107                                        + "\n"
     108                                        + "What is the expected result?\n\n"
     109                                        + "What happens instead?\n\n"
     110                                        + "Please provide any additional information below. Attach a screenshot if\n"
     111                                        + "possible.\n\n"
     112                                        + "{{{\n" + urltext + "\n}}}\n",
     113                                "UTF-8")));
     114
     115                JPanel p = new JPanel(new GridBagLayout());
     116                p.add(new JMultilineLabel(
     117                        tr("You have encountered an error in JOSM. Before you file a bug report" +
     118                        "make sure you have updated to the latest version of JOSM here:")), GBC.eol());
     119                p.add(new UrlLabel("http://josm.openstreetmap.de/#Download"), GBC.eop().insets(8,0,0,0));
     120                p.add(new JMultilineLabel(
     121                        tr("You should also update your plugins. If neither of those help please" +
     122                        "file a bug report in our bugtracker using this link:")), GBC.eol());
     123                p.add(new UrlLabel(url.toString(), "http://josm.openstreetmap.de/josmticket?..."), GBC.eop().insets(8,0,0,0));
     124                p.add(new JMultilineLabel(
     125                        tr("There the error information provided below should already be" +
     126                                "filled in for you. Please include information on how to reproduce" +
     127                        "the error and try to supply as much detail as possible.")), GBC.eop());
     128                p.add(new JMultilineLabel(
     129                        tr("Alternatively, if that does not work you can manually fill in the information" +
     130                        "below at this URL:")), GBC.eol());
     131                p.add(new UrlLabel("http://josm.openstreetmap.de/newticket"), GBC.eop().insets(8,0,0,0));
    63132                try {
    64                     final int maxlen = 7000;
    65                     StringWriter stack = new StringWriter();
    66                     e.printStackTrace(new PrintWriter(stack));
     133                    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(text), new ClipboardOwner(){
     134                        public void lostOwnership(Clipboard clipboard, Transferable contents) {}
     135                    });
     136                    p.add(new JLabel(tr("(The text has already been copied to your clipboard.)")), GBC.eop());
     137                }
     138                catch (RuntimeException x) {}
    67139
    68                     String text = ShowStatusReportAction.getReportHeader()
    69                     + stack.getBuffer().toString();
    70                     String urltext = text.replaceAll("\r",""); /* strip useless return chars */
    71                     if(urltext.length() > maxlen)
    72                     {
    73                         urltext = urltext.substring(0,maxlen);
    74                         int idx = urltext.lastIndexOf("\n");
    75                         /* cut whole line when not loosing too much */
    76                         if(maxlen-idx < 200) {
    77                             urltext = urltext.substring(0,idx+1);
    78                         }
    79                         urltext += "...<snip>...\n";
     140                JTextArea info = new JTextArea(text, 20, 60);
     141                info.setCaretPosition(0);
     142                info.setEditable(false);
     143                p.add(new JScrollPane(info), GBC.eop());
     144
     145                for (Component c: p.getComponents()) {
     146                    if (c instanceof JMultilineLabel) {
     147                        ((JMultilineLabel)c).setMaxWidth(400);
    80148                    }
     149                }
    81150
    82                     URL url = new URL("http://josm.openstreetmap.de/josmticket?" +
    83                             "data="+
    84                             Base64.encode(
    85                                     // To note that it came from this code
    86                                     "keywords=template_report&" +
    87                                     "description=" + java.net.URLEncoder.encode(
    88                                             // Note: This doesn't use tr() intentionally, we want bug reports in English
    89                                             "What steps will reproduce the problem?\n"
    90                                             + " 1. \n"
    91                                             + " 2. \n"
    92                                             + " 3. \n"
    93                                             + "\n"
    94                                             + "What is the expected result?\n\n"
    95                                             + "What happens instead?\n\n"
    96                                             + "Please provide any additional information below. Attach a screenshot if\n"
    97                                             + "possible.\n\n"
    98                                             + "{{{\n" + urltext + "\n}}}\n",
    99                                     "UTF-8")));
    100 
    101                     JPanel p = new JPanel(new GridBagLayout());
    102                     p.add(new JMultilineLabel(
    103                             tr("You have encountered an error in JOSM. Before you file a bug report" +
    104                             "make sure you have updated to the latest version of JOSM here:")), GBC.eol());
    105                     p.add(new UrlLabel("http://josm.openstreetmap.de/#Download"), GBC.eop().insets(8,0,0,0));
    106                     p.add(new JMultilineLabel(
    107                             tr("You should also update your plugins. If neither of those help please" +
    108                             "file a bug report in our bugtracker using this link:")), GBC.eol());
    109                     p.add(new UrlLabel(url.toString(), "http://josm.openstreetmap.de/josmticket?..."), GBC.eop().insets(8,0,0,0));
    110                     p.add(new JMultilineLabel(
    111                             tr("There the error information provided below should already be" +
    112                                     "filled in for you. Please include information on how to reproduce" +
    113                             "the error and try to supply as much detail as possible.")), GBC.eop());
    114                     p.add(new JMultilineLabel(
    115                             tr("Alternatively, if that does not work you can manually fill in the information" +
    116                             "below at this URL:")), GBC.eol());
    117                     p.add(new UrlLabel("http://josm.openstreetmap.de/newticket"), GBC.eop().insets(8,0,0,0));
    118                     try {
    119                         Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(text), new ClipboardOwner(){
    120                             public void lostOwnership(Clipboard clipboard, Transferable contents) {}
    121                         });
    122                         p.add(new JLabel(tr("(The text has already been copied to your clipboard.)")), GBC.eop());
    123                     }
    124                     catch (RuntimeException x) {}
    125 
    126                     JTextArea info = new JTextArea(text, 20, 60);
    127                     info.setCaretPosition(0);
    128                     info.setEditable(false);
    129                     p.add(new JScrollPane(info), GBC.eop());
    130 
    131                     for (Component c: p.getComponents()) {
    132                         if (c instanceof JMultilineLabel) {
    133                             ((JMultilineLabel)c).setMaxWidth(400);
    134                         }
    135                     }
    136 
    137                     JOptionPane.showMessageDialog(Main.parent, p, tr("You have encountered a bug in JOSM"), JOptionPane.ERROR_MESSAGE);
    138                 } catch (Exception e1) {
    139                     e1.printStackTrace();
    140                 }
     151                JOptionPane.showMessageDialog(Main.parent, p, tr("You have encountered a bug in JOSM"), JOptionPane.ERROR_MESSAGE);
     152            } catch (Exception e1) {
     153                e1.printStackTrace();
    141154            }
    142155        }
Note: See TracChangeset for help on using the changeset viewer.