西门子 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 {
public static final String PLUGIN_ID = "com.xu.work.tool2";
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);
}
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;
}
}
