From 87f4966a7d09840d5b372408a84d82d6e9185be1 Mon Sep 17 00:00:00 2001 From: Sylvain Bertrand Date: Thu, 4 Apr 2024 16:16:09 -0500 Subject: [PATCH] [GUI] Bugfix: Some search fields wouldn't validate yoVariables. --- .../searchTextField/DoubleSearchField.java | 29 ++++++++++++++----- .../searchTextField/IntegerSearchField.java | 29 ++++++++++++++----- .../searchTextField/PropertySearchField.java | 12 +++++++- 3 files changed, 54 insertions(+), 16 deletions(-) diff --git a/scs2-session-visualizer-jfx/src/main/java/us/ihmc/scs2/sessionVisualizer/jfx/controllers/editor/searchTextField/DoubleSearchField.java b/scs2-session-visualizer-jfx/src/main/java/us/ihmc/scs2/sessionVisualizer/jfx/controllers/editor/searchTextField/DoubleSearchField.java index ebdd3f7e..92a6a661 100644 --- a/scs2-session-visualizer-jfx/src/main/java/us/ihmc/scs2/sessionVisualizer/jfx/controllers/editor/searchTextField/DoubleSearchField.java +++ b/scs2-session-visualizer-jfx/src/main/java/us/ihmc/scs2/sessionVisualizer/jfx/controllers/editor/searchTextField/DoubleSearchField.java @@ -18,6 +18,7 @@ import java.util.Collection; import java.util.List; +import java.util.Objects; import java.util.stream.Collectors; public class DoubleSearchField extends PropertySearchField @@ -42,22 +43,34 @@ protected boolean isTextValid(String text) { if (text == null || text.isEmpty()) return false; + if (CompositePropertyTools.isParsableAsDouble(text)) + return true; + else + return findYoComposite(text) != null; + } + private YoComposite findYoComposite(String name) + { YoCompositeCollection yoVariableCollection = searchManager.getYoVariableCollection(); - YoComposite yoComposite = yoVariableCollection.getYoCompositeFromUniqueName(text); - if (yoComposite == null) - yoComposite = yoVariableCollection.getYoCompositeFromFullname(text); - return yoComposite != null || CompositePropertyTools.isParsableAsDouble(text); + YoComposite yoComposite = yoVariableCollection.getYoCompositeFromFullname(name); + if (yoComposite != null) + return yoComposite; + else + return yoVariableCollection.getYoCompositeFromUniqueName(name); } @Override protected String simplifyText(String text) { - YoCompositeCollection yoVariableCollection = searchManager.getYoVariableCollection(); - YoComposite yoComposite = yoVariableCollection.getYoCompositeFromFullname(text); + if (CompositePropertyTools.isParsableAsDouble(text)) + return null; + + YoComposite yoComposite = findYoComposite(text); + if (yoComposite == null) - yoComposite = yoVariableCollection.getYoCompositeFromUniqueName(text); - return yoComposite == null ? null : yoComposite.getUniqueShortName(); + return null; + else + return !Objects.equals(yoComposite.getUniqueShortName(), text) ? yoComposite.getUniqueShortName() : null; } @Override diff --git a/scs2-session-visualizer-jfx/src/main/java/us/ihmc/scs2/sessionVisualizer/jfx/controllers/editor/searchTextField/IntegerSearchField.java b/scs2-session-visualizer-jfx/src/main/java/us/ihmc/scs2/sessionVisualizer/jfx/controllers/editor/searchTextField/IntegerSearchField.java index 3177b7ba..467ad981 100644 --- a/scs2-session-visualizer-jfx/src/main/java/us/ihmc/scs2/sessionVisualizer/jfx/controllers/editor/searchTextField/IntegerSearchField.java +++ b/scs2-session-visualizer-jfx/src/main/java/us/ihmc/scs2/sessionVisualizer/jfx/controllers/editor/searchTextField/IntegerSearchField.java @@ -18,6 +18,7 @@ import java.util.Collection; import java.util.List; +import java.util.Objects; import java.util.stream.Collectors; public class IntegerSearchField extends PropertySearchField @@ -53,20 +54,34 @@ protected boolean isTextValid(String text) { if (text == null || text.isEmpty()) return isInputOptional; + if (CompositePropertyTools.isParsableAsInteger(text)) + return true; + else + return findYoComposite(text) != null; + } + private YoComposite findYoComposite(String name) + { YoCompositeCollection yoIntegerCollection = searchManager.getYoIntegerCollection(); - YoComposite yoComposite = yoIntegerCollection.getYoCompositeFromUniqueName(text); - if (yoComposite == null) - yoComposite = yoIntegerCollection.getYoCompositeFromFullname(text); // TODO Happens when loading file, needs to update TextField to use unique name. - return yoComposite != null || CompositePropertyTools.isParsableAsInteger(text); + YoComposite yoComposite = yoIntegerCollection.getYoCompositeFromFullname(name); + if (yoComposite != null) + return yoComposite; + else + return yoIntegerCollection.getYoCompositeFromUniqueName(name); } @Override protected String simplifyText(String text) { - YoCompositeCollection yoIntegerCollection = searchManager.getYoIntegerCollection(); - YoComposite yoComposite = yoIntegerCollection.getYoCompositeFromFullname(text); - return yoComposite == null ? null : yoComposite.getUniqueShortName(); + if (CompositePropertyTools.isParsableAsInteger(text)) + return null; + + YoComposite yoComposite = findYoComposite(text); + + if (yoComposite == null) + return null; + else + return !Objects.equals(yoComposite.getUniqueShortName(), text) ? yoComposite.getUniqueShortName() : null; } @Override diff --git a/scs2-session-visualizer-jfx/src/main/java/us/ihmc/scs2/sessionVisualizer/jfx/controllers/editor/searchTextField/PropertySearchField.java b/scs2-session-visualizer-jfx/src/main/java/us/ihmc/scs2/sessionVisualizer/jfx/controllers/editor/searchTextField/PropertySearchField.java index c506eebc..22e4cb1f 100644 --- a/scs2-session-visualizer-jfx/src/main/java/us/ihmc/scs2/sessionVisualizer/jfx/controllers/editor/searchTextField/PropertySearchField.java +++ b/scs2-session-visualizer-jfx/src/main/java/us/ihmc/scs2/sessionVisualizer/jfx/controllers/editor/searchTextField/PropertySearchField.java @@ -50,7 +50,7 @@ public PropertySearchField(TextField textField, ImageView validImageView) return; String simplifiedText = simplifyText(newValue); - if (simplifiedText != null) + if (simplifiedText != null && !simplifiedText.equals(newValue)) { textField.setText(simplifiedText); return; @@ -71,6 +71,16 @@ public PropertySearchField(TextField textField, ImageView validImageView) protected abstract boolean isTextValid(String text); + /** + * Simplify the text to be displayed in the text field. + *

+ * This method is useful to simplify the text displayed in the text field when the user types. + * It is expected to return {@code null} if the text is already in a simplified form. + *

+ * + * @param text the text to simplify. + * @return the simplified text, or {@code null} if the text is already in a simplified form. + */ protected abstract String simplifyText(String text); protected abstract Callback> createSuggestions();