Skip to content

Commit

Permalink
Improve text field for port settings
Browse files Browse the repository at this point in the history
Allow better editing.
  • Loading branch information
zapek committed Jan 4, 2025
1 parent 1462dc8 commit b0d5bee
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2023 by David Gerber - https://zapek.com
* Copyright (c) 2019-2025 by David Gerber - https://zapek.com
*
* This file is part of Xeres.
*
Expand Down Expand Up @@ -79,10 +79,10 @@ public SettingsNetworksController(ConfigClient configClient)
public void initialize()
{
TextFieldUtils.setHost(torSocksHost);
TextFieldUtils.setNumeric(torSocksPort, 0, 65535);
TextFieldUtils.setNumeric(torSocksPort, 0, 6);

TextFieldUtils.setHost(i2pSocksHost);
TextFieldUtils.setNumeric(i2pSocksPort, 0, 65535);
TextFieldUtils.setNumeric(i2pSocksPort, 0, 6);

configClient.getExternalIpAddress()
.doOnSuccess(ipAddressResponse -> Platform.runLater(() -> {
Expand Down Expand Up @@ -127,10 +127,10 @@ public void onLoad(Settings settings)
public Settings onSave()
{
settings.setTorSocksHost(TextFieldUtils.getString(torSocksHost));
settings.setTorSocksPort(TextFieldUtils.getAsNumber(torSocksPort));
settings.setTorSocksPort(limitPort(TextFieldUtils.getAsNumber(torSocksPort)));

settings.setI2pSocksHost(TextFieldUtils.getString(i2pSocksHost));
settings.setI2pSocksPort(TextFieldUtils.getAsNumber(i2pSocksPort));
settings.setI2pSocksPort(limitPort(TextFieldUtils.getAsNumber(i2pSocksPort)));

settings.setUpnpEnabled(upnpEnabled.isSelected());

Expand All @@ -140,4 +140,13 @@ public Settings onSave()

return settings;
}

private int limitPort(int port)
{
if (port > 65535)
{
port = 65535;
}
return port;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public SettingsRemoteController(TrayService trayService, ResourceBundle bundle)
@Override
public void initialize() throws IOException
{
TextFieldUtils.setNumeric(port, 1025, 65535);
TextFieldUtils.setNumeric(port, 0, 6);

var icon = new FontIcon("mdi2e-eye-off");
icon.setCursor(Cursor.HAND);
Expand Down Expand Up @@ -122,7 +122,7 @@ public Settings onSave()
if (!port.getText().isEmpty())
{
var portValue = Integer.parseInt(port.getText());
if (portValue != Objects.requireNonNull(StartupProperties.getInteger(CONTROL_PORT)))
if (portValue >= 1025 && portValue <= 65535 && portValue != Objects.requireNonNull(StartupProperties.getInteger(CONTROL_PORT)))
{
settings.setRemotePort(portValue);
portChanged = true;
Expand Down
16 changes: 8 additions & 8 deletions ui/src/main/java/io/xeres/ui/support/util/TextFieldUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2023 by David Gerber - https://zapek.com
* Copyright (c) 2019-2025 by David Gerber - https://zapek.com
*
* This file is part of Xeres.
*
Expand Down Expand Up @@ -36,15 +36,15 @@ private TextFieldUtils()
throw new UnsupportedOperationException("Utility class");
}

public static void setNumeric(TextField textField, int min, int max)
public static void setNumeric(TextField textField, int minChars, int maxChars)
{
if (min < 0 || max < 0)
if (minChars < 0 || maxChars < 0)
{
throw new IllegalArgumentException("Negative numbers are not supported");
throw new IllegalArgumentException("Negative char limits are not supported");
}
if (max < min)
if (maxChars < minChars)
{
throw new IllegalArgumentException("Max cannot be smaller than min");
throw new IllegalArgumentException("maxChars cannot be smaller than minChars");
}

var textFormatter = new TextFormatter<String>(change -> {
Expand All @@ -56,8 +56,8 @@ public static void setNumeric(TextField textField, int min, int max)
}
try
{
var value = Integer.parseInt(change.getControlNewText());
if (value >= min && value <= max)
Integer.parseInt(change.getControlNewText());
if (change.getControlNewText().length() >= minChars && change.getControlNewText().length() <= maxChars)
{
return change;
}
Expand Down

0 comments on commit b0d5bee

Please sign in to comment.