Improve resilience to unknown language version

This commit is contained in:
Clément Fournier 2019-01-24 00:25:58 +01:00
parent 70ab933d2c
commit e4ea6d7f71
3 changed files with 31 additions and 8 deletions

View File

@ -184,7 +184,7 @@ public class SourceEditorController extends AbstractController {
languageSelectionMenuButton.getItems().add(item);
});
languageVersionUIProperty = DesignerUtil.mapToggleGroupToUserData(languageToggleGroup);
languageVersionUIProperty = DesignerUtil.mapToggleGroupToUserData(languageToggleGroup, DesignerUtil::defaultLanguageVersion);
}

View File

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

View File

@ -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 <T> Var<T> mapToggleGroupToUserData(ToggleGroup toggleGroup) {
public static <T> Var<T> mapToggleGroupToUserData(ToggleGroup toggleGroup, Supplier<T> 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 <T> Optional<T> selectFirst(Supplier<Optional<T>>... opts) {
for (Supplier<Optional<T>> optGetter : opts) {
Optional<T> o = optGetter.get();
if (o.isPresent()) {
return o;
}
}
return Optional.empty();
}
private static <T> Optional<Toggle> findToggleWithUserData(ToggleGroup toggleGroup, T data) {
return toggleGroup.getToggles()
.stream()
.filter(toggle -> toggle.getUserData().equals(data))
.findFirst();
}
public static StringConverter<LanguageVersion> languageVersionStringConverter() {
return DesignerUtil.stringConverter(LanguageVersion::getShortName,
s -> LanguageRegistry.findLanguageVersionByTerseName(s.toLowerCase(Locale.ROOT)));