Added functionality to select trust store and enter password for troststore.

This commit is contained in:
WickedJack99
2023-12-10 00:07:20 +01:00
parent 0913c24ace
commit f7da387607
3 changed files with 257 additions and 43 deletions

View File

@@ -0,0 +1,50 @@
/**
* @author Aaron Moser
* @date 09.12.2023
*/
package gui.src.panels.ConnectToServerPanel;
import data.src.ViewToController.ConnectToServerData;
import gui.src.constants.GUIConstants;
import gui.src.panels.StatusPanel;
import queues.src.ViewToControllerQueue;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ConnectToServerButton extends JButton implements ActionListener {
private JTextField ipAddressTextField;
private JTextField portTextField;
private JTextField trustStorePasswordTextField;
public ConnectToServerButton(JTextField ipAddressTextField, JTextField portTextField, JTextField trustStorePasswordTextField) {
this.ipAddressTextField = ipAddressTextField;
this.portTextField = portTextField;
this.trustStorePasswordTextField = trustStorePasswordTextField;
this.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Connect to server button was pressed.");
ConnectToServerPanel.getInstance().disableFields();
// Reads in fields text
String ipAddress = this.ipAddressTextField.getText();
int port = Integer.parseInt(this.portTextField.getText());
String trustStorePath = ConnectToServerPanel.getInstance().getServerCertStoreFilePath();
String trustStorePassword = this.trustStorePasswordTextField.getText();
StatusPanel.getInstance().setStatus(GUIConstants.Status.Connecting);
// Sends data to queue
ViewToControllerQueue.getInstance().add(new ConnectToServerData(
ipAddress,
port,
trustStorePath,
trustStorePassword
));
}
}

View File

@@ -4,22 +4,9 @@
*/
package gui.src.panels.ConnectToServerPanel;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.Queue;
import java.awt.*;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import data.src.ControllerToView.ControllerToViewData;
import data.src.ControllerToView.ControllerToViewData.ControllerEvent;
import data.src.ViewToController.ViewToControllerData;
import javax.swing.JLabel;
import javax.swing.*;
import gui.src.constants.GUIConstants.Languages;
import gui.src.constants.GUIConstants.Theme;
@@ -28,22 +15,23 @@ import gui.src.interfaces.Themeable;
import gui.src.interfaces.Translateable;
public final class ConnectToServerPanel extends JPanel implements Themeable, Translateable {
private String serverCertStoreFilePath;
private static ConnectToServerPanel instance;
private static Queue<ControllerToViewData> controllerToViewQueue;
private static Queue<ViewToControllerData> viewToControllerQueue;
private JLabel mainMessageLabel;
private JLabel ipAddressLabel;
private JLabel portLabel;
private JLabel serverCertStoreLabel;
private JLabel serverCertStorePasswordLabel;
private JRadioButton[] ipVersionRadioButtons;
private ButtonGroup ipVersionRadioButtonsGroup;
private JTextField ipAddressTextField;
private JTextField portTextField;
private JTextField trustStorePasswordTextField;
private JButton connectButton;
private JButton trustStoreFileChooserButton;
private final String[] translationsDE = {
"Verbinden Sie sich mit dem Server",
@@ -51,6 +39,9 @@ public final class ConnectToServerPanel extends JPanel implements Themeable, Tra
"IPv6",
"IP Adresse",
"Port",
"Waehlen Sie den Trust Store, welcher das Server-Zertifikat enthaelt.",
"Datei waehlen",
"Trust Store Passwort",
"Verbinden"
};
private final String[] translationsEN = {
@@ -59,6 +50,9 @@ public final class ConnectToServerPanel extends JPanel implements Themeable, Tra
"IPv6",
"IP address",
"Port",
"Select the trust store, which contains the server certificate",
"Choose file",
"Trust store password",
"Connect"
};
@@ -80,22 +74,39 @@ public final class ConnectToServerPanel extends JPanel implements Themeable, Tra
*/
private void createComponents() {
this.createMainMessageLabel();
this.createIPVersionRadioButtons();
this.createIPAddressLabel();
this.createIPAddressTextField();
this.createPortLabel();
this.createPortTextField();
this.createServerCertStoreLabel();
this.createServerCertStoreFileChooserButton();
this.createTrustStorePasswordLabel();
this.createTrustStorePasswordTextField();
this.createConnectButton();
}
private void addComponents() {
this.setLayout(new GridBagLayout());
this.addMainMessageLabel();
this.addIPVersionRadioButtons();
this.addIPAddressLabel();
this.addIPAddressTextField();
this.addPortLabel();
this.addPortTextField();
this.addServerCertStoreLabel();
this.addServerCertStoreFileChooserButton();
this.addTrustStorePasswordLabel();
this.addTrustStorePasswordTextField();
this.addConnectButton();
}
@@ -110,20 +121,20 @@ public final class ConnectToServerPanel extends JPanel implements Themeable, Tra
gridBagConstraints.gridy = 0;
gridBagConstraints.weightx = 1;
gridBagConstraints.weighty = 1;
gridBagConstraints.insets = new Insets(0, 0, 0, 0);
gridBagConstraints.insets = new Insets(0, 0, 10, 0);
this.add(this.mainMessageLabel, gridBagConstraints);
}
private void createIPVersionRadioButtons() {
this.ipVersionRadioButtonsGroup = new ButtonGroup();
ButtonGroup 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]);
ipVersionRadioButtonsGroup.add(this.ipVersionRadioButtons[0]);
ipVersionRadioButtonsGroup.add(this.ipVersionRadioButtons[1]);
this.ipVersionRadioButtons[0].setSelected(true);
}
@@ -152,7 +163,7 @@ public final class ConnectToServerPanel extends JPanel implements Themeable, Tra
gridBagConstraints.gridy = 3;
gridBagConstraints.weightx = 1;
gridBagConstraints.weighty = 1;
gridBagConstraints.insets = new Insets(0, 0, 0, 0);
gridBagConstraints.insets = new Insets(10, 0, 0, 0);
this.add(this.ipAddressLabel, gridBagConstraints);
}
@@ -201,19 +212,78 @@ public final class ConnectToServerPanel extends JPanel implements Themeable, Tra
this.add(this.portTextField, gridBagConstraints);
}
private void createConnectButton() {
this.connectButton = new JButton();
this.connectButton.addActionListener(new ConnectToServerButtonListener(this.ipAddressTextField, this.portTextField));
private void createServerCertStoreLabel() {
this.serverCertStoreLabel = new JLabel();
}
private void addConnectButton() {
private void addServerCertStoreLabel() {
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 7;
gridBagConstraints.weightx = 1;
gridBagConstraints.weighty = 1;
gridBagConstraints.insets = new Insets(10, 0, 0, 0);
this.add(this.serverCertStoreLabel, gridBagConstraints);
}
private void createServerCertStoreFileChooserButton() {
this.trustStoreFileChooserButton = new TrustStoreButton();
}
private void addServerCertStoreFileChooserButton() {
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 8;
gridBagConstraints.weightx = 1;
gridBagConstraints.weighty = 1;
gridBagConstraints.insets = new Insets(10, 0, 0, 0);
this.add(this.trustStoreFileChooserButton, gridBagConstraints);
}
private void createTrustStorePasswordLabel() {
this.serverCertStorePasswordLabel = new JLabel();
}
private void addTrustStorePasswordLabel() {
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 9;
gridBagConstraints.weightx = 1;
gridBagConstraints.weighty = 1;
gridBagConstraints.insets = new Insets(10, 0, 0, 0);
this.add(this.serverCertStorePasswordLabel, gridBagConstraints);
}
private void createTrustStorePasswordTextField() {
this.trustStorePasswordTextField = new JPasswordField(24);
}
private void addTrustStorePasswordTextField() {
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 10;
gridBagConstraints.weightx = 1;
gridBagConstraints.weighty = 1;
gridBagConstraints.insets = new Insets(0, 0, 0, 0);
this.add(this.trustStorePasswordTextField, gridBagConstraints);
}
private void createConnectButton() {
this.connectButton = new ConnectToServerButton(this.ipAddressTextField, this.portTextField, this.trustStorePasswordTextField);
}
private void addConnectButton() {
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 11;
gridBagConstraints.weightx = 1;
gridBagConstraints.weighty = 1;
gridBagConstraints.insets = new Insets(10, 0, 0, 0);
this.add(this.connectButton, gridBagConstraints);
}
@@ -249,6 +319,9 @@ public final class ConnectToServerPanel extends JPanel implements Themeable, Tra
changeLanguageIPAddressLabel(language);
changeLanguagePortLabel(language);
changeLanguageConnectButton(language);
changeLanguageServerCertStoreLabel(language);
changeLanguageTrustStoreFileChooserButton(language);
changeLanguageServerCertStorePasswordLabel(language);
}
/**
@@ -334,14 +407,14 @@ public final class ConnectToServerPanel extends JPanel implements Themeable, Tra
}
}
private void changeLanguageConnectButton(Languages language) {
private void changeLanguageServerCertStoreLabel(Languages language) {
try {
switch (language) {
case German: {
this.connectButton.setText(translationsDE[5]);
this.serverCertStoreLabel.setText(translationsDE[5]);
}break;
case English: {
this.connectButton.setText(translationsEN[5]);
this.serverCertStoreLabel.setText(translationsEN[5]);
}break;
default: {
throw new Exception("Unknown kind of language.");
@@ -353,29 +426,88 @@ public final class ConnectToServerPanel extends JPanel implements Themeable, Tra
}
}
public static void setControllerToViewQueue(Queue<ControllerToViewData> newControllerToViewQueue) {
controllerToViewQueue = newControllerToViewQueue;
private void changeLanguageTrustStoreFileChooserButton(Languages language) {
try {
switch (language) {
case German: {
this.trustStoreFileChooserButton.setText(translationsDE[6]);
}break;
case English: {
this.trustStoreFileChooserButton.setText(translationsEN[6]);
}break;
default: {
throw new Exception("Unknown kind of language.");
}
}
} catch (Exception e) {
System.err.println(e.getMessage());
this.changeLanguagePortLabel(Languages.German);
}
}
public static void setViewToControllerQueue(Queue<ViewToControllerData> newViewToControllerQueue) {
viewToControllerQueue = newViewToControllerQueue;
private void changeLanguageServerCertStorePasswordLabel(Languages language) {
try {
switch (language) {
case German: {
this.serverCertStorePasswordLabel.setText(translationsDE[7]);
}break;
case English: {
this.serverCertStorePasswordLabel.setText(translationsEN[7]);
}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[8]);
}break;
case English: {
this.connectButton.setText(translationsEN[8]);
}break;
default: {
throw new Exception("Unknown kind of language.");
}
}
} catch (Exception e) {
System.err.println(e.getMessage());
this.changeLanguagePortLabel(Languages.German);
}
}
public void setServerCertStoreFilePath(String serverCertStoreFilePath) {
this.serverCertStoreFilePath = serverCertStoreFilePath;
}
public String getServerCertStoreFilePath() {
return serverCertStoreFilePath;
}
public void enableFields() {
for (JRadioButton radioButton : ipVersionRadioButtons) {
for (JRadioButton radioButton : this.ipVersionRadioButtons) {
radioButton.setEnabled(true);
}
ipAddressTextField.setEditable(true);
portTextField.setEditable(true);
connectButton.setEnabled(true);
this.ipAddressTextField.setEditable(true);
this.portTextField.setEditable(true);
this.trustStorePasswordTextField.setEditable(true);
this.connectButton.setEnabled(true);
}
public void disableFields() {
for (JRadioButton radioButton : ipVersionRadioButtons) {
for (JRadioButton radioButton : this.ipVersionRadioButtons) {
radioButton.setEnabled(false);
}
ipAddressTextField.setEditable(false);
portTextField.setEditable(false);
connectButton.setEnabled(false);
this.ipAddressTextField.setEditable(false);
this.portTextField.setEditable(false);
this.trustStorePasswordTextField.setEditable(false);
this.connectButton.setEnabled(false);
}
}

View File

@@ -0,0 +1,32 @@
/**
* @author Aaron Moser
* @date 09.12.2023
*/
package gui.src.panels.ConnectToServerPanel;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileSystemView;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TrustStoreButton extends JButton implements ActionListener {
public TrustStoreButton() {
this.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
int fileChooserOption = fileChooser.showSaveDialog(null);
// if the user selects a file
if (fileChooserOption == JFileChooser.APPROVE_OPTION) {
ConnectToServerPanel.getInstance().setServerCertStoreFilePath(fileChooser.getSelectedFile().getAbsolutePath());
}
}
}