Skip to content

Commit

Permalink
Reduce cognitive complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
RetGal committed Aug 30, 2024
1 parent 3327b78 commit b7263a2
Showing 1 changed file with 134 additions and 125 deletions.
259 changes: 134 additions & 125 deletions src/main/java/mpo/dayon/common/gui/common/BaseFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -277,111 +277,27 @@ protected Action createConnectionSettingsAction(Assistant assistant) {
@Override
public void actionPerformed(ActionEvent ev) {
JFrame networkFrame = (JFrame) SwingUtilities.getRoot((Component) ev.getSource());
final Font titleFont = new Font("Sans Serif", Font.BOLD, 14);
final String custom = "custom";

final JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());

int gridy = 0;

final JTextField addressTextField = new JTextField();
final JTextField portNumberTextField = new JTextField();
final JCheckBox autoConnectCheckBox = new JCheckBox();
String currentTokenServer;

if (frameType.equals(FrameType.ASSISTED)) {
final NetworkAssistedEngineConfiguration networkConfiguration = new NetworkAssistedEngineConfiguration();
currentTokenServer = networkConfiguration.getTokenServerUrl();
final JLabel hostLbl = new JLabel(toUpperFirst(translate("assistant")));
hostLbl.setFont(titleFont);
panel.add(hostLbl, createGridBagConstraints(gridy++));

final JPanel assistantPanel = new JPanel(new GridLayout(4, 2, 10, 0));
assistantPanel.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));
final JLabel addressLbl = new JLabel(translate("connection.settings.assistantIpAddress"));
addressTextField.setText(networkConfiguration.getServerName());
assistantPanel.add(addressLbl);
assistantPanel.add(addressTextField);
final JLabel portNumberLbl = new JLabel(translate("connection.settings.assistantPortNumber"));
portNumberTextField.setText(format("%d", networkConfiguration.getServerPort()));
assistantPanel.add(portNumberLbl);
assistantPanel.add(portNumberTextField);
autoConnectCheckBox.setText(translate("connection.settings.autoConnect"));
autoConnectCheckBox.setSelected(networkConfiguration.isAutoConnect());
assistantPanel.add(autoConnectCheckBox);
panel.add(assistantPanel, createGridBagConstraints(gridy++));
} else {
final NetworkAssistantEngineConfiguration networkConfiguration = new NetworkAssistantEngineConfiguration();
currentTokenServer = networkConfiguration.getTokenServerUrl();
final JLabel hostLbl = new JLabel(toUpperFirst(translate("host")));
hostLbl.setFont(titleFont);
panel.add(hostLbl, createGridBagConstraints(gridy++));

final JPanel upnpPanel = new JPanel(new GridLayout(1, 1, 10, 0));
upnpPanel.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));
boolean upnpActive = assistant.isUpnpEnabled();
final JLabel upnpStatus = new JLabel(format("<html>%s<br>%s</html>", format(translate(format("connection.settings.upnp.%s", upnpActive)), UPnP.getDefaultGatewayIP()), translate(format("connection.settings.portforward.%s", upnpActive))));
upnpPanel.add(upnpStatus);
panel.add(upnpPanel, createGridBagConstraints(gridy++));

final JPanel portPanel = new JPanel(new GridLayout(1, 2, 10, 0));
portPanel.setBorder(BorderFactory.createEmptyBorder(10,0,20,0));
final JLabel portNumberLbl = new JLabel(translate("connection.settings.portNumber"));
portNumberLbl.setToolTipText(translate("connection.settings.portNumber.tooltip"));
portNumberTextField.setText(format("%d", networkConfiguration.getPort()));
portPanel.add(portNumberLbl);
portPanel.add(portNumberTextField);
panel.add(portPanel, createGridBagConstraints(gridy++));
}

final JLabel tokenServerLbl = new JLabel(toUpperFirst(translate("token.server")));
tokenServerLbl.setFont(titleFont);
panel.add(tokenServerLbl, createGridBagConstraints(gridy++));

final JPanel tokenPanel = new JPanel(new GridLayout(3, 2, 10, 0));
tokenPanel.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));

final ButtonGroup tokenRadioGroup = new ButtonGroup();
final JRadioButton defaultTokenRadio = new JRadioButton(translate("token.default.server"));
defaultTokenRadio.setActionCommand("default");
final JRadioButton customTokenRadio = new JRadioButton(translate("token.custom.server"));
customTokenRadio.setActionCommand(custom);
tokenRadioGroup.add(defaultTokenRadio);
tokenRadioGroup.add(customTokenRadio);
boolean customTextFieldEditable = false;
if (currentTokenServer.isEmpty() || currentTokenServer.equals(DEFAULT_TOKEN_SERVER_URL)) {
currentTokenServer = "";
defaultTokenRadio.setSelected(true);
} else {
customTokenRadio.setSelected(true);
customTextFieldEditable = true;
}

final JTextField defaultTokenTextField = new JTextField(DEFAULT_TOKEN_SERVER_URL);
defaultTokenTextField.setEditable(false);
defaultTokenTextField.setFocusable(false);
final JTextField customTokenTextField = new JTextField(currentTokenServer);
customTokenTextField.setEditable(customTextFieldEditable);

defaultTokenRadio.addActionListener(evt -> {defaultTokenRadio.requestFocus(); customTokenTextField.setEditable(false);});
customTokenRadio.addActionListener(evt -> {customTokenTextField.requestFocus(); customTokenTextField.setEditable(true);});
final JTextField customTokenTextField = new JTextField();

tokenPanel.add(defaultTokenRadio);
tokenPanel.add(defaultTokenTextField);

tokenPanel.add(customTokenRadio);
tokenPanel.add(customTokenTextField);
panel.add(tokenPanel, createGridBagConstraints(gridy));
JPanel panel = createPanel(addressTextField, portNumberTextField, autoConnectCheckBox, tokenRadioGroup, customTokenTextField, (assistant != null && assistant.isUpnpEnabled()));

final boolean ok = DialogFactory.showOkCancel(networkFrame, translate("connection.network"), panel, true,
() -> validateInputFields(addressTextField, portNumberTextField, tokenRadioGroup, customTokenTextField));

if (ok) {
final String newTokenServerUrl = tokenRadioGroup.getSelection().getActionCommand().equals(custom) &&
final String newTokenServerUrl = tokenRadioGroup.getSelection().getActionCommand().equals("custom") &&
isValidUrl(customTokenTextField.getText()) ? customTokenTextField.getText() : "";
updateSystemProperty(newTokenServerUrl);
updateNetworkConfiguration(addressTextField, portNumberTextField, autoConnectCheckBox, newTokenServerUrl);
if (frameType.equals(FrameType.ASSISTED)) {
updateAssistedNetworkConfiguration(addressTextField, portNumberTextField, autoConnectCheckBox, newTokenServerUrl);
} else {
updateAssistantNetworkConfiguration(addressTextField, portNumberTextField, autoConnectCheckBox, newTokenServerUrl, assistant.getNetworkEngine());
}
}
}

Expand All @@ -407,45 +323,138 @@ private String validateInputFields(JTextField addressTextField, JTextField portN
}
return null;
}
};
conf.putValue(Action.SHORT_DESCRIPTION, translate("connection.settings"));
conf.putValue(Action.SMALL_ICON, getOrCreateIcon(ImageNames.NETWORK_SETTINGS));
return conf;
}

private void updateNetworkConfiguration(JTextField addressTextField, JTextField portNumberTextField, JCheckBox autoConnectCheckBox, String newTokenServerUrl) {
if (frameType.equals(FrameType.ASSISTED)) {
final NetworkAssistedEngineConfiguration newNetworkConfiguration = new NetworkAssistedEngineConfiguration(
addressTextField.getText(), Integer.parseInt(portNumberTextField.getText()), autoConnectCheckBox.isSelected(), newTokenServerUrl);
private JPanel createPanel(JTextField addressTextField, JTextField portNumberTextField, JCheckBox autoConnectCheckBox, ButtonGroup tokenRadioGroup, JTextField customTokenTextField, boolean upnpActive) {
final Font titleFont = new Font("Sans Serif", Font.BOLD, 14);
final JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
int gridy = 0;
String currentTokenServer;

if (frameType.equals(FrameType.ASSISTED)) {
final NetworkAssistedEngineConfiguration networkConfiguration = new NetworkAssistedEngineConfiguration();
currentTokenServer = networkConfiguration.getTokenServerUrl();
final JLabel hostLbl = new JLabel(toUpperFirst(translate("assistant")));
hostLbl.setFont(titleFont);
panel.add(hostLbl, createGridBagConstraints(gridy++));

final JPanel assistantPanel = new JPanel(new GridLayout(4, 2, 10, 0));
assistantPanel.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));
final JLabel addressLbl = new JLabel(translate("connection.settings.assistantIpAddress"));
addressTextField.setText(networkConfiguration.getServerName());
assistantPanel.add(addressLbl);
assistantPanel.add(addressTextField);
final JLabel portNumberLbl = new JLabel(translate("connection.settings.assistantPortNumber"));
portNumberTextField.setText(format("%d", networkConfiguration.getServerPort()));
assistantPanel.add(portNumberLbl);
assistantPanel.add(portNumberTextField);
autoConnectCheckBox.setText(translate("connection.settings.autoConnect"));
autoConnectCheckBox.setSelected(networkConfiguration.isAutoConnect());
assistantPanel.add(autoConnectCheckBox);
panel.add(assistantPanel, createGridBagConstraints(gridy++));
} else {
final NetworkAssistantEngineConfiguration networkConfiguration = new NetworkAssistantEngineConfiguration();
currentTokenServer = networkConfiguration.getTokenServerUrl();
final JLabel hostLbl = new JLabel(toUpperFirst(translate("host")));
hostLbl.setFont(titleFont);
panel.add(hostLbl, createGridBagConstraints(gridy++));

final JPanel upnpPanel = new JPanel(new GridLayout(1, 1, 10, 0));
upnpPanel.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));
//boolean upnpActive = assistant.isUpnpEnabled();
final JLabel upnpStatus = new JLabel(format("<html>%s<br>%s</html>", format(translate(format("connection.settings.upnp.%s", upnpActive)), UPnP.getDefaultGatewayIP()), translate(format("connection.settings.portforward.%s", upnpActive))));
upnpPanel.add(upnpStatus);
panel.add(upnpPanel, createGridBagConstraints(gridy++));

final JPanel portPanel = new JPanel(new GridLayout(1, 2, 10, 0));
portPanel.setBorder(BorderFactory.createEmptyBorder(10,0,20,0));
final JLabel portNumberLbl = new JLabel(translate("connection.settings.portNumber"));
portNumberLbl.setToolTipText(translate("connection.settings.portNumber.tooltip"));
portNumberTextField.setText(format("%d", networkConfiguration.getPort()));
portPanel.add(portNumberLbl);
portPanel.add(portNumberTextField);
panel.add(portPanel, createGridBagConstraints(gridy++));
}

if (!newNetworkConfiguration.equals(new NetworkAssistedEngineConfiguration())) {
newNetworkConfiguration.persist();
}
return;
}
final JLabel tokenServerLbl = new JLabel(toUpperFirst(translate("token.server")));
tokenServerLbl.setFont(titleFont);
panel.add(tokenServerLbl, createGridBagConstraints(gridy++));

final JPanel tokenPanel = new JPanel(new GridLayout(3, 2, 10, 0));
tokenPanel.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));


final JRadioButton defaultTokenRadio = new JRadioButton(translate("token.default.server"));
defaultTokenRadio.setActionCommand("default");
final JRadioButton customTokenRadio = new JRadioButton(translate("token.custom.server"));
customTokenRadio.setActionCommand( "custom");
tokenRadioGroup.add(defaultTokenRadio);
tokenRadioGroup.add(customTokenRadio);
boolean customTextFieldEditable = false;
if (currentTokenServer.isEmpty() || currentTokenServer.equals(DEFAULT_TOKEN_SERVER_URL)) {
currentTokenServer = "";
defaultTokenRadio.setSelected(true);
} else {
customTokenRadio.setSelected(true);
customTextFieldEditable = true;
}

final NetworkAssistantEngineConfiguration newNetworkConfiguration = new NetworkAssistantEngineConfiguration(
Integer.parseInt(portNumberTextField.getText()), newTokenServerUrl);
final JTextField defaultTokenTextField = new JTextField(DEFAULT_TOKEN_SERVER_URL);
defaultTokenTextField.setEditable(false);
defaultTokenTextField.setFocusable(false);
//final JTextField customTokenTextField = new JTextField(currentTokenServer);
customTokenTextField.setText(currentTokenServer);
customTokenTextField.setEditable(customTextFieldEditable);

NetworkAssistantEngineConfiguration networkConfiguration = new NetworkAssistantEngineConfiguration();
if (!newNetworkConfiguration.equals(networkConfiguration)) {
final NetworkAssistantEngine networkEngine = assistant.getNetworkEngine();
networkEngine.manageRouterPorts(networkConfiguration.getPort(), newNetworkConfiguration.getPort());
newNetworkConfiguration.persist();
networkEngine.reconfigure(newNetworkConfiguration);
}
}
defaultTokenRadio.addActionListener(evt -> {defaultTokenRadio.requestFocus(); customTokenTextField.setEditable(false);});
customTokenRadio.addActionListener(evt -> {customTokenTextField.requestFocus(); customTokenTextField.setEditable(true);});

private void updateSystemProperty(String newTokenServerUrl) {
if (newTokenServerUrl.isEmpty()) {
System.clearProperty("dayon.custom.tokenServer");
return;
}
System.setProperty("dayon.custom.tokenServer", newTokenServerUrl);
}
tokenPanel.add(defaultTokenRadio);
tokenPanel.add(defaultTokenTextField);

private String toUpperFirst(String text) {
return Pattern.compile("^.").matcher(text).replaceFirst(m -> m.group().toUpperCase());
}
};
conf.putValue(Action.SHORT_DESCRIPTION, translate("connection.settings"));
conf.putValue(Action.SMALL_ICON, getOrCreateIcon(ImageNames.NETWORK_SETTINGS));
return conf;
tokenPanel.add(customTokenRadio);
tokenPanel.add(customTokenTextField);
panel.add(tokenPanel, createGridBagConstraints(gridy));

return panel;
}

private String toUpperFirst(String text) {
return Pattern.compile("^.").matcher(text).replaceFirst(m -> m.group().toUpperCase());
}

private void updateAssistedNetworkConfiguration(JTextField addressTextField, JTextField portNumberTextField, JCheckBox autoConnectCheckBox, String newTokenServerUrl) {
final NetworkAssistedEngineConfiguration newNetworkConfiguration = new NetworkAssistedEngineConfiguration(
addressTextField.getText(), Integer.parseInt(portNumberTextField.getText()), autoConnectCheckBox.isSelected(), newTokenServerUrl);

if (!newNetworkConfiguration.equals(new NetworkAssistedEngineConfiguration())) {
newNetworkConfiguration.persist();
}
}

private void updateAssistantNetworkConfiguration(JTextField addressTextField, JTextField portNumberTextField, JCheckBox autoConnectCheckBox, String newTokenServerUrl, NetworkAssistantEngine networkEngine) {
final NetworkAssistantEngineConfiguration newNetworkConfiguration = new NetworkAssistantEngineConfiguration(
Integer.parseInt(portNumberTextField.getText()), newTokenServerUrl);

NetworkAssistantEngineConfiguration networkConfiguration = new NetworkAssistantEngineConfiguration();
if (!newNetworkConfiguration.equals(networkConfiguration)) {
networkEngine.manageRouterPorts(networkConfiguration.getPort(), newNetworkConfiguration.getPort());
newNetworkConfiguration.persist();
networkEngine.reconfigure(newNetworkConfiguration);
}
}

private void updateSystemProperty(String newTokenServerUrl) {
if (newTokenServerUrl.isEmpty()) {
System.clearProperty("dayon.custom.tokenServer");
return;
}
System.setProperty("dayon.custom.tokenServer", newTokenServerUrl);
}

private GridBagConstraints createGridBagConstraints(int gridy) {
Expand Down

0 comments on commit b7263a2

Please sign in to comment.