西门子 Teamcenter13 Eclipse RCP 开发 1.1 工具栏 普通按钮
- [1 配置文件](#1 配置文件)
- [2 插件控制](#2 插件控制)
- [3 命令框架](#3 命令框架)
位置 | locationURI | 备注 |
---|---|---|
菜单栏 | menu:org.eclipse.ui.main.menu | 添加到传统菜单 |
工具栏 | toolbar:org.eclipse.ui.main.toolbar | 添加到工具栏 |
style 值 | 含义 | 显示效果 |
---|---|---|
push | 普通按钮(默认) | 普通的点击按钮,点一下执行一次 |
toggle | 切换按钮 | 有按下/弹起两种状态,比如"开关" |
radio | 单选按钮 | 多个按钮互斥选择,比如 "模式切换" |
1 配置文件
在 Teamcenter 13 + Eclipse RCP 开发中,plugin.xml 是插件的核心配置文件,定义了插件的:
1、唯一身份(ID、版本)。
2、所依赖的其他插件。
3、注册的扩展点(Extensions)。
4、提供的功能,比如菜单项、视图、编辑器、命令、图标、语言包等。
内容 | plugin.xml 的角色 |
---|---|
插件身份 | 定义 id 、版本、启动类 |
插件依赖 | 在 MANIFEST.MF 中定义(与 plugin.xml 配合) |
扩展注册 | 注册视图、菜单、命令、动作等 |
Teamcenter 支持 | 使用 Teamcenter 自定义扩展点,实现插件集成 |
xml
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
<extension point="org.eclipse.ui.menus">
<menuContribution locationURI="toolbar:org.eclipse.ui.main.toolbar">
<toolbar id="com.example.toolbar">
<command commandId="com.example.commands.helloCommand" icon="icons/sample.png" tooltip="普通按钮" label="普通按钮" style="push">
</command>
</toolbar>
</menuContribution>
</extension>
<extension point="org.eclipse.ui.handlers">
<handler class="com.xu.work.tool1.handlers.SampleHandler" commandId="com.example.commands.helloCommand">
</handler>
</extension>
</plugin>
2 插件控制
在使用 Siemens Teamcenter 13 进行 Eclipse RCP 插件开发 时,Activator.java 是插件生命周期管理的关键类。这个类通常由 Eclipse PDE 插件开发环境自动生成,并实现了 org.osgi.framework.BundleActivator 接口或继承了 AbstractUIPlugin(用户界面) / Plugin(后台应用)。
序号 | 作用 |
---|---|
1 | 控制插件的生命周期:包括启动(start())和停止(stop())。 |
2 | 提供插件范围的共享资源访问:例如共享的图标、配置文件、日志工具等。 |
3 | 保存插件实例(单例):方便在其他地方访问插件上下文。 |
java
package com.xu.work.tool1;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;
/**
* 插件激活器类,控制插件的生命周期
*/
public class Activator extends AbstractUIPlugin {
/**
* 插件ID常量,通常与MANIFEST.MF中的Bundle-SymbolicName一致
*/
public static final String PLUGIN_ID = "com.xu.work.tool1"; //$NON-NLS-1$
/**
* 单例实例引用
*/
private static Activator plugin;
/**
* 构造函数
*/
public Activator() {
}
/**
* 插件启动时调用
*/
@Override
public void start(BundleContext context) throws Exception {
super.start(context);
plugin = this;
// 在这里注册监听器、服务、加载配置等
}
/**
* 插件停止时调用
*/
@Override
public void stop(BundleContext context) throws Exception {
plugin = null;
super.stop(context);
}
/**
* 返回此插件的共享实例
*
* @return 共享实例
*/
public static Activator getDefault() {
return plugin;
}
}
3 命令框架
它是命令框架(Command Framework)的基础,用来处理 UI 中的命令(Command)。
当用户点击菜单或按钮,Eclipse 会根据 plugin.xml 中绑定的命令,调用对应的 Handler 类的 execute() 方法。
java
package com.xu.work.tool1.handlers;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.jface.dialogs.MessageDialog;
public class SampleHandler extends AbstractHandler {
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
MessageDialog.openInformation(
window.getShell(),
"Tool1",
"Hello, Eclipse world");
return null;
}
}
