我的EMF模型中有一个枚举数据类型。
这就是我要的:
plugin.properties
文件中设置显示名称。IItemPropertyDescriptor
。我可以看到枚举常量在我的EMF编辑项目中的plugin.properties
中生成了条目。所以应该有一些方法来获得这些名称。但我无法弄清楚如何。
我可以在我的Xcore模型文件中设置显示名称,但这不是我想要的。我想要从我的plugin.properties
文件中读取它们。
手动从属性文件中获取枚举显示名称很简单。但应该有一些方法让EMF处理这个问题。每次从模型中获取值时,我都必须编写特殊代码来处理枚举值,这似乎很奇怪。
这是一个例子:
public enum Constants {
PROP1,
PROP2;
private static final String PATH = "/constants.properties";
private static final Logger logger = LoggerFactory.getLogger(Constants.class);
private static Properties properties;
private String value;
private void init() {
if (properties == null) {
properties = new Properties();
try {
properties.load(Constants.class.getResourceAsStream(PATH));
}
catch (Exception e) {
logger.error("Unable to load " + PATH + " file from classpath.", e);
System.exit(1);
}
}
value = (String) properties.get(this.toString());
}
public String getValue() {
if (value == null) {
init();
}
return value;
}
}
这可以是constans.properties
文件:
#This is property file...
PROP1=some text
PROP2=some other text
我已设法使用EMF获取枚举显示值。但由于受到严重限制,所以我不认为这个问题有待解决。
该解决方案通过调用ItemPropertyDescriptor.getLabelProvider
来工作。返回的标签提供程序是一个ItemDelegator
,它是我发现的唯一一个具有从属性文件中读取枚举文字的代码的类。因此,只需在该标签提供商处调用getText
即可。
IItemPropertySource adapter = (IItemPropertySource) adapterFactory.adapt(eObject, IItemPropertySource.class);
IItemPropertyDescriptor descriptor = adapter.getPropertyDescriptor(eObject, feature);
Object propertyValue = descriptor.getPropertyValue(eObject);
Object value = ((IItemPropertySource) propertyValue).getEditableValue(null);
String text descriptor.getLabelProvider(value).getText(value);
限制是ItemDelegator
通过用...
替换换行符之后的所有文本来“裁剪”返回的文本。
不幸的是,我必须在我的应用程序的某些地方处理多行字符串,所以我仍然无法使用此解决方案以统一的方式处理所有功能。所以我真的想找到一个更好的。