It is possible to write to server consoles

This commit is contained in:
2018-01-31 21:24:54 +01:00
parent 30263d7753
commit 2591f3ed05
8 changed files with 86 additions and 36 deletions

View File

@ -1,7 +1,14 @@
package net.knarcraft.serverlauncher.userinterface;
import net.knarcraft.serverlauncher.profile.Profile;
import javax.swing.*;
import javax.swing.text.DefaultCaret;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import static javax.swing.text.DefaultCaret.ALWAYS_UPDATE;
/**
* Acts as a single writable/readable tab
@ -11,15 +18,13 @@ import java.awt.*;
* @version 0.0.0.1
* @since 0.0.0.1
*/
public class Console {
public class Console implements ActionListener {
private final JTextField textInput;
private final JTextArea textOutput;
private final String name;
void output(String text) {
this.textOutput.setText(text);
}
public Console(JTabbedPane tab, String name) {
Console(JTabbedPane tab, String name) {
this.name = name;
JPanel panel = new JPanel();
tab.addTab(name, null, panel, null);
panel.setLayout(new BorderLayout(0, 0));
@ -27,10 +32,28 @@ public class Console {
textInput = new JTextField();
panel.add(textInput, BorderLayout.SOUTH);
textInput.setColumns(10);
textInput.addActionListener(this);
textOutput = new JTextArea();
JScrollPane scroll = new JScrollPane(textOutput);
panel.add(scroll, BorderLayout.CENTER);
textOutput.setEditable(false);
DefaultCaret caret = (DefaultCaret) textOutput.getCaret();
caret.setUpdatePolicy(ALWAYS_UPDATE);
textOutput.setLineWrap(true);
}
public void output(String text) {
this.textOutput.setText(this.textOutput.getText() + "\n" + text);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == textInput) {
String text = textInput.getText();
Profile profile = GUI.getGUI().currentProfile();
profile.sendCommand(this.name, text);
textInput.setText("");
}
}
}

View File

@ -101,17 +101,18 @@ public class GUI implements ActionListener {
}
/**
* Removes a server's tab, removes it from the list of tabs, and removes the server from Profile.java
* Removes a server from the profile's collection, the list of tabs, and the actual tabs.
*
* @param tab The current tab object
* @param tab The tab object where remove was clicked
*/
public void removeServer(ServerTab tab) {
for (int i = 0; i < this.serverTabs.size(); i++) {
if(this.serverTabs.get(i) == tab) {
serversPane.remove(i);
currentProfile().removeCollection(i);
this.serversPane.remove(i);
this.currentProfile().removeCollection(i);
ServerConsoles.getGUI().removeTab(i);
this.serverTabs.remove(i);
i = serverTabs.size();
i = this.serverTabs.size();
}
}
@ -121,11 +122,11 @@ public class GUI implements ActionListener {
* Initialize the contents of the frame.
*/
private void initialize() {
/*try {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | UnsupportedLookAndFeelException | InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}*/
}
frame = new JFrame("Minecraft server launcher");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
@ -475,8 +476,7 @@ public class GUI implements ActionListener {
currentProfile().sendCommand(selectedServerValue, "reload");
}
} else if (e.getSource() == btnServerConsoles) {
//TODO: Make server consoles window, and toggle visibility on this action.
ServerConsoles.getGUI().show();
ServerConsoles.show();
}
}

View File

@ -3,7 +3,6 @@ package net.knarcraft.serverlauncher.userinterface;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import java.awt.BorderLayout;
import java.util.ArrayList;
/**
* A parent window for server consoles
@ -16,8 +15,8 @@ import java.util.ArrayList;
*/
public class ServerConsoles {
private static ServerConsoles serverConsoles;
private final JFrame frame;
private final JTabbedPane consolesTab;
private static JFrame frame;
private static JTabbedPane consolesTab;
public ServerConsoles() {
frame = new JFrame();
@ -33,10 +32,14 @@ public class ServerConsoles {
return serverConsoles;
}
public void show() {
public static void show() {
frame.setVisible(true);
}
public void removeTab(int i) {
consolesTab.remove(i);
}
public Console addTab(String name) {
return new Console(consolesTab, name);
}