samples/trycatchredo/src/org/apidesign/exceptions/trycatchredo/api/SaveActionWithQuery.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sun, 01 Feb 2009 16:29:46 +0100
changeset 312 0678c9589013
child 314 03a451fc2256
permissions -rw-r--r--
Renaming the project to 'Try Catch Redo'
     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         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                 setVisible(d, p);
    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 
    51     private static void setVisible(JDialog d, JOptionPane p) {
    52         IOManager.setVisible(d, p);
    53     }
    54 }