VAADIN:为什么我不能将转换器设置为 ComboBox? [英] VAADIN: Why I can't set a converter to a ComboBox?

查看:24
本文介绍了VAADIN:为什么我不能将转换器设置为 ComboBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个转换器:

public class BooleanToDateConverter implements Converter<Boolean, Date> {
    private static final long serialVersionUID = 1L;

    @Override
    public Date convertToModel(Boolean value, Class<? extends Date> targetType, Locale locale)
            throws com.vaadin.data.util.converter.Converter.ConversionException {
        if (value == true) {
            return new Date();
        } else {
            return null;
        }
    }

    @Override
    public Boolean convertToPresentation(Date value, Class<? extends Boolean> targetType, Locale locale)
            throws com.vaadin.data.util.converter.Converter.ConversionException {
        if (value == null) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public Class<Date> getModelType() {
        return Date.class;
    }

    @Override
    public Class<Boolean> getPresentationType() {
        return Boolean.class;
    }
}

然后我有一个 Vaadin ComboBox myComboBox我尝试将我的转换器设置为它:

Then I have a Vaadin ComboBox myComboBox I try to set my converter to it:

myComboBox.setConverter(new BooleanToDateConverter());

然后我在 Eclipse 中收到一条错误消息:

Then I get an error in Eclipse saying:

The method setConverter(Class<?>) in the type AbstractField<Object> is not applicable for the arguments (BooleanToDateConverter)

但是,我已经看到其他转换器的使用方式类似,并且没有出现错误.为什么?

However, I've seen other converters being used similarly and they don't get errors. Why?

推荐答案

您的代码无法编译,因为在类 ComboBox 上没有适合的 setConverter() 方法您的自定义转换器.让我解释一下如何在选择的组件上使用转换器,以及您找到的用于在 ComboBox 上设置转换器的特定方法签名背后的想法是什么.

Your code cannot be compiled because there is no setConverter() method available on class ComboBox that fits your custom converter. Let me explain how converters are used on select components and what is the idea behind the specific method signatures you find for setting converters on a ComboBox.

ComboBox 提供了 setConverter() 的两个重载版本:

ComboBox provides two overloaded versions of setConverter():

  • setConverter(Class datamodelType):为给定的数据模型类型设置一个预先注册的转换器
  • setConverter(Converter converter):设置一个具体的转换器实例
  • setConverter(Class<?> datamodelType): set a pre-registered converter for the given data model type
  • setConverter(Converter<Object, ?> converter): set a concrete converter instance

这两个方法实际上都继承自 AbstractField 类,其中 T 是字段管理的数据类型(例如文本字段的字符串,DateField 的 Date,ComboBoxesObject).转换器通常用于在表示类型(例如 UI 上值的文本表示)与其内部模型类型(例如日期、货币值或自定义 JavaBean)之间进行转换.因此,例如,如果您有一个 Label,您可以使用 StringToDateConverter 来正确显示一个 Date 对象,该对象已被设置为值Label,以正确本地化的方式.

Both of these methods are actually inherited from class AbstractField<T> where T is the data type managed by the field (e.g. Strings for text fields, Date for a DateField, Object for ComboBoxes). A converter is typically used to convert between a presentation type (such as the textual representation of a value on the UI) and its internal model type (such as a date, a monetary value or a custom JavaBean). So, for instance, if you have a Label you can use a StringToDateConverter to correctly display a Date object, which has been set as the value of the Label, in a properly localized way.

ComboBox 等选择组件如何?这里的类型TObject.选择组件的数据类型实际上表示来自底层容器数据源的所选项目的项目 ID.因此,如果您使用 BeanItemContainer 作为 ComboBox 的数据源,则容器的项目 ID(以及ComboBox 的选定值)是包含的 JavaBean 对象本身.项目 ID 的具体类型取决于所使用的容器实现.因此,选择组件是具有值类型ObjectField 组件.换句话说,选择组件使用 Object 作为表示类型.

How is that with select components such as ComboBox? Here the type T is Object. The data type of a select component actually represents the item ID of the selected item from the underlying container data source. So, if you use a BeanItemContainer as the data source of a ComboBox, the container's item IDs (and hence the selected value of the ComboBox) are the contained JavaBean objects themselves. The concrete type of the item IDs depends on the container implementation used. Therefore, select components are Field components with value type Object. In other words, select components use Object as presentation type.

这就是为什么您只能在通用PRESENTATION 类型为Object 的选择组件上设置转换器实例.模型类型可以自由选择.这也解释了为什么不能在 ComboBox -- ComboBox 上设置具有表示类型 Boolean 和模型类型 Date 的转换器 不使用 Boolean 作为表示类型.

That is why you can only set a converter instance on a select component whose generic PRESENTATION type is Object. The model type can be chosen freely. And this also explain why you can't set a converter with presentation type Boolean and model type Date on a ComboBox -- ComboBox doesn't use Boolean as presentation type.

我写了一篇关于 Vaadin FieldGroups 的博客文章,它也提供了一个很好的例子来说明何时在 Converter 上使用 Converter>组合框.您可以在 http://blog 上找到这篇文章.oio.de/2014/04/25/select-nested-javabeans-vaadin-fieldgroup/.

I wrote a blog post about Vaadin FieldGroups which also provides a good example for a use case when to use a Converter<Object, ?> on a ComboBox. You can find this article at http://blog.oio.de/2014/04/25/select-nested-javabeans-vaadin-fieldgroup/.

我不知道你想用你的代码实现什么,因为 Boolean 的表示类型和 Date 的模型类型之间的转换器不会使很有意义.我只能猜测您想实现某种决策逻辑,也许是决定是否已设置日期?在这种情况下,您需要采取不同的方法.

I don't know what you want to achieve with your code, because a converter between a presentation type of Boolean and a model type of Date doesn't make much sense. I can only guess that you want to implementat some sort of decision logic, maybe to decide whether or not a date has been set? In that case you need to take a different approach.

作为参考,请查看Vaadin 关于转换器的书.

这篇关于VAADIN:为什么我不能将转换器设置为 ComboBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆