大部分来自忘记了出处的转载。
1、 扩展org.eclipse.ui.decorators扩展点
清单 1. Decorators 扩展点 XML 定义
<! ELEMENT decorator (description? , enablement?)> <! ATTLIST decorator id CDATA #REQUIRED label CDATA #REQUIRED class CDATA #IMPLIED objectClass CDATA #IMPLIED adaptable (true | false) state (true | false) lightweight (true | false) icon CDATA #IMPLIED location (TOP_LEFT|TOP_RIGHT|BOTTOM_LEFT|BOTTOM_RIGHT|UNDERLAY|REPLACE)> |
对于扩展点中一些比较重要的属性我们需要先了解一下:
label 和 description 属性用来定义 Decoration 实现在配置页面中的标签和描述,用户可以通过这两个属性的定义很方便的在配置页面中找到定义的 Decoration。
state 属性用来描述默认情况下 Decoration 是否开启 , 用户可以在配置页面中手动开启或关闭某一个 Decoration 的定义。
lightweight 属性用来描述当前 Decoration 是否是轻量级。对于轻量级的 Decoration, 用户可以不定义 Decoration 的 class 实现类属性,取而代之可以使用 icon 属性定义装饰用的小图标和用 location 属性定义图标被装饰的位置。如果需要对轻量级的 Decoration 设置实现类,可以使用class 属性并且对应的实现类应该继承自org.Eclipse.jface.viewers.ILightweightLabelDecorator。对于重量级的 Decoration,必须使用 class 属性定义 Decoration 的实现类并且实现类应该继承自org.Eclipse.jface.viewers.ILabelDecorator。
objectClass 属性用来定义被装饰的对象类型,结合 adaptable 属性使用用来定义 objectClass 属性所定义的对象类型是否接受被适配过来的形式。
2、 添加decorator,并增加一个Decorator类
ILightweightLabelDecorator接口提供了轻量级的Decorator
3、 实现ILightweightLabelDecorator接口
public void decorate(Object element, IDecoration decoration) {
boolean containsError = false;
if (element instanceof IContainer) {
containsError = verifyContainer((IContainer) element);
} else if (element instanceof IFile) {
containsError = verifyFile((IFile) element);
}
if (containsError) {
decoration.addSuffix(" [Error]");
decoration.addOverlay(
Activator.getImageDescriptor("/icons/error.gif"),
IDecoration.BOTTOM_RIGHT);
}
4、 }