2017-01-10 34 views
0

this question我被展示瞭如何通過改變它的包裝對象來處理改變屬性的問題,從而不發送改變的更新。溶液使用ReactFX:如何使用ReactFX的Var進行自定義綁定?

class Cell { 

    private final ObjectProperty<Shape> shape = new SimpleObjectProperty<>(new Shape()); 
    // all getters and setterts 

    public static class Shape { 

     private final IntegerProperty size = new SimpleIntegerProperty(0); 
     // all getters and setterts 
    } 

    public static void main(String[] args) { 

     Var<Number> sizeVar = Val.selectVar(cell.shapeProperty(), Shape::sizeProperty); 
     sizeVar.addListener(
      (obs, oldSize, newSize) -> System.out.println("Size changed from "+oldSize+" to "+newSize)); 
} 

所以,現在如果shape財產本身的變化就觸發size變化太大(除非新的形狀大小相同)。但是現在我想用自定義綁定綁定到屬性,並且我在下面解釋了一個問題。

我的數據類是這些:

class Cell { 

    private final ObjectProperty<Shape> shape = new SimpleObjectProperty<>(); 
    public final ObjectProperty<Shape> shapeProperty() { return shape; } 
    public final Shape getShape()      { return shapeProperty().get(); } 
    public final void setShape(Shape shape)   { shapeProperty().set(shape); } 

    // other properties 
} 

class Shape { 

    private final IntegerProperty size = new SimpleIntegerProperty(); 
    public final IntegerProperty sizeProperty() { return size; } 
    public final int getSize()     { return size.get(); } 
    public final void setSize(int size)   { sizeProperty().set(size); } 

    // other properties 
} 

,我想通過自己的特性結合GUI性質爲他們創造一個GUI表示。我做這種方式:

class CellRepresentation extends Group { 

    private final Cell cell; 

    CellRepresentation(Cell cell) { 

     this.cell = cell; 
     getChildren().add(new ShapeRepresentation() /*, other representations of things in the cell*/); 
    } 

    private class ShapeRepresentation extends Cylinder { 

     ObjectProperty<Shape> shape; 

     private ShapeRepresentation() { 

      super(100, 100); 

      shape = new SimpleObjectProperty<Shape>(cell.getShape()); 
      shape.bind(cell.shapeProperty()); 

      Var<Number> sizeVar = Val.selectVar(cell.shapeProperty(), Shape::sizeProperty); 

      // THIS WILL WORK 
      materialProperty().bind(Bindings.createObjectBinding(() -> { 
       if (shape.get() == null) 
        return new PhongMaterial(Color.TRANSPARENT); 
       return new PhongMaterial(Color.RED); 
      }, sizeVar)); 

      // THIS WILL NOT WORK 
      materialProperty().bind(sizeVar.map(n -> { 
       if (shape.get() == null) 
        return new PhongMaterial(Color.TRANSPARENT); 
       return new PhongMaterial(Color.RED); 
      })); 
     } 
    } 

    // the other representations of things in the cell 
} 

當我運行的結合將創造一個透明圓柱體的第一個選項下面的代碼。第二個選項將創建一個白色(默認顏色)圓柱體。我不知道爲什麼會發生這種情況。

public class Example extends Application { 

    public static void main(String[] args) { 

     launch(args); 
    } 

    @Override 
    public void start(Stage stage) throws Exception { 

     Cell cell = new Cell(); 
     CellRepresentation cellRep = new CellRepresentation(cell); 

     Group group = new Group(cellRep); 
     Scene scene = new Scene(group, 200, 200, Color.AQUA); 
     stage.setScene(scene); 
     stage.show(); 
    } 
} 

如果這不是一種使用綁定爲數據類創建表示的好方法,我也可以提供設計建議。

+0

什麼是'在你最後的代碼塊size' ?難道你不需要像if(sizeVar.getValue()。intValue()== 1){...}等等嗎? –

+0

@James_D'size'是'Shape'的屬性。我之前用你寫的這行代碼嘗試過(從'sizeVar'中取值),但沒有區別。我將創建一個MCVE。我認爲這只是錯誤的用法。 – Mark

+0

是的,我很困惑。當然'size'是'Shape'的成員,但綁定不在Shape類中。而且這無法編譯,因爲'size'是一個'IntegerProperty',不能用'=='與'1'進行比較。如果該版本不起作用,綁定可能會被垃圾收集。我的答案不起作用嗎? –

回答

2

ValVar是「可觀察的monadics」(想象可觀察的Optional s)。他們要麼是空的,要麼保持價值。 map方法的工作原理與Optional.map一樣:如果Val爲空,則map會產生空的Val;否則會導致Val包含將函數應用於原始Val的值的結果。因此,如果sizeVar的計算結果爲null,則映射將產生空的Val(因此您的材料設置爲null),甚至無需評估您的lambda表達式。

要處理null(即空Val S),你應該使用orElse或類似的方法:

private class ShapeRepresentation extends Cylinder { 

    Val<Shape> shape; 

    private ShapeRepresentation() { 

     super(100, 100); 

     shape = Val.wrap(cell.shapeProperty()); 

     Var<Number> sizeVar = shape.selectVar(Shape::sizeProperty); 

     // THIS WILL WORK 

     materialProperty().bind(shape 
      .map(s -> new PhongMaterial(Color.RED)) 
      .orElseConst(new PhongMaterial(Color.TRANSPARENT))); 

     // SO WILL THIS 

     materialProperty().bind(sizeVar 
       .map(n -> { 
        if (n.intValue() == 1) return new PhongMaterial(Color.RED) ; 
        if (n.intValue() == 2) return new PhongMaterial(Color.BLUE) ; 
        return new PhongMaterial(Color.WHITE); 
       }) 
       .orElseConst(new PhongMaterial(Color.TRANSPARENT))); 

    } 
} 

更新用於測試示例:

import javafx.application.Application; 
import javafx.beans.binding.Bindings; 
import javafx.geometry.Insets; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.control.CheckBox; 
import javafx.scene.control.ComboBox; 
import javafx.scene.layout.BorderPane; 
import javafx.scene.layout.HBox; 
import javafx.scene.paint.Color; 
import javafx.stage.Stage; 

public class Example extends Application { 

    public static void main(String[] args) { 

     launch(args); 
    } 

    @Override 
    public void start(Stage stage) throws Exception { 

     Cell cell = new Cell(); 

     CellRepresentation cellRep = new CellRepresentation(cell); 

     Group group = new Group(cellRep); 

     ComboBox<Integer> sizeCombo = new ComboBox<>(); 
     sizeCombo.getItems().addAll(0, 1, 2); 

     Shape shape = new Shape(); 
     shape.sizeProperty().bind(sizeCombo.valueProperty()); 


     CheckBox showShape = new CheckBox("Show shape"); 
     cell.shapeProperty().bind(Bindings.when(showShape.selectedProperty()).then(shape).otherwise((Shape)null)); 

     HBox controls = new HBox(5, showShape, sizeCombo); 
     controls.setPadding(new Insets(5)); 

     BorderPane root = new BorderPane(group, controls, null, null, null); 
     root.setBackground(null); 

     Scene scene = new Scene(root, 400, 400, Color.AQUA); 
     stage.setScene(scene); 
     stage.show(); 
    } 
} 
+0

這裏使用的'sizeVar'在哪裏? – Mark

+0

@標記它不是,但是您發佈的綁定也不依賴於它(它們只是需要知道形狀是否爲空/空)。我認爲你仍然需要它在你的代碼中的其他地方。 –

+0

好吧,我明白了。非常感謝你! – Mark

相關問題