Skip to content

Commit

Permalink
Merge pull request #2740 from Mathis-Hu/master
Browse files Browse the repository at this point in the history
Spinners (widget properties) can be edited by typing a number #2738
  • Loading branch information
kasemir authored Jul 19, 2023
2 parents c47d883 + f3c08e0 commit 9885de7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@
public class ArraySizePropertyBinding extends WidgetPropertyBinding<Spinner<Integer>, ArrayWidgetProperty<WidgetProperty<?>>>
{
private PropertyPanelSection panel_section;
private int min_value, max_value;

/** Check text element from array property in response to property UI */
private final ChangeListener<? super String> text_listener = (observable, oldValue, newValue) ->
{
if(!newValue.isEmpty()){
if(!newValue.matches("\\d*")){
jfx_node.getEditor().setText(oldValue);
} else if(Integer.parseInt(newValue) < min_value){
jfx_node.getEditor().setText(String.valueOf(min_value));
} else if(Integer.parseInt(newValue) > max_value){
jfx_node.getEditor().setText(String.valueOf(max_value));
}
}
};

/** Add/remove elements from array property in response to property UI */
@SuppressWarnings({ "rawtypes", "unchecked" })
Expand All @@ -47,8 +62,10 @@ public class ArraySizePropertyBinding extends WidgetPropertyBinding<Spinner<Inte
// By removing the focus from the spinner, the property panel seems to "stay put".
FocusUtil.removeFocus(jfx_node);

if(jfx_node.getValue() == null){
jfx_node.getValueFactory().setValue(min_value);
}
final int desired = jfx_node.getValue();

// Grow/shrink array via undo-able actions
final String path = widget_property.getPath();
while (widget_property.size() < desired)
Expand All @@ -72,7 +89,7 @@ public class ArraySizePropertyBinding extends WidgetPropertyBinding<Spinner<Inte
};

/** Update property sub-panel as array elements are added/removed */
private WidgetPropertyListener<List<WidgetProperty<?>>> prop_listener = (prop, removed, added) ->
private final WidgetPropertyListener<List<WidgetProperty<?>>> prop_listener = (prop, removed, added) ->
{
// Re-populate the complete property panel.
// Combined with the un-focus call above when changing the array size, this "works":
Expand All @@ -90,21 +107,26 @@ public class ArraySizePropertyBinding extends WidgetPropertyBinding<Spinner<Inte
* @param other Widgets that also have this array property
*/
public ArraySizePropertyBinding(final PropertyPanelSection panel_section,
final UndoableActionManager undo,
final Spinner<Integer> node,
final ArrayWidgetProperty<WidgetProperty<?>> widget_property,
final List<Widget> other)
final UndoableActionManager undo,
final Spinner<Integer> node,
final ArrayWidgetProperty<WidgetProperty<?>> widget_property,
final List<Widget> other,
final int min_value,
final int max_value)
{
super(undo, node, widget_property, other);
this.min_value = min_value;
this.max_value = max_value;
this.panel_section = panel_section;
}

@Override
public void bind()
{
jfx_node.getEditor().textProperty().addListener(text_listener);
jfx_node.setEditable(true);
jfx_node.valueProperty().addListener(ui_listener);
jfx_node.getValueFactory().setValue(widget_property.size());

widget_property.addPropertyListener(prop_listener);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ else if (property instanceof EnumWidgetProperty<?>)
final EventHandler<ActionEvent> macro_handler = event ->
{
final boolean use_macro = macroButton.isSelected() ||
MacroHandler.containsMacros(enum_prop.getSpecification());
MacroHandler.containsMacros(enum_prop.getSpecification());
combo.setEditable(use_macro);
// Combo's text field has been set to the current value
// while the combo was non-editable.
Expand Down Expand Up @@ -546,7 +546,7 @@ else if (property instanceof MacroizedWidgetProperty)
Tooltip.install(text, new Tooltip(text.getText()));

if (CommonWidgetProperties.propText.getName().equals(property.getName()) ||
CommonWidgetProperties.propTooltip.getName().equals(property.getName()))
CommonWidgetProperties.propTooltip.getName().equals(property.getName()))
{ // Allow editing multi-line text in dialog
final Button open_editor = new Button("...");
Tooltip.install(open_editor, new Tooltip("Open Editor"));
Expand Down Expand Up @@ -603,11 +603,11 @@ else if (property instanceof PointsWidgetProperty)
* @param indentationLevel Indentation level
*/
private void createPropertyUI(
final UndoableActionManager undo,
final WidgetProperty<?> property,
final List<Widget> other,
final int structureIndex,
final int indentationLevel)
final UndoableActionManager undo,
final WidgetProperty<?> property,
final List<Widget> other,
final int structureIndex,
final int indentationLevel)
{
// Skip runtime properties
if (property.getCategory() == WidgetPropertyCategory.RUNTIME)
Expand Down Expand Up @@ -711,8 +711,10 @@ else if (property instanceof ArrayWidgetProperty)
final ArrayWidgetProperty<WidgetProperty<?>> array = (ArrayWidgetProperty<WidgetProperty<?>>) property;

// UI for changing array size
final Spinner<Integer> spinner = new Spinner<>(array.getMinimumSize(), 100, 0);
final ArraySizePropertyBinding count_binding = new ArraySizePropertyBinding(this, undo, spinner, array, other);
final int min_value = array.getMinimumSize();
final int max_value = 100;
final Spinner<Integer> spinner = new Spinner<>(min_value, max_value, 0);
final ArraySizePropertyBinding count_binding = new ArraySizePropertyBinding(this, undo, spinner, array, other, min_value, max_value);
bindings.add(count_binding);
count_binding.bind();

Expand Down Expand Up @@ -820,7 +822,7 @@ else if (property instanceof ArrayWidgetProperty)

final Widget widget = property.getWidget();
if (! (property == widget.getProperty("type") ||
property == widget.getProperty("name")))
property == widget.getProperty("name")))
{
if (class_mode)
{ // Class definition mode:
Expand Down

0 comments on commit 9885de7

Please sign in to comment.