2018-01-30 19:35:42 +01:00
|
|
|
package net.knarcraft.serverlauncher.userinterface;
|
|
|
|
|
2018-01-31 21:24:54 +01:00
|
|
|
import net.knarcraft.serverlauncher.profile.Profile;
|
|
|
|
|
2018-01-30 19:35:42 +01:00
|
|
|
import javax.swing.*;
|
2018-01-31 21:24:54 +01:00
|
|
|
import javax.swing.text.DefaultCaret;
|
2018-01-30 19:35:42 +01:00
|
|
|
import java.awt.*;
|
2018-01-31 21:24:54 +01:00
|
|
|
import java.awt.event.ActionEvent;
|
|
|
|
import java.awt.event.ActionListener;
|
2018-02-21 11:47:23 +01:00
|
|
|
import java.awt.event.KeyEvent;
|
|
|
|
import java.awt.event.KeyListener;
|
|
|
|
import java.util.ArrayList;
|
2018-01-31 21:24:54 +01:00
|
|
|
|
|
|
|
import static javax.swing.text.DefaultCaret.ALWAYS_UPDATE;
|
2018-01-30 19:35:42 +01:00
|
|
|
|
2018-01-30 19:46:14 +01:00
|
|
|
/**
|
|
|
|
* Acts as a single writable/readable tab
|
|
|
|
* Has a box for user input, and a textArea for server output.
|
|
|
|
*
|
|
|
|
* @author Kristian Knarvik <kristian.knarvik@knett.no>
|
2018-02-22 14:12:42 +01:00
|
|
|
* @version 1.0.0
|
|
|
|
* @since 1.0.0
|
2018-01-30 19:46:14 +01:00
|
|
|
*/
|
2018-02-21 11:47:23 +01:00
|
|
|
public class Console implements ActionListener, KeyListener {
|
2018-01-30 23:38:22 +01:00
|
|
|
private final JTextField textInput;
|
|
|
|
private final JTextArea textOutput;
|
2018-01-31 21:24:54 +01:00
|
|
|
private final String name;
|
2018-02-02 20:09:54 +01:00
|
|
|
private final JPanel panel;
|
2018-02-21 11:47:23 +01:00
|
|
|
private final ArrayList<String> commands = new ArrayList<>();
|
|
|
|
private int commandIndex;
|
2018-01-30 19:35:42 +01:00
|
|
|
|
2018-01-31 21:24:54 +01:00
|
|
|
Console(JTabbedPane tab, String name) {
|
|
|
|
this.name = name;
|
2018-02-02 20:09:54 +01:00
|
|
|
panel = new JPanel();
|
2018-01-30 19:35:42 +01:00
|
|
|
tab.addTab(name, null, panel, null);
|
|
|
|
panel.setLayout(new BorderLayout(0, 0));
|
|
|
|
textInput = new JTextField();
|
|
|
|
panel.add(textInput, BorderLayout.SOUTH);
|
|
|
|
textInput.setColumns(10);
|
2018-01-31 21:24:54 +01:00
|
|
|
textInput.addActionListener(this);
|
2018-01-30 19:35:42 +01:00
|
|
|
textOutput = new JTextArea();
|
|
|
|
JScrollPane scroll = new JScrollPane(textOutput);
|
|
|
|
panel.add(scroll, BorderLayout.CENTER);
|
|
|
|
textOutput.setEditable(false);
|
2018-01-31 21:24:54 +01:00
|
|
|
DefaultCaret caret = (DefaultCaret) textOutput.getCaret();
|
|
|
|
caret.setUpdatePolicy(ALWAYS_UPDATE);
|
|
|
|
textOutput.setLineWrap(true);
|
2018-02-21 11:47:23 +01:00
|
|
|
textInput.addKeyListener(this);
|
2018-01-31 21:24:54 +01:00
|
|
|
}
|
|
|
|
|
2018-02-02 20:09:54 +01:00
|
|
|
public JPanel getPanel() {
|
|
|
|
return this.panel;
|
|
|
|
}
|
|
|
|
|
2018-02-22 11:49:12 +01:00
|
|
|
/**
|
|
|
|
* Prints a string to the textArea.
|
|
|
|
*
|
|
|
|
* @param text The text to print
|
|
|
|
*/
|
2018-02-05 00:00:51 +01:00
|
|
|
public void output(String text) {
|
|
|
|
this.textOutput.setText(this.textOutput.getText() + "\n" + text);
|
|
|
|
}
|
|
|
|
|
2018-01-31 21:24:54 +01:00
|
|
|
@Override
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
2018-02-02 22:02:57 +01:00
|
|
|
if (e.getSource() == textInput) { //Sends the command from the input to the server with the same name.
|
2018-02-21 11:47:23 +01:00
|
|
|
java.lang.String text = textInput.getText();
|
2018-02-02 22:02:57 +01:00
|
|
|
Profile.getCurrent().sendCommand(this.name, text);
|
2018-02-21 11:47:23 +01:00
|
|
|
commands.add(text);
|
|
|
|
if (commands.size() > 25) {
|
|
|
|
commands.remove(0);
|
|
|
|
}
|
|
|
|
commandIndex = commands.size();
|
2018-01-31 21:24:54 +01:00
|
|
|
textInput.setText("");
|
|
|
|
}
|
2018-01-30 19:35:42 +01:00
|
|
|
}
|
2018-02-21 11:47:23 +01:00
|
|
|
|
|
|
|
@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) {
|
|
|
|
}
|
2018-01-30 19:35:42 +01:00
|
|
|
}
|