简体   繁体   English

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

[英]Enum constant display name from properties file

I have an enum datatype in my EMF model. 我的EMF模型中有一个枚举数据类型。

This is what I want: 这就是我要的:

  1. I'd like to have nice display names for the enum constants. 我想为枚举常量提供漂亮的显示名称。
  2. The display names should be set in a plugin.properties file. 应在plugin.properties文件中设置显示名称。
  3. The mechanism for must integrate well with the EMF system for getting values from properties, so that I can treat all values, enum or not, in a uniform way. 机制必须与EMF系统很好地集成,以便从属性中获取值,这样我才能以统一的方式处理所有值,枚举与否。 That probably means that the solution must use IItemPropertyDescriptor somehow. 这可能意味着解决方案必须以某种方式使用IItemPropertyDescriptor

I can see that enum constants get generated entries in my plugin.properties in my EMF Edit project. 我可以看到枚举常量在我的EMF编辑项目中的plugin.properties中生成了条目。 So there should be some way to get at those names. 所以应该有一些方法来获得这些名称。 But I can't figure out how. 但我无法弄清楚如何。

I can set the display names in my Xcore model file, but that is not what I want. 我可以在我的Xcore模型文件中设置显示名称,但这不是我想要的。 I want them to be read from my plugin.properties file. 我希望从我的plugin.properties文件中读取它们。

It is simple to manually get the enum display names from the properties file. 手动从属性文件中获取枚举显示名称很简单。 But there should be some way to make EMF handle this. 但应该有一些方法让EMF处理这个问题。 It seems weird that I should have to write special code to handle enum values each time I get a value from the model. 每次从模型中获取值时,我都必须编写特殊代码来处理枚举值,这似乎很奇怪。

Here is an example: 这是一个例子:

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;
    }

}

And this can be the constans.properties file: 这可以是constans.properties文件:

#This is property file...
PROP1=some text
PROP2=some other text

I have managed to get the enum display values using EMF. 我已设法使用EMF获取枚举显示值。 But with a sever limitation, so I don't consider this to be solved. 但由于受到严重限制,所以我不认为这个问题有待解决。

The solution works by calling ItemPropertyDescriptor.getLabelProvider . 该解决方案通过调用ItemPropertyDescriptor.getLabelProvider The returned label provider is an ItemDelegator , which is the only class I have found that has code for reading enum literals from property files. 返回的标签提供程序是一个ItemDelegator ,它是我发现的唯一一个具有从属性文件中读取枚举文字的代码的类。 So it's just to call getText on that label provider. 所以只是在该标签提供者上调用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);

The limitation is that ItemDelegator "crops" the returned text, by replacing all text after a newline with ... . 该限制是ItemDelegator “作物”返回的文字,用一个换行符替换后的所有文字...

Unfortunately I have to handle multi-line strings in some places in my application, so I still can't handle all features in a uniform way with this solution. 不幸的是,我必须在我的应用程序的某些地方处理多行字符串,所以我仍然无法使用此解决方案以统一的方式处理所有功能。 So I would really like to find a better one. 所以我真的想找到一个更好的。

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

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