Skip to content

Commit 05b869d

Browse files
committed
♻️ (core) Selectable: add convenience onSelected method and change onSelectionChanged to throw UnsupportedOperationException by default
1 parent f5da99b commit 05b869d

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

modules/core/src/main/java/io/github/palexdev/mfxcore/popups/menu/MenuBuilder.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ public CheckMenuBuilder onSelectionChanged(Consumer<Boolean> onSelectionChanged)
173173
return this;
174174
}
175175

176+
public CheckMenuBuilder onSelected(Runnable onSelected) {
177+
return onSelectionChanged(s -> {if (s) onSelected.run();});
178+
}
179+
176180
public CheckMenuBuilder closeOnAction(boolean closeOnAction) {
177181
this.closeOnAction = closeOnAction;
178182
return this;

modules/core/src/main/java/io/github/palexdev/mfxcore/selection/Selectable.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ default void setSelectionGroup(SelectionGroup group) {
7070

7171
/// Allows specifying a callback to invoke when the selection state of this `Selectable` changes.
7272
///
73-
/// **This is an optional API. By default, does nothing.**
74-
default void onSelectionChanged(Consumer<Boolean> action) {}
73+
/// **This is an optional API. By default, throws an `UnsupportedOperationException`.**
74+
default void onSelectionChanged(Consumer<Boolean> action) {
75+
throw new UnsupportedOperationException("API not implemented by " + getClass());
76+
}
77+
78+
/// Delegate to [#onSelectionChanged(Consumer)] that runs the given action only when selection state is `true`.
79+
default void onSelected(Runnable action) {
80+
onSelectionChanged(s -> {if (s) action.run();});
81+
}
7582
}

0 commit comments

Comments
 (0)