add calculating methods to bind

Tomas Mikula tomas.mikula at gmail.com
Wed May 21 20:00:01 UTC 2014


Just a side note, you could use

    EasyBind.map(heightProperty(), value ->
NodeUtil.snapXY(value.doubleValue() * 0.2));

instead of

    snap(heightProperty().multiply(0.2))

and you don't need your custom DoubleBinding implementation. But note
that it returns Binding<Double>, not DoubleBinding.

EasyBind is at http://www.fxmisc.org/easybind/

Tomas

On Wed, May 21, 2014 at 9:47 PM, Tomas Mikula <tomas.mikula at gmail.com> wrote:
> Yeah, and you could also eliminate "Observable... dependencies" from
> snap's signature, since now you are not using them to invalidate your
> binding.
>
> On Wed, May 21, 2014 at 9:36 PM, Tom Eugelink <tbee at tbee.org> wrote:
>>
>> Found it! Slightly different DoubleBinding was required (copied it from
>> negate).
>>
>>     private DoubleBinding snap(final ObservableNumberValue value, final
>> Observable... dependencies) {
>>         return new DoubleBinding() {
>>             {
>>                 super.bind(value);
>>             }
>>
>>             @Override
>>             public void dispose() {
>>                 super.unbind(value);
>>             }
>>
>>             @Override
>>             protected double computeValue() {
>>                 return NodeUtil.snapXY(value.doubleValue());
>>
>>             }
>>
>>             @Override
>>             public ObservableList<?> getDependencies() {
>>                 return FXCollections.singletonObservableList(value);
>>             }
>>         };
>>
>>     }
>>
>>
>>
>> On 2014-5-21 21:34, Tomas Mikula wrote:
>>>
>>> Hi Tom,
>>>
>>> it seems to me that in your custom DoubleBinding implementation
>>> `other` is not its dependency, thus it does not react to it's
>>> invalidations.
>>>
>>> Tomas
>>>
>>> On Wed, May 21, 2014 at 9:25 PM, Tom Eugelink <tbee at tbee.org> wrote:
>>>>
>>>> I came up with this:
>>>>
>>>> startXProperty().bind( *snap(*heightProperty().multiply(0.2)) )
>>>>
>>>>
>>>> And then:
>>>>
>>>>      private DoubleBinding snap(final ObservableNumberValue other, final
>>>> Observable... dependencies) {
>>>>          return new DoubleBinding() {
>>>>              {
>>>>                  super.bind(dependencies);
>>>>              }
>>>>
>>>>              @Override
>>>>              public void dispose() {
>>>>                  super.unbind(dependencies);
>>>>              }
>>>>
>>>>              @Override
>>>>              protected double computeValue() {
>>>>                  return NodeUtil.snap(other.doubleValue());
>>>>              }
>>>>
>>>>              @Override
>>>>              public ObservableList<?> getDependencies() {
>>>>                  return new
>>>> ImmutableObservableList<Observable>(dependencies);
>>>>              }
>>>>          };
>>>>      }
>>>>
>>>> But that always returns 0... I feel this should have worked.
>>>>
>>>> Tom
>>
>>
>>


More information about the openjfx-dev mailing list