Consoles can now store and retrieve the last commands typed

This commit is contained in:
Kristian Knarvik 2018-02-21 11:47:23 +01:00
parent 2ff3c765a6
commit 97382d035b

View File

@ -7,6 +7,9 @@ import javax.swing.text.DefaultCaret;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import static javax.swing.text.DefaultCaret.ALWAYS_UPDATE;
@ -18,11 +21,13 @@ import static javax.swing.text.DefaultCaret.ALWAYS_UPDATE;
* @version 0.0.0.1
* @since 0.0.0.1
*/
public class Console implements ActionListener {
public class Console implements ActionListener, KeyListener {
private final JTextField textInput;
private final JTextArea textOutput;
private final String name;
private final JPanel panel;
private final ArrayList<String> commands = new ArrayList<>();
private int commandIndex;
Console(JTabbedPane tab, String name) {
this.name = name;
@ -40,6 +45,7 @@ public class Console implements ActionListener {
DefaultCaret caret = (DefaultCaret) textOutput.getCaret();
caret.setUpdatePolicy(ALWAYS_UPDATE);
textOutput.setLineWrap(true);
textInput.addKeyListener(this);
}
public JPanel getPanel() {
@ -53,9 +59,40 @@ public class Console implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == textInput) { //Sends the command from the input to the server with the same name.
String text = textInput.getText();
java.lang.String text = textInput.getText();
Profile.getCurrent().sendCommand(this.name, text);
commands.add(text);
if (commands.size() > 25) {
commands.remove(0);
}
commandIndex = commands.size();
textInput.setText("");
}
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP) {
if (commands.size() > 0 && commandIndex > 0) {
textInput.setText(commands.get(--commandIndex));
}
} else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
if (commands.size() > 0) {
if (commandIndex == commands.size() - 1) {
commandIndex++;
textInput.setText("");
} else if (commandIndex >= 0 && commandIndex <= commands.size() - 1) {
textInput.setText(commands.get(++commandIndex));
}
}
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
}