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

}


相关推荐
lee_curry8 分钟前
第四章 jvm中的垃圾回收器
java·jvm·垃圾收集器
九转成圣1 小时前
Java 性能优化实战:如何将海量扁平数据高效转化为类目字典树?
java·开发语言·json
yc_12242 小时前
用 Visual Studio 远程调试 Linux:从零到流畅的完整指南
linux·ide·visual studio
直奔標竿2 小时前
Java开发者AI转型第二十七课!Spring AI 个人知识库实战(六)——全栈闭环收官,解锁前端流式渲染终极技巧
java·开发语言·前端·人工智能·后端·spring
金銀銅鐵2 小时前
[java] 编译之后的记录类(Record Classes)长什么样子(上)
java·jvm·后端
野生技术架构师4 小时前
金三银四面试总结篇,汇总 Java 面试突击班后的面试小册
java·面试·职场和发展
小袁拒绝摆烂4 小时前
多表关联大平层转JSON树形结构
java·json
ja哇5 小时前
大厂面试高频八股
java·面试·职场和发展
yoyo_zzm6 小时前
Laravel6.x新特性全解析
java·spring boot·后端