Skip to content

Commit 452f837

Browse files
committed
refine command input, uses text field and layout
1 parent 0a13191 commit 452f837

File tree

2 files changed

+98
-57
lines changed

2 files changed

+98
-57
lines changed

src/ui/KeyHandler.java

Lines changed: 86 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,60 @@
11
package ui;
22

3+
import java.awt.BorderLayout;
34
import 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;
612
import script.ExecutionErrorException;
713
import 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
}

src/ui/PaintPanel.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package ui;
22

3+
import java.awt.BorderLayout;
34
import java.awt.Cursor;
45
import java.awt.Graphics;
56
import java.awt.Point;
7+
import java.awt.Rectangle;
68
import java.awt.event.KeyEvent;
79
import java.awt.event.KeyListener;
810
import java.awt.event.MouseEvent;
@@ -11,6 +13,7 @@
1113
import java.awt.image.BufferedImage;
1214
import java.util.ArrayList;
1315

16+
import javax.swing.JLabel;
1417
import javax.swing.JPanel;
1518

1619
import paintcomponents.PaintComponent;
@@ -20,7 +23,7 @@
2023

2124
public class PaintPanel extends JPanel implements ToolBarListener {
2225

23-
public ArrayList<PaintComponent> components;
26+
private ArrayList<PaintComponent> components;
2427

2528

2629
enum State {
@@ -80,6 +83,10 @@ private void resetTool() {
8083

8184
public PaintPanel() {
8285
requestFocusInWindow();
86+
87+
88+
setLayout(new BorderLayout());
89+
8390

8491
this.components = new ArrayList<>();
8592
this.keyHandler = new KeyHandler(this);
@@ -128,19 +135,20 @@ public void mouseDragged(MouseEvent e) {
128135

129136
@Override
130137
public void keyTyped(KeyEvent e) {
131-
138+
keyHandler.keyEntered(e);
132139
}
133140

134141
@Override
135142
public void keyReleased(KeyEvent e) {
136143
switch (e.getKeyCode()) {
137144
case KeyEvent.VK_ESCAPE:
138-
resetTool();
145+
146+
if(state == State.TOOLS)
147+
resetTool();
139148
break;
140149

141150

142151
default:
143-
keyHandler.keyPressed(e);
144152
break;
145153
}
146154
}

0 commit comments

Comments
 (0)