安装插件

建立工程

LIB

Activator.java

package com.my.plugin;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 IDpublic static final String PLUGIN_ID = "com.my.plugin"; //$NON-NLS-1$// The shared instanceprivate static Activator plugin;/*** The constructor*/public Activator() {}@Overridepublic void start(BundleContext context) throws Exception {super.start(context);plugin = this;}@Overridepublic 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;}}

Activator 是插件的激活类 start()和stop()分别用于插件开始与停止调用的函数。

plugin.xml

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin><extensionpoint="org.eclipse.ui.commands"><categoryid="com.my.plugin.commands.category"name="Sample Category"></category><commandcategoryId="com.my.plugin.commands.category"name="Sample Command"id="com.my.plugin.commands.sampleCommand"></command></extension><extensionpoint="org.eclipse.ui.handlers"><handlerclass="com.my.plugin.handlers.MyPluginHandler"commandId="com.my.plugin.commands.sampleCommand"></handler></extension><extensionpoint="org.eclipse.ui.bindings"><keycommandId="com.my.plugin.commands.sampleCommand"schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"contextId="org.eclipse.ui.contexts.window"sequence="CTRL+P"></key></extension><extensionpoint="org.eclipse.ui.menus"><menuContributionlocationURI="menu:org.eclipse.ui.main.menu?after=additions"><menuid="com.my.plugin.menus.sampleMenu"label="我的插件O"mnemonic="O"><commandcommandId="com.my.plugin.commands.sampleCommand"id="com.my.plugin.menus.sampleCommand"mnemonic="S"></command></menu></menuContribution><menuContributionlocationURI="toolbar:org.eclipse.ui.main.toolbar?after=additions"><toolbarid="com.my.plugin.toolbars.sampleToolbar"><commandid="com.my.plugin.toolbars.sampleCommand"commandId="com.my.plugin.commands.sampleCommand"icon="icons/sample.png"tooltip="Say hello world"></command></toolbar></menuContribution></extension><extensionpoint="org.eclipse.ui.preferencePages"><pageid="com.my.plugin.preferences.MyPluginPreferencePage"name="MyPlugin Preferences"class="com.my.plugin.preferences.MyPluginPreferencePage"></page></extension><extensionpoint="org.eclipse.core.runtime.preferences"><initializerclass="com.my.plugin.preferences.PreferenceInitializer"></initializer></extension><extensionpoint="org.eclipse.ui.splashHandlers"><splashHandlerid="com.my.plugin.splashHandlers.interactive"class="com.my.plugin.splashHandlers.InteractiveSplashHandler"></splashHandler><splashHandlerProductBindingproductId="org.eclipse.equinox.p2.director.app.product"splashId="com.my.plugin.splashHandlers.interactive"></splashHandlerProductBinding></extension><extensionpoint="org.eclipse.ui.views"><categoryname="Sample Category"id="com.my.plugin"></category><viewid="com.my.plugin.views.SampleView"name="Sample View"icon="icons/sample.png"class="com.my.plugin.views.SampleView"category="com.my.plugin"inject="true"></view></extension><extensionpoint="org.eclipse.ui.perspectiveExtensions"><perspectiveExtensiontargetID="org.eclipse.jdt.ui.JavaPerspective"><viewid="com.my.plugin.views.SampleView"relative="org.eclipse.ui.views.ProblemView"relationship="right"ratio="0.5"></view></perspectiveExtension></extension><extensionpoint="org.eclipse.help.contexts"><contextsfile="contexts.xml"></contexts></extension></plugin>

MENU

      <menuContributionlocationURI="menu:org.eclipse.ui.main.menu?after=additions"><menuid="com.my.plugin.menus.sampleMenu"label="我的插件O"mnemonic="O"><commandcommandId="com.my.plugin.commands.sampleCommand"id="com.my.plugin.menus.sampleCommand"mnemonic="S"></command></menu></menuContribution>

这个代码就是菜单,mnemonic是快捷键,按ALT+mnemonic的值,但是mnemonic的值必须要在label的值里出现。

里面子菜单(Sample Command)就是这里的command,这个command只是引用,应用的key是commandId

真正的command名字在

   <extensionpoint="org.eclipse.ui.commands"><categoryid="com.my.plugin.commands.category"name="Sample Category"></category><commandcategoryId="com.my.plugin.commands.category"name="Sample Command"id="com.my.plugin.commands.sampleCommand"></command></extension>

点击后的执行的类在

   <extensionpoint="org.eclipse.ui.handlers"><handlerclass="com.my.plugin.handlers.MyPluginHandler"commandId="com.my.plugin.commands.sampleCommand"></handler></extension>

就是com.my.plugin.handlers.MyPluginHandler

package com.my.plugin.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 MyPluginHandler extends AbstractHandler {@Overridepublic Object execute(ExecutionEvent event) throws ExecutionException {IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);MessageDialog.openInformation(window.getShell(),"Plugin","Hello, Eclipse world");return null;}
}

对于这个Command还可以定义个全局的快捷键

   <extensionpoint="org.eclipse.ui.bindings"><keycommandId="com.my.plugin.commands.sampleCommand"schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"contextId="org.eclipse.ui.contexts.window"sequence="CTRL+P"></key></extension>

TOOLBAR

         <toolbarid="com.my.plugin.toolbars.sampleToolbar"><commandid="com.my.plugin.toolbars.sampleCommand"commandId="com.my.plugin.commands.sampleCommand"icon="icons/sample.png"tooltip="Say hello world"></command></toolbar>

上面菜单的"Sample Command" 还可以做成一个快捷按钮,就是

相应的配置如上图

Preferences

代码如下:

package com.my.plugin.preferences;import org.eclipse.jface.preference.*;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.eclipse.ui.IWorkbench;
import com.my.plugin.Activator;public class MyPluginPreferencePageextends FieldEditorPreferencePageimplements IWorkbenchPreferencePage {public MyPluginPreferencePage() {super(GRID);setPreferenceStore(Activator.getDefault().getPreferenceStore());setDescription("这是插件说明");}public void createFieldEditors() {addField(new DirectoryFieldEditor(PreferenceConstants.P_PATH, "&F文件位置::", getFieldEditorParent()));addField(new BooleanFieldEditor(PreferenceConstants.P_BOOLEAN,"&A可用:",getFieldEditorParent()));addField(new RadioGroupFieldEditor(PreferenceConstants.P_CHOICE,"性别",1,new String[][] { { "&N男", "0" }, {"&V女", "1" }}, getFieldEditorParent()));addField(new StringFieldEditor(PreferenceConstants.P_STRING, "&H请输入你的名字:", getFieldEditorParent()));}public void init(IWorkbench workbench) {}}

这里面有4个UI控件,都是通过addField放置上去的,每个ui都有个引用名字PreferenceConstants.P_XXXXXX

package com.my.plugin.preferences;/*** Constant definitions for plug-in preferences*/
public class PreferenceConstants {public static final String P_PATH = "pathPreference";public static final String P_BOOLEAN = "booleanPreference";public static final String P_CHOICE = "choicePreference";public static final String P_STRING = "stringPreference";}

这里的应用名字字段的值可以随意,但是不要相同

这里的UI,还有一个控制初始值(也叫默认值)

package com.my.plugin.preferences;import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer;
import org.eclipse.jface.preference.IPreferenceStore;import com.my.plugin.Activator;/*** Class used to initialize default preference values.*/
public class PreferenceInitializer extends AbstractPreferenceInitializer {public void initializeDefaultPreferences() {IPreferenceStore store = Activator.getDefault().getPreferenceStore();store.setDefault(PreferenceConstants.P_BOOLEAN, true);store.setDefault(PreferenceConstants.P_CHOICE, "0");store.setDefault(PreferenceConstants.P_STRING,"初期值");}}

如果在别的class要获取这里控件的值

IPreferenceStore ipreferenceStore = Activator.getDefault().getPreferenceStore();
// 获取控件的值当前值
ipreferenceStore.getBoolean(PreferenceConstants.CLIPBOARD_BOOLEAN);
// 获取控件的值默认值(初期的值)
ipreferenceStore.getDefaultBoolean(PreferenceConstants.CLIPBOARD_BOOLEAN)

相关的配置如下

   <extensionpoint="org.eclipse.ui.preferencePages"><pageid="com.my.plugin.preferences.MyPluginPreferencePage"name="MyPlugin Preferences"class="com.my.plugin.preferences.MyPluginPreferencePage"></page></extension><extensionpoint="org.eclipse.core.runtime.preferences"><initializerclass="com.my.plugin.preferences.PreferenceInitializer"></initializer></extension>

视图

   <extensionpoint="org.eclipse.ui.views"><categoryname="插件视图"id="com.my.plugin"></category><viewid="com.my.plugin.views.SampleView"name="Sample View"icon="icons/sample.png"class="com.my.plugin.views.SampleView"category="com.my.plugin"inject="true"></view></extension>
package com.my.plugin.views;import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.*;
import org.eclipse.jface.viewers.*;
import org.eclipse.swt.graphics.Image;
import org.eclipse.jface.action.*;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.ui.*;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.SWT;
import javax.inject.Inject;public class SampleView extends ViewPart {/*** The ID of the view as specified by the extension.*/public static final String ID = "com.my.plugin.views.SampleView";@Inject IWorkbench workbench;private TableViewer viewer;private Action action1;private Action action2;private Action doubleClickAction;class ViewLabelProvider extends LabelProvider implements ITableLabelProvider {@Overridepublic String getColumnText(Object obj, int index) {return getText(obj);}@Overridepublic Image getColumnImage(Object obj, int index) {return getImage(obj);}@Overridepublic Image getImage(Object obj) {return workbench.getSharedImages().getImage(ISharedImages.IMG_OBJ_ELEMENT);}}public TableViewer getViewer () {return this.viewer;}@Overridepublic void createPartControl(Composite parent) {viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);viewer.setContentProvider(ArrayContentProvider.getInstance());viewer.setInput(new String[] { "One", "Two", "Three" });viewer.setLabelProvider(new ViewLabelProvider());// Create the help context id for the viewer's controlworkbench.getHelpSystem().setHelp(viewer.getControl(), "com.my.plugin.viewer");getSite().setSelectionProvider(viewer);makeActions();hookContextMenu();hookDoubleClickAction();contributeToActionBars();}private void hookContextMenu() {MenuManager menuMgr = new MenuManager("#PopupMenu");menuMgr.setRemoveAllWhenShown(true);menuMgr.addMenuListener(new IMenuListener() {public void menuAboutToShow(IMenuManager manager) {SampleView.this.fillContextMenu(manager);}});Menu menu = menuMgr.createContextMenu(viewer.getControl());viewer.getControl().setMenu(menu);getSite().registerContextMenu(menuMgr, viewer);}private void contributeToActionBars() {IActionBars bars = getViewSite().getActionBars();fillLocalPullDown(bars.getMenuManager());fillLocalToolBar(bars.getToolBarManager());}private void fillLocalPullDown(IMenuManager manager) {manager.add(action1);manager.add(new Separator());manager.add(action2);}private void fillContextMenu(IMenuManager manager) {manager.add(action1);manager.add(action2);// Other plug-ins can contribute there actions heremanager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));}private void fillLocalToolBar(IToolBarManager manager) {manager.add(action1);manager.add(action2);}private void makeActions() {action1 = new Action() {public void run() {showMessage("Action 1 executed");}};action1.setText("文件路径1:");action1.setToolTipText("Action 1 tooltip");action1.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));action2 = new Action() {public void run() {showMessage("Action 2 executed");}};action2.setText("\"文件路径2:");action2.setToolTipText("Action 2 tooltip");action2.setImageDescriptor(workbench.getSharedImages().getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));doubleClickAction = new Action() {public void run() {IStructuredSelection selection = viewer.getStructuredSelection();Object obj = selection.getFirstElement();showMessage("Double-click detected on "+obj.toString());}};}private void hookDoubleClickAction() {viewer.addDoubleClickListener(new IDoubleClickListener() {public void doubleClick(DoubleClickEvent event) {doubleClickAction.run();}});}private void showMessage(String message) {MessageDialog.openInformation(viewer.getControl().getShell(),"Sample View",message);}@Overridepublic void setFocus() {viewer.getControl().setFocus();}
}

其中实现下图效果 是 函数(contributeToActionBars)

实现右键菜单的是  (hookContextMenu)

如果要要在别的class 获取这里的控件的值

        IWorkbenchPage page = workbenchWindow.getActivePage();SampleView sampleView = (SampleView) page.findView(SampleView.ID);sampleView.getViewer();

追加开发时在包

运行插件

导出插件

到时候E盘就会出现plugins的文件夹,插件就在里面

plugin.xml的国际化

可以利用%

      <categoryid="org.eclipse.sts4.sqllogparser.commands.category"name="%org.eclipse.sts4.sqllogparser.commands">

在plugin.xml同级目录做一个language_zh_CN.properties

org.eclipse.sts4.sqllogparser.commands=SQL解析

在META-INF/MANIFEST.MF里设置

Bundle-Localization: language

在做测试的时候language_zh_CN.properties必须放置在plugin.xml同级目录才有效。但是实际打包的后,language_zh_CN.properties放置在src目录里也可以,因为打成包后language_zh_CN.properties都会出现在最外层目录

eclipse plugin相关推荐

  1. Fat Jar Eclipse Plug-In Tutorial

    Fat Jar Eclipse Plug-In [FJEP] (http://fjep.sourceforge.net/) 是应用One-Jar(http://one-jar.sourceforge. ...

  2. GWT(Google Web Tookit) Eclipse Plugin的zip下载地址(同时提供GWT Designer下载地址)

    按照Eclipse Help->Install new software->....(这里是官方安装文档:http://code.google.com/intl/zh-CN/eclipse ...

  3. hadoop eclipse plugin windows下载集合

    收集了hadoop稳定版本的eclipse plugin for windows.资源分一律为0分 hadoop-eclipse-plugin-1.2.1.jar http://download.cs ...

  4. Eclipse+ GNU ARM Eclipse Plug-in+ Sourcery G++ Lite Edition for ARM+OPENCD+Jlink的开源开发环境。

    Eclipse+ GNU ARM Eclipse Plug-in+ Sourcery G++ Lite Edition for ARM+OPENCD+Jlink的开源开发环境. 具体介绍: Eclip ...

  5. Eclipse+ GNU ARM Eclipse Plug-in+ Sourcery G++ Lite Edition for ARM+OPENCD+Jlink的开源开发环境

    作者:emouse 转自:http://blog.csdn.net/haozi_1989/article/details/6023242 Eclipse+ GNU ARM Eclipse Plug-i ...

  6. Eclipse+ GNU ARM Eclipse Plug-in+ Sourcery G++ Lite Edition for ARM+OPENCD+Jlink

    这两天在尝试着搭建Eclipse+ GNU ARM Eclipse Plug-in+ Sourcery G++ Lite Edition for ARM+OPENCD+Jlink的STM32开发环境, ...

  7. RubyOnRails的安装和eclipse plugin

    Windows系统下开发环境的搭建 本文中将介绍如何在Windows操作系统下搭建RoR开发环境. 1.远程安装 a.步骤1:下载并安装Ruby一键安装包 下载最新的Ruby 1.8.4-16一键安装 ...

  8. EGit(Git Eclipse Plugin)使用(转载)

    发现一片篇egit深度好文章先转载一下 以下是原文 原先博客地址http://shihlei.iteye.com/blog/2124411 -– EGit(Git Eclipse Plugin)使用 ...

  9. Eclipse Plug-in Hello world

    这一篇就简单说下一个hello world插件工程创建过程. 1.创建一个Plug-in Project 2.填写project name 3.第二个确认框勾上,然后直接下一步   4.选择Hello ...

  10. eclipse Plugin execution not covered by lifecycle configuration:maven.***.plugin

    具体是pom 不能引入和识别对应maven-plugin: 例如:Plugin execution not covered by lifecycle configuration: org.apache ...

最新文章

  1. [CF314C](Sereja and Subsequences)
  2. 如何测试java支持的最大内存
  3. mysql mmm 主主_Microsoft Azure部署MYSQL-MMM(2)配置主主复制
  4. POJ 3468 A Simple Problem with Integers (1)
  5. 我的Notepad++环境配置
  6. 禁用内存清理_win10电脑开机内存占用高达80%以上如何解决
  7. eclipse下使用git插件上传代码至github
  8. p2p linux 开源项目,权威开源项目(linux系统、sip、live555)
  9. 贪心算法设计作业调度c语言,贪心算法 - 数据结构与算法教程 - C语言网
  10. 今天一看,自己发的文章原来这么多了。openeim
  11. 针对Properties中实时性要求不高的配置参数,用Java缓存起来
  12. JQuery合并table单元格--有限制(table格式需要注意)
  13. [LeetCode] 3Sum
  14. python刚出来多少薪资-Python工资待遇的几个层级,你工作几年了?目前是哪个层级?...
  15. Android InputType
  16. Java经典设计模式(1):五大创建型模式(附实例和详解)
  17. 《HTML and CSS Design and Build Websites》学习笔记之HTML5表单新增功能
  18. 火山安卓文件读写操作演示
  19. mysql 不等于 符号写法
  20. c#程序设计实训报告心得体会_c#学习心得体会范文

热门文章

  1. python 画图marker标记汇总(matplotlib.pyplot)
  2. 蓉创蜀兴丨HTTPS建设全解析
  3. Java实现CSV文件的读写
  4. 关系规范化之满足第三范式3NF的函数依赖保持分解算法
  5. 推断统计学 假设检验 分布
  6. 基于MIT协议的详细介绍
  7. 英语语法回顾4——定语和定语从句
  8. MySQL---查看数据表结构
  9. linux与linux驱动
  10. 浅谈表值函数和标量值函数