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;
	}

}


相关推荐
wangbing11251 小时前
springboot应用关闭时触发动作
java·spring boot·后端
weixin_460443564 小时前
2026年新修订《网络安全法》实施:Java私有化考试系统如何做好SBOM、漏洞治理与安全升级?
java·开发语言·在线考试系统·企业培训考试系统·宏远培训考试系统·国企培训考试系统·国产化培训系统
海鸥-w7 小时前
什么是AOP
java
Cc.Y7 小时前
Java零基础入门:可变字符串与包装类:StringBuilder、StringBuffer 与 通讯录管理系统实战
java·开发语言·python
golang学习记8 小时前
VSCode多光标迎来重大更新
ide·vscode·编辑器
paopaokaka_luck8 小时前
基于springboot+vue的智慧家政管理系统(AI问答、 WebSocket实时沟通、ECharts运营分析、订单规则与服务追踪)
java·javascript·spring boot·数据分析·echarts·mybatis
sm_926787058 小时前
RFID 标签打印的技术实现要点与二次开发实践
java·大数据·前端·c++·编辑器
阿森_dev8 小时前
2027 届毕设选题怎么选:30 个 Java 方向选题的实现难度排序
java·开发语言·spring boot·毕业设计·课程设计
Sarvartha8 小时前
Lambda 表达式和函数式接口
java·开发语言
计算机毕业设计杰瑞8 小时前
【计算机毕设精品】基于SpringBoot的一体化教务与学业质量管理系统的设计与实现,附源码_增删改查_java项目_可完美运行部署_网站项目
java·spring boot·课程设计