5 Replies Latest reply: Sep 17, 2013 1:17 PM by jrguenther RSS

    How do I implement my own JavaFX properties?

    jrguenther

      Please bear with me as I give some context, so my question makes sense. I recently discovered JavaFX and am using it to create the UI for my dissertation. As I have been learning JavaFX, I bumped into to properties and binding. Ironically, I'm working on something quite similar for my PhD. I'm working on extending parametric systems, the kind of thing used in CAD programs like Grasshopper, and Generative Components. If you're not familiar with parametric CAD, it is where an architect programs geometry with a graphical programming language. For example, two boxes are dragged into the programming area. You fill in the x and y components for each point node (this is what they are called CAD) then you drag in a line node and hook its two end points up to the points. The idea is if you change the coordinates of the points, the line will update accordingly. I have my own implementation of a parametric system that uses a directed acyclic graph and a topological sort to get the evaluation order of nodes.

       

      Then I saw JavaFX properties while reading Pro JavaFX 2 and the bindings tutorial, same general concept. Wow. I had considered an event based approach for updating ports (JavaFXers read properties), but ignored it for a variety of reasons. Having seen the way Properties work in JavaFX, I want to try to extend them do something unique. My thesis is about new type of parametric system called a subjunctive parametric system, basically it is a parametric system where properties can have multiple values and all of the possible solutions are calculated when a change is made.

       

      I would like to introduce a new concept called a subjunctive property.


      A SubjunctiveIntegerProperty has map of alternative values.

      values = { a1 -> 3, a2 ->} The map keys are names of the alternative values for the property.

       

      Imagining this in java code:

       

      SubjunctiveIntegerProperty int1 = new SubjunctiveIntegerProperty(); // map is empty

      int1.addAltValue("a1", 3) // {"a1" -> 3}

      int1.addAltValue("a2", 22) // {"a1" -> 3, "a2" -> 22}

       

       

      SubjunctiveIntegerProperty int2 = new SubjunctiveIntegerProperty(); // map is empty

      int2.addAltValue("a0", 47) // {"a0" -> 47} Note: I have not duplicated keys to ensure that it is clear that alternative must be explicitly selected for each property. No magic here.

      int2.addAltValue("a3", -5) // {"a0" -> 47, "a3" -> -5}

       

      SimpleIntegerProperty int3 = new SimpleIntegerProperty();

      int3.bind(int2)

       

      Now, to evaluate some alternatives. I want to evaluate the sum of int1 and int2 with a1 selected in int1 and a0 selected in int2

      int sum = int1.get() + int3.get()

       

      Before we can evaluate the sum, we must set the active values

       

      int1.setAlt("a1");

      int2.setAlt("a3");

      int sum = int1.get() + int3.get()

      println(sum) // will give us 3 + -5 = -2

       

      Do you have any suggestions how I might go about creating this sort of type of property? I would like my new properties to play well with the JavaFX properties if I can. I have looked at the code for SimpleDoubleProperty and it is straightforward enough on the surface. The problem comes when I dive into the source it uses things like ExpressionHelper<T> and a bunch of other classes that are in the com.sun.javafx.binding package. What is the best way to go about implementing my own properties? Or is what I'm trying to do some different that I need to go out on my own?