Java Eclipse:尝试转换cmbo“;“显示”;对“的价值”;储存;价值
我正在HeliosSR2中构建插件。支持伽利略会很好,但我现在并不过分担心。Windows 7:64位、64位JVM(1.6_21 IIRC)Java Eclipse:尝试转换cmbo“;“显示”;对“的价值”;储存;价值,java,data-binding,eclipse-plugin,swt,Java,Data Binding,Eclipse Plugin,Swt,我正在HeliosSR2中构建插件。支持伽利略会很好,但我现在并不过分担心。Windows 7:64位、64位JVM(1.6_21 IIRC) 我有一个swt.custom.CCombo框,其中包含条目列表的显示值。我有一张“显示值”->“存储值”的地图 我要存储存储值,并显示显示值。算了吧 我将数据绑定与BeanObservables、IObservables、数据绑定上下文等一起使用 我目前的方法(不起作用或者我不会要求)是使用自定义的IConverter创建我自己的UpdateValue
我有一个
swt.custom.CCombo
框,其中包含条目列表的显示值。我有一张“显示值”->“存储值”的地图
我要存储存储值,并显示显示值。算了吧
我将数据绑定与BeanObservables、IObservables、数据绑定上下文等一起使用
我目前的方法(不起作用或者我不会要求)是使用自定义的IConverter
创建我自己的UpdateValueStrategy
,它将在两者之间进行内部映射
我目前正在尝试扩展org.eclipse.core.databinding.conversion.Converter
,因为IConverter被标记为@noimplement
和@noextend
。@noimplement
声明客户端应该扩展Converter
,而不是直接实现IConverter(即使Converter非常简单)
不幸的是,Eclipse的Java编译器告诉我这是一个禁忌:
访问限制:由于对所需库{install}\plugins\org.eclipse.core.databinding_1.3.100.I20100601-0800.jar的限制,无法访问类型转换器
在相关的“.api_说明”中,我看到了以下XML:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<component name="org.eclipse.core.databinding_1.3.100.I20100601-0800" version="1.2">
<plugin id="org.eclipse.core.databinding_1.3.100.I20100601-0800"/>
<package name="org.eclipse.core.databinding" visibility="1">
<type name="ObservablesManager" restrictions="2"/>
<type name="UpdateListStrategy" restrictions="0">
<method name="useMoveAndReplace" restrictions="8" signature="()Z"/>
</type>
</package>
<package name="org.eclipse.core.databinding.conversion" visibility="1">
<type name="IConverter" restrictions="3"/>
</package>
</component>
多亏了谷歌代码,我发现visibility=“1”
意味着一个公共API。好的,很好
restrictions=“3”
另一方面,表示@noextend@noimplement。听起来熟悉吗?好的,很好
但似乎对IConverter的限制正在影响转换器,使其无法使用。我不能扩展转换器,也不能从构造函数调用super(废话,废话)
。有趣的是,编译器并没有抱怨我实现了公共对象转换(Object fromObject)
,即使转换器没有自己的转换,所以人们可能会认为IConverter
限制适用
显然不是
有人有吗
- 完全不同的方法李>
- 有些方法可以打破这种访问限制,让我屈服,最好是以一种本地的方式,这样我就不会在其他地方做傻事了
说到代码。以下是我正在做的(或多或少):
enum MapValueDirection{
值到键,
键到值
};
私有类映射转换器扩展转换器{
Map=null;
公共映射转换器(映射映射,映射值方向目录){
super(String.class,String.class);
if(dir==MapValueDirection.VALUE\u到\u键){
映射=反向映射(映射);
}否则{
map=map;
}
}
私有映射反向映射(映射映射){
Map reversedMap=newtreemap();
Set entries=map_u.entrySet();
对于(条目当前:条目){
reversedMap.put(curEnt.getValue(),curEnt.getKey());
}
返回反向映射;
}
/*(非Javadoc)
*@see org.eclipse.core.databinding.conversion.IConverter#convert(java.lang.Object)
*/
公共对象转换(对象来自对象){
if(map!=null&&fromObject!=null&&String.class.equals(fromObject.getClass())){
Object newVal=map.get(fromObject);
if(newVal==null){
newVal=fromObject;
}
返回newVal;
}
从对象返回;
}
}
/**
*这就是实际工作的完成情况。
*/
公共void bindBean(对象bean、PropertyDescriptor prop){
Control curControl=getControl(prop.getPropertyType());
IObservableValue uiElement=getObserver(prop,curControl);
IOvservableValue modelElement=BeanObservables.observValue(bean,prop.getName());
//“显示”=键,“存储”=值
Map profileFlds=getProfileFields();
UpdateValueStrategy toStorage=新的UpdateValueStrategy();
toStorage.setConverter(新的映射转换器(profileFlds,MapValueDirection.KEY_到_值));
UpdateValueStrategy toDisplay=新的UpdateValueStrategy();
toDisplay.setConverter(新的映射转换器(profileFlds,MapValueDirection.VALUE_到_键));
bindingContext.bindValue(uiElement、modelElement、toDisplay、toStorage);
}
我的实际代码比这要复杂一点,但你明白了。我怀疑它的效率不是很高,但从概念上讲(基于我公认的有限经验),它在整个数据绑定框架中工作得很好
制作MappingConverter的通用版本应该相当简单,但我将把它作为一个例子
enum MapValueDirection {
VALUE_TO_KEY,
KEY_TO_VALUE
};
private class MappingConverter extends Converter {
Map<String, String> map = null;
public MappingConverter( Map<String, String> map_, MapValueDirection dir) {
super(String.class, String.class);
if (dir == MapValueDirection.VALUE_TO_KEY) {
map = reverseMap(map_);
} else {
map = map_;
}
}
private Map<String, String> reverseMap(Map<String, String> map_) {
Map<String, String> reversedMap = new TreeMap<String, String>();
Set<Entry<String, String>> entries = map_.entrySet();
for (Entry<String, String> curEnt : entries) {
reversedMap.put(curEnt.getValue(), curEnt.getKey());
}
return reversedMap;
}
/* (non-Javadoc)
* @see org.eclipse.core.databinding.conversion.IConverter#convert(java.lang.Object)
*/
public Object convert(Object fromObject) {
if (map != null && fromObject != null && String.class.equals(fromObject.getClass())) {
Object newVal = map.get(fromObject);
if (newVal == null) {
newVal = fromObject;
}
return newVal;
}
return fromObject;
}
}
/**
* And this is were the actual work gets done.
*/
public void bindBean(Object bean, PropertyDescriptor prop) {
Control curControl = getControl(prop.getPropertyType());
IObservableValue uiElement = getObserver(prop, curControl);
IOvservableValue modelElement = BeanObservables.observValue(bean, prop.getName());
// "display" = key, "storage" = value
Map<String, String> profileFlds = getProfileFields();
UpdateValueStrategy toStorage = new UpdateValueStrategy();
toStorage.setConverter( new MappingConverter( profileFlds, MapValueDirection.KEY_TO_VALUE));
UpdateValueStrategy toDisplay = new UpdateValueStrategy();
toDisplay .setConverter( new MappingConverter( profileFlds, MapValueDirection.VALUE_TO_KEY));
m_bindingContext.bindValue( uiElement, modelElement, toDisplay , toStorage);
}