1 | package org.armedbear.lisp.java; |
---|
2 | |
---|
3 | import java.io.IOException; |
---|
4 | import java.io.Reader; |
---|
5 | import java.io.StringReader; |
---|
6 | import java.io.StringWriter; |
---|
7 | |
---|
8 | import org.armedbear.lisp.Stream; |
---|
9 | /** |
---|
10 | * A bidirectional stream that captures input from a modal dialog. The dialog reports a label (prompt line) |
---|
11 | * which shows to the user everything that has been printed to the stream up to the moment when the dialog |
---|
12 | * became visible. It is usable as a drop-in replacement for e.g. *debug-io*.<br /> |
---|
13 | * This is an abstract class that does not depend on any GUI library. Subclasses are expected to provide |
---|
14 | * the actual code to show the dialog and read input from the user. |
---|
15 | * @author Alessio Stalla |
---|
16 | * |
---|
17 | */ |
---|
18 | public abstract class DialogPromptStream extends Stream { |
---|
19 | |
---|
20 | StringWriter writtenSoFar = new StringWriter(); |
---|
21 | private Reader reader = new Reader() { |
---|
22 | |
---|
23 | private StringReader stringReader = null; |
---|
24 | private int inputSize = 0; |
---|
25 | |
---|
26 | public void close() throws IOException { |
---|
27 | closeDialog(); |
---|
28 | } |
---|
29 | |
---|
30 | public int read(char[] cbuf, int off, int len) throws IOException { |
---|
31 | if(stringReader == null) { |
---|
32 | writtenSoFar.flush(); |
---|
33 | String promptText = writtenSoFar.toString(); |
---|
34 | writtenSoFar.getBuffer().delete(0, Integer.MAX_VALUE); |
---|
35 | String inputStr = readInputFromModalDialog(promptText) + System.getProperty("line.separator", "\n"); |
---|
36 | stringReader = new StringReader(inputStr); |
---|
37 | inputSize = inputStr.length(); |
---|
38 | } |
---|
39 | int read = stringReader.read(cbuf, off, len); |
---|
40 | if(read != -1) { |
---|
41 | inputSize -= read; |
---|
42 | } |
---|
43 | if(read == -1 || inputSize == 0) { |
---|
44 | inputSize = 0; |
---|
45 | stringReader = null; |
---|
46 | } |
---|
47 | return read; |
---|
48 | } |
---|
49 | |
---|
50 | }; |
---|
51 | |
---|
52 | /** |
---|
53 | * Inits this stream. Should be called by subclasses' constructors. |
---|
54 | */ |
---|
55 | protected DialogPromptStream() { |
---|
56 | super(org.armedbear.lisp.Symbol.SYSTEM_STREAM); |
---|
57 | initAsCharacterOutputStream(writtenSoFar); |
---|
58 | initAsCharacterInputStream(reader); |
---|
59 | } |
---|
60 | |
---|
61 | /** |
---|
62 | * Closes the dialog when this stream is closed, aborting the read operation. |
---|
63 | */ |
---|
64 | protected abstract void closeDialog(); |
---|
65 | |
---|
66 | /** |
---|
67 | * Shows the dialog and blocks the calling thread until the user has closed the dialog. |
---|
68 | * @param promptText the text to be shown to the user (the prompt). |
---|
69 | * @return a string holding input from the user. |
---|
70 | */ |
---|
71 | protected abstract String readInputFromModalDialog(String promptText); |
---|
72 | |
---|
73 | } |
---|