author | Jaroslav Tulach <jaroslav.tulach@apidesign.org> |
Thu, 30 Oct 2014 21:30:10 +0100 | |
changeset 409 | 40cabcdcd2be |
parent 312 | 0678c9589013 |
permissions | -rw-r--r-- |
jtulach@312 | 1 |
package org.apidesign.exceptions.trycatchredo.usage; |
jtulach@312 | 2 |
|
jtulach@312 | 3 |
import org.apidesign.exceptions.trycatchredo.api.UserQuestionException; |
jtulach@312 | 4 |
import java.io.ByteArrayOutputStream; |
jtulach@312 | 5 |
import java.io.IOException; |
jtulach@312 | 6 |
import java.io.OutputStream; |
jtulach@312 | 7 |
import javax.swing.JOptionPane; |
jtulach@312 | 8 |
|
jtulach@314 | 9 |
// BEGIN: trycatchredo.stream |
jtulach@312 | 10 |
public final class QueryStream extends OutputStream { |
jtulach@312 | 11 |
private ByteArrayOutputStream arr = new ByteArrayOutputStream(); |
jtulach@312 | 12 |
/** this field can be manipulated by the QueryException */ |
jtulach@312 | 13 |
Boolean reverse; |
jtulach@312 | 14 |
|
jtulach@312 | 15 |
@Override |
jtulach@314 | 16 |
public synchronized void write(byte[] b, int off, int len) |
jtulach@314 | 17 |
throws IOException { |
jtulach@312 | 18 |
if (reverse == null) { |
jtulach@312 | 19 |
throw new QueryException(); |
jtulach@312 | 20 |
} |
jtulach@312 | 21 |
arr.write(b, off, len); |
jtulach@312 | 22 |
} |
jtulach@312 | 23 |
|
jtulach@312 | 24 |
@Override |
jtulach@312 | 25 |
public synchronized void write(int b) throws IOException { |
jtulach@312 | 26 |
if (reverse == null) { |
jtulach@312 | 27 |
throw new QueryException(); |
jtulach@312 | 28 |
} |
jtulach@312 | 29 |
arr.write(b); |
jtulach@312 | 30 |
} |
jtulach@312 | 31 |
|
jtulach@312 | 32 |
@Override |
jtulach@312 | 33 |
public String toString() { |
jtulach@312 | 34 |
if (reverse == null) { |
jtulach@312 | 35 |
return "Reverse question was not answered yet!"; |
jtulach@312 | 36 |
} |
jtulach@312 | 37 |
if (reverse) { |
jtulach@312 | 38 |
StringBuilder sb = new StringBuilder(); |
jtulach@312 | 39 |
sb.append(arr.toString()); |
jtulach@312 | 40 |
sb.reverse(); |
jtulach@312 | 41 |
return sb.toString(); |
jtulach@312 | 42 |
} |
jtulach@312 | 43 |
return arr.toString(); |
jtulach@312 | 44 |
} |
jtulach@312 | 45 |
|
jtulach@312 | 46 |
private class QueryException extends UserQuestionException { |
jtulach@312 | 47 |
|
jtulach@312 | 48 |
@Override |
jtulach@312 | 49 |
public JOptionPane getQuestionPane() { |
jtulach@312 | 50 |
JOptionPane p = new JOptionPane("Store in reverse way?"); |
jtulach@312 | 51 |
p.setOptionType(JOptionPane.YES_NO_CANCEL_OPTION); |
jtulach@312 | 52 |
return p; |
jtulach@312 | 53 |
} |
jtulach@312 | 54 |
|
jtulach@312 | 55 |
@Override |
jtulach@312 | 56 |
public void confirm(Object option) { |
jtulach@312 | 57 |
if (option.equals(JOptionPane.YES_OPTION)) { |
jtulach@312 | 58 |
reverse = Boolean.TRUE; |
jtulach@312 | 59 |
return; |
jtulach@312 | 60 |
} |
jtulach@312 | 61 |
if (option.equals(JOptionPane.NO_OPTION)) { |
jtulach@312 | 62 |
reverse = Boolean.FALSE; |
jtulach@312 | 63 |
return; |
jtulach@312 | 64 |
} |
jtulach@312 | 65 |
} |
jtulach@312 | 66 |
} |
jtulach@312 | 67 |
} |
jtulach@314 | 68 |
// END: trycatchredo.stream |