Example usage for javafx.beans.binding ObjectBinding ObjectBinding

List of usage examples for javafx.beans.binding ObjectBinding ObjectBinding

Introduction

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

Prototype

ObjectBinding

Source Link

Usage

From source file:gov.va.isaac.gui.ConceptNode.java

/**
 * descriptionReader is optional//from w w  w .ja v a 2s.co  m
 */
public ConceptNode(ConceptVersionBI initialConcept, boolean flagAsInvalidWhenBlank,
        ObservableList<SimpleDisplayConcept> dropDownOptions,
        Function<ConceptVersionBI, String> descriptionReader) {
    c_ = initialConcept;
    //We can't simply use the ObservableList from the CommonlyUsedConcepts, because it infinite loops - there doesn't seem to be a way 
    //to change the items in the drop down without changing the selection.  So, we have this hack instead.
    listChangeListener_ = new ListChangeListener<SimpleDisplayConcept>() {
        @Override
        public void onChanged(Change<? extends SimpleDisplayConcept> c) {
            //TODO I still have an infinite loop here.  Find and fix.
            logger.debug("updating concept dropdown");
            disableChangeListener_ = true;
            SimpleDisplayConcept temp = cb_.getValue();
            cb_.setItems(FXCollections.observableArrayList(dropDownOptions_));
            cb_.setValue(temp);
            cb_.getSelectionModel().select(temp);
            disableChangeListener_ = false;
        }
    };
    descriptionReader_ = (descriptionReader == null ? (conceptVersion) -> {
        return conceptVersion == null ? "" : OTFUtility.getDescription(conceptVersion);
    } : descriptionReader);
    dropDownOptions_ = dropDownOptions == null
            ? AppContext.getService(CommonlyUsedConcepts.class).getObservableConcepts()
            : dropDownOptions;
    dropDownOptions_.addListener(new WeakListChangeListener<SimpleDisplayConcept>(listChangeListener_));
    conceptBinding_ = new ObjectBinding<ConceptVersionBI>() {
        @Override
        protected ConceptVersionBI computeValue() {
            return c_;
        }
    };

    flagAsInvalidWhenBlank_ = flagAsInvalidWhenBlank;
    cb_ = new ComboBox<>();
    cb_.setConverter(new StringConverter<SimpleDisplayConcept>() {
        @Override
        public String toString(SimpleDisplayConcept object) {
            return object == null ? "" : object.getDescription();
        }

        @Override
        public SimpleDisplayConcept fromString(String string) {
            return new SimpleDisplayConcept(string, 0);
        }
    });
    cb_.setValue(new SimpleDisplayConcept("", 0));
    cb_.setEditable(true);
    cb_.setMaxWidth(Double.MAX_VALUE);
    cb_.setPrefWidth(ComboBox.USE_COMPUTED_SIZE);
    cb_.setMinWidth(200.0);
    cb_.setPromptText("Type, drop or select a concept");

    cb_.setItems(FXCollections.observableArrayList(dropDownOptions_));
    cb_.setVisibleRowCount(11);

    cm_ = new ContextMenu();

    MenuItem copyText = new MenuItem("Copy Description");
    copyText.setGraphic(Images.COPY.createImageView());
    copyText.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            CustomClipboard.set(cb_.getEditor().getText());
        }
    });
    cm_.getItems().add(copyText);

    CommonMenusNIdProvider nidProvider = new CommonMenusNIdProvider() {
        @Override
        public Set<Integer> getNIds() {
            Set<Integer> nids = new HashSet<>();
            if (c_ != null) {
                nids.add(c_.getNid());
            }
            return nids;
        }
    };
    CommonMenuBuilderI menuBuilder = CommonMenus.CommonMenuBuilder.newInstance();
    menuBuilder.setInvisibleWhenFalse(isValid);
    CommonMenus.addCommonMenus(cm_, menuBuilder, nidProvider);

    cb_.getEditor().setContextMenu(cm_);

    updateGUI();

    new LookAheadConceptPopup(cb_);

    if (cb_.getValue().getNid() == 0) {
        if (flagAsInvalidWhenBlank_) {
            isValid.setInvalid("Concept Required");
        }
    } else {
        isValid.setValid();
    }

    cb_.valueProperty().addListener(new ChangeListener<SimpleDisplayConcept>() {
        @Override
        public void changed(ObservableValue<? extends SimpleDisplayConcept> observable,
                SimpleDisplayConcept oldValue, SimpleDisplayConcept newValue) {
            if (newValue == null) {
                logger.debug("Combo Value Changed - null entry");
            } else {
                logger.debug("Combo Value Changed: {} {}", newValue.getDescription(), newValue.getNid());
            }

            if (disableChangeListener_) {
                logger.debug("change listener disabled");
                return;
            }

            if (newValue == null) {
                //This can happen if someone calls clearSelection() - it passes in a null.
                cb_.setValue(new SimpleDisplayConcept("", 0));
                return;
            } else {
                if (newValue.shouldIgnoreChange()) {
                    logger.debug("One time change ignore");
                    return;
                }
                //Whenever the focus leaves the combo box editor, a new combo box is generated.  But, the new box will have 0 for an id.  detect and ignore
                if (oldValue != null && oldValue.getDescription().equals(newValue.getDescription())
                        && newValue.getNid() == 0) {
                    logger.debug("Not a real change, ignore");
                    newValue.setNid(oldValue.getNid());
                    return;
                }
                lookup();
            }
        }
    });

    AppContext.getService(DragRegistry.class).setupDragAndDrop(cb_, new SingleConceptIdProvider() {
        @Override
        public String getConceptId() {
            return cb_.getValue().getNid() + "";
        }
    }, true);

    pi_ = new ProgressIndicator(ProgressIndicator.INDETERMINATE_PROGRESS);
    pi_.visibleProperty().bind(isLookupInProgress_);
    pi_.setPrefHeight(16.0);
    pi_.setPrefWidth(16.0);
    pi_.setMaxWidth(16.0);
    pi_.setMaxHeight(16.0);

    lookupFailImage_ = Images.EXCLAMATION.createImageView();
    lookupFailImage_.visibleProperty().bind(isValid.not().and(isLookupInProgress_.not()));
    Tooltip t = new Tooltip();
    t.textProperty().bind(isValid.getReasonWhyInvalid());
    Tooltip.install(lookupFailImage_, t);

    StackPane sp = new StackPane();
    sp.setMaxWidth(Double.MAX_VALUE);
    sp.getChildren().add(cb_);
    sp.getChildren().add(lookupFailImage_);
    sp.getChildren().add(pi_);
    StackPane.setAlignment(cb_, Pos.CENTER_LEFT);
    StackPane.setAlignment(lookupFailImage_, Pos.CENTER_RIGHT);
    StackPane.setMargin(lookupFailImage_, new Insets(0.0, 30.0, 0.0, 0.0));
    StackPane.setAlignment(pi_, Pos.CENTER_RIGHT);
    StackPane.setMargin(pi_, new Insets(0.0, 30.0, 0.0, 0.0));

    hbox_ = new HBox();
    hbox_.setSpacing(5.0);
    hbox_.setAlignment(Pos.CENTER_LEFT);

    hbox_.getChildren().add(sp);
    HBox.setHgrow(sp, Priority.SOMETIMES);
}

From source file:com.hengyi.japp.print.client.controller.MdController.java

@FXML
private void handleAutoXd() {
    try {/*from  w  ww  .ja v a2  s .c  o  m*/
        MdValidate.checkMdInput(md);
        md.xdsProperty().clear();
        md.setXds(FXCollections.observableArrayList(IntStream.range(0, md.getZcanmge()).mapToObj(i -> {
            Xd xd = new Xd();
            xd.zboxsnrProperty().set(i + 1);
            xd.sapYmmmachProperty().bind(md.sapYmmmachProperty());
            xd.zrolmgeProperty().bind(md.zrolmgeProperty());
            return xd;
        }).collect(Collectors.toList())));
        if (md.getZdzflg()) {
            zsgwghtColumn.setEditable(false);
            md.getXds().forEach(xd -> {
                xd.zsnwghtProperty().bind(md.zcnwghtProperty());
                xd.zsgwghtProperty().bind(new ObjectBinding<BigDecimal>() {
                    {
                        bind(xd.zsnwghtProperty(), xd.zrolmgeProperty(), md.sapYmmzhixProperty(),
                                md.sapYmmtonggProperty());
                    }

                    @Override
                    protected BigDecimal computeValue() {
                        return xd.getZsnwght().add(tareF.apply(md, xd));
                    }
                });
            });
        } else {
            zsgwghtColumn.setEditable(true);
            md.getXds().forEach(xd -> {
                xd.zsnwghtProperty().bind(new ObjectBinding<BigDecimal>() {
                    {
                        bind(xd.zsgwghtProperty(), xd.zrolmgeProperty(), md.sapYmmzhixProperty(),
                                md.sapYmmtonggProperty());
                    }

                    @Override
                    protected BigDecimal computeValue() {
                        return xd.getZsgwght().add(tareF.apply(md, xd).negate());
                    }
                });
            });
        }
        md.zsgwghtProperty().bind(new ObjectBinding<BigDecimal>() {
            {
                md.getXds().forEach(xd -> bind(xd.zsgwghtProperty()));
            }

            @Override
            protected BigDecimal computeValue() {
                BigDecimal result = BigDecimal.ZERO;
                for (Xd xd : md.getXds()) {
                    result = result.add(xd.getZsgwght());
                }
                return result;
            }
        });
        md.zsnwghtProperty().bind(new ObjectBinding<BigDecimal>() {
            {
                md.getXds().forEach(xd -> bind(xd.zsnwghtProperty()));
            }

            @Override
            protected BigDecimal computeValue() {
                BigDecimal result = BigDecimal.ZERO;
                for (Xd xd : md.getXds()) {
                    result = result.add(xd.getZsnwght());
                }
                return result;
            }
        });
    } catch (AppException e) {
        Util.alertDialog(e);
    }
}