枚举属性文件中的常量显示名称

问题描述 投票:1回答:2

我的EMF模型中有一个枚举数据类型。

这就是我要的:

  1. 我想为枚举常量提供漂亮的显示名称。
  2. 应在plugin.properties文件中设置显示名称。
  3. 机制必须与EMF系统很好地集成,以便从属性中获取值,这样我才能以统一的方式处理所有值,枚举与否。这可能意味着解决方案必须以某种方式使用IItemPropertyDescriptor

我可以看到枚举常量在我的EMF编辑项目中的plugin.properties中生成了条目。所以应该有一些方法来获得这些名称。但我无法弄清楚如何。

我可以在我的Xcore模型文件中设置显示名称,但这不是我想要的。我想要从我的plugin.properties文件中读取它们。

手动从属性文件中获取枚举显示名称很简单。但应该有一些方法让EMF处理这个问题。每次从模型中获取值时,我都必须编写特殊代码来处理枚举值,这似乎很奇怪。

java enums eclipse-emf emf
2个回答
0
投票

这是一个例子:

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

0
投票

我已设法使用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通过用...替换换行符之后的所有文本来“裁剪”返回的文本。

不幸的是,我必须在我的应用程序的某些地方处理多行字符串,所以我仍然无法使用此解决方案以统一的方式处理所有功能。所以我真的想找到一个更好的。

© www.soinside.com 2019 - 2024. All rights reserved.