source: branches/0.17.x/abcl/src/org/armedbear/lisp/java/DialogPromptStream.java

Last change on this file was 12035, checked in by astalla, 16 years ago

Removed @Override annotations on methods directly implementing an interface -
it doesn't compile with Java 1.5.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.2 KB
Line 
1package org.armedbear.lisp.java;
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 dialog reports a label (prompt line)
12 * which shows to the user everything that has been printed to the stream up to the moment when the dialog
13 * became visible. It is usable as a drop-in replacement for e.g. *debug-io*.<br />
14 * This is an abstract class that does not depend on any GUI library. Subclasses are expected to provide
15 * the actual code to show the dialog and read input from the user.
16 * @author Alessio Stalla
17 *
18 */
19public abstract class DialogPromptStream extends Stream {
20
21  private StringWriter writtenSoFar = new StringWriter();
22  private Reader reader = new Reader() {
23
24    private StringReader stringReader = null;
25    private int inputSize = 0;
26   
27    public void close() throws IOException {
28      closeDialog();
29    }
30
31    public int read(char[] cbuf, int off, int len) throws IOException {
32      if(stringReader == null) {
33        writtenSoFar.flush();
34        String promptText = writtenSoFar.toString();
35        writtenSoFar.getBuffer().delete(0, Integer.MAX_VALUE);
36        String inputStr = readInputFromModalDialog(promptText) + System.getProperty("line.separator", "\n");
37        stringReader = new StringReader(inputStr);
38        inputSize = inputStr.length();
39      }
40      int read = stringReader.read(cbuf, off, len);
41      if(read != -1) {
42        inputSize -= read;
43      }
44      if(read == -1 || inputSize == 0) {
45        inputSize = 0;
46        stringReader = null;
47      }
48      return read;
49    }
50   
51  };
52
53  /**
54   * Inits this stream. Should be called by subclasses' constructors.
55   */
56  protected DialogPromptStream() {
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}
Note: See TracBrowser for help on using the repository browser.