-
Notifications
You must be signed in to change notification settings - Fork 553
8290040: Provide simplified deterministic way to manage listeners #830
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 17 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
4c8453a
Fix bug where ObjectBinding returns null when revalidated immediately
hjohn 3485656
Change explanatory comment to block comment
hjohn a70161c
Add conditional bindings to ObservableValue
hjohn fd19fc3
Rename showing property to shown as it is already used in subclasses
hjohn 07e9d88
Update javadoc of "when" with better phrasing
hjohn 13c6e39
Small wording fixes
hjohn 2353852
Add missing new line
hjohn 44d2ced
Fix review comments
hjohn 4fa45c7
Merge remote-tracking branch 'origin/master' into
hjohn ffba692
Improve documentation of shown property
hjohn ea54699
Fix comment in test
hjohn 46f206a
Fix javadoc error
hjohn 5c22fdd
Adjust Node
hjohn 67c277c
Improve wording in javadoc and comments
hjohn 94b50b5
Remove changes to javafx.graphics Node
hjohn 75ccca6
Remove example referencing Node#shownProperty
hjohn 24872f0
Merge branch 'openjdk:master' into feature/conditional-bindings
hjohn 7318af2
Fix formatting in test
hjohn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
modules/javafx.base/src/main/java/com/sun/javafx/binding/ConditionalBinding.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package com.sun.javafx.binding; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| import javafx.beans.value.ObservableValue; | ||
|
|
||
| public class ConditionalBinding<T> extends LazyObjectBinding<T> { | ||
|
|
||
| private final ObservableValue<T> source; | ||
| private final ObservableValue<Boolean> nonNullCondition; | ||
|
|
||
| private Subscription subscription; | ||
|
|
||
| public ConditionalBinding(ObservableValue<T> source, ObservableValue<Boolean> condition) { | ||
| this.source = Objects.requireNonNull(source, "source cannot be null"); | ||
| this.nonNullCondition = Objects.requireNonNull(condition, "condition cannot be null").orElse(false); | ||
|
|
||
| // condition is always observed and never unsubscribed | ||
| Subscription.subscribe(nonNullCondition, current -> { | ||
hjohn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| invalidate(); | ||
|
|
||
| if (!current) { | ||
| getValue(); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * This binding is valid whenever it is observed, or it is currently inactive. | ||
| * When inactive, the binding has the value of its source at the time it became | ||
| * inactive. | ||
| */ | ||
| @Override | ||
| protected boolean allowValidation() { | ||
| return super.allowValidation() || !isActive(); | ||
| } | ||
|
|
||
| @Override | ||
| protected T computeValue() { | ||
| if (isObserved() && isActive()) { | ||
| if (subscription == null) { | ||
| subscription = Subscription.subscribeInvalidations(source, this::invalidate); | ||
| } | ||
| } | ||
| else { | ||
| unsubscribe(); | ||
| } | ||
|
|
||
| return source.getValue(); | ||
| } | ||
|
|
||
| @Override | ||
| protected Subscription observeSources() { | ||
| return this::unsubscribe; | ||
| } | ||
|
|
||
| private boolean isActive() { | ||
| return nonNullCondition.getValue(); | ||
| } | ||
|
|
||
| private void unsubscribe() { | ||
| if (subscription != null) { | ||
| subscription.unsubscribe(); | ||
| subscription = null; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.