samples/trycatchredo/src/org/apidesign/exceptions/trycatchredo/api/SaveActionWithQuery.java
changeset 312 0678c9589013
child 314 03a451fc2256
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/trycatchredo/src/org/apidesign/exceptions/trycatchredo/api/SaveActionWithQuery.java	Sun Feb 01 16:29:46 2009 +0100
     1.3 @@ -0,0 +1,54 @@
     1.4 +package org.apidesign.exceptions.trycatchredo.api;
     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 +                setVisible(d, p);
    1.40 +                ex.confirm(p.getValue());
    1.41 +                if (
    1.42 +                    !p.getValue().equals(JOptionPane.CANCEL_OPTION) &&
    1.43 +                    !p.getValue().equals(JOptionPane.CLOSED_OPTION)
    1.44 +                ) {
    1.45 +                    continue;
    1.46 +                }
    1.47 +            } catch (IOException ex) {
    1.48 +                JOptionPane.showMessageDialog(null, ex);
    1.49 +            }
    1.50 +            break;
    1.51 +        }
    1.52 +    }
    1.53 +
    1.54 +    private static void setVisible(JDialog d, JOptionPane p) {
    1.55 +        IOManager.setVisible(d, p);
    1.56 +    }
    1.57 +}