Eclipse 插件开发 6 右键菜单

Eclipse 插件开发 6 右键菜单

  • [1 plugin.xml](#1 plugin.xml)
  • [2 SampleHandler.java](#2 SampleHandler.java)
  • [3 Activator.java](#3 Activator.java)

1 plugin.xml

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>

    <!-- 定义命令 -->
    <extension point="org.eclipse.ui.commands">
        <command id="HelloWorldPlugin.commands.helloCommand" name="测试">
        </command>
    </extension>

    <!-- 定义右键菜单 -->
    <extension point="org.eclipse.ui.menus">
        <menuContribution locationURI="popup:org.eclipse.ui.popup.any">
            <command commandId="HelloWorldPlugin.commands.helloCommand" label="测试" style="push">
            </command>
        </menuContribution>
    </extension>

    <!-- 定义命令处理器 -->
    <extension point="org.eclipse.ui.handlers">
        <handler class="com.xu.work04.handlers.SampleHandler" commandId="HelloWorldPlugin.commands.helloCommand">
        </handler>
    </extension>

</plugin>

2 SampleHandler.java

java 复制代码
package com.xu.work04.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(), "Work04", "右击菜单弹出框!");
		return null;
	}
	
}

3 Activator.java

java 复制代码
package com.xu.work04;

import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

/**
 * The activator class controls the plug-in life cycle
 */
public class Activator extends AbstractUIPlugin {

	// The plug-in ID
	public static final String PLUGIN_ID = "com.xu.work04"; //$NON-NLS-1$

	// The shared instance
	private static Activator plugin;

	/**
	 * The constructor
	 */
	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);
	}

	/**
	 * Returns the shared instance
	 *
	 * @return the shared instance
	 */
	public static Activator getDefault() {
		return plugin;
	}

}


相关推荐
钢铁男儿2 分钟前
C# 委托(调用带引用参数的委托)
java·mysql·c#
Apex Predator8 分钟前
windows安装maven环境
java·maven
Bug退退退12317 分钟前
RabbitMQ 工作模式
java·分布式·rabbitmq
小莫分享2 小时前
github 镜像节点
java
链上Sniper2 小时前
智能合约状态快照技术:实现 EVM 状态的快速同步与回滚
java·大数据·linux·运维·web3·区块链·智能合约
缘来是庄2 小时前
设计模式之建造者模式
java·设计模式·建造者模式
小湘西3 小时前
Apache HttpClient 的请求模型和 I/O 类型
java·http·apache
沃夫上校3 小时前
Feign调Post接口异常:Incomplete output stream
java·后端·微服务
q567315233 小时前
Java Selenium反爬虫技术方案
java·爬虫·selenium
张小洛3 小时前
Spring IOC容器核心阶段解密:★Bean实例化全流程深度剖析★
java·后端·spring·ioc容器·bean实例化