[armedbear-cvs] r12023 - in trunk/abcl/src/org/armedbear/lisp/java: . awt swing

Alessio Stalla astalla at common-lisp.net
Mon Jun 29 21:12:39 UTC 2009


Author: astalla
Date: Mon Jun 29 17:12:35 2009
New Revision: 12023

Log:
Added simple GUI-based stream to query the user for input.


Added:
   trunk/abcl/src/org/armedbear/lisp/java/DialogPromptStream.java   (contents, props changed)
   trunk/abcl/src/org/armedbear/lisp/java/awt/AwtDialogPromptStream.java   (contents, props changed)
   trunk/abcl/src/org/armedbear/lisp/java/swing/
   trunk/abcl/src/org/armedbear/lisp/java/swing/SwingDialogPromptStream.java   (contents, props changed)

Added: trunk/abcl/src/org/armedbear/lisp/java/DialogPromptStream.java
==============================================================================
--- (empty file)
+++ trunk/abcl/src/org/armedbear/lisp/java/DialogPromptStream.java	Mon Jun 29 17:12:35 2009
@@ -0,0 +1,75 @@
+package org.armedbear.lisp.java;
+
+import java.io.IOException;
+import java.io.Reader;
+import java.io.StringReader;
+import java.io.StringWriter;
+
+import org.armedbear.lisp.Stream;
+
+/**
+ * A bidirectional stream that captures input from a modal dialog. The dialog reports a label (prompt line)
+ * which shows to the user everything that has been printed to the stream up to the moment when the dialog
+ * became visible. It is usable as a drop-in replacement for e.g. *debug-io*.<br />
+ * This is an abstract class that does not depend on any GUI library. Subclasses are expected to provide
+ * the actual code to show the dialog and read input from the user.
+ * @author Alessio Stalla
+ *
+ */
+public abstract class DialogPromptStream extends Stream {
+
+	private StringWriter writtenSoFar = new StringWriter();
+	private Reader reader = new Reader() {
+
+		private StringReader stringReader = null;
+		private int inputSize = 0;
+		
+		@Override
+		public void close() throws IOException {
+			closeDialog();
+		}
+
+		@Override
+		public int read(char[] cbuf, int off, int len) throws IOException {
+			if(stringReader == null) {
+				writtenSoFar.flush();
+				String promptText = writtenSoFar.toString();
+				writtenSoFar.getBuffer().delete(0, Integer.MAX_VALUE);
+				String inputStr = readInputFromModalDialog(promptText) + System.getProperty("line.separator", "\n");
+				stringReader = new StringReader(inputStr);
+				inputSize = inputStr.length();
+			}
+			int read = stringReader.read(cbuf, off, len);
+			if(read != -1) {
+				inputSize -= read;
+			}
+			if(read == -1 || inputSize == 0) {
+				inputSize = 0;
+				stringReader = null;
+			}
+			return read;
+		}
+		
+	};
+
+	/**
+	 * Inits this stream. Should be called by subclasses' constructors.
+	 */
+	protected DialogPromptStream() {
+		initAsCharacterOutputStream(writtenSoFar);
+		initAsCharacterInputStream(reader);
+	}
+	
+	/**
+	 * Closes the dialog when this stream is closed, aborting the read operation.
+	 */
+	protected abstract void closeDialog();
+	
+	/**
+	 * Shows the dialog and blocks the calling thread until the user has closed the dialog.
+	 * @param promptText the text to be shown to the user (the prompt).
+	 * @return a string holding input from the user.
+	 */
+	protected abstract String readInputFromModalDialog(String promptText); 
+	
+}

Added: trunk/abcl/src/org/armedbear/lisp/java/awt/AwtDialogPromptStream.java
==============================================================================
--- (empty file)
+++ trunk/abcl/src/org/armedbear/lisp/java/awt/AwtDialogPromptStream.java	Mon Jun 29 17:12:35 2009
@@ -0,0 +1,62 @@
+package org.armedbear.lisp.java.awt;
+
+import java.awt.BorderLayout;
+import java.awt.Dialog;
+import java.awt.FlowLayout;
+import java.awt.Frame;
+import java.awt.Label;
+import java.awt.Panel;
+import java.awt.TextField;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+
+import javax.swing.JButton;
+
+import org.armedbear.lisp.java.DialogPromptStream;
+
+public class AwtDialogPromptStream extends DialogPromptStream {
+
+	private Dialog dialog = new Dialog((Frame)null, true);
+	private Label prompt = new Label();
+	private TextField input = new TextField(32);
+	
+	public AwtDialogPromptStream() {
+		this("Prompt");
+	}
+	
+	public AwtDialogPromptStream(String title) {
+		super();
+		dialog.setTitle(title);
+		Panel tmpPanel = new Panel();
+		tmpPanel.add(prompt);
+		tmpPanel.add(input);
+		dialog.add(tmpPanel);
+		JButton okBtn = new JButton("Ok");
+		okBtn.addActionListener(new ActionListener() {
+
+			@Override
+			public void actionPerformed(ActionEvent e) {
+				synchronized(dialog) {
+					dialog.dispose();
+				}
+			}
+		});
+		tmpPanel = new Panel(new FlowLayout());
+		tmpPanel.add(okBtn);
+		dialog.add(tmpPanel, BorderLayout.SOUTH);
+	}
+	
+	@Override
+	protected void closeDialog() {
+		dialog.dispose();
+	}
+	
+	@Override
+	protected String readInputFromModalDialog(String promptText) {
+		prompt.setText(promptText);
+		dialog.pack();
+		dialog.setVisible(true);
+		return input.getText();
+	}
+	
+}

Added: trunk/abcl/src/org/armedbear/lisp/java/swing/SwingDialogPromptStream.java
==============================================================================
--- (empty file)
+++ trunk/abcl/src/org/armedbear/lisp/java/swing/SwingDialogPromptStream.java	Mon Jun 29 17:12:35 2009
@@ -0,0 +1,62 @@
+package org.armedbear.lisp.java.swing;
+
+import java.awt.BorderLayout;
+import java.awt.FlowLayout;
+import java.awt.Frame;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+
+import javax.swing.JButton;
+import javax.swing.JDialog;
+import javax.swing.JLabel;
+import javax.swing.JPanel;
+import javax.swing.JTextField;
+
+import org.armedbear.lisp.java.DialogPromptStream;
+
+public class SwingDialogPromptStream extends DialogPromptStream {
+
+	private JDialog dialog = new JDialog((Frame)null, true);
+	private JLabel prompt = new JLabel();
+	private JTextField input = new JTextField(32);
+	
+	public SwingDialogPromptStream() {
+		this("Prompt");
+	}
+	
+	public SwingDialogPromptStream(String title) {
+		super();
+		dialog.setTitle(title);
+		JPanel tmpPanel = new JPanel();
+		tmpPanel.add(prompt);
+		tmpPanel.add(input);
+		dialog.add(tmpPanel);
+		JButton okBtn = new JButton("Ok");
+		okBtn.addActionListener(new ActionListener() {
+
+			@Override
+			public void actionPerformed(ActionEvent e) {
+				synchronized(dialog) {
+					dialog.dispose();
+				}
+			}
+		});
+		tmpPanel = new JPanel(new FlowLayout());
+		tmpPanel.add(okBtn);
+		dialog.add(tmpPanel, BorderLayout.SOUTH);
+	}
+	
+	@Override
+	protected void closeDialog() {
+		dialog.dispose();
+	}
+	
+	@Override
+	protected String readInputFromModalDialog(String promptText) {
+		prompt.setText(promptText);
+		dialog.pack();
+		dialog.setVisible(true);
+		return input.getText();
+	}
+	
+}




More information about the armedbear-cvs mailing list