简体   繁体   English

JFace数据绑定MultiValidator不重新评估

[英]JFace databinding MultiValidator not reevaluating

I have set up a MultiValidator to check if the sum of 2 text fields equals the value of another field. 我已经设置了一个MultiValidator来检查2个文本字段的总和是否等于另一个字段的值。 Yet, for some reason, the validator is only validated once, upon initialization. 然而,由于某种原因,验证器在初始化时仅被验证一次。 Upon initialization, it always turns out successful as it should. 初始化后,它总是总是成功。 The fail condition is only triggered at a later point within the wizard (condition to fail on 1st wizard page and validator on 3rd). 失败条件仅在向导中的稍后点触发(失败的条件在第一个向导页面上,验证器在第3个页面上)。

Following is the code for the databindings themselves as well as the MultiValidator . 以下是数据绑定本身以及MultiValidator的代码。 The dbFactory is a convenience class to set up the bindings themselves and that part works just fine. dbFactory是一个便利类,用于自己设置绑定,该部分可以正常工作。

fixedFeeAmountBinding = dbFactory.addBinding(fixedFeeAmountTxt, "fixedAmount", null,
        new UpdateValueStrategy().setConverter(NumberToStringConverter
                .fromBigDecimal(new com.ibm.icu.text.DecimalFormat(Formats.AMOUNT_FORMAT))));
variableFeeAmountBinding = dbFactory.addBinding(variableFeeAmountTxt, "variableAmount", null,
        new UpdateValueStrategy().setConverter(NumberToStringConverter
                .fromBigDecimal(new com.ibm.icu.text.DecimalFormat(Formats.AMOUNT_FORMAT))));

MultiValidator feeAmountValidaotr = new MultiValidator() {

    @Override
    protected IStatus validate() {
        if (model.getVal().getClientChequePaymentRecordType().equals(ClientChequePaymentRecordType.INVOICE)
                && model.getVal().getInvoiceType() != null
                && !model.getVal().getInvoiceType().equals(InvoiceType.OTHER)) {
            BigDecimal fixedAmountValue = new BigDecimal(
                    String.valueOf(((IObservableValue) fixedFeeAmountBinding.getTarget()).getValue())
                            .replaceAll(",", ""));
            BigDecimal variableAmountValue = new BigDecimal(
                    String.valueOf(((IObservableValue) variableFeeAmountBinding.getTarget()).getValue())
                            .replaceAll(",", ""));
            if (fixedAmountValue.add(variableAmountValue).compareTo(model.getVal().getInvoiceAmount()) == 0) {
                return ValidationStatus.OK_STATUS;
            }
            return ValidationStatus.error("Fee Amounts don't add up to Invoice Amount");
        }
        return ValidationStatus.OK_STATUS; // <-- Doesn't update after returning this
    }
};

dbFactory.addValidationStatusProvider(feeAmountValidaotr);

I also used to tip from my other question , but that didn't solve my issue this time. 我也经常从其他问题中提出建议 ,但这一次并没有解决我的问题。

What also makes this issue harder for me to understand is the fact, that the exact same code is working at another place in my code just fine. 使这个问题让我更难理解的是事实,就是完全相同的代码可以在我代码的另一个地方正常工作。

Why is the MultiValidator not updating when the binding updates? 为什么在绑定更新时MultiValidator没有更新? and How can I fix it? 以及如何解决?


If you need any more infos, don't hesitate to ask. 如果您需要更多信息,请随时询问。

EDIT: 编辑:

After some more testing, I found that ValidationStatusProvider status does not seem to be updated with the new result of validate() . 经过更多测试后,我发现ValidationStatusProvider状态似乎未使用validate()的新结果进行更新。 Also I just found out that this code does not work properly in the other place, though the impact there is not that huge, as users can't change values to fail/succeed the first IF. 我也刚刚发现该代码在其他地方不能正常工作,尽管影响并不大,因为用户无法更改值来使第一个IF失败/成功。

So basically the issue with the validator is that its not updating as soon as it returned the ValidationStatus.OK_STATUS outside of all IFs. 因此,基本上,验证器的问题在于,它在所有IF之外返回ValidationStatus.OK_STATUS不会立即更新。

It seems I found the solution to my issue... I replaced the model.getVal().getInvoiceAmount() call with the binding value for the amount field and then moved the creation of the BigDecimals in before the IF . 看来我找到了解决问题的方法...我用对value字段的绑定值替换了model.getVal().getInvoiceAmount()调用,然后将创建的BigDecimals移到了IF之前。 The whole code looks like this now: 整个代码现在看起来像这样:

MultiValidator feeAmountValidaotr = new MultiValidator() {

    @Override
    protected IStatus validate() {
        // Only validate Fee Amounts if cheque can have fees
        BigDecimal fixedAmountValue = new BigDecimal(
                String.valueOf(((IObservableValue) fixedFeeAmountBinding.getTarget()).getValue())
                        .replaceAll(",", ""));
        BigDecimal variableAmountValue = new BigDecimal(
                String.valueOf(((IObservableValue) variableFeeAmountBinding.getTarget()).getValue())
                        .replaceAll(",", ""));
        BigDecimal totalAmountValue = new BigDecimal(
                String.valueOf(((IObservableValue) amountBinding.getTarget()).getValue()).replaceAll(",", ""));
        if (getModelObject().getClientChequePaymentRecordType().equals(ClientChequePaymentRecordType.INVOICE)
                && getModelObject().getInvoiceType() != null
                && !getModelObject().getInvoiceType().equals(InvoiceType.OTHER)) {
            if (fixedAmountValue.add(variableAmountValue).compareTo(totalAmountValue) == 0) {
                return ValidationStatus.OK_STATUS;
            }
            return ValidationStatus.error("Fee Amounts don't add up to Invoice Amount");
        }
        return ValidationStatus.OK_STATUS;
    }
};

dbf.addValidationStatusProvider(feeAmountValidaotr);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM