西门子 Teamcenter13 Eclipse RCP 开发 1.2 工具栏 开关按钮

西门子 Teamcenter13 Eclipse RCP 开发 1.2 工具栏 开关按钮

位置locationURI备注
菜单栏menu:org.eclipse.ui.main.menu添加到传统菜单
工具栏toolbar:org.eclipse.ui.main.toolbar添加到工具栏
style 值含义显示效果
push普通按钮(默认)普通的点击按钮,点一下执行一次
toggle切换按钮有按下/弹起两种状态,比如"开关"
radio单选按钮多个按钮互斥选择,比如 “模式切换”

1 配置文件

<?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="toggle">
                </command>
            </toolbar>
        </menuContribution>
    </extension>

    <extension point="org.eclipse.ui.handlers">
        <handler class="com.xu.work.tool2.handlers.SampleHandler" commandId="com.example.commands.helloCommand">
        </handler>
    </extension>

    <extension point="org.eclipse.ui.commands">
        <command id="com.example.commands.toggleCommand" name="开关按钮"/>
    </extension>

</plugin>

2 插件控制

package com.xu.work.tool2;

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.tool2"; //$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 命令框架

package com.xu.work.tool2.handlers;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.Command;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.commands.ToggleState;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;

public class SampleHandler extends AbstractHandler {

	private static final String STATS = "org.eclipse.ui.commands.toggleState";

	private static IPreferenceStore preferenceStore;

	public static void setPreferenceStore(IPreferenceStore store) {
		preferenceStore = store;
	}

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		Command command = event.getCommand();

		// 获取状态
		ToggleState state = (ToggleState) command.getState(STATS);
		if (state == null) {
			state = new ToggleState();
			command.addState(STATS, state);
		}

		// 打印状态
		boolean currentState = HandlerUtil.toggleCommandState(command);
		IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
		MessageDialog.openInformation(window.getShell(), "切换按钮", "切换按钮的状态是" + currentState);
		
		// 保存状态
		if (preferenceStore != null) {
			preferenceStore.setValue(STATS, currentState);
		}

		return null;
	}

}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值