Commit e2fecdf9 authored by Joost Rijneveld's avatar Joost Rijneveld
Browse files

Added 'trap 2' and 'trap 3' for int/char prompts

parent 574228ee
......@@ -163,6 +163,8 @@ public class Instruction
*/
protected final static int TR_PR_INT = 0 ;
protected final static int TR_PR_CHAR = 1 ;
protected final static int TR_IN_INT = 2 ;
protected final static int TR_IN_CHAR = 3 ;
/**
* Metas
......
......@@ -490,6 +490,12 @@ public class Machine
messenger.println("Error: UTF-32 encoding missing.");
}
break;
case Instruction.TR_IN_INT :
push(messenger.promptInt());
break;
case Instruction.TR_IN_CHAR :
push(messenger.promptChar());
break;
default : break ;
}
break ;
......
......@@ -12,4 +12,16 @@ public interface Messenger
{
public void println( String s ) ;
/**
* Asks the user for an integer value (i.e. via a dialog)
* @return Returns the integer value as provided by the user
*/
public int promptInt();
/**
* Asks the user for a character (i.e. via a dialog)
* @return Returns the unicode integer code point of the provided character
*/
public int promptChar();
}
\ No newline at end of file
......@@ -12,6 +12,8 @@ import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileReader;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Vector;
......@@ -22,6 +24,7 @@ import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollBar;
import javax.swing.JTable;
import javax.swing.KeyStroke;
......@@ -653,6 +656,42 @@ public class SSMRunner extends JFrame
outputTextArea.append( s ) ;
outputTextArea.append( "\n" ) ;
}
public int promptInt() {
while(true) {
try {
String s = JOptionPane.showInputDialog(this,
"Please enter an integer.","Integer requested", JOptionPane.QUESTION_MESSAGE);
if (s == null) {
return 0;
}
return Integer.parseInt(s);
}
catch (NumberFormatException e) {
continue; // Ask again
}
}
}
public int promptChar() {
while(true) {
String s = JOptionPane.showInputDialog(this,
"Please enter a character.","Character requested", JOptionPane.QUESTION_MESSAGE);
if (s == null) {
return 0;
}
if (s.length() == 0) {
continue;
}
byte[] b = {};
try {
b = s.getBytes("UTF-32");
} catch (UnsupportedEncodingException e) {
println("Error: UTF-32 encoding missing.");
}
return ByteBuffer.wrap(b).getInt();
}
}
// Close the window when the close box is clicked
void thisWindowClosing(java.awt.event.WindowEvent e)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment