<commit_before>package rv.ui.menus;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JSeparator;
import javax.swing.SwingUtilities;
import rv.Configuration;
import rv.Viewer;

public class ConnectionMenu extends JMenu
{
	private final Viewer viewer;

	private final Configuration.Networking config;

	private final List<String> serverHosts;

	public ConnectionMenu(Viewer viewer)
	{
		super("Connection");
		this.viewer = viewer;
		this.config = viewer.getConfig().networking;

		setMnemonic('C');
		serverHosts = new ArrayList<>(config.serverHosts);

		String overriddenHost = config.overriddenServerHost;
		if (overriddenHost != null) {
			serverHosts.remove(overriddenHost);
			serverHosts.add(0, overriddenHost);
		}

		add(new JSeparator());
		JMenuItem m = new JMenuItem("Connect to ...");
		m.addActionListener((e) -> {
			int port = config.serverPort; // the default port
			String message =
					"<html>Enter a SimSpark IP address or host name<br><small>You can also specify a port (e.g. 'localhost:4200', 'example.com:4321')</small></html>";
			String host = JOptionPane.showInputDialog(
					this.viewer.getFrame(), message, "Simspark server", JOptionPane.PLAIN_MESSAGE);

			// canceled
			if (host == null) {
				return;
			}

			// check if host string contains port info
			if (host.contains(":")) {
				String[] parts = host.split(":");

				if (parts.length != 2) {
					JOptionPane.showMessageDialog(this.viewer.getFrame(),
							String.format("The entered server address ('%s') is invalid", host),
							"Invalid server address", JOptionPane.ERROR_MESSAGE);
					return;
				}

				host = parts[0];

				// if an exception is thrown, the port part is invalid!
				try {
					port = Integer.parseInt(parts[1]);
				} catch (NumberFormatException ex) {
					JOptionPane.showMessageDialog(this.viewer.getFrame(),
							String.format("The entered port ('%s') is invalid", parts[1]), "Invalid port",
							JOptionPane.ERROR_MESSAGE);
					return;
				}
			}

			// if an exception is thrown, the host part is invalid!
			try {
				InetAddress.getByName(host);
			} catch (UnknownHostException ex) {
				JOptionPane.showMessageDialog(this.viewer.getFrame(),
						String.format("The entered host ('%s') is invalid", host), "Invalid host",
						JOptionPane.ERROR_MESSAGE);
				return;
			}

			// add the valid host to the menu and host list
			serverHosts.add(host);
			selectServer(addHostItem(host, port));
		});
		add(m);

		for (String host : serverHosts) {
			addHostItem(host, config.getServerPort());
		}
	}

	/**
	 * Adds a new remote menu item to this menu.
	 * @param host
	 * @param port
	 * @return
	 */
	private RemoteMenuItem addHostItem(String host, int port)
	{
		final RemoteMenuItem item = new RemoteMenuItem(host, port, getItemCount() == 2);
		item.addActionListener(e -> SwingUtilities.invokeLater(() -> selectServer(item)));
		add(item, getItemCount() - 2); // append to the end, but before the seperator
		return item;
	}

	/**
	 * Selects the given remote menu item and connect to the corresponding server.
	 * @param item
	 */
	private void selectServer(RemoteMenuItem item)
	{
		for (int i = 0; i < getItemCount(); i++) {
			if (getItem(i) instanceof JRadioButtonMenuItem) {<commit_msg>Minor cleanup<commit_after>package rv.ui.menus;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JSeparator;
import javax.swing.SwingUtilities;
import rv.Configuration;
import rv.Viewer;

public class ConnectionMenu extends JMenu
{
	private final Viewer viewer;

	private final Configuration.Networking config;

	private final List<String> serverHosts;

	public ConnectionMenu(Viewer viewer)
	{
		super("Connection");
		this.viewer = viewer;
		this.config = viewer.getConfig().networking;

		setMnemonic('C');
		serverHosts = new ArrayList<>(config.serverHosts);

		String overriddenHost = config.overriddenServerHost;
		if (overriddenHost != null) {
			serverHosts.remove(overriddenHost);
			serverHosts.add(0, overriddenHost);
		}

		add(new JSeparator());
		JMenuItem m = new JMenuItem("Connect to ...");
		m.addActionListener((e) -> {
			int port = config.serverPort; // the default port
			String message =
					"<html>Enter a SimSpark IP address or host name<br><small>You can also specify a port (e.g. 'localhost:4200', 'example.com:4321')</small></html>";
			String host = JOptionPane.showInputDialog(
					this.viewer.getFrame(), message, "SimSpark server", JOptionPane.PLAIN_MESSAGE);

			// canceled
			if (host == null) {
				return;
			}

			// check if host string contains port info
			if (host.contains(":")) {
				String[] parts = host.split(":");

				if (parts.length != 2) {
					JOptionPane.showMessageDialog(this.viewer.getFrame(),
							String.format("The entered server address ('%s') is invalid", host),
							"Invalid server address", JOptionPane.ERROR_MESSAGE);
					return;
				}

				host = parts[0];

				// if an exception is thrown, the port part is invalid!
				try {
					port = Integer.parseInt(parts[1]);
				} catch (NumberFormatException ex) {
					JOptionPane.showMessageDialog(this.viewer.getFrame(),
							String.format("The entered port ('%s') is invalid", parts[1]), "Invalid port",
							JOptionPane.ERROR_MESSAGE);
					return;
				}
			}

			// if an exception is thrown, the host part is invalid!
			try {
				InetAddress.getByName(host);
			} catch (UnknownHostException ex) {
				JOptionPane.showMessageDialog(this.viewer.getFrame(),
						String.format("The entered host ('%s') is invalid", host), "Invalid host",
						JOptionPane.ERROR_MESSAGE);
				return;
			}

			// add the valid host to the menu and host list
			serverHosts.add(host);
			selectServer(addHostItem(host, port));
		});
		add(m);

		for (String host : serverHosts) {
			addHostItem(host, config.getServerPort());
		}
	}

	/**
	 * Adds a new remote menu item to this menu.
	 */
	private RemoteMenuItem addHostItem(String host, int port)
	{
		final RemoteMenuItem item = new RemoteMenuItem(host, port, getItemCount() == 2);
		item.addActionListener(e -> SwingUtilities.invokeLater(() -> selectServer(item)));
		add(item, getItemCount() - 2); // append to the end, but before the separator
		return item;
	}

	/**
	 * Selects the given remote menu item and connect to the corresponding server.
	 */
	private void selectServer(RemoteMenuItem item)
	{
		for (int i = 0; i < getItemCount(); i++) {
			if (getItem(i) instanceof JRadioButtonMenuItem) {
{'fraction_non_alphanumeric': 0.12467076382791922, 'fraction_numerical': 0.004682470002926544, 'mean_word_length': 3.7964912280701753, 'pattern_counts': {'":': 4, '<': 19, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2997709', 'n_tokens_mistral': 2469, 'n_tokens_neox': 2086, 'n_words': 570}