西門子 Teamcenter13 Eclipse RCP 開發 1.1 工具欄 普通按鈕
- 1 配置文件
- 2 插件控制
- 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 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 | 保存插件實例(單例):方便在其他地方訪問插件上下文。 |
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() {}/*** 插件啟動時調用*/@Overridepublic void start(BundleContext context) throws Exception {super.start(context);plugin = this;// 在這里注冊監聽器、服務、加載配置等}/*** 插件停止時調用*/@Overridepublic 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() 方法。
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 {@Overridepublic Object execute(ExecutionEvent event) throws ExecutionException {IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);MessageDialog.openInformation(window.getShell(),"Tool1","Hello, Eclipse world");return null;}}