JGoodies Binding + constraints: how to achieve it?

167 views Asked by At

I have a tough question which probably has an easy answer (I hope).

I'm using JGoodies Binding to bind a slider to a ValueModel; works great.

Suppose I have the following (contrived) situation, where I want to model buying chocolates and caramels and I have $1.00 with the following UI indicators

  • C1 = "caramel" slider = budget for caramel candies = 0 to 50 cents
  • C2 = "chocolate" slider = budget for chocolate candies = 0 to 50 cents
  • C3 = "change" slider = remainder left over

I want to force a constraint C1+C2+C3 = 100, so that:

  • if I adjust C1 up/down by Δx, C3 adjusts by -Δx
  • if I adjust C2 up/down by Δx, C3 adjusts by -Δx
  • if I adjust C3 up/down by Δx, C1 adjusts by y=-floor(Δx/2), and C2 adjusts by -x-y

(so that the sum of adjustments is exactly 0 in all cases)

The obvious approach is to add a listener to each ValueModel to take the given actions in each case. But then I have to worry about two things:

  • circularity (changes in C1 trigger changes in C3 which then changes C1)
  • synchronization: I want to have a means of accessing the complete set (C1, C2, C3) such that the sum of the three numbers add up to 100.

How can I do this?

0

There are 0 answers