Consoles can now store and retrieve the last commands typed
This commit is contained in:
parent
2ff3c765a6
commit
97382d035b
@ -7,6 +7,9 @@ import javax.swing.text.DefaultCaret;
|
|||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
import java.awt.event.ActionListener;
|
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;
|
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
|
* @version 0.0.0.1
|
||||||
* @since 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 JTextField textInput;
|
||||||
private final JTextArea textOutput;
|
private final JTextArea textOutput;
|
||||||
private final String name;
|
private final String name;
|
||||||
private final JPanel panel;
|
private final JPanel panel;
|
||||||
|
private final ArrayList<String> commands = new ArrayList<>();
|
||||||
|
private int commandIndex;
|
||||||
|
|
||||||
Console(JTabbedPane tab, String name) {
|
Console(JTabbedPane tab, String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
@ -40,6 +45,7 @@ public class Console implements ActionListener {
|
|||||||
DefaultCaret caret = (DefaultCaret) textOutput.getCaret();
|
DefaultCaret caret = (DefaultCaret) textOutput.getCaret();
|
||||||
caret.setUpdatePolicy(ALWAYS_UPDATE);
|
caret.setUpdatePolicy(ALWAYS_UPDATE);
|
||||||
textOutput.setLineWrap(true);
|
textOutput.setLineWrap(true);
|
||||||
|
textInput.addKeyListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public JPanel getPanel() {
|
public JPanel getPanel() {
|
||||||
@ -53,9 +59,40 @@ public class Console implements ActionListener {
|
|||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
if (e.getSource() == textInput) { //Sends the command from the input to the server with the same name.
|
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);
|
Profile.getCurrent().sendCommand(this.name, text);
|
||||||
|
commands.add(text);
|
||||||
|
if (commands.size() > 25) {
|
||||||
|
commands.remove(0);
|
||||||
|
}
|
||||||
|
commandIndex = commands.size();
|
||||||
textInput.setText("");
|
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) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user