基本自定义 > 用户界面自定义 > 在 UI 中呈现信息 > 创建自定义图形表示 > 过程 - 创建简单的自定义图形表示
过程 - 创建简单的自定义图形表示
要说明如何创建新的自定义图形表示,请创建新的“多状态图标组件”
预置“红绿灯”图形组件 (在参考中进行讨论) 为多状态图标组件的示例。您可以很容易创建遵循此模型的其他自定义图形组件。例如,客户可以创建具有更多状态的红绿灯类型组件,也可以创建新的“雨天或晴天”图形组件。接下来的部分将说明如何创建新的“雨天或晴天”组件。
创建“雨天或晴天”自定义图形表示类
您的自定义组件类应扩展所提供的 MultiStateIconComponent 类,并定义 getIconComponent() 的实现。通常,此类还将定义枚举,其中包含允许的值和要呈现的图像。
您的“雨天或晴天”组件可能如下所示:
public class RainOrShineComponent extends MultiStateIconComponent {
public enum RainOrShineEnum {
// Maps an integer to a specific .gif graphical representation
// All possible states supported are listed here
RAIN(0, "com/mycompany/images/rain.gif", "Rain"),
SHINE(1, "com/mycompany/images/sunshine.gif", "Shine");
private final int key;
private final String image;
private final String tooltip;
private RainOrShineEnum(int key, String image, String tooltip) {
this.key = key;
this.image = image;
this.tooltip = tooltip;
}
@Override
public IconComponent getIconComponent() {
// create the icon component using the correct image path
final IconComponent component = new IconComponent(image);
// set the tooltip
component.setTooltip(tooltip);
return component;
}
@Override
public static RainOrShineEnum getElement(int key) throws WTException {
for (RainOrShineEnum tls : values()) {
if (key == tls.key) {
return tls;
}
}
LOGGER.error("Undefined RainOrShineState: " + key);
LOGGER.error("Change the formula to return one of the
following states: 0, 1");
throw new WTException("Undefined RainOrShineState: " + key);
}
}
@Override
public IconComponent getIconComponent(int key) throws WTException {
return RainOrShineEnum.getElement(key).getIconComponent();
}
}
将条目添加到图形表示的枚举中
使用“类型和属性管理”实用程序将条目添加到图形表示的枚举中。枚举名为“图形属性表示”,位于名为“PTC 枚举”的系统级组织器中。记住条目的内部名称。该名称将在下一步中使用。在本示例中,假定将 "rainOrShine" 作为新条目的内部名称。
为组件创建 xconf 映射
添加 xconf 条目,用于将图形表示枚举条目的内部名称映射到前面创建的自定义 "RainOrShineComponent" 类。对于雨天或晴天组件,新条目可能如下所示。请注意,选择器必须与上一节中枚举条目的内部名称匹配,并且 serviceClass 必须与自定义组件类的完全限定名称匹配。
<Service name=
"com.ptc.core.lwc.common.graphicalatts.GraphicalAttRepresentationHandler">
<Option serviceClass="com.mycompany.RainOrShineComponent" requestor="null"
selector="rainOrShine" />
</Service>
更新 xconf 文件后,需要将其传播到特性文件,方法为在 Windchill shell 中运行 XConfManager ("xconfmanager -pF"),然后重新启动方法服务器。
测试
1. 首先在“类型和属性管理”实用程序中创建新的计算属性 (可能针对文档或部件类型)。
2. 然后,对于此属性,将“图形表示”特性设置为“雨天或晴天”,并将公式设置为 0 或 1。
3. 保存更改。
4. Windchill 中导航到此类型的实例 (或创建新实例)。
只要此属性在 Windchill 中以“视图”模式显示,即会显示为 "rain.gif" 或 "sunshine.gif" 图像!
这对您有帮助吗?