Example usage for javafx.beans.binding Bindings add

List of usage examples for javafx.beans.binding Bindings add

Introduction

In this page you can find the example usage for javafx.beans.binding Bindings add.

Prototype

public static NumberBinding add(int op1, final ObservableNumberValue op2) 

Source Link

Document

Creates a new javafx.beans.binding.NumberBinding that calculates the sum of the value of a javafx.beans.value.ObservableNumberValue and a constant value.

Usage

From source file:Main.java

public static void main(String[] args) {
    IntegerProperty x1 = new SimpleIntegerProperty(0);
    IntegerProperty y1 = new SimpleIntegerProperty(0);

    final NumberBinding x1y2 = Bindings.multiply(x1, y1);

    final NumberBinding sum1 = Bindings.add(x1y2, 2);
    final NumberBinding diff1 = Bindings.subtract(sum1, 1);
    final NumberBinding determinant = Bindings.subtract(diff1, 2);
    final NumberBinding area = Bindings.divide(determinant, 2.0D);

    x1.set(0);/*from  www.j ava2 s  . c  o m*/
    y1.set(0);

    printResult(x1, y1, area);

    x1.set(1);
    y1.set(0);

    printResult(x1, y1, area);
}

From source file:MyClass.java

public static void main(String[] args) {

    MyClass myObject1 = new MyClass();
    MyClass myObject2 = new MyClass();
    MyClass myObject3 = new MyClass();

    NumberBinding total = Bindings.add(myObject1.amountDueProperty().add(myObject2.amountDueProperty()),
            myObject3.amountDueProperty());
    total.addListener(new InvalidationListener() {
        @Override//from ww  w.j av a 2 s. c om
        public void invalidated(Observable o) {
            System.out.println("The binding is now invalid.");
        }
    });
    myObject1.setAmountDue(200.00);
    myObject2.setAmountDue(100.00);
    myObject3.setAmountDue(75.00);
    System.out.println(total.getValue());
    myObject3.setAmountDue(150.00);
    System.out.println(total.getValue());
}

From source file:edu.kit.trufflehog.view.jung.visualization.FXVisualizationViewer.java

synchronized private void initEdge(IConnection edge) {

    final Pair<INode> pair = this.layout.getGraph().getEndpoints(edge);

    if (pair.getSecond().getAddress().isMulticast()) {

        // TODO check or do something with this (generify the getShape command?)
        final Circle destCircle = (Circle) pair.getFirst().getComponent(ViewComponent.class).getRenderer()
                .getShape();/*from w w w  . j  ava 2  s. co m*/
        final Shape shape = edge.getComponent(ViewComponent.class).getRenderer().getShape();
        shape.layoutXProperty().bind(destCircle.layoutXProperty());
        shape.layoutYProperty().bind(destCircle.layoutYProperty());
        canvas.getChildren().add(shape);
        shape.setPickOnBounds(false);
        shape.setMouseTransparent(true);
        return;
    }

    // source and destination shape
    final Shape srcShape = pair.getFirst().getComponent(ViewComponent.class).getRenderer().getShape();
    final Shape destShape = pair.getSecond().getComponent(ViewComponent.class).getRenderer().getShape();

    // the positions of the shapes
    final DoubleBinding srcX = srcShape.layoutXProperty().add(srcShape.translateXProperty());
    final DoubleBinding srcY = srcShape.layoutYProperty().add(srcShape.translateYProperty());
    final DoubleBinding destX = destShape.layoutXProperty().add(destShape.translateXProperty());
    final DoubleBinding destY = destShape.layoutYProperty().add(destShape.translateYProperty());

    // the direction vector from source to destination (deltaX, deltaY)
    final DoubleBinding deltaX = destX.subtract(srcX);
    final DoubleBinding deltaY = destY.subtract(srcY);

    // the length of the direction vector
    final DoubleBinding length = MyBindings
            .sqrt(Bindings.add(MyBindings.pow2(deltaX), MyBindings.pow2(deltaY)));

    // the normalized direction vector
    final DoubleBinding normalX = deltaX.divide(length);
    final DoubleBinding normalY = deltaY.divide(length);

    // cast the shapes to circles (because right now i know they are circles) //TODO make this for arbitrary shapes
    final Circle destCircle = (Circle) destShape;
    final Circle srcCircle = (Circle) srcShape;

    // get the real source by multiplying the normal with the radius and the scale of the shape
    final DoubleBinding realSoureX = srcX
            .add(normalX.multiply(srcCircle.radiusProperty().multiply(srcShape.scaleXProperty())));
    final DoubleBinding realSoureY = srcY
            .add(normalY.multiply(srcCircle.radiusProperty().multiply(srcShape.scaleYProperty())));

    // get the real destination by multipling the normal vector with the length minus the radius and scale of the destination shape
    final DoubleBinding realDestX = srcX.add(normalX
            .multiply(length.subtract(destCircle.radiusProperty().multiply(destShape.scaleXProperty()))));
    final DoubleBinding realDestY = srcY.add(normalY
            .multiply(length.subtract(destCircle.radiusProperty().multiply(destShape.scaleYProperty()))));

    final IEdgeRenderer edgeRenderer = (IEdgeRenderer) edge.getComponent(ViewComponent.class).getRenderer();

    edgeRenderer.getArrowShape().layoutXProperty().bind(realDestX);
    edgeRenderer.getArrowShape().layoutYProperty().bind(realDestY);

    final QuadCurve curve = edgeRenderer.getLine();
    curve.setCacheHint(CacheHint.SPEED);

    curve.addEventFilter(MouseEvent.MOUSE_PRESSED, edgeGestures.getOnMousePressedEventHandler(edge));
    curve.addEventFilter(MouseEvent.MOUSE_RELEASED, edgeGestures.getOnMouseReleasedEventHandler(edge));
    edgeRenderer.getArrowShape().addEventFilter(MouseEvent.MOUSE_PRESSED,
            edgeGestures.getOnMousePressedEventHandler(edge));
    edgeRenderer.getArrowShape().addEventFilter(MouseEvent.MOUSE_RELEASED,
            edgeGestures.getOnMouseReleasedEventHandler(edge));

    /*        // make the edge clickable
            curve.setOnMouseClicked(e -> {
            
    if (e.getButton() != MouseButton.PRIMARY) {
        return;
    }
            
    logger.debug(edge.getSrc().getComponent(NodeInfoComponent.class).toString() + " --[" + edge.getComponent(EdgeStatisticsComponent.class).getTraffic() +
            "]-->" + edge.getDest().getComponent(NodeInfoComponent.class).toString());
            
    //            edge.getComponent(ViewComponent.class).getRenderer().togglePicked();
            });
            
            edgeRenderer.getArrowShape().setOnMouseClicked(e -> {
            
    if (e.getButton() != MouseButton.PRIMARY) {
        return;
    }
            
    logger.debug(edge.getSrc().getComponent(NodeInfoComponent.class).toString() + " --[" + edge.getComponent(EdgeStatisticsComponent.class).getTraffic() +
    "]-->" + edge.getDest().getComponent(NodeInfoComponent.class).toString());
            
    //            edge.getComponent(ViewComponent.class).getRenderer().togglePicked();
            
            
            });*/

    //get the edge size binding by dividing the total traffic with the local traffic
    DoubleBinding edgeSize = MyBindings
            .divideIntToDouble(edge.getComponent(EdgeStatisticsComponent.class).getTrafficProperty(),
                    port.getMaxConnectionSizeProperty())
            .multiply(8).add(2);
    curve.strokeWidthProperty().bind(edgeSize.multiply(edgeRenderer.edgeWidthMultiplierProperty()));

    // bind the ending to the real destionaion point
    curve.endXProperty().bind(realDestX);
    curve.endYProperty().bind(realDestY);

    // bind the source to the real source point
    curve.startXProperty().bind(realSoureX);
    curve.startYProperty().bind(realSoureY);

    NumberBinding normalVectorX = Bindings.subtract(realDestY, realSoureY).negate();
    NumberBinding normalVectorY = Bindings.subtract(realDestX, realSoureX);

    NumberBinding centerPointX = Bindings.divide(Bindings.add(curve.endXProperty(), curve.startXProperty()), 2);
    NumberBinding centerPointY = Bindings.divide(Bindings.add(curve.endYProperty(), curve.startYProperty()), 2);

    NumberBinding normalLength = MyBindings
            .sqrt(Bindings.add(normalVectorX.multiply(normalVectorX), normalVectorY.multiply(normalVectorY)));

    NumberBinding normalizedNVX = normalVectorX.divide(normalLength);
    NumberBinding normalizedNVY = normalVectorY.divide(normalLength);

    NumberBinding bezierPointOffset = length.multiply(.1);

    curve.controlXProperty().bind(Bindings.add(centerPointX, normalizedNVX.multiply(bezierPointOffset)));
    curve.controlYProperty().bind(Bindings.add(centerPointY, normalizedNVY.multiply(bezierPointOffset)));

    // TODO do this in component
    curve.setFill(null);

    canvas.getChildren().add(edgeRenderer.getArrowShape());
    // add the edge to the canvas
    canvas.getChildren().add(curve);
}