11package ui ;
22
3+ import java .awt .BorderLayout ;
34import java .awt .event .KeyEvent ;
5+ import java .awt .event .KeyListener ;
6+ import java .util .Iterator ;
7+ import java .util .LinkedList ;
8+ import java .util .ListIterator ;
9+
10+ import javax .swing .JTextField ;
411
5- import paintcomponents .TextPaintComponent ;
612import script .ExecutionErrorException ;
713import script .Interpreter ;
814
9- public class KeyHandler {
15+ public class KeyHandler implements KeyListener {
1016
11- private static final int VERTICAL_OFFSET = 5 ;
17+ //the prompt does not work, i.e. focusing issues
18+ private static final String PROMPT = "" ;
1219 private PaintPanel paintPanel ;
13- private String pendingCommand ;
1420 private boolean inCommandMode ;
15- private TextPaintComponent component ;
1621 Interpreter interpreter ;
22+
23+
24+ JTextField textField ;
25+
26+ ListIterator <String > commandHistoryIter ;
27+
28+
29+
1730 public KeyHandler (PaintPanel paintPanel ) {
18- pendingCommand = "" ;
1931 this .paintPanel = paintPanel ;
2032
21- //initialize text component
22- this .component = new TextPaintComponent ("" , 0 , 0 );
23- this .paintPanel .addPaintComponent (this .component );
24-
2533 interpreter = new Interpreter (paintPanel );
2634
2735
28- //not in command
29- inCommandMode = false ;
36+ this .textField = new JTextField ("Press : to enter commands" );
37+ this .paintPanel .add (textField , BorderLayout .SOUTH );
38+
39+ this .textField .addKeyListener (this );
40+
41+ this .commandHistoryIter = new LinkedList <String >().listIterator ();
42+
43+
44+ // exitCommandMode();
3045 }
3146
32- public void keyPressed (KeyEvent e ) {
33-
34- // backspace
35- if (e .getKeyCode () == KeyEvent .VK_BACK_SPACE && inCommandMode ) {
36- if (pendingCommand .length () != 0 ) {
37- pendingCommand = pendingCommand .substring (0 ,
38- pendingCommand .length () - 1 );
39- } else {
40- inCommandMode = false ;
41- }
42-
43- } else if (e .getKeyCode () == KeyEvent .VK_ENTER && inCommandMode ) {
44-
45- executeCommand (pendingCommand );
46- pendingCommand = "" ;
47- inCommandMode = false ;
48- } else {
49-
50- char keyChar = e .getKeyChar ();
51-
52- if (keyChar == ':' ) {
53- pendingCommand = "" ;
54- inCommandMode = true ;
55- } else if (e .getKeyChar () != KeyEvent .CHAR_UNDEFINED
56- && inCommandMode )/* if(e.isActionKey()) */ {
57- pendingCommand += e .getKeyChar ();
58- }
59-
60- }
61-
62- update ();
47+ private void enterCommandMode () {
48+ inCommandMode = true ;
49+ textField .setVisible (true );
50+ textField .requestFocusInWindow ();
51+ textField .setText (PROMPT );
52+ }
6353
54+ private void exitCommandMode () {
55+ inCommandMode = false ;
56+ textField .setVisible (false );
57+ paintPanel .requestFocusInWindow ();
6458 }
6559
6660 private void executeCommand (String pendingCommand2 ) {
@@ -72,18 +66,57 @@ private void executeCommand(String pendingCommand2) {
7266 }
7367 }
7468
75- private void update () {
76- if (inCommandMode ) {
77- component .setDisplayingText (": " + pendingCommand );
69+ public void keyEntered (KeyEvent e ) {
70+ char keyChar = e .getKeyChar ();
71+
72+ if (keyChar == ':' ) {
73+ enterCommandMode ();
74+ }
75+ }
76+
77+ @ Override
78+ public void keyTyped (KeyEvent e ) {
79+ if (e .getKeyChar () == '\n' ){
80+ //get rid of the prompt
81+
82+ //record command
83+
84+ while (commandHistoryIter .hasNext ())
85+ this .commandHistoryIter .next ();
86+
87+ commandHistoryIter .add (textField .getText ());
88+
89+ executeCommand (textField .getText ().substring (PROMPT .length ()));
90+ exitCommandMode ();
91+ } else if (e .getKeyChar () == '\b' && textField .getText ().length () < PROMPT .length ()){
92+ //do not allow prompt to be deleted
93+ textField .setText (PROMPT );
94+
95+ //if it is escape
96+ }
97+
98+ }
99+
100+ public void keyPressed (KeyEvent e ) {
101+ if (e .getKeyCode () == KeyEvent .VK_ESCAPE ){
102+ exitCommandMode ();
78103
79- } else {
80- component .setDisplayingText (pendingCommand );
104+ //if it is up arrow, go back in history
105+ } else if (e .getKeyCode () == KeyEvent .VK_UP ){
106+ if (commandHistoryIter .hasPrevious ()){
107+ this .textField .setText (commandHistoryIter .previous ());
108+ }
109+ } else if (e .getKeyCode () == KeyEvent .VK_DOWN ){
110+ if (commandHistoryIter .hasNext ()){
111+ this .textField .setText (commandHistoryIter .next ());
112+ }
81113 }
82- int height = paintPanel .getHeight ();
83- int rowHeight = component .getRowHeight ();
84- component .setX (0 );
85- component .setY (height - rowHeight - VERTICAL_OFFSET );
86- paintPanel .repaint ();
114+
115+ }
116+
117+ @ Override
118+ public void keyReleased (KeyEvent e ) {
119+
87120 }
88121
89122}
0 commit comments