mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 14:28:43 +00:00
-Validation on server list screen and server creation dialog done.
tags : #story[1245]
This commit is contained in:
@@ -5,6 +5,7 @@ import com.jfoenix.controls.JFXDialog;
|
|||||||
import com.jfoenix.controls.JFXDialog.DialogTransition;
|
import com.jfoenix.controls.JFXDialog.DialogTransition;
|
||||||
import com.jfoenix.controls.JFXTextField;
|
import com.jfoenix.controls.JFXTextField;
|
||||||
import com.jfoenix.validation.RequiredFieldValidator;
|
import com.jfoenix.validation.RequiredFieldValidator;
|
||||||
|
import com.jfoenix.validation.base.ValidatorBase;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
@@ -28,6 +29,8 @@ import seng302.gameServer.ServerDescription;
|
|||||||
import seng302.visualiser.ServerListener;
|
import seng302.visualiser.ServerListener;
|
||||||
import seng302.visualiser.ServerListenerDelegate;
|
import seng302.visualiser.ServerListenerDelegate;
|
||||||
import seng302.visualiser.controllers.cells.ServerCell;
|
import seng302.visualiser.controllers.cells.ServerCell;
|
||||||
|
import seng302.visualiser.validators.HostNameFieldValidator;
|
||||||
|
import seng302.visualiser.validators.NumberRangeValidator;
|
||||||
|
|
||||||
public class ServerListController implements Initializable, ServerListenerDelegate {
|
public class ServerListController implements Initializable, ServerListenerDelegate {
|
||||||
|
|
||||||
@@ -53,6 +56,11 @@ public class ServerListController implements Initializable, ServerListenerDelega
|
|||||||
private JFXTextField serverPortNumber;
|
private JFXTextField serverPortNumber;
|
||||||
//---------FXML END---------//
|
//---------FXML END---------//
|
||||||
|
|
||||||
|
//Validators
|
||||||
|
private HostNameFieldValidator hostNameValidator;
|
||||||
|
private NumberRangeValidator portNumberValidator;
|
||||||
|
|
||||||
|
|
||||||
private Label noServersFound;
|
private Label noServersFound;
|
||||||
private Logger logger = LoggerFactory.getLogger(ServerListController.class);
|
private Logger logger = LoggerFactory.getLogger(ServerListController.class);
|
||||||
|
|
||||||
@@ -63,17 +71,27 @@ public class ServerListController implements Initializable, ServerListenerDelega
|
|||||||
// Set Event Bindings
|
// Set Event Bindings
|
||||||
connectButton.setOnMouseReleased(event -> attemptToDirectConnect());
|
connectButton.setOnMouseReleased(event -> attemptToDirectConnect());
|
||||||
for (JFXTextField textField : Arrays.asList(serverHostName, serverPortNumber)) {
|
for (JFXTextField textField : Arrays.asList(serverHostName, serverPortNumber)) {
|
||||||
|
// Event for pressing enter to submit direct connection
|
||||||
textField.setOnKeyPressed(event -> {
|
textField.setOnKeyPressed(event -> {
|
||||||
if (event.getCode().equals(KeyCode.ENTER)) {
|
if (event.getCode().equals(KeyCode.ENTER)) {
|
||||||
attemptToDirectConnect();
|
attemptToDirectConnect();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Validators as empty fields are invalid.
|
||||||
RequiredFieldValidator validator = new RequiredFieldValidator();
|
RequiredFieldValidator validator = new RequiredFieldValidator();
|
||||||
|
validator.setMessage("Field is Required");
|
||||||
textField.getValidators().add(validator);
|
textField.getValidators().add(validator);
|
||||||
}
|
}
|
||||||
serverHostName.getValidators().get(0).setMessage("Incorrect Host Name");
|
|
||||||
serverPortNumber.getValidators().get(0).setMessage("Incorrect Port Number");
|
// Validating the hostname
|
||||||
|
hostNameValidator = new HostNameFieldValidator();
|
||||||
|
hostNameValidator.setMessage("Host Name is Incorrect");
|
||||||
|
serverHostName.getValidators().add(hostNameValidator);
|
||||||
|
|
||||||
|
portNumberValidator = new NumberRangeValidator(1025, 65536);
|
||||||
|
portNumberValidator.setMessage("Port Number is Incorrect");
|
||||||
|
serverPortNumber.getValidators().add(portNumberValidator);
|
||||||
|
|
||||||
// Start listening for servers on network
|
// Start listening for servers on network
|
||||||
try {
|
try {
|
||||||
@@ -119,51 +137,26 @@ public class ServerListController implements Initializable, ServerListenerDelega
|
|||||||
|
|
||||||
|
|
||||||
private Boolean validateDirectConnection(String hostName, String portNumber) {
|
private Boolean validateDirectConnection(String hostName, String portNumber) {
|
||||||
Boolean hostNameValid = validateHostName(hostName);
|
Boolean hostNameValid = validateTextField(serverHostName);
|
||||||
Boolean portNumberValid = validatePortNumber(portNumber);
|
Boolean portNumberValid = validateTextField(serverPortNumber);
|
||||||
|
|
||||||
return hostNameValid && portNumberValid;
|
return hostNameValid && portNumberValid;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
private Boolean validateHostName(String hostName) {
|
|
||||||
if (hostName.length() == 0) {
|
|
||||||
serverHostName.validate();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
// Validate the Host Name here.
|
|
||||||
try {
|
|
||||||
/* The result of this is ignored. All We want to do is test that
|
|
||||||
the hostname matches some form. I'm not 100% sure how valid this is. */
|
|
||||||
InetAddress.getByName(hostName);
|
|
||||||
return true;
|
|
||||||
} catch (UnknownHostException e) {
|
|
||||||
// Fails to resolve the host name.
|
|
||||||
serverHostName.validate();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
private Boolean validatePortNumber(String portNumber) {
|
private Boolean validateTextField(JFXTextField textField) {
|
||||||
try {
|
textField.validate();
|
||||||
Integer portNum = Integer.parseInt(serverPortNumber.getText());
|
for (ValidatorBase validator : textField.getValidators()) {
|
||||||
if (portNum > 1024 && portNum <= 65536) {
|
if (validator.getHasErrors()) {
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
System.out.println(portNum.toString() + " is not a valid port number");
|
|
||||||
}
|
|
||||||
} catch (NumberFormatException e) {
|
|
||||||
System.out.println("Not a valid number.");
|
|
||||||
}
|
|
||||||
serverPortNumber.validate();
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
|
|||||||
+10
-11
@@ -4,7 +4,6 @@ import com.jfoenix.controls.JFXButton;
|
|||||||
import com.jfoenix.controls.JFXSlider;
|
import com.jfoenix.controls.JFXSlider;
|
||||||
import com.jfoenix.validation.RequiredFieldValidator;
|
import com.jfoenix.validation.RequiredFieldValidator;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.ResourceBundle;
|
import java.util.ResourceBundle;
|
||||||
|
|
||||||
import com.jfoenix.controls.JFXTextField;
|
import com.jfoenix.controls.JFXTextField;
|
||||||
@@ -12,6 +11,7 @@ import javafx.fxml.FXML;
|
|||||||
import javafx.fxml.Initializable;
|
import javafx.fxml.Initializable;
|
||||||
import javafx.scene.control.Label;
|
import javafx.scene.control.Label;
|
||||||
import seng302.gameServer.ServerDescription;
|
import seng302.gameServer.ServerDescription;
|
||||||
|
import seng302.visualiser.validators.FieldLengthValidator;
|
||||||
import seng302.visualiser.controllers.ViewManager;
|
import seng302.visualiser.controllers.ViewManager;
|
||||||
|
|
||||||
public class ServerCreationController implements Initializable {
|
public class ServerCreationController implements Initializable {
|
||||||
@@ -19,17 +19,16 @@ public class ServerCreationController implements Initializable {
|
|||||||
//--------FXML BEGIN--------//
|
//--------FXML BEGIN--------//
|
||||||
@FXML
|
@FXML
|
||||||
private JFXTextField serverName;
|
private JFXTextField serverName;
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private JFXSlider maxPlayersSlider;
|
private JFXSlider maxPlayersSlider;
|
||||||
@FXML
|
@FXML
|
||||||
private Label maxPlayersLabel;
|
private Label maxPlayersLabel;
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private JFXButton submitBtn;
|
private JFXButton submitBtn;
|
||||||
//---------FXML END---------//
|
//---------FXML END---------//
|
||||||
|
|
||||||
RequiredFieldValidator validator;
|
FieldLengthValidator fieldLengthValidator;
|
||||||
|
RequiredFieldValidator fieldRequiredValidator;
|
||||||
|
|
||||||
public void initialize(URL location, ResourceBundle resources) {
|
public void initialize(URL location, ResourceBundle resources) {
|
||||||
updateMaxPlayerLabel();
|
updateMaxPlayerLabel();
|
||||||
@@ -37,22 +36,22 @@ public class ServerCreationController implements Initializable {
|
|||||||
updateMaxPlayerLabel();
|
updateMaxPlayerLabel();
|
||||||
});
|
});
|
||||||
|
|
||||||
validator = new RequiredFieldValidator();
|
fieldLengthValidator = new FieldLengthValidator(40);
|
||||||
serverName.getValidators().add(validator);
|
fieldLengthValidator.setMessage("Server Name Too Long");
|
||||||
|
fieldRequiredValidator = new RequiredFieldValidator();
|
||||||
|
fieldRequiredValidator.setMessage("Server Name is Required.");
|
||||||
|
serverName.setValidators(fieldLengthValidator, fieldRequiredValidator);
|
||||||
|
|
||||||
submitBtn.setOnMouseClicked(event -> submitBtn.setText("CREATING..."));
|
submitBtn.setOnMouseClicked(event -> submitBtn.setText("CREATING..."));
|
||||||
submitBtn.setOnMouseReleased(event -> validateServerSettings());
|
submitBtn.setOnMouseReleased(event -> validateServerSettings());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void validateServerSettings() {
|
private void validateServerSettings() {
|
||||||
if (serverName.getText().length() >= 40) {
|
|
||||||
validator.setMessage("HI");
|
|
||||||
} else if (serverName.getText().length() == 0) {
|
|
||||||
validator.setMessage("Server Name Required.");
|
|
||||||
}
|
|
||||||
serverName.validate();
|
serverName.validate();
|
||||||
|
System.out.println(serverName.getActiveValidator());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void createServer() {
|
public void createServer() {
|
||||||
|
|
||||||
ServerDescription serverDescription = ViewManager.getInstance().getGameClient()
|
ServerDescription serverDescription = ViewManager.getInstance().getGameClient()
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
//
|
||||||
|
// Source code recreated from a .class file by IntelliJ IDEA
|
||||||
|
// (powered by Fernflower decompiler)
|
||||||
|
//
|
||||||
|
|
||||||
|
package seng302.visualiser.validators;
|
||||||
|
|
||||||
|
import com.jfoenix.controls.JFXComboBox;
|
||||||
|
import com.jfoenix.validation.base.ValidatorBase;
|
||||||
|
import javafx.beans.DefaultProperty;
|
||||||
|
import javafx.scene.control.TextInputControl;
|
||||||
|
|
||||||
|
@DefaultProperty("icon")
|
||||||
|
public class FieldLengthValidator extends ValidatorBase {
|
||||||
|
|
||||||
|
Integer maxLength;
|
||||||
|
|
||||||
|
public FieldLengthValidator(Integer length) {
|
||||||
|
maxLength = length;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void eval() {
|
||||||
|
if(this.srcControl.get() instanceof TextInputControl) {
|
||||||
|
this.evalTextInputField();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void evalTextInputField() {
|
||||||
|
TextInputControl textField = (TextInputControl)this.srcControl.get();
|
||||||
|
if(textField.getLength() > maxLength) {
|
||||||
|
this.hasErrors.set(true);
|
||||||
|
} else {
|
||||||
|
this.hasErrors.set(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package seng302.visualiser.validators;
|
||||||
|
|
||||||
|
import com.jfoenix.validation.base.ValidatorBase;
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
|
import javafx.beans.DefaultProperty;
|
||||||
|
import javafx.scene.control.TextInputControl;
|
||||||
|
|
||||||
|
@DefaultProperty("icon")
|
||||||
|
public class HostNameFieldValidator extends ValidatorBase {
|
||||||
|
|
||||||
|
|
||||||
|
public HostNameFieldValidator() {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected void eval() {
|
||||||
|
if(this.srcControl.get() instanceof TextInputControl) {
|
||||||
|
this.evalTextInputField();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void evalTextInputField() {
|
||||||
|
TextInputControl textField = (TextInputControl)this.srcControl.get();
|
||||||
|
try{
|
||||||
|
InetAddress.getByName(textField.getText());
|
||||||
|
this.hasErrors.set(false);
|
||||||
|
} catch (UnknownHostException e) {
|
||||||
|
this.hasErrors.set(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package seng302.visualiser.validators;
|
||||||
|
|
||||||
|
import com.jfoenix.validation.base.ValidatorBase;
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
|
import javafx.beans.DefaultProperty;
|
||||||
|
import javafx.scene.control.TextInputControl;
|
||||||
|
|
||||||
|
@DefaultProperty("icon")
|
||||||
|
public class NumberRangeValidator extends ValidatorBase {
|
||||||
|
|
||||||
|
Integer lowerLimit;
|
||||||
|
Integer upperLimit;
|
||||||
|
|
||||||
|
public NumberRangeValidator(Integer lower, Integer upper) {
|
||||||
|
lowerLimit = lower;
|
||||||
|
upperLimit = upper;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected void eval() {
|
||||||
|
if(this.srcControl.get() instanceof TextInputControl) {
|
||||||
|
this.evalTextInputField();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void evalTextInputField() {
|
||||||
|
TextInputControl textField = (TextInputControl)this.srcControl.get();
|
||||||
|
try {
|
||||||
|
Integer portNum = Integer.parseInt(textField.getText());
|
||||||
|
if (lowerLimit <= portNum && portNum <= upperLimit) {
|
||||||
|
this.hasErrors.set(false);
|
||||||
|
} else {
|
||||||
|
this.hasErrors.set(true);
|
||||||
|
}
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
this.hasErrors.set(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user