QueryException interactive example
authorJaroslav Tulach <jtulach@netbeans.org>
Sun, 01 Feb 2009 16:03:37 +0100
changeset 310fba31e9504a1
parent 309 1687adb2b7f0
child 311 cb8db49f9d1c
QueryException interactive example
samples/exceptions/src/org/apidesign/exceptions/trycatchredo/IOManager.java
samples/exceptions/src/org/apidesign/exceptions/trycatchredo/SaveActionWithQuery.java
samples/exceptions/src/org/apidesign/exceptions/trycatchredo/UserQuestionException.java
samples/exceptions/test/org/apidesign/exceptions/trycatchredo/IOManagerTest.java
samples/exceptions/test/org/apidesign/exceptions/trycatchredo/MemoryURL.java
     1.1 --- a/samples/exceptions/src/org/apidesign/exceptions/trycatchredo/IOManager.java	Sun Feb 01 13:38:08 2009 +0100
     1.2 +++ b/samples/exceptions/src/org/apidesign/exceptions/trycatchredo/IOManager.java	Sun Feb 01 16:03:37 2009 +0100
     1.3 @@ -8,10 +8,22 @@
     1.4   * @author Jaroslav Tulach <jtulach@netbeans.org>
     1.5   */
     1.6  public final class IOManager {
     1.7 +    static boolean old;
     1.8 +
     1.9      IOManager() {
    1.10      }
    1.11  
    1.12 +    /** Action that can store a text to given URL.
    1.13 +     *
    1.14 +     * @param where the url to upload the text to
    1.15 +     * @param what the text to upload
    1.16 +     * @return action that can be invoked anytime to save the content
    1.17 +     */
    1.18      public static Action createSaveAction(URL where, CharSequence what) {
    1.19 -        return new SaveActionWithQuery(where, what);
    1.20 +        if (old) {
    1.21 +            return new SaveAction(where, what);
    1.22 +        } else {
    1.23 +            return new SaveActionWithQuery(where, what);
    1.24 +        }
    1.25      }
    1.26  }
     2.1 --- a/samples/exceptions/src/org/apidesign/exceptions/trycatchredo/SaveActionWithQuery.java	Sun Feb 01 13:38:08 2009 +0100
     2.2 +++ b/samples/exceptions/src/org/apidesign/exceptions/trycatchredo/SaveActionWithQuery.java	Sun Feb 01 16:03:37 2009 +0100
     2.3 @@ -35,7 +35,12 @@
     2.4                  JDialog d = p.createDialog(ex.getLocalizedMessage());
     2.5                  d.setVisible(true);
     2.6                  ex.confirm(p.getValue());
     2.7 -                continue;
     2.8 +                if (
     2.9 +                    !p.getValue().equals(JOptionPane.CANCEL_OPTION) &&
    2.10 +                    !p.getValue().equals(JOptionPane.CLOSED_OPTION)
    2.11 +                ) {
    2.12 +                    continue;
    2.13 +                }
    2.14              } catch (IOException ex) {
    2.15                  JOptionPane.showMessageDialog(null, ex);
    2.16              }
     3.1 --- a/samples/exceptions/src/org/apidesign/exceptions/trycatchredo/UserQuestionException.java	Sun Feb 01 13:38:08 2009 +0100
     3.2 +++ b/samples/exceptions/src/org/apidesign/exceptions/trycatchredo/UserQuestionException.java	Sun Feb 01 16:03:37 2009 +0100
     3.3 @@ -3,11 +3,27 @@
     3.4  import java.io.IOException;
     3.5  import javax.swing.JOptionPane;
     3.6  
     3.7 -/**
     3.8 +/** Specialized I/O exception to request some kind of user confirmation.
     3.9 + * A code that needs to ask user shall not attempt to open a dialog itself,
    3.10 + * rather it shall emit this exception and let its callers show the dialog
    3.11 + * at appropriate time.
    3.12   *
    3.13   * @author Jaroslav Tulach
    3.14 + * @since 2.0
    3.15   */
    3.16  public abstract class UserQuestionException extends IOException {
    3.17 +    /** Description of the dialog to show to the user. Whoever catches
    3.18 +     * this exception shall use {@link #getQuestionPane()}.{@link JOptionPane#createDialog(java.lang.String)}
    3.19 +     * to construct and display the dialog.
    3.20 +     * 
    3.21 +     * @return the pane to display to user
    3.22 +     */
    3.23      public abstract JOptionPane getQuestionPane();
    3.24 +    /** When the user confirms (or rejects) message presented by the
    3.25 +     * {@link #getQuestionPane()} dialog, the exception shall be notified
    3.26 +     * by calling this method with {@link JOptionPane#getValue()} option.
    3.27 +     *
    3.28 +     * @param option the option selected by the user
    3.29 +     */
    3.30      public abstract void confirm(Object option);
    3.31  }
     4.1 --- a/samples/exceptions/test/org/apidesign/exceptions/trycatchredo/IOManagerTest.java	Sun Feb 01 13:38:08 2009 +0100
     4.2 +++ b/samples/exceptions/test/org/apidesign/exceptions/trycatchredo/IOManagerTest.java	Sun Feb 01 16:03:37 2009 +0100
     4.3 @@ -3,8 +3,12 @@
     4.4  
     4.5  import java.awt.EventQueue;
     4.6  import java.awt.event.ActionEvent;
     4.7 +import java.io.ByteArrayOutputStream;
     4.8 +import java.io.IOException;
     4.9 +import java.io.OutputStream;
    4.10  import java.net.URL;
    4.11  import javax.swing.Action;
    4.12 +import javax.swing.JOptionPane;
    4.13  import org.junit.After;
    4.14  import org.junit.Before;
    4.15  import org.junit.Test;
    4.16 @@ -32,13 +36,86 @@
    4.17      public void simpleWrite() throws Exception {
    4.18          URL u = new URL("memory://simpleWrite.txt");
    4.19          MemoryURL.registerURL(u.toExternalForm(), "", null);
    4.20 -        final Action a = IOManager.createSaveAction(u, "Ahoj");
    4.21 +        final Action a = IOManager.createSaveAction(u, "Hello World!");
    4.22          EventQueue.invokeAndWait(new Runnable() {
    4.23              public void run() {
    4.24                  a.actionPerformed(new ActionEvent(this, 0, ""));
    4.25              }
    4.26          });
    4.27 -        byte[] out = MemoryURL.getOutputForURL(u.toExternalForm());
    4.28 -        assertEquals("Four bytes", 4, out.length);
    4.29 +        String out = MemoryURL.getOutputForURL(u.toExternalForm());
    4.30 +        assertEquals("Hello World!", out);
    4.31      }
    4.32 +
    4.33 +    @Test
    4.34 +    public void writeWithAQuestion() throws Exception {
    4.35 +        URL u = new URL("memory://queryEncoding.txt");
    4.36 +
    4.37 +        MemoryURL.registerURL(u.toExternalForm(), "", new QueryStream());
    4.38 +        final Action a = IOManager.createSaveAction(u, "Ask a Question");
    4.39 +        EventQueue.invokeAndWait(new Runnable() {
    4.40 +            public void run() {
    4.41 +                a.actionPerformed(new ActionEvent(this, 0, ""));
    4.42 +            }
    4.43 +        });
    4.44 +        String out = MemoryURL.getOutputForURL(u.toExternalForm());
    4.45 +        assertEquals("Text is reversed", "noitseuQ a ksA", out);
    4.46 +    }
    4.47 +
    4.48 +    private static class QueryStream extends OutputStream {
    4.49 +        Boolean reverse;
    4.50 +        ByteArrayOutputStream arr = new ByteArrayOutputStream();
    4.51 +
    4.52 +        @Override
    4.53 +        public synchronized void write(byte[] b, int off, int len) throws IOException {
    4.54 +            if (reverse == null) {
    4.55 +                throw new QueryException();
    4.56 +            }
    4.57 +            arr.write(b, off, len);
    4.58 +        }
    4.59 +
    4.60 +        @Override
    4.61 +        public synchronized void write(int b) throws IOException {
    4.62 +            if (reverse == null) {
    4.63 +                throw new QueryException();
    4.64 +            }
    4.65 +            arr.write(b);
    4.66 +        }
    4.67 +
    4.68 +        @Override
    4.69 +        public String toString() {
    4.70 +            if (reverse == null) {
    4.71 +                return "Reverse question was not answered yet!";
    4.72 +            }
    4.73 +            if (reverse) {
    4.74 +                StringBuilder sb = new StringBuilder();
    4.75 +                sb.append(arr.toString());
    4.76 +                sb.reverse();
    4.77 +                return sb.toString();
    4.78 +            }
    4.79 +            return arr.toString();
    4.80 +        }
    4.81 +
    4.82 +        private class QueryException extends UserQuestionException {
    4.83 +            @Override
    4.84 +            public JOptionPane getQuestionPane() {
    4.85 +                JOptionPane p = new JOptionPane("Store in reverse way?");
    4.86 +                p.setOptionType(JOptionPane.YES_NO_CANCEL_OPTION);
    4.87 +                return p;
    4.88 +            }
    4.89 +
    4.90 +            @Override
    4.91 +            public void confirm(Object option) {
    4.92 +                if (option.equals(JOptionPane.YES_OPTION)) {
    4.93 +                    reverse = Boolean.TRUE;
    4.94 +                    return;
    4.95 +                }
    4.96 +                if (option.equals(JOptionPane.NO_OPTION)) {
    4.97 +                    reverse = Boolean.FALSE;
    4.98 +                    return;
    4.99 +                }
   4.100 +            }
   4.101 +        } // end of QueryException
   4.102 +    } // end of QueryStream
   4.103 +
   4.104 +
   4.105  }
   4.106 \ No newline at end of file
     5.1 --- a/samples/exceptions/test/org/apidesign/exceptions/trycatchredo/MemoryURL.java	Sun Feb 01 13:38:08 2009 +0100
     5.2 +++ b/samples/exceptions/test/org/apidesign/exceptions/trycatchredo/MemoryURL.java	Sun Feb 01 16:03:37 2009 +0100
     5.3 @@ -39,18 +39,16 @@
     5.4      }
     5.5      
     5.6      private static Map<String,InputStream> contents = new HashMap<String,InputStream>();
     5.7 -    private static Map<String,MC> outputs = new HashMap<String,MC>();
     5.8 -    public static void registerURL(String u, String content, ByteArrayOutputStream out) throws MalformedURLException {
     5.9 +    private static Map<String,OutputStream> outputs = new HashMap<String,OutputStream>();
    5.10 +    public static void registerURL(String u, String content, OutputStream out) throws MalformedURLException {
    5.11          contents.put(u, new ByteArrayInputStream(content.getBytes()));
    5.12 -        if (out != null) {
    5.13 -            new MC(new URL(u)).out = out;
    5.14 -        }
    5.15 +        outputs.put(u, out);
    5.16      }
    5.17      
    5.18 -    public static byte[] getOutputForURL(String u) {
    5.19 -        MC out = outputs.get(u);
    5.20 +    public static String getOutputForURL(String u) {
    5.21 +        OutputStream out = outputs.get(u);
    5.22          Assert.assertNotNull("No output for " + u, out);
    5.23 -        return out.out.toByteArray();
    5.24 +        return out.toString();
    5.25      }
    5.26      
    5.27      protected URLConnection openConnection(URL u) throws IOException {
    5.28 @@ -59,11 +57,15 @@
    5.29      
    5.30      private static final class MC extends URLConnection {
    5.31          private InputStream values;
    5.32 -        private ByteArrayOutputStream out;
    5.33 +        private OutputStream out;
    5.34          
    5.35          public MC(URL u) {
    5.36              super(u);
    5.37 -            outputs.put(u.toExternalForm(), this);
    5.38 +            out = outputs.get(u.toExternalForm());
    5.39 +            if (out == null) {
    5.40 +                out = new ByteArrayOutputStream();
    5.41 +                outputs.put(u.toExternalForm(), out);
    5.42 +            }
    5.43          }
    5.44  
    5.45          public void connect() throws IOException {