Replaces the "Remove server" button with an (X) on the tab itself
All checks were successful
KnarCraft/Minecraft-Server-Launcher/pipeline/head This commit looks good

This commit is contained in:
Kristian Knarvik 2021-09-29 13:16:57 +02:00
parent a1ae162b07
commit b021de5594
4 changed files with 90 additions and 27 deletions

View File

@ -141,14 +141,16 @@ public class Profile {
* @param name <p>The name of the collection and its elements</p>
*/
public void addCollection(String name) throws ConfigurationException {
if (name == null) { //If a user cancels or crosses out window
//Skip if no name was given
if (name == null) {
return;
}
if (getCollection(name) == null && !name.equals("All") && CommonFunctions.nameIsValid(name)) {
collections.add(new Collection(name));
} else {
serverLauncherGui.showError("A server name must my unique and not empty or \"All\"." +
"It can't contain any of the characters \"!\", \"?\" or \";\".");
serverLauncherGui.showError("A server name must be unique and not empty. " +
"In addition, a server cannot be named: \"All\". " +
"It cannot contain any of the characters \"!\", \"?\" or \";\".");
}
}

View File

@ -0,0 +1,47 @@
package net.knarcraft.minecraftserverlauncher.userinterface;
import net.knarcraft.minecraftserverlauncher.Main;
import net.knarcraft.minecraftserverlauncher.profile.ServerLauncherController;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* Listener for clicking the close button of a server tab
*/
public class CloseTabActionListener implements ActionListener {
private final String tabName;
private final JTabbedPane tabPane;
/**
* Instantiates a new close tab action listener
* @param tabPane <p>The tab pane containing all tabs</p>
* @param tabName <p>The name of the tab connected to this action listener</p>
*/
public CloseTabActionListener(JTabbedPane tabPane, String tabName) {
this.tabName = tabName;
this.tabPane = tabPane;
}
@Override
public void actionPerformed(ActionEvent evt) {
int index = tabPane.indexOfTab(tabName);
if (index >= 0) {
//Abort if the user just mis-clicked
int answer = JOptionPane.showConfirmDialog(null,
String.format("Do you really want to remove the server %s?", tabName), "Remove server",
JOptionPane.YES_NO_OPTION
);
if (answer == JOptionPane.YES_NO_OPTION) {
//Remove the server
ServerLauncherController controller = Main.getController();
controller.getCurrentProfile().removeCollection(tabName);
controller.getGUI().updateWithSavedProfileData();
controller.getCurrentProfile().updateConsoles();
}
}
}
}

View File

@ -9,6 +9,7 @@ import net.knarcraft.minecraftserverlauncher.utility.BackupUtil;
import javax.imageio.ImageIO;
import javax.naming.ConfigurationException;
import javax.swing.*;
import javax.swing.plaf.basic.BasicButtonUI;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
@ -160,9 +161,44 @@ public class ServerLauncherGUI extends MessageHandler implements ActionListener,
serverControlTab.update();
for (Collection collection : controller.getCurrentProfile().getCollections()) {
serversPane.addTab(collection.getName(), collection.getServerTab().getPanel());
addCloseButtonToServerTab(collection.getName());
}
}
/**
*
* @param serverName <p>The name of the server/tab to add a close button to</p>
*/
private void addCloseButtonToServerTab(String serverName) {
int index = serversPane.indexOfTab(serverName);
JPanel tabPanel = new JPanel(new GridBagLayout());
tabPanel.setOpaque(false);
JLabel serverTitleLabel = new JLabel(serverName);
JButton removeServerButton = new JButton("(X)");
removeServerButton.setBorder(BorderFactory.createEtchedBorder());
removeServerButton.setFocusable(false);
removeServerButton.setBorderPainted(false);
removeServerButton.setContentAreaFilled(false);
removeServerButton.setRolloverEnabled(true);
removeServerButton.setPreferredSize(new Dimension(18, 17));
removeServerButton.setUI(new BasicButtonUI());
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.weightx = 1;
tabPanel.add(serverTitleLabel, gridBagConstraints);
gridBagConstraints.gridx++;
gridBagConstraints.weightx = 0;
tabPanel.add(removeServerButton, gridBagConstraints);
serversPane.setTabComponentAt(index, tabPanel);
removeServerButton.addActionListener(new CloseTabActionListener(serversPane, serverName));
}
/**
* Initializes the server launcher GUI
*

View File

@ -23,11 +23,9 @@ public class ServerTab implements ActionListener {
private final JComboBox<String> serverVersions;
private final JComboBox<String> maxRam;
private final JCheckBox enabledCheckbox;
private final JButton removeServerButton;
private final JButton browseButton;
private final JTextField directory;
private final JPanel panel;
private final String name;
/**
* Initializes a new server tab with the given name
@ -36,7 +34,6 @@ public class ServerTab implements ActionListener {
* @throws ConfigurationException <p>If unable to create the new tab</p>
*/
public ServerTab(String name) throws ConfigurationException {
this.name = name;
panel = new JPanel();
Main.getController().getGUI().getPane().addTab(name, null, panel, null);
SpringLayout sl_panel_3 = new SpringLayout();
@ -88,14 +85,6 @@ public class ServerTab implements ActionListener {
panel.add(enabledCheckbox);
enabledCheckbox.addActionListener(this);
removeServerButton = new JButton("Remove server");
sl_panel_3.putConstraint(SpringLayout.NORTH, removeServerButton, 0, SpringLayout.NORTH, serverVersions);
sl_panel_3.putConstraint(SpringLayout.SOUTH, removeServerButton, 0, SpringLayout.SOUTH, serverVersions);
sl_panel_3.putConstraint(SpringLayout.WEST, removeServerButton, 6, SpringLayout.EAST, serverVersions);
sl_panel_3.putConstraint(SpringLayout.EAST, removeServerButton, -10, SpringLayout.EAST, panel);
panel.add(removeServerButton);
removeServerButton.addActionListener(this);
JLabel lblDirectory = new JLabel("Directory");
sl_panel_3.putConstraint(SpringLayout.WEST, lblDirectory, 6, SpringLayout.EAST, enabledCheckbox);
panel.add(lblDirectory);
@ -112,7 +101,7 @@ public class ServerTab implements ActionListener {
browseButton = new JButton("Browse");
sl_panel_3.putConstraint(SpringLayout.EAST, directory, -6, SpringLayout.WEST, browseButton);
sl_panel_3.putConstraint(SpringLayout.NORTH, browseButton, 3, SpringLayout.SOUTH, removeServerButton);
sl_panel_3.putConstraint(SpringLayout.NORTH, browseButton, 3, SpringLayout.SOUTH, serverVersions);
sl_panel_3.putConstraint(SpringLayout.EAST, browseButton, -10, SpringLayout.EAST, panel);
sl_panel_3.putConstraint(SpringLayout.SOUTH, directory, 0, SpringLayout.SOUTH, browseButton);
sl_panel_3.putConstraint(SpringLayout.NORTH, directory, 0, SpringLayout.NORTH, browseButton);
@ -209,9 +198,7 @@ public class ServerTab implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == removeServerButton) {
remove();
} else if (e.getSource() == browseButton) {
if (e.getSource() == browseButton) {
browse();
} else if (e.getSource() == serverTypes) {
try {
@ -222,15 +209,6 @@ public class ServerTab implements ActionListener {
}
}
/**
* Removes the collection containing this ServerTab, and updates everything necessary
*/
private void remove() {
Main.getController().getCurrentProfile().removeCollection(this.name);
Main.getController().getGUI().updateWithSavedProfileData();
Main.getController().getCurrentProfile().updateConsoles();
}
/**
* Asks the user for server location and updates the GUI if given a valid value
*/