1. 程式人生 > >跟著例項學eclipse外掛開發--第一篇:翻譯外掛

跟著例項學eclipse外掛開發--第一篇:翻譯外掛

本文並非外掛開發的最基礎教程,推薦2個網站大家可以看一下(國外的,國內資源太少):

本教程通過開發一系列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>

(本教程是從一個專案中抽取出來的,可能會多引用了一些jar,另外Msun是以前專案名稱,可自行修改)