Added structure and components to ConnectToServerPanel.

This commit is contained in:
WickedJack99
2023-12-04 01:30:23 +01:00
parent 3a424c1faa
commit 948a1d0458
3 changed files with 385 additions and 119 deletions

View File

@@ -1,119 +0,0 @@
/**
* @author Aaron Moser
* @date 02.12.2023
*/
package gui.src.panels;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import gui.src.constants.GUIConstants.Languages;
import gui.src.constants.GUIConstants.Theme;
import gui.src.interfaces.Themeable;
import gui.src.interfaces.Translateable;
public final class ConnectToServerPanel extends JPanel implements Themeable, Translateable {
private static ConnectToServerPanel instance;
private JRadioButton[] ipVersionRadioButtons;
private final String[] translationsDE = {
"Verbinden Sie sich mit dem Server",
"IPv4",
"IPv6"
};
private final String[] translationsEN = {
"Connect to server",
"IPv4",
"IPv6"
};
public static ConnectToServerPanel getInstance() {
if (null == instance) {
instance = new ConnectToServerPanel();
}
return instance;
}
private ConnectToServerPanel() {
this.build();
}
/**
* Builds ConnectToServerPanel by setting the layout, creating
* interactable elements, setting their language and theme.
*/
private void build() {
this.setLayout(new GridBagLayout());
GridBagConstraints gridBagConstraints = new GridBagConstraints();
this.buildIPVersionRadioButtons();
this.changeLanguage(Languages.German);
}
private void buildIPVersionRadioButtons() {
this.ipVersionRadioButtons = new JRadioButton[2];
this.ipVersionRadioButtons[0] = new JRadioButton();
this.ipVersionRadioButtons[1] = new JRadioButton();
}
public void setThemeColor(Theme themeColor) {
try {
switch (themeColor) {
case Dark: {
}break;
case Light: {
}break;
default: {
throw new Exception("Unknown kind of theme color.");
}
}
} catch (Exception e) {
System.err.println(e.getMessage());
this.setThemeColor(Theme.Light);
}
}
/**
* Changes text of panel elements.
* Panel elements are: descriptive text, ip version radio buttons,
* ip address field descriptive text, port field descriptive text,
* connect button text.
* @param language the language the elements to set to.
*/
public void changeLanguage(Languages language) {
changeLanguageIPVersionRadioButtons(language);
}
/**
* Changes translation of radio buttons for selection of ip-version.
* Prints an error message if the language is unknown.
* @param language the language the elements to set to.
*/
private void changeLanguageIPVersionRadioButtons(Languages language) {
try {
switch (language) {
case German: {
ipVersionRadioButtons[0].setText(translationsDE[1]);
ipVersionRadioButtons[1].setText(translationsDE[2]);
}break;
case English: {
ipVersionRadioButtons[0].setText(translationsEN[1]);
ipVersionRadioButtons[1].setText(translationsEN[2]);
}break;
default: {
throw new Exception("Unknown kind of language.");
}
}
} catch (Exception e) {
System.err.println(e.getMessage());
this.changeLanguageIPVersionRadioButtons(Languages.German);
}
}
}

View File

@@ -0,0 +1,23 @@
/**
* @author Aaron Moser
* @date 03.12.2023
*/
package gui.src.panels.ConnectToServerPanel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import data.src.ViewToControllerData;
import data.src.ViewToControllerData.ViewEvent;
import queues.src.ViewToControllerQueue;
public class ConnectToServerButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("Connect to server button was pressed.");
ViewToControllerData viewToControllerData = new ViewToControllerData();
viewToControllerData.setViewEvent(ViewEvent.ConnectToServerButtonWasPressed);
ViewToControllerQueue.getInstance().add(viewToControllerData);
}
}

View File

@@ -0,0 +1,362 @@
/**
* @author Aaron Moser
* @date 02.12.2023
*/
package gui.src.panels.ConnectToServerPanel;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.Queue;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import data.src.ControllerToViewData;
import data.src.ViewToControllerData;
import javax.swing.JLabel;
import gui.src.constants.GUIConstants.Languages;
import gui.src.constants.GUIConstants.Theme;
import gui.src.interfaces.Themeable;
import gui.src.interfaces.Translateable;
public final class ConnectToServerPanel extends JPanel implements Themeable, Translateable {
private static ConnectToServerPanel instance;
private static Queue<ControllerToViewData> controllerToViewQueue;
private static Queue<ViewToControllerData> viewToControllerQueue;
private JLabel mainMessageLabel;
private JLabel ipAddressLabel;
private JLabel portLabel;
private JRadioButton[] ipVersionRadioButtons;
private ButtonGroup ipVersionRadioButtonsGroup;
private JTextField ipAddressTextField;
private JTextField portTextField;
private JButton connectButton;
private final String[] translationsDE = {
"Verbinden Sie sich mit dem Server",
"IPv4",
"IPv6",
"IP Adresse",
"Port",
"Verbinden"
};
private final String[] translationsEN = {
"Connect to server",
"IPv4",
"IPv6",
"IP address",
"Port",
"Connect"
};
public static ConnectToServerPanel getInstance() {
if (null == instance) {
instance = new ConnectToServerPanel();
}
return instance;
}
private ConnectToServerPanel() {
this.createComponents();
this.addComponents();
this.changeLanguage(Languages.German);
}
/**
* Creates interactable components.
*/
private void createComponents() {
this.createMainMessageLabel();
this.createIPVersionRadioButtons();
this.createIPAddressLabel();
this.createIPAddressTextField();
this.createPortLabel();
this.createPortTextField();
this.createConnectButton();
}
private void addComponents() {
this.setLayout(new GridBagLayout());
this.addMainMessageLabel();
this.addIPVersionRadioButtons();
this.addIPAddressLabel();
this.addIPAddressTextField();
this.addPortLabel();
this.addPortTextField();
this.addConnectButton();
}
private void createMainMessageLabel() {
this.mainMessageLabel = new JLabel();
}
private void addMainMessageLabel() {
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.weightx = 1;
gridBagConstraints.weighty = 1;
gridBagConstraints.insets = new Insets(0, 0, 0, 0);
this.add(this.mainMessageLabel, gridBagConstraints);
}
private void createIPVersionRadioButtons() {
this.ipVersionRadioButtonsGroup = new ButtonGroup();
this.ipVersionRadioButtons = new JRadioButton[2];
this.ipVersionRadioButtons[0] = new JRadioButton();
this.ipVersionRadioButtons[1] = new JRadioButton();
this.ipVersionRadioButtonsGroup.add(this.ipVersionRadioButtons[0]);
this.ipVersionRadioButtonsGroup.add(this.ipVersionRadioButtons[1]);
this.ipVersionRadioButtons[0].setSelected(true);
}
private void addIPVersionRadioButtons() {
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.weightx = 1;
gridBagConstraints.weighty = 1;
gridBagConstraints.insets = new Insets(0, 0, 0, 0);
this.add(this.ipVersionRadioButtons[0], gridBagConstraints);
gridBagConstraints.gridy = 2;
this.add(this.ipVersionRadioButtons[1], gridBagConstraints);
}
private void createIPAddressLabel() {
this.ipAddressLabel = new JLabel();
}
private void addIPAddressLabel() {
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 3;
gridBagConstraints.weightx = 1;
gridBagConstraints.weighty = 1;
gridBagConstraints.insets = new Insets(0, 0, 0, 0);
this.add(this.ipAddressLabel, gridBagConstraints);
}
private void createIPAddressTextField() {
this.ipAddressTextField = new JTextField(24);
}
private void addIPAddressTextField() {
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 4;
gridBagConstraints.weightx = 1;
gridBagConstraints.weighty = 1;
gridBagConstraints.insets = new Insets(0, 0, 0, 0);
this.add(this.ipAddressTextField, gridBagConstraints);
}
private void createPortLabel() {
this.portLabel = new JLabel();
}
private void addPortLabel() {
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 5;
gridBagConstraints.weightx = 1;
gridBagConstraints.weighty = 1;
gridBagConstraints.insets = new Insets(0, 0, 0, 0);
this.add(this.portLabel, gridBagConstraints);
}
private void createPortTextField() {
this.portTextField = new JTextField(24);
}
private void addPortTextField() {
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 6;
gridBagConstraints.weightx = 1;
gridBagConstraints.weighty = 1;
gridBagConstraints.insets = new Insets(0, 0, 0, 0);
this.add(this.portTextField, gridBagConstraints);
}
private void createConnectButton() {
this.connectButton = new JButton();
this.connectButton.addActionListener(new ConnectToServerButtonListener());
}
private void addConnectButton() {
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 7;
gridBagConstraints.weightx = 1;
gridBagConstraints.weighty = 1;
gridBagConstraints.insets = new Insets(0, 0, 0, 0);
this.add(this.connectButton, gridBagConstraints);
}
public void setThemeColor(Theme themeColor) {
try {
switch (themeColor) {
case Dark: {
}break;
case Light: {
}break;
default: {
throw new Exception("Unknown kind of theme color.");
}
}
} catch (Exception e) {
System.err.println(e.getMessage());
this.setThemeColor(Theme.Light);
}
}
/**
* Changes text of panel elements.
* Panel elements are: descriptive text, ip version radio buttons,
* ip address field descriptive text, port field descriptive text,
* connect button text.
* @param language the language the elements to set to.
*/
public void changeLanguage(Languages language) {
changeLanguageMainMessageLabel(language);
changeLanguageIPVersionRadioButtons(language);
changeLanguageIPAddressLabel(language);
changeLanguagePortLabel(language);
changeLanguageConnectButton(language);
}
/**
* Changes translation of radio buttons for selection of ip-version.
* Prints an error message if the language is unknown.
* @param language the language the elements to set to.
*/
private void changeLanguageIPVersionRadioButtons(Languages language) {
try {
switch (language) {
case German: {
ipVersionRadioButtons[0].setText(translationsDE[1]);
ipVersionRadioButtons[1].setText(translationsDE[2]);
}break;
case English: {
ipVersionRadioButtons[0].setText(translationsEN[1]);
ipVersionRadioButtons[1].setText(translationsEN[2]);
}break;
default: {
throw new Exception("Unknown kind of language.");
}
}
} catch (Exception e) {
System.err.println(e.getMessage());
this.changeLanguageIPVersionRadioButtons(Languages.German);
}
}
private void changeLanguageMainMessageLabel(Languages language) {
try {
switch (language) {
case German: {
mainMessageLabel.setText(translationsDE[0]);
}break;
case English: {
mainMessageLabel.setText(translationsEN[0]);
}break;
default: {
throw new Exception("Unknown kind of language.");
}
}
} catch (Exception e) {
System.err.println(e.getMessage());
this.changeLanguageMainMessageLabel(Languages.German);
}
}
private void changeLanguageIPAddressLabel(Languages language) {
try {
switch (language) {
case German: {
ipAddressLabel.setText(translationsDE[3]);
}break;
case English: {
ipAddressLabel.setText(translationsEN[3]);
}break;
default: {
throw new Exception("Unknown kind of language.");
}
}
} catch (Exception e) {
System.err.println(e.getMessage());
this.changeLanguageIPAddressLabel(Languages.German);
}
}
private void changeLanguagePortLabel(Languages language) {
try {
switch (language) {
case German: {
this.portLabel.setText(translationsDE[4]);
}break;
case English: {
this.portLabel.setText(translationsEN[4]);
}break;
default: {
throw new Exception("Unknown kind of language.");
}
}
} catch (Exception e) {
System.err.println(e.getMessage());
this.changeLanguagePortLabel(Languages.German);
}
}
private void changeLanguageConnectButton(Languages language) {
try {
switch (language) {
case German: {
this.connectButton.setText(translationsDE[5]);
}break;
case English: {
this.connectButton.setText(translationsEN[5]);
}break;
default: {
throw new Exception("Unknown kind of language.");
}
}
} catch (Exception e) {
System.err.println(e.getMessage());
this.changeLanguagePortLabel(Languages.German);
}
}
public static void setControllerToViewQueue(Queue<ControllerToViewData> newControllerToViewQueue) {
controllerToViewQueue = newControllerToViewQueue;
}
public static void setViewToControllerQueue(Queue<ViewToControllerData> newViewToControllerQueue) {
viewToControllerQueue = newViewToControllerQueue;
}
}