eclipse plugin

安装插件

建立工程

 

 

 

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 ID
	public static final String PLUGIN_ID = "com.my.plugin"; //$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;
	}

}

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

plugin.xml

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>

   <extension
         point="org.eclipse.ui.commands">
      <category
            id="com.my.plugin.commands.category"
            name="Sample Category">
      </category>
      <command
            categoryId="com.my.plugin.commands.category"
            name="Sample Command"
            id="com.my.plugin.commands.sampleCommand">
      </command>
   </extension>
   <extension
         point="org.eclipse.ui.handlers">
      <handler
            class="com.my.plugin.handlers.MyPluginHandler"
            commandId="com.my.plugin.commands.sampleCommand">
      </handler>
   </extension>
   <extension
         point="org.eclipse.ui.bindings">
      <key
            commandId="com.my.plugin.commands.sampleCommand"
            schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
            contextId="org.eclipse.ui.contexts.window"
            sequence="CTRL+P">
      </key>
   </extension>
   <extension
         point="org.eclipse.ui.menus">
      <menuContribution
            locationURI="menu:org.eclipse.ui.main.menu?after=additions">
         <menu
               id="com.my.plugin.menus.sampleMenu"
               label="我的插件O"
               mnemonic="O">
            <command
                  commandId="com.my.plugin.commands.sampleCommand"
                  id="com.my.plugin.menus.sampleCommand"
                  mnemonic="S">
            </command>
         </menu>
      </menuContribution>
      <menuContribution
            locationURI="toolbar:org.eclipse.ui.main.toolbar?after=additions">
         <toolbar
               id="com.my.plugin.toolbars.sampleToolbar">
            <command
                  id="com.my.plugin.toolbars.sampleCommand"
                  commandId="com.my.plugin.commands.sampleCommand"
                  icon="icons/sample.png"
                  tooltip="Say hello world">
            </command>
         </toolbar>
      </menuContribution>
   </extension>
   <extension
         point="org.eclipse.ui.preferencePages">
      <page
            id="com.my.plugin.preferences.MyPluginPreferencePage"
            name="MyPlugin Preferences"
            class="com.my.plugin.preferences.MyPluginPreferencePage">
      </page>
   </extension>
   <extension
         point="org.eclipse.core.runtime.preferences">
      <initializer
            class="com.my.plugin.preferences.PreferenceInitializer">
      </initializer>
   </extension>
   <extension
         point="org.eclipse.ui.splashHandlers">
      <splashHandler
            id="com.my.plugin.splashHandlers.interactive"
            class="com.my.plugin.splashHandlers.InteractiveSplashHandler">
      </splashHandler>
      <splashHandlerProductBinding
            productId="org.eclipse.equinox.p2.director.app.product"
            splashId="com.my.plugin.splashHandlers.interactive">
      </splashHandlerProductBinding>
   </extension>
   <extension
         point="org.eclipse.ui.views">
      <category
            name="Sample Category"
            id="com.my.plugin">
      </category>
      <view
            id="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>
   <extension
         point="org.eclipse.ui.perspectiveExtensions">
      <perspectiveExtension
            targetID="org.eclipse.jdt.ui.JavaPerspective">
         <view
               id="com.my.plugin.views.SampleView"
               relative="org.eclipse.ui.views.ProblemView"
               relationship="right"
               ratio="0.5">
         </view>
      </perspectiveExtension>
   </extension>
   <extension
         point="org.eclipse.help.contexts">
      <contexts
            file="contexts.xml">
      </contexts>
   </extension>

</plugin>
      <menuContribution
            locationURI="menu:org.eclipse.ui.main.menu?after=additions">
         <menu
               id="com.my.plugin.menus.sampleMenu"
               label="我的插件O"
               mnemonic="O">
            <command
                  commandId="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名字在

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

点击后的执行的类在 

   <extension
         point="org.eclipse.ui.handlers">
      <handler
            class="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 {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
		MessageDialog.openInformation(
				window.getShell(),
				"Plugin",
				"Hello, Eclipse world");
		return null;
	}
}

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

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

TOOLBAR

         <toolbar
               id="com.my.plugin.toolbars.sampleToolbar">
            <command
                  id="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 MyPluginPreferencePage
	extends FieldEditorPreferencePage
	implements 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)

相关的配置如下

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

视图

   <extension
         point="org.eclipse.ui.views">
      <category
            name="插件视图"
            id="com.my.plugin">
      </category>
      <view
            id="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 {
		@Override
		public String getColumnText(Object obj, int index) {
			return getText(obj);
		}
		@Override
		public Image getColumnImage(Object obj, int index) {
			return getImage(obj);
		}
		@Override
		public Image getImage(Object obj) {
			return workbench.getSharedImages().getImage(ISharedImages.IMG_OBJ_ELEMENT);
		}
	}

    public TableViewer getViewer () {
        return this.viewer;
    }
	@Override
	public 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 control
		workbench.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 here
		manager.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);
	}

	@Override
	public 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的国际化

可以利用%

      <category
            id="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都会出现在最外层目录

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值