From b021de559408fa2d12a4aae2fa2e822fe1afdb38 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Wed, 29 Sep 2021 13:16:57 +0200 Subject: [PATCH] Replaces the "Remove server" button with an (X) on the tab itself --- .../profile/Profile.java | 8 ++-- .../userinterface/CloseTabActionListener.java | 47 +++++++++++++++++++ .../userinterface/ServerLauncherGUI.java | 36 ++++++++++++++ .../userinterface/ServerTab.java | 26 +--------- 4 files changed, 90 insertions(+), 27 deletions(-) create mode 100644 src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/CloseTabActionListener.java diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/profile/Profile.java b/src/main/java/net/knarcraft/minecraftserverlauncher/profile/Profile.java index 57b442c..397375f 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/profile/Profile.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/profile/Profile.java @@ -141,14 +141,16 @@ public class Profile { * @param name

The name of the collection and its elements

*/ 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 \";\"."); } } diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/CloseTabActionListener.java b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/CloseTabActionListener.java new file mode 100644 index 0000000..4ecb8db --- /dev/null +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/CloseTabActionListener.java @@ -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

The tab pane containing all tabs

+ * @param tabName

The name of the tab connected to this action listener

+ */ + 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(); + } + } + } + +} \ No newline at end of file diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/ServerLauncherGUI.java b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/ServerLauncherGUI.java index b7f4f00..d4a7d9b 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/ServerLauncherGUI.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/ServerLauncherGUI.java @@ -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

The name of the server/tab to add a close button to

+ */ + 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 * diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/ServerTab.java b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/ServerTab.java index dc6957d..a5bf3be 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/ServerTab.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/ServerTab.java @@ -23,11 +23,9 @@ public class ServerTab implements ActionListener { private final JComboBox serverVersions; private final JComboBox 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

If unable to create the new tab

*/ 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 */