mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 06:18:44 +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.JFXTextField;
|
||||
import com.jfoenix.validation.RequiredFieldValidator;
|
||||
import com.jfoenix.validation.base.ValidatorBase;
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.URL;
|
||||
@@ -28,6 +29,8 @@ import seng302.gameServer.ServerDescription;
|
||||
import seng302.visualiser.ServerListener;
|
||||
import seng302.visualiser.ServerListenerDelegate;
|
||||
import seng302.visualiser.controllers.cells.ServerCell;
|
||||
import seng302.visualiser.validators.HostNameFieldValidator;
|
||||
import seng302.visualiser.validators.NumberRangeValidator;
|
||||
|
||||
public class ServerListController implements Initializable, ServerListenerDelegate {
|
||||
|
||||
@@ -53,6 +56,11 @@ public class ServerListController implements Initializable, ServerListenerDelega
|
||||
private JFXTextField serverPortNumber;
|
||||
//---------FXML END---------//
|
||||
|
||||
//Validators
|
||||
private HostNameFieldValidator hostNameValidator;
|
||||
private NumberRangeValidator portNumberValidator;
|
||||
|
||||
|
||||
private Label noServersFound;
|
||||
private Logger logger = LoggerFactory.getLogger(ServerListController.class);
|
||||
|
||||
@@ -63,17 +71,27 @@ public class ServerListController implements Initializable, ServerListenerDelega
|
||||
// Set Event Bindings
|
||||
connectButton.setOnMouseReleased(event -> attemptToDirectConnect());
|
||||
for (JFXTextField textField : Arrays.asList(serverHostName, serverPortNumber)) {
|
||||
// Event for pressing enter to submit direct connection
|
||||
textField.setOnKeyPressed(event -> {
|
||||
if (event.getCode().equals(KeyCode.ENTER)) {
|
||||
attemptToDirectConnect();
|
||||
}
|
||||
});
|
||||
|
||||
// Validators as empty fields are invalid.
|
||||
RequiredFieldValidator validator = new RequiredFieldValidator();
|
||||
validator.setMessage("Field is Required");
|
||||
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
|
||||
try {
|
||||
@@ -119,50 +137,25 @@ public class ServerListController implements Initializable, ServerListenerDelega
|
||||
|
||||
|
||||
private Boolean validateDirectConnection(String hostName, String portNumber) {
|
||||
Boolean hostNameValid = validateHostName(hostName);
|
||||
Boolean portNumberValid = validatePortNumber(portNumber);
|
||||
Boolean hostNameValid = validateTextField(serverHostName);
|
||||
Boolean portNumberValid = validateTextField(serverPortNumber);
|
||||
|
||||
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
|
||||
*/
|
||||
private Boolean validatePortNumber(String portNumber) {
|
||||
try {
|
||||
Integer portNum = Integer.parseInt(serverPortNumber.getText());
|
||||
if (portNum > 1024 && portNum <= 65536) {
|
||||
return true;
|
||||
} else {
|
||||
System.out.println(portNum.toString() + " is not a valid port number");
|
||||
private Boolean validateTextField(JFXTextField textField) {
|
||||
textField.validate();
|
||||
for (ValidatorBase validator : textField.getValidators()) {
|
||||
if (validator.getHasErrors()) {
|
||||
return false;
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
System.out.println("Not a valid number.");
|
||||
}
|
||||
serverPortNumber.validate();
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+10
-11
@@ -4,7 +4,6 @@ import com.jfoenix.controls.JFXButton;
|
||||
import com.jfoenix.controls.JFXSlider;
|
||||
import com.jfoenix.validation.RequiredFieldValidator;
|
||||
import java.net.URL;
|
||||
import java.util.Arrays;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
import com.jfoenix.controls.JFXTextField;
|
||||
@@ -12,6 +11,7 @@ import javafx.fxml.FXML;
|
||||
import javafx.fxml.Initializable;
|
||||
import javafx.scene.control.Label;
|
||||
import seng302.gameServer.ServerDescription;
|
||||
import seng302.visualiser.validators.FieldLengthValidator;
|
||||
import seng302.visualiser.controllers.ViewManager;
|
||||
|
||||
public class ServerCreationController implements Initializable {
|
||||
@@ -19,17 +19,16 @@ public class ServerCreationController implements Initializable {
|
||||
//--------FXML BEGIN--------//
|
||||
@FXML
|
||||
private JFXTextField serverName;
|
||||
|
||||
@FXML
|
||||
private JFXSlider maxPlayersSlider;
|
||||
@FXML
|
||||
private Label maxPlayersLabel;
|
||||
|
||||
@FXML
|
||||
private JFXButton submitBtn;
|
||||
//---------FXML END---------//
|
||||
|
||||
RequiredFieldValidator validator;
|
||||
FieldLengthValidator fieldLengthValidator;
|
||||
RequiredFieldValidator fieldRequiredValidator;
|
||||
|
||||
public void initialize(URL location, ResourceBundle resources) {
|
||||
updateMaxPlayerLabel();
|
||||
@@ -37,22 +36,22 @@ public class ServerCreationController implements Initializable {
|
||||
updateMaxPlayerLabel();
|
||||
});
|
||||
|
||||
validator = new RequiredFieldValidator();
|
||||
serverName.getValidators().add(validator);
|
||||
fieldLengthValidator = new FieldLengthValidator(40);
|
||||
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.setOnMouseReleased(event -> 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();
|
||||
System.out.println(serverName.getActiveValidator());
|
||||
}
|
||||
|
||||
|
||||
public void createServer() {
|
||||
|
||||
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