Sfoglia il codice sorgente

Filtering key events using Guava

Sebastian Stenzel 8 anni fa
parent
commit
245a995203

+ 6 - 6
main/ui/src/main/java/org/cryptomator/ui/controllers/SettingsController.java

@@ -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();
 		}
 	}

+ 9 - 5
main/ui/src/main/java/org/cryptomator/ui/controllers/UnlockController.java

@@ -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();
 		}
 	}