Add: network interface selection GUI

This commit is contained in:
2026-05-06 15:56:40 +02:00
parent 5d31e6346d
commit 9b206629c0
4 changed files with 45 additions and 3 deletions
@@ -1,8 +1,16 @@
package it.polimi.ingsw.gc14.View.GUI;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
public class LoginFXMLController {
@FXML private TextField campoNome;
@@ -12,14 +20,38 @@ public class LoginFXMLController {
@FXML private RadioButton btnTCP;
@FXML private Button btnAccedi;
@FXML private Label labelErrore;
@FXML private ComboBox<String> comboInterfaccia;
@FXML
public void initialize() {
// I RadioButton vanno collegati a un ToggleGroup a mano
// perché FXML non lo fa automaticamente
ToggleGroup group = new ToggleGroup();
btnRMI.setToggleGroup(group);
btnTCP.setToggleGroup(group);
// Carica le interfacce in background
new Thread(() -> {
try {
List<String> ips = new ArrayList<>();
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface ni = interfaces.nextElement();
if (!ni.isUp() || ni.isLoopback() || ni.isVirtual()) continue;
Enumeration<InetAddress> addresses = ni.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress addr = addresses.nextElement();
if (addr instanceof Inet4Address) {
ips.add(ni.getDisplayName() + " -> " + addr.getHostAddress());
}
}
}
Platform.runLater(() -> {
comboInterfaccia.getItems().addAll(ips);
if (!ips.isEmpty()) comboInterfaccia.getSelectionModel().selectFirst();
});
} catch (Exception e) {
Platform.runLater(() -> setErrore("Errore nel rilevare le interfacce."));
}
}).start();
}
// --- Getter esposti a LoginView ---
@@ -40,5 +72,11 @@ public class LoginFXMLController {
public String getIP() { return campoIP.getText().trim(); }
public String getInterfaccia() {
String selected = comboInterfaccia.getValue();
if (selected == null) return "";
return selected.contains("->") ? selected.split("->")[1].trim() : selected;
}
public void setErrore(String messaggio) { labelErrore.setText(messaggio); }
}
@@ -31,5 +31,6 @@ public class LoginView {
public int getNumPlayers() { return fxmlController.getNumPlayers(); }
public int getNetworkType() { return fxmlController.getNetworkType(); }
public String getIP() { return fxmlController.getIP(); }
public String getInterfaccia() {return fxmlController.getInterfaccia();}
public void setErrore(String messaggio) { fxmlController.setErrore(messaggio); }
}