samples/exceptions/src/org/apidesign/exceptions/trycatchredo/SaveActionWithQuery.java
changeset 309 1687adb2b7f0
child 310 fba31e9504a1
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/exceptions/src/org/apidesign/exceptions/trycatchredo/SaveActionWithQuery.java	Sun Feb 01 13:38:08 2009 +0100
     1.3 @@ -0,0 +1,45 @@
     1.4 +package org.apidesign.exceptions.trycatchredo;
     1.5 +
     1.6 +import java.awt.EventQueue;
     1.7 +import java.awt.event.ActionEvent;
     1.8 +import java.io.IOException;
     1.9 +import java.io.OutputStream;
    1.10 +import java.net.URL;
    1.11 +import javax.swing.AbstractAction;
    1.12 +import javax.swing.JDialog;
    1.13 +import javax.swing.JOptionPane;
    1.14 +
    1.15 +/**
    1.16 + *
    1.17 + * @author Jaroslav Tulach
    1.18 + */
    1.19 +final class SaveActionWithQuery extends AbstractAction {
    1.20 +    private final URL where;
    1.21 +    private final CharSequence what;
    1.22 +    
    1.23 +    SaveActionWithQuery(URL where, CharSequence what) {
    1.24 +        this.where = where;
    1.25 +        this.what = what;
    1.26 +    }
    1.27 +    
    1.28 +    
    1.29 +    public void actionPerformed(ActionEvent ev) {
    1.30 +        assert EventQueue.isDispatchThread();
    1.31 +        for (;;) {
    1.32 +            try {
    1.33 +                OutputStream os = where.openConnection().getOutputStream();
    1.34 +                os.write(what.toString().getBytes());
    1.35 +                os.close();
    1.36 +            } catch (UserQuestionException ex) {
    1.37 +                JOptionPane p = ex.getQuestionPane();
    1.38 +                JDialog d = p.createDialog(ex.getLocalizedMessage());
    1.39 +                d.setVisible(true);
    1.40 +                ex.confirm(p.getValue());
    1.41 +                continue;
    1.42 +            } catch (IOException ex) {
    1.43 +                JOptionPane.showMessageDialog(null, ex);
    1.44 +            }
    1.45 +            break;
    1.46 +        }
    1.47 +    }
    1.48 +}