source: branches/1.1.x/examples/gui/abcl/DialogPromptStream.java

Last change on this file was 12732, checked in by Mark Evenson, 14 years ago

Make GUI examples buildable; putative attempt at a README.

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