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