| // |
| // Licensed to the Apache Software Foundation (ASF) under one |
| // or more contributor license agreements. See the NOTICE file |
| // distributed with this work for additional information |
| // regarding copyright ownership. The ASF licenses this file |
| // to you 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. |
| // |
| |
| = DevFaqCustomizingUnexpectedExceptionDialog |
| :jbake-type: wiki |
| :jbake-tags: wiki, devfaq, needsreview |
| :jbake-status: published |
| |
| == How can I customize the Unexpected Exception dialog ? |
| |
| Problem: How do I add new functionality to the Unexpected Exception dialog? |
| |
| Solution: First, one needs to create a new class as follows. |
| |
| The first part of this class is that it must extend `Handler`: |
| |
| [source,java] |
| ---- |
| |
| public class NewFunctionExceptionHandler extends Handler { |
| |
| |
| @Override |
| public void publish(LogRecord record) { |
| if (record.getThrown() != null) { |
| // This is an uncaught exception being thrown. |
| } |
| } |
| |
| @Override |
| public void flush() { |
| } |
| |
| @Override |
| public void close() throws SecurityException { |
| } |
| } |
| ---- |
| |
| In order to properly process the exceptions, one must do a check for the exception in the `publish()` method; `LogRecord`s created due to exceptions being thrown will always have `Throwable`s present. |
| |
| This class also must implement `Callable<JButton>`. The button we want displayed in the Uncaught Exception dialog needs to be returned in the call() method: |
| |
| [source,java] |
| ---- |
| |
| public class NewFunctionExceptionHandler extends Handler implements Callable<JButton> { |
| |
| |
| @Override |
| public void publish(LogRecord record) { |
| if (record.getThrown() != null) { |
| // This is an uncaught exception being thrown. |
| } |
| } |
| |
| @Override |
| public void flush() { |
| } |
| |
| @Override |
| public void close() throws SecurityException { |
| } |
| |
| // Return the button we want to be displayed in the Uncaught Exception Dialog. |
| @Override |
| public JButton call() throws Exception { |
| ... |
| } |
| } |
| ---- |
| |
| The JButton's action listener needs to be passed the LogRecord that passed via the `publish()` method. Then, within said action listener for the button, the developer can do what is needed with that record (e.g. Open a Top Component to email a bug report or do anything else). |
| |
| The final result will look similar to: |
| |
| [source,java] |
| ---- |
| |
| public class NewFunctionExceptionHandler extends Handler implements Callable<JButton> { |
| |
| private JButton newFunctionButton; |
| private NewFunctionActionListener newFunctionActionListener = new NewFunctionActionListener(); |
| |
| @Override |
| public void publish(LogRecord record) { |
| if (record.getThrown() != null) { |
| newFunctionActionListener.setLogRecord(record); |
| } |
| } |
| |
| @Override |
| public void flush() { |
| } |
| |
| @Override |
| public void close() throws SecurityException { |
| } |
| |
| // Return the button we want to be displayed in the Uncaught Exception Dialog. |
| @Override |
| public JButton call() throws Exception { |
| if (newFunctionButton == null) { |
| newFunctionButton = new JButton("Review and Submit Issue"); |
| newFunctionButton.addActionListener(newFunctionActionListener); |
| } |
| |
| return reviewIssueButton; |
| } |
| |
| private class NewFunctionActionListener implements ActionListener { |
| |
| private LogRecord logRecord; |
| |
| public NewFunctionActionListener() { |
| } |
| |
| @Override |
| public void actionPerformed(ActionEvent e) { |
| // Close our Uncaught Exception Dialog first. |
| SwingUtilities.windowForComponent(reviewIssueButton).setVisible(false); |
| } |
| |
| public void setLogRecord(LogRecord logRecord) { |
| this.logRecord = logRecord; |
| } |
| } |
| } |
| ---- |
| |
| To register this exception handler, one only needs to add the new `Handler` to a `java.util.Logger` named with the empty string: |
| |
| [source,java] |
| ---- |
| |
| Logger.getLogger("").addHandler(new NewFunctionExceptionHandler()); |
| ---- |
| |
| Any `Handler` attached to the "" Logger that also `implements Callable<JButton>` will have its button displayed in the Uncaught Exception Dialog. |
| |
| This could be done in a module's `Installer` class. |
| |
| Applies to: NetBeans IDE 6.0 and newer |
| |
| Platforms: All |
| |
| === Apache Migration Information |
| |
| The content in this page was kindly donated by Oracle Corp. to the |
| Apache Software Foundation. |
| |
| This page was exported from link:http://wiki.netbeans.org/DevFaqCustomizingUnexpectedExceptionDialog[http://wiki.netbeans.org/DevFaqCustomizingUnexpectedExceptionDialog] , |
| that was last modified by NetBeans user Skygo |
| on 2013-12-17T22:39:45Z. |
| |
| |
| *NOTE:* This document was automatically converted to the AsciiDoc format on 2018-01-10, and needs to be reviewed. |