samples/exceptions/src/org/apidesign/exceptions/trycatchredo/SaveActionWithQuery.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sun, 01 Feb 2009 16:03:37 +0100
changeset 310 fba31e9504a1
parent 309 1687adb2b7f0
permissions -rw-r--r--
QueryException interactive example
     1 package org.apidesign.exceptions.trycatchredo;
     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         for (;;) {
    29             try {
    30                 OutputStream os = where.openConnection().getOutputStream();
    31                 os.write(what.toString().getBytes());
    32                 os.close();
    33             } catch (UserQuestionException ex) {
    34                 JOptionPane p = ex.getQuestionPane();
    35                 JDialog d = p.createDialog(ex.getLocalizedMessage());
    36                 d.setVisible(true);
    37                 ex.confirm(p.getValue());
    38                 if (
    39                     !p.getValue().equals(JOptionPane.CANCEL_OPTION) &&
    40                     !p.getValue().equals(JOptionPane.CLOSED_OPTION)
    41                 ) {
    42                     continue;
    43                 }
    44             } catch (IOException ex) {
    45                 JOptionPane.showMessageDialog(null, ex);
    46             }
    47             break;
    48         }
    49     }
    50 }