diff --git a/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/SourceEditorController.java b/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/SourceEditorController.java index 46d9c6c206..3db0d477fe 100644 --- a/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/SourceEditorController.java +++ b/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/SourceEditorController.java @@ -184,7 +184,7 @@ public class SourceEditorController extends AbstractController { languageSelectionMenuButton.getItems().add(item); }); - languageVersionUIProperty = DesignerUtil.mapToggleGroupToUserData(languageToggleGroup); + languageVersionUIProperty = DesignerUtil.mapToggleGroupToUserData(languageToggleGroup, DesignerUtil::defaultLanguageVersion); } diff --git a/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/XPathPanelController.java b/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/XPathPanelController.java index fdd40208da..ac97483934 100644 --- a/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/XPathPanelController.java +++ b/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/XPathPanelController.java @@ -178,7 +178,7 @@ public class XPathPanelController extends AbstractController { xpathVersionMenuButton.getItems().add(item); }); - xpathVersionUIProperty = DesignerUtil.mapToggleGroupToUserData(xpathVersionToggleGroup); + xpathVersionUIProperty = DesignerUtil.mapToggleGroupToUserData(xpathVersionToggleGroup, DesignerUtil::defaultXPathVersion); setXpathVersion(XPathRuleQuery.XPATH_2_0); } diff --git a/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/util/DesignerUtil.java b/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/util/DesignerUtil.java index 5bcfb5cf15..1f2fbc98ef 100644 --- a/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/util/DesignerUtil.java +++ b/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/util/DesignerUtil.java @@ -21,6 +21,7 @@ import java.util.function.BiFunction; import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Predicate; +import java.util.function.Supplier; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -44,6 +45,7 @@ import javafx.beans.value.ObservableValue; import javafx.collections.ObservableList; import javafx.scene.control.ListCell; import javafx.scene.control.ListView; +import javafx.scene.control.Toggle; import javafx.scene.control.ToggleGroup; import javafx.scene.control.Tooltip; import javafx.util.Callback; @@ -142,19 +144,40 @@ public final class DesignerUtil { * maps the selected toggle property to a Var<T> */ @SuppressWarnings("unchecked") - public static Var mapToggleGroupToUserData(ToggleGroup toggleGroup) { + public static Var mapToggleGroupToUserData(ToggleGroup toggleGroup, Supplier defaultValue) { return Var.fromVal(toggleGroup.selectedToggleProperty(), toggleGroup::selectToggle) .mapBidirectional( item -> (T) item.getUserData(), - t -> toggleGroup.getToggles() - .stream() - .filter(toggle -> toggle.getUserData().equals(t)) - .findFirst() - .orElseThrow(() -> new IllegalStateException("Unknown toggle " + t)) + t -> selectFirst( + () -> findToggleWithUserData(toggleGroup, t), + () -> findToggleWithUserData(toggleGroup, defaultValue.get()) + ) + .orElseThrow(() -> new IllegalStateException("Unknown toggle " + t)) ); } + /** Returns the first non-empty optional in the arguments, or else Optional.empty. */ + @SafeVarargs + public static Optional selectFirst(Supplier>... opts) { + for (Supplier> optGetter : opts) { + Optional o = optGetter.get(); + if (o.isPresent()) { + return o; + } + } + return Optional.empty(); + } + + + private static Optional findToggleWithUserData(ToggleGroup toggleGroup, T data) { + return toggleGroup.getToggles() + .stream() + .filter(toggle -> toggle.getUserData().equals(data)) + .findFirst(); + } + + public static StringConverter languageVersionStringConverter() { return DesignerUtil.stringConverter(LanguageVersion::getShortName, s -> LanguageRegistry.findLanguageVersionByTerseName(s.toLowerCase(Locale.ROOT)));