【eclipse外掛開發實戰】 Eclipse外掛開發6——eclipse線上翻譯外掛Translator開發例項詳解
阿新 • • 發佈:2019-02-18
Eclipse外掛開發6——eclipse線上翻譯外掛Translator開發例項詳解
在上一篇文章中講到了一個簡單的eclipse外掛開發例項,主要是對外掛工程的基本建立步驟進行了講解,這篇文章當中給出一個翻譯小外掛的例項,呼叫有道翻譯API實現實時取詞查詢。
一、專案中需要引用的庫
httpclient-4.1.3.jar
httpclient-cache-4.1.3.jar
httpcore-4.1.4.jar
commons-io-2.1.jar
commons-logging-1.1.1.jar
httpmine-4.1.3.jar
org.jdom-1.1.1.jar
二、窗體設計
先說明一下,API的key可以自行申請以獲取連結。
下面先看主要程式碼,這裡為QueryDialog.java,通過新增文字框、按鈕等,最後響應事件:
import org.eclipse.jface.dialogs.Dialog; import org.eclipse.jface.dialogs.IDialogConstants; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.swt.SWT; import org.eclipse.swt.events.MouseAdapter; import org.eclipse.swt.events.MouseEvent; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Group; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Monitor; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; import org.jdom.Document; import org.trans.fanyi.Activator; import org. trans.fanyi.httpclient.HttpClientUtil; import org. trans.fanyi.model.ResultModel; public class QueryDialog extends Dialog{ private Text queryText; private Text resultTextText; private Button queryButton; private Group infoGroup; protected QueryDialog(Shell parentShell) { super(parentShell); } @Override protected Control createContents(Composite parent) { Shell shell = this.getShell(); shell.setSize(400, 300); Monitor primary = shell.getMonitor(); Rectangle bounds = primary.getBounds(); Rectangle rect = shell.getBounds(); int x = bounds.x + (bounds.width - rect.width) / 2; int y = bounds.y + (bounds.height - rect.height) / 2 - 50; shell.setText("翻譯小助手"); shell.setLocation (x, y); shell.setImage(Activator.getImageDescriptor("/icon/menu.png").createImage()); /*佈局開始*/ Composite composite = new Composite(parent, SWT.NONE); GridLayout layout = new GridLayout(3, false); layout.marginBottom = 10; layout.marginTop = 10; layout.marginLeft = 10; layout.marginRight = -30; layout.horizontalSpacing = 30; layout.verticalSpacing = 0; composite.setLayout(layout); composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); /*headerComposite...*/ Composite headerComposite = new Composite(composite, SWT.NONE); headerComposite.setLayout(new GridLayout(3, false)); headerComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); new Label(headerComposite, SWT.NONE).setText("請輸入:"); queryText = new Text(headerComposite, SWT.BORDER | SWT.MULTI); queryText.setText(DialogUtil.getSelecedTextFromEditor());//設定選中的文字 queryText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); queryButton = new Button(headerComposite, SWT.NONE); queryButton.setText("查詢"); //給Button新增事件 addListenerToButton(); //******************************// //***GROUP START***// Composite infoComposite = new Composite(parent, SWT.NONE); infoComposite.setLayout(new GridLayout(1, true)); infoComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); infoGroup = new Group(infoComposite, SWT.NONE); infoGroup.setText("查詢結果"); GridLayout groupLayout = new GridLayout(1, false); groupLayout.marginBottom = 5; groupLayout.marginTop = 5; groupLayout.marginLeft = 10; groupLayout.marginRight = 10; infoGroup.setLayout(groupLayout); infoGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); infoGroup.pack(); resultTextText = new Text(infoGroup, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL | SWT.READ_ONLY); resultTextText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); resultTextText.setText("" + System.getProperty("line.separator") + ""+ System.getProperty("line.separator") + ""+ System.getProperty("line.separator") + ""+ System.getProperty("line.separator") + ""+ System.getProperty("line.separator") + ""+ System.getProperty("line.separator") + ""+ System.getProperty("line.separator") + ""+ System.getProperty("line.separator") + // ""+ System.getProperty("line.separator") + ""); return super.createContents(parent); } @Override protected Button createButton(Composite parent, int id, String label, boolean defaultButton) { if (id == IDialogConstants.CANCEL_ID || id == IDialogConstants.OK_ID) { return null; } return super.createButton(parent, id, label, defaultButton); } public void addListenerToButton(){ queryButton.addMouseListener(new MouseAdapter() { @Override public void mouseDown(MouseEvent e) { String qtext = queryText.getText(); if(qtext.isEmpty()){ MessageDialog.openInformation(Display.getCurrent().getActiveShell(), "提示", "請輸入或選擇查詢"); } else { Document doc = HttpClientUtil.getDocumentByQuery(qtext); if(doc != null){ ResultModel rm = HttpClientUtil.convertDocToModel(doc); resultTextText.setText(ResultModel.getFormattedDisplatString(rm)); } } super.mouseDown(e); } }); } }
把匯出的jar包安裝到plugins下,最終執行效果圖:
專案原始碼已經commit在github上,有興趣的可以去看一下:https://github.com/DianaCody/Translator.git,並且外掛的jar包下載也在README.md文件裡有下載連結,把jar包放到plugin目錄下即可安裝,jar包也可以到我的csdn資源頁下載http://download.csdn.net/detail/dianacody/7659093