samples/trycatchredo/src/org/apidesign/exceptions/trycatchredo/api/SaveActionWithQuery.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 30 Oct 2014 21:30:10 +0100
changeset 409 40cabcdcd2be
parent 312 0678c9589013
permissions -rw-r--r--
Updating to NBMs from NetBeans 8.0.1 as some of them are required to run on JDK8
     1 package org.apidesign.exceptions.trycatchredo.api;
     2 
     3 import java.awt.EventQueue;
     4 import java.awt.event.ActionEvent;
     5 import java.io.IOException;
     6 import java.io.OutputStream;
     7 import java.net.URL;
     8 import javax.swing.AbstractAction;
     9 import javax.swing.JDialog;
    10 import javax.swing.JOptionPane;
    11 
    12 /**
    13  *
    14  * @author Jaroslav Tulach
    15  */
    16 final class SaveActionWithQuery extends AbstractAction {
    17     private final URL where;
    18     private final CharSequence what;
    19     
    20     SaveActionWithQuery(URL where, CharSequence what) {
    21         this.where = where;
    22         this.what = what;
    23     }
    24     
    25     
    26     public void actionPerformed(ActionEvent ev) {
    27         assert EventQueue.isDispatchThread();
    28         // BEGIN: trycatchredo.SaveActionWithQuery
    29         for (;;) {
    30             try {
    31                 OutputStream os = where.openConnection().getOutputStream();
    32                 os.write(what.toString().getBytes());
    33                 os.close();
    34             } catch (UserQuestionException ex) {
    35                 JOptionPane p = ex.getQuestionPane();
    36                 JDialog d = p.createDialog(ex.getLocalizedMessage());
    37                 setVisible(d, p);
    38                 ex.confirm(p.getValue());
    39                 if (
    40                     !p.getValue().equals(JOptionPane.CANCEL_OPTION) &&
    41                     !p.getValue().equals(JOptionPane.CLOSED_OPTION)
    42                 ) {
    43                     continue;
    44                 }
    45             } catch (IOException ex) {
    46                 JOptionPane.showMessageDialog(null, ex);
    47             }
    48             break;
    49         }
    50         // END: trycatchredo.SaveActionWithQuery
    51     }
    52 
    53     private static void setVisible(JDialog d, JOptionPane p) {
    54         IOManager.setVisible(d, p);
    55     }
    56 }