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