本文并非插件开发的最基础教程,推荐2个网站大家可以看一下(国外的,国内资源太少):
eclipse功能点demo:https://wiki.eclipse.org/Eclipse_Corner
eclipse开发API:http://help.eclipse.org/photon/index.jsp
本教程通过开发一系列elipse的插件,为大家讲解elipse插件的使用。本文为大家讲解:org.eclipse.ui.views扩展点、org.eclipse.ui.handlers扩展点、org.eclipse.ui.bindings扩展点。翻译直接使用百度翻译的API,这里不再讲解。
首先需要使用org.eclipse.ui.views,创建自定义的属性视图,该节点使用起来很简单,只需要在plugin.xml中添加:
<extension point="org.eclipse.ui.views">
<category name="Msun Category" id="MsunCategory"></category>
<view
name="Baidu Translate"
icon="icons/translate.png"
category="MsunCategory"
class="com.msun.plug.view.MyTranslateView"
id="com.msun.plug.view.MyTranslateView">
</view>
</extension>
然后创建MyTranslateView类,该类需要继承org.eclipse.ui.part.ViewPart类:
package com.msun.plug.view;
import org.eclipse.jface.text.ITextOperationTarget;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPartSite;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;
import com.msun.plug.util.bdFanyi.TransApi;
/**
* @Title:插件开发
* @Description:MyTranslateView业务类
* @Copyright:MSun (c) 2018年9月3日
*
* @author:jiujiya
* @version:1.0
*/
public class MyTranslateView extends ViewPart {
public static final String ID = "com.msun.plug.view.MyTranslateView";
public static volatile StyledText resultText;
@Override
public void createPartControl(Composite parent) {
resultText = new StyledText(parent, 0);
}
/**
* 翻译
*/
public static void translate() {
// 先获取选中的文本
IWorkbenchPartSite activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPartService().getActivePart().getSite();
IEditorPart editorPart = activePage.getWorkbenchWindow().getActivePage().getActiveEditor();
if (editorPart != null) {
ITextOperationTarget target = (ITextOperationTarget)editorPart.getAdapter(ITextOperationTarget.class);
if (target instanceof ITextViewer) {
ITextViewer textViewer = (ITextViewer)target;
String text = textViewer.getTextWidget().getSelectionText();
resultText.setText("原文:" + text + "\r\n译文:" + TransApi.getTransResult(text, "auto", "zh"));
}
}
}
@Override
public void setFocus() {
}
}
然后使用org.eclipse.ui.handlers,创建类TranslateHandler:
package com.msun.plug.handlers;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.ui.PlatformUI;
import com.msun.plug.view.MyTranslateView;
/**
* Title: 插件开发
* Description: 翻译入口
* Copyright: MSun (c) 2018年9月3日
*
* @author jiujiya
* @version 1.0
*/
public class TranslateHandler extends BaseHandler {
public Object execute(ExecutionEvent event) throws ExecutionException {
try {
// 打开MyTranslateView
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(MyTranslateView.ID);
MyTranslateView.translate();
} catch (Exception e) {
alert(event, e.getMessage());
}
return null;
}
}
最后绑定快捷键F9:
<extension point="org.eclipse.ui.bindings">
<key sequence="f9"
commandId="com.msun.plug.handlers.TranslateHandler"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration">
</key>
</extension>
源码下载地址:https://download.csdn.net/download/jiujiya123/10643080
(本教程是从一个项目中抽取出来的,可能会多引用了一些jar,另外Msun是以前项目名称,可自行修改)