Filtering key events using Guava

This commit is contained in:
Sebastian Stenzel
2017-04-30 01:00:39 +02:00
parent c29d7fb6a2
commit 245a995203
2 changed files with 15 additions and 11 deletions

View File

@@ -14,11 +14,13 @@ import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import org.apache.commons.lang3.CharUtils;
import org.apache.commons.lang3.SystemUtils;
import org.cryptomator.common.settings.Settings;
import org.cryptomator.ui.settings.Localization;
import com.google.common.base.CharMatcher;
import com.google.common.base.Strings;
import javafx.beans.binding.Bindings;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
@@ -34,6 +36,8 @@ import javafx.scene.layout.VBox;
@Singleton
public class SettingsController implements ViewController {
private static final CharMatcher DIGITS_MATCHER = CharMatcher.inRange('0', '9');
private final Localization localization;
private final Settings settings;
private final Optional<String> applicationVersion;
@@ -130,11 +134,7 @@ public class SettingsController implements ViewController {
}
private void filterNumericKeyEvents(KeyEvent t) {
if (t.getCharacter() == null || t.getCharacter().length() == 0) {
return;
}
char c = CharUtils.toChar(t.getCharacter());
if (!(CharUtils.isAsciiNumeric(c) || c == '_')) {
if (!Strings.isNullOrEmpty(t.getCharacter()) && !DIGITS_MATCHER.matchesAllOf(t.getCharacter())) {
t.consume();
}
}

View File

@@ -31,6 +31,9 @@ import org.cryptomator.ui.util.DialogBuilderUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.base.CharMatcher;
import com.google.common.base.Strings;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
@@ -55,6 +58,11 @@ import javafx.util.StringConverter;
public class UnlockController implements ViewController {
private static final Logger LOG = LoggerFactory.getLogger(UnlockController.class);
private static final CharMatcher ALPHA_NUMERIC_MATCHER = CharMatcher.inRange('a', 'z') //
.or(CharMatcher.inRange('A', 'Z')) //
.or(CharMatcher.inRange('0', '9')) //
.or(CharMatcher.is('_')) //
.precomputed();
private final Application app;
private final Localization localization;
@@ -212,11 +220,7 @@ public class UnlockController implements ViewController {
}
private void filterAlphanumericKeyEvents(KeyEvent t) {
if (t.getCharacter() == null || t.getCharacter().length() == 0) {
return;
}
char c = CharUtils.toChar(t.getCharacter());
if (!(CharUtils.isAsciiAlphanumeric(c) || c == '_')) {
if (!Strings.isNullOrEmpty(t.getCharacter()) && !ALPHA_NUMERIC_MATCHER.matchesAllOf(t.getCharacter())) {
t.consume();
}
}