Files
2026-06-19 22:57:43 +02:00

330 lines
13 KiB
HTML

<!DOCTYPE html>
<html id="htmlId">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Coverage Report > LoginFXMLController</title>
<style type="text/css">
@import "../../css/coverage.css";
@import "../../css/idea.min.css";
</style>
<script type="text/javascript" src="../../js/highlight.min.js"></script>
<script type="text/javascript" src="../../js/highlightjs-line-numbers.min.js"></script>
</head>
<body>
<div class="content">
<div class="breadCrumbs">
Current scope: <a href="../../index.html">all classes</a>
<span class="separator">|</span>
<a href="../index.html">it.polimi.ingsw.gc14.View.GUI</a>
</div>
<h1>Coverage Summary for Class: LoginFXMLController (it.polimi.ingsw.gc14.View.GUI)</h1>
<table class="coverageStats">
<tr>
<th class="name">Class</th>
<th class="coverageStat
">
Class, %
</th>
<th class="coverageStat
">
Method, %
</th>
<th class="coverageStat
">
Branch, %
</th>
<th class="coverageStat
">
Line, %
</th>
</tr>
<tr>
<td class="name">LoginFXMLController</td>
<td class="coverageStat">
<span class="percent">
0%
</span>
<span class="absValue">
(0/1)
</span>
</td>
<td class="coverageStat">
<span class="percent">
0%
</span>
<span class="absValue">
(0/14)
</span>
</td>
<td class="coverageStat">
<span class="percent">
0%
</span>
<span class="absValue">
(0/14)
</span>
</td>
<td class="coverageStat">
<span class="percent">
0%
</span>
<span class="absValue">
(0/63)
</span>
</td>
</tr>
</table>
<br/>
<br/>
<pre>
<code class="sourceCode" id="sourceCode">&nbsp;package it.polimi.ingsw.gc14.View.GUI;
&nbsp;
&nbsp;import it.polimi.ingsw.gc14.Controller.ClientController;
&nbsp;import it.polimi.ingsw.gc14.ErrorType;
&nbsp;import it.polimi.ingsw.gc14.Network.IClient;
&nbsp;import it.polimi.ingsw.gc14.Network.NetworkConfig;
&nbsp;import it.polimi.ingsw.gc14.Network.InterfaceResolver;
&nbsp;import it.polimi.ingsw.gc14.Network.RMI.Client.RMIClient;
&nbsp;import it.polimi.ingsw.gc14.Network.TCP.Client.TCPClient;
&nbsp;import javafx.animation.*;
&nbsp;import javafx.application.Platform;
&nbsp;import javafx.css.PseudoClass;
&nbsp;import javafx.fxml.FXML;
&nbsp;import javafx.geometry.Rectangle2D;
&nbsp;import javafx.scene.control.*;
&nbsp;import javafx.scene.image.Image;
&nbsp;import javafx.scene.image.ImageView;
&nbsp;import javafx.scene.layout.*;
&nbsp;import javafx.scene.paint.Color;
&nbsp;import javafx.stage.Screen;
&nbsp;import javafx.util.Duration;
&nbsp;
&nbsp;import java.util.Objects;
&nbsp;
&nbsp;/**
&nbsp; * FXML controller for the login scene.
&nbsp; *
&nbsp; * &lt;p&gt;Handles username/IP/player-count input, protocol selection (TCP/RMI),
&nbsp; * and initiates the connection to the server.
&nbsp; */
<b class="nc">&nbsp;public class LoginFXMLController {</b>
&nbsp; /** Background image displayed behind the login form. */
&nbsp; @FXML private ImageView backgroundImage;
&nbsp; /** Text field for the player&#39;s username. */
&nbsp; @FXML private TextField campoNome;
&nbsp; /** Text field for the desired number of players. */
&nbsp; @FXML private TextField campoNumPlayers;
&nbsp; /** Text field for the server IP address. */
&nbsp; @FXML private TextField campoIP;
&nbsp; /** Button that submits the login form. */
&nbsp; @FXML private Button btnAccedi;
&nbsp; /** Label used to show error or success messages. */
&nbsp; @FXML private Label labelErrore;
&nbsp; /** VBox containing the login form controls. */
&nbsp; @FXML private VBox formPanel;
&nbsp; /** Toggle container for switching between RMI and TCP. */
&nbsp; @FXML private StackPane protocolToggle;
&nbsp; /** Sliding thumb inside the protocol toggle. */
&nbsp; @FXML private Region toggleThumb;
&nbsp; /** Label for the RMI side of the protocol toggle. */
&nbsp; @FXML private Label labelRMI;
&nbsp; /** Label for the TCP side of the protocol toggle. */
&nbsp; @FXML private Label labelTCP;
&nbsp; /** {@code true} when RMI is selected; {@code false} for TCP. */
<b class="nc">&nbsp; private boolean isRMI = true;</b>
&nbsp; /** Client controller used to initiate the connection. */
&nbsp; private ClientController controller;
&nbsp; /** CSS pseudo-class applied to the active protocol label. */
<b class="nc">&nbsp; private final PseudoClass activeProtocolPseudo = PseudoClass.getPseudoClass(&quot;active-protocol&quot;);</b>
&nbsp;
&nbsp; /**
&nbsp; * Injects the client controller into this FXML controller.
&nbsp; *
&nbsp; * @param controller the client controller to use.
&nbsp; */
&nbsp; public void setController(ClientController controller) {
<b class="nc">&nbsp; this.controller = controller;</b>
&nbsp; }
&nbsp;
&nbsp; /** Initializes the scene: loads background, sets up animations, and binds input listeners. */
&nbsp; @FXML
&nbsp; public void initialize() {
&nbsp; // Background setup
<b class="nc">&nbsp; Image img = new Image(Objects.requireNonNull(getClass().getResourceAsStream(&quot;/GUIImages/BackgroundLogin.png&quot;)));</b>
<b class="nc">&nbsp; backgroundImage.setImage(img);</b>
<b class="nc">&nbsp; Rectangle2D screenBounds = Screen.getPrimary().getBounds();</b>
<b class="nc">&nbsp; backgroundImage.setFitWidth(screenBounds.getWidth());</b>
<b class="nc">&nbsp; backgroundImage.setFitHeight(screenBounds.getHeight());</b>
&nbsp;
&nbsp; // Protocol toggle configuration
<b class="nc">&nbsp; protocolToggle.setOnMouseClicked(e -&gt; switchProtocol());</b>
<b class="nc">&nbsp; protocolToggle.setStyle(protocolToggle.getStyle() + &quot; -fx-cursor: hand;&quot;);</b>
&nbsp;
<b class="nc">&nbsp; labelRMI.pseudoClassStateChanged(activeProtocolPseudo, true);</b>
<b class="nc">&nbsp; labelTCP.pseudoClassStateChanged(activeProtocolPseudo, false);</b>
&nbsp;
&nbsp; // Login Action
<b class="nc">&nbsp; btnAccedi.setOnAction(e -&gt; onAccediClick());</b>
&nbsp; }
&nbsp;
&nbsp; /** Toggles the selected protocol between RMI and TCP, animating the toggle thumb and updating label styles. */
&nbsp; private void switchProtocol() {
<b class="nc">&nbsp; isRMI = !isRMI;</b>
&nbsp;
<b class="nc">&nbsp; TranslateTransition tt = new TranslateTransition(Duration.millis(200), toggleThumb);</b>
<b class="nc">&nbsp; tt.setToX(isRMI ? 0 : 110);</b>
<b class="nc">&nbsp; tt.play();</b>
&nbsp;
<b class="nc">&nbsp; labelRMI.pseudoClassStateChanged(activeProtocolPseudo, isRMI);</b>
<b class="nc">&nbsp; labelTCP.pseudoClassStateChanged(activeProtocolPseudo, !isRMI);</b>
&nbsp; }
&nbsp;
&nbsp; /** Validates form input and starts a background thread to connect to the server. */
&nbsp; @FXML
&nbsp; private void onAccediClick() {
<b class="nc">&nbsp; String name = campoNome.getText().trim();</b>
<b class="nc">&nbsp; String ip = campoIP.getText().trim();</b>
&nbsp;
<b class="nc">&nbsp; if (name.isEmpty()) {</b>
<b class="nc">&nbsp; showError(&quot;Please select a name.&quot;);</b>
&nbsp; return;
&nbsp; }
&nbsp;
&nbsp; int numPlayers;
&nbsp; try {
<b class="nc">&nbsp; numPlayers = Integer.parseInt(campoNumPlayers.getText().trim());</b>
&nbsp; } catch (NumberFormatException e) {
<b class="nc">&nbsp; showError(&quot;Invalid number of players.&quot;);</b>
&nbsp; return;
&nbsp; }
&nbsp;
<b class="nc">&nbsp; updateLoginButton(false);</b>
<b class="nc">&nbsp; controller.setMyUsername(name);</b>
&nbsp;
<b class="nc">&nbsp; new Thread(() -&gt; {</b>
&nbsp; try {
<b class="nc">&nbsp; String localInterface = InterfaceResolver.resolveLocalInterface(ip);</b>
<b class="nc">&nbsp; System.setProperty(&quot;java.rmi.server.hostname&quot;, localInterface);</b>
<b class="nc">&nbsp; connect(name, ip, numPlayers, localInterface);</b>
&nbsp; } catch (Exception ex) {
<b class="nc">&nbsp; Platform.runLater(() -&gt; {</b>
<b class="nc">&nbsp; showError(&quot;Network error: &quot; + ex.getMessage());</b>
<b class="nc">&nbsp; updateLoginButton(true);</b>
&nbsp; });
&nbsp; }
<b class="nc">&nbsp; }).start();</b>
&nbsp; }
&nbsp;
&nbsp; /**
&nbsp; * Enables or disables the login button and updates its visual style.
&nbsp; *
&nbsp; * @param enabled {@code true} to enable the button, {@code false} to disable it.
&nbsp; */
&nbsp; public void updateLoginButton(boolean enabled) {
<b class="nc">&nbsp; btnAccedi.setDisable(!enabled);</b>
<b class="nc">&nbsp; btnAccedi.setStyle(</b>
&nbsp; &quot;-fx-font-family: &#39;Cinzel&#39;; -fx-font-size: 12; -fx-font-weight: bold;&quot; +
&nbsp; &quot;-fx-letter-spacing: 4; -fx-text-fill: #0c0601;&quot; +
&nbsp; &quot;-fx-background-color: linear-gradient(to right, #f4c05a, #c8791a, #f4c05a);&quot; +
&nbsp; &quot;-fx-padding: 13 48 13 48; -fx-background-radius: 2;&quot; +
<b class="nc">&nbsp; &quot;-fx-opacity: &quot; + (enabled ? &quot;1.0&quot; : &quot;0.3&quot;) + &quot;;&quot; +</b>
<b class="nc">&nbsp; &quot;-fx-cursor: &quot; + (enabled ? &quot;hand&quot; : &quot;default&quot;) + &quot;;&quot;</b>
&nbsp; );
&nbsp; }
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp; /** Connects to the server using the selected protocol and transitions to the waiting state on success. */
&nbsp; private void connect(String nome, String ip, int numPlayers, String localInterface) {
&nbsp; IClient client;
<b class="nc">&nbsp; if (isRMI) {</b>
<b class="nc">&nbsp; client = new RMIClient(controller, ip, NetworkConfig.RMI_PORT, localInterface);</b>
<b class="nc">&nbsp; ErrorType serverResponse = client.connect(nome, numPlayers);</b>
<b class="nc">&nbsp; if (serverResponse == null) {</b>
<b class="nc">&nbsp; controller.setClient(client);</b>
<b class="nc">&nbsp; Platform.runLater(() -&gt; showSuccess(&quot;Connected! Waiting for other players…&quot;));</b>
&nbsp; } else {
<b class="nc">&nbsp; Platform.runLater(() -&gt; {</b>
<b class="nc">&nbsp; showError(serverResponse);</b>
<b class="nc">&nbsp; updateLoginButton(true);</b>
&nbsp; });
&nbsp; }
&nbsp; } else {
<b class="nc">&nbsp; client = new TCPClient(controller, ip, NetworkConfig.TCP_PORT, NetworkConfig.HEARTBEAT_PORT);</b>
<b class="nc">&nbsp; ErrorType serverResponse = client.connect(nome, numPlayers);</b>
<b class="nc">&nbsp; if (serverResponse == null) {</b>
<b class="nc">&nbsp; controller.setClient(client);</b>
<b class="nc">&nbsp; Platform.runLater(() -&gt; showSuccess(&quot;Connected! Waiting for other players…&quot;));</b>
&nbsp; } else {
<b class="nc">&nbsp; Platform.runLater(() -&gt; {</b>
<b class="nc">&nbsp; showError(serverResponse);</b>
<b class="nc">&nbsp; updateLoginButton(true);</b>
&nbsp; });
&nbsp; }
&nbsp; }
&nbsp; }
&nbsp; /**
&nbsp; * Displays an error from an {@link ErrorType} constant in the login form label.
&nbsp; *
&nbsp; * @param errorType the error to display.
&nbsp; */
&nbsp; public void showError(ErrorType errorType) {
<b class="nc">&nbsp; labelErrore.setTextFill(Color.web(&quot;#e05050&quot;));</b>
<b class="nc">&nbsp; labelErrore.setText(errorType.toString());</b>
&nbsp; }
&nbsp;
&nbsp; /**
&nbsp; * Displays an arbitrary error message in the login form label.
&nbsp; *
&nbsp; * @param msg the error message to display.
&nbsp; */
&nbsp; public void showError(String msg) {
<b class="nc">&nbsp; labelErrore.setTextFill(Color.web(&quot;#e05050&quot;));</b>
<b class="nc">&nbsp; labelErrore.setText(msg);</b>
&nbsp; }
&nbsp;
&nbsp; /** Displays a success message in green in the login form label. */
&nbsp; private void showSuccess(String msg) {
<b class="nc">&nbsp; labelErrore.setTextFill(Color.web(&quot;#6fcf8a&quot;));</b>
<b class="nc">&nbsp; labelErrore.setText(msg);</b>
&nbsp; }
&nbsp;}
</code>
</pre>
</div>
<script type="text/javascript">
(function() {
var msie = false, msie9 = false;
/*@cc_on
msie = true;
@if (@_jscript_version >= 9)
msie9 = true;
@end
@*/
if (!msie || msie && msie9) {
hljs.highlightAll()
hljs.initLineNumbersOnLoad();
}
})();
</script>
<div class="footer">
<div style="float:right;">generated on 2026-06-19 22:53</div>
</div>
</body>
</html>