Advanced Error Handling for Dialogs

In some rare cases, it may be desirable to leave a dialog open after the user clicks OK so that the user may correct invalid data. This can be done using the NetBeans dialog descriptor. This example shows how to do this using an existing JPanel.

To set up the descriptor:

        final MyPanel myPanel = new MyPanel();
        // Use an existing IntViewerDialogDisplayer option type.
        // OptionType has provides "closing options", which specify which actions will close the dialog 
        // (in this case they will be "Ok" and "Cancel").
        IntViewerDialogDisplayer.OptionType optionType = IntViewerDialogDisplayer.OK_APPLY_CANCEL_OPTION;
        // Create an action listener for the dialog. Pass the closing options into the listener.
        MyDialogActionListener myDialogActionListener = new MyDialogActionListener(myPanel,
            optionType.getClosingOptions());
        // Create a dialog descriptor.
        final DialogDescriptor dialogDesc =
             new DialogDescriptor(myPanel, "Dialog Title", false, optionType.getOptions(),
                optionType.getInitialOption(), DialogDescriptor.DEFAULT_ALIGN, null,
                myDialogActionListener);
        // Now pass the DialogDescriptor into the listener.
        myDialogActionListener.setDialogDescriptor(dialogDesc);
        // Display the dialog.
        DialogDisplayer.getDefault().notify(dialogDesc);

The listener looks like this:

    private class MyDialogActionListener implements ActionListener {
        private final MyPanel myPanel;
        private final Object[] defaultClosingOptions;
        private DialogDescriptor dialogDesc;
        public MyDialogActionListener( MyPanel myPanel,
                Object[] defaultClosingOptions) {
            this.myPanel = myPanel;
            this.defaultClosingOptions = defaultClosingOptions;
        }
        public void setDialogDescriptor(DialogDescriptor dd) {
            this.dialogDesc = dd;
        }
        @Override
        public void actionPerformed(ActionEvent e) {
            // Set the dialog to close when the Ok or Cancel button is selected
            dialogDesc.setClosingOptions(defaultClosingOptions);
            // Set up the closing options for an error case
            // We only want the dialog to close when CANCEL is clicked.
            Object[] cancelOnly = new Object[]{DialogDisplayerUtils.CANCEL};
            final String command = e.getActionCommand();
            if (command.equals(DialogDisplayerUtils.OK) || command.equals(DialogDisplayerUtils.APPLY)) {
                try {
                    final Object data = myPanel.getSomeData();
                    doSomething(data);
                }
                catch (Exception ex) {
                    // If there is an error, set the cancel only closing option in the DialogDescriptor.
                    dialogDesc.setClosingOptions(cancelOnly);
                    // Use the INTViewer DialogManager to display an error message.
                    DialogManager.getDefault().showMessageDialog(ex.getMessage(), "Error",
                        DialogManager.ERROR);
                }
            }
        }
    }