# HG changeset patch # User Jaroslav Tulach # Date 1233500617 -3600 # Node ID fba31e9504a15a5c3f2568ef374bf2cfec7a25d1 # Parent 1687adb2b7f0946af3d66e2dbc92ba0dc690cbbd QueryException interactive example diff -r 1687adb2b7f0 -r fba31e9504a1 samples/exceptions/src/org/apidesign/exceptions/trycatchredo/IOManager.java --- a/samples/exceptions/src/org/apidesign/exceptions/trycatchredo/IOManager.java Sun Feb 01 13:38:08 2009 +0100 +++ b/samples/exceptions/src/org/apidesign/exceptions/trycatchredo/IOManager.java Sun Feb 01 16:03:37 2009 +0100 @@ -8,10 +8,22 @@ * @author Jaroslav Tulach */ public final class IOManager { + static boolean old; + IOManager() { } + /** Action that can store a text to given URL. + * + * @param where the url to upload the text to + * @param what the text to upload + * @return action that can be invoked anytime to save the content + */ public static Action createSaveAction(URL where, CharSequence what) { - return new SaveActionWithQuery(where, what); + if (old) { + return new SaveAction(where, what); + } else { + return new SaveActionWithQuery(where, what); + } } } diff -r 1687adb2b7f0 -r fba31e9504a1 samples/exceptions/src/org/apidesign/exceptions/trycatchredo/SaveActionWithQuery.java --- a/samples/exceptions/src/org/apidesign/exceptions/trycatchredo/SaveActionWithQuery.java Sun Feb 01 13:38:08 2009 +0100 +++ b/samples/exceptions/src/org/apidesign/exceptions/trycatchredo/SaveActionWithQuery.java Sun Feb 01 16:03:37 2009 +0100 @@ -35,7 +35,12 @@ JDialog d = p.createDialog(ex.getLocalizedMessage()); d.setVisible(true); ex.confirm(p.getValue()); - continue; + if ( + !p.getValue().equals(JOptionPane.CANCEL_OPTION) && + !p.getValue().equals(JOptionPane.CLOSED_OPTION) + ) { + continue; + } } catch (IOException ex) { JOptionPane.showMessageDialog(null, ex); } diff -r 1687adb2b7f0 -r fba31e9504a1 samples/exceptions/src/org/apidesign/exceptions/trycatchredo/UserQuestionException.java --- a/samples/exceptions/src/org/apidesign/exceptions/trycatchredo/UserQuestionException.java Sun Feb 01 13:38:08 2009 +0100 +++ b/samples/exceptions/src/org/apidesign/exceptions/trycatchredo/UserQuestionException.java Sun Feb 01 16:03:37 2009 +0100 @@ -3,11 +3,27 @@ import java.io.IOException; import javax.swing.JOptionPane; -/** +/** Specialized I/O exception to request some kind of user confirmation. + * A code that needs to ask user shall not attempt to open a dialog itself, + * rather it shall emit this exception and let its callers show the dialog + * at appropriate time. * * @author Jaroslav Tulach + * @since 2.0 */ public abstract class UserQuestionException extends IOException { + /** Description of the dialog to show to the user. Whoever catches + * this exception shall use {@link #getQuestionPane()}.{@link JOptionPane#createDialog(java.lang.String)} + * to construct and display the dialog. + * + * @return the pane to display to user + */ public abstract JOptionPane getQuestionPane(); + /** When the user confirms (or rejects) message presented by the + * {@link #getQuestionPane()} dialog, the exception shall be notified + * by calling this method with {@link JOptionPane#getValue()} option. + * + * @param option the option selected by the user + */ public abstract void confirm(Object option); } diff -r 1687adb2b7f0 -r fba31e9504a1 samples/exceptions/test/org/apidesign/exceptions/trycatchredo/IOManagerTest.java --- a/samples/exceptions/test/org/apidesign/exceptions/trycatchredo/IOManagerTest.java Sun Feb 01 13:38:08 2009 +0100 +++ b/samples/exceptions/test/org/apidesign/exceptions/trycatchredo/IOManagerTest.java Sun Feb 01 16:03:37 2009 +0100 @@ -3,8 +3,12 @@ import java.awt.EventQueue; import java.awt.event.ActionEvent; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; import java.net.URL; import javax.swing.Action; +import javax.swing.JOptionPane; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -32,13 +36,86 @@ public void simpleWrite() throws Exception { URL u = new URL("memory://simpleWrite.txt"); MemoryURL.registerURL(u.toExternalForm(), "", null); - final Action a = IOManager.createSaveAction(u, "Ahoj"); + final Action a = IOManager.createSaveAction(u, "Hello World!"); EventQueue.invokeAndWait(new Runnable() { public void run() { a.actionPerformed(new ActionEvent(this, 0, "")); } }); - byte[] out = MemoryURL.getOutputForURL(u.toExternalForm()); - assertEquals("Four bytes", 4, out.length); + String out = MemoryURL.getOutputForURL(u.toExternalForm()); + assertEquals("Hello World!", out); } + + @Test + public void writeWithAQuestion() throws Exception { + URL u = new URL("memory://queryEncoding.txt"); + + MemoryURL.registerURL(u.toExternalForm(), "", new QueryStream()); + final Action a = IOManager.createSaveAction(u, "Ask a Question"); + EventQueue.invokeAndWait(new Runnable() { + public void run() { + a.actionPerformed(new ActionEvent(this, 0, "")); + } + }); + String out = MemoryURL.getOutputForURL(u.toExternalForm()); + assertEquals("Text is reversed", "noitseuQ a ksA", out); + } + + private static class QueryStream extends OutputStream { + Boolean reverse; + ByteArrayOutputStream arr = new ByteArrayOutputStream(); + + @Override + public synchronized void write(byte[] b, int off, int len) throws IOException { + if (reverse == null) { + throw new QueryException(); + } + arr.write(b, off, len); + } + + @Override + public synchronized void write(int b) throws IOException { + if (reverse == null) { + throw new QueryException(); + } + arr.write(b); + } + + @Override + public String toString() { + if (reverse == null) { + return "Reverse question was not answered yet!"; + } + if (reverse) { + StringBuilder sb = new StringBuilder(); + sb.append(arr.toString()); + sb.reverse(); + return sb.toString(); + } + return arr.toString(); + } + + private class QueryException extends UserQuestionException { + @Override + public JOptionPane getQuestionPane() { + JOptionPane p = new JOptionPane("Store in reverse way?"); + p.setOptionType(JOptionPane.YES_NO_CANCEL_OPTION); + return p; + } + + @Override + public void confirm(Object option) { + if (option.equals(JOptionPane.YES_OPTION)) { + reverse = Boolean.TRUE; + return; + } + if (option.equals(JOptionPane.NO_OPTION)) { + reverse = Boolean.FALSE; + return; + } + } + } // end of QueryException + } // end of QueryStream + + } \ No newline at end of file diff -r 1687adb2b7f0 -r fba31e9504a1 samples/exceptions/test/org/apidesign/exceptions/trycatchredo/MemoryURL.java --- a/samples/exceptions/test/org/apidesign/exceptions/trycatchredo/MemoryURL.java Sun Feb 01 13:38:08 2009 +0100 +++ b/samples/exceptions/test/org/apidesign/exceptions/trycatchredo/MemoryURL.java Sun Feb 01 16:03:37 2009 +0100 @@ -39,18 +39,16 @@ } private static Map contents = new HashMap(); - private static Map outputs = new HashMap(); - public static void registerURL(String u, String content, ByteArrayOutputStream out) throws MalformedURLException { + private static Map outputs = new HashMap(); + public static void registerURL(String u, String content, OutputStream out) throws MalformedURLException { contents.put(u, new ByteArrayInputStream(content.getBytes())); - if (out != null) { - new MC(new URL(u)).out = out; - } + outputs.put(u, out); } - public static byte[] getOutputForURL(String u) { - MC out = outputs.get(u); + public static String getOutputForURL(String u) { + OutputStream out = outputs.get(u); Assert.assertNotNull("No output for " + u, out); - return out.out.toByteArray(); + return out.toString(); } protected URLConnection openConnection(URL u) throws IOException { @@ -59,11 +57,15 @@ private static final class MC extends URLConnection { private InputStream values; - private ByteArrayOutputStream out; + private OutputStream out; public MC(URL u) { super(u); - outputs.put(u.toExternalForm(), this); + out = outputs.get(u.toExternalForm()); + if (out == null) { + out = new ByteArrayOutputStream(); + outputs.put(u.toExternalForm(), out); + } } public void connect() throws IOException {