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

【eclipse插件开发实战】 Eclipse插件开发6——eclipse在线翻译插件Translator开发实例详解的更多相关文章

  1. 【eclipse插件开发实战】 Eclipse插件开发5——时间插件Timer开发实例详解

    Eclipse插件开发5--时间插件Timer开发实例详解 这里做的TimeHelper插件设定为在菜单栏.工具栏提供快捷方式,需要在相应地方设置扩展点,最后弹出窗体显示时间. 在上一篇文章里创建好了 ...

  2. JavaEE实战——XML文档DOM、SAX、STAX解析方式详解

    原 JavaEE实战--XML文档DOM.SAX.STAX解析方式详解 2016年06月22日 23:10:35 李春春_ 阅读数:3445 标签: DOMSAXSTAXJAXPXML Pull 更多 ...

  3. 全网最详细的IDEA里如何正确新建普通的Java web项目并发布到Tomcat上运行成功【博主强烈推荐】(类似eclipse里同一个workspace下【一个子项目】并存)(图文详解)

    不多说,直接上干货! 首先,大家要明确,IDEA.Eclipse和MyEclipse等编辑器之间的新建和运行手法是不一样的. 如果是在Myeclipse里,则是File -> new -> ...

  4. 【UML 建模】在线UML建模工具 ProcessOn 使用详解

    总结 : -- 推荐理由 : 最近从 Windows 操作系统 转到 MAC 上, 正在看设计模式 和 重构, 找不到好用的 UML 工具, 因此在网上找了一款可以在线使用的 UML 工具, 用起来发 ...

  5. Java eclipse下 Ant build.xml实例详解

    在有eclipse集成环境下ant其实不是很重要,但有些项目需要用到,另外通过eclipse来学习和理解ant是个很好的途径,所以写他demo总结下要点,希望能够帮到大家. 一.本人测试环境eclip ...

  6. 转:Java eclipse下 Ant build.xml实例详解

    在有eclipse集成环境下ant其实不是很重要,但有些项目需要用到,另外通过eclipse来学习和理解ant是个很好的途径,所以写他demo总结下要点,希望能够帮到大家. 一.本人测试环境eclip ...

  7. eclipse自动提示设置以及问题:去除变量自动提示(图文详解)

    第一件事 eclipse设置为自动提示 配置步骤: 1 Window > Preferences > Java > Editor > Content Assist 2 &quo ...

  8. Java eclipse下 Ant build.xml实例详解 附完整项目源码

    在有eclipse集成环境下ant其实不是很重要,但有些项目需要用到,另外通过eclipse来学习和理解ant是个很好的途径,所以写他demo总结下要点,希望能够帮到大家. 一.本人测试环境eclip ...

  9. 使用Eclipse创建Web项目时WEB-INF下找不到web.xml问题详解

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/yjrguxing/article/deta ...

随机推荐

  1. $modal 参数 以及 使用方法

    $modal是一个可以迅速创建模态窗口的服务,创建部分页,控制器,并关联他们 $modal仅有一个方法open(options) templateUrl:模态窗口的地址 template:用于显示ht ...

  2. ios开发动物园管理 继承多态的实现

    // // main.m // 继承 // // #import <Foundation/Foundation.h> #import "Animal.h" #impor ...

  3. vue 生命周期钩子

    每个vue实例被创建时都会经历一系列初始化的过程,像是一个生命从无到有的过程,所以叫生命周期,而这个过程都有对应的不同阶段,也就对应了生命周期不同的钩子函数,这些生命周期函数,作为vue实例的属性使用 ...

  4. Redis 3.2.4编译安装

    1. 下载安装包 wget url tar zxvf redis-3.2.4.tar.gz 2. 编译安装 cd redis-3.2.4/src/ sudo make && make ...

  5. 【TensorFlow-windows】(四) CNN(卷积神经网络)进行手写数字识别(mnist)

    主要内容: 1.基于CNN的mnist手写数字识别(详细代码注释) 2.该实现中的函数总结 平台: 1.windows 10 64位 2.Anaconda3-4.2.0-Windows-x86_64. ...

  6. httpurlconnection模拟post提交form表单(普通文本和上传文件) (

    http://blog.sina.com.cn/s/blog_8417657f0101gvpc.html 用HttpUrlConnection模拟post表单进行文件上传平时很少使用,比较麻烦. 原理 ...

  7. javascript中提高代码的封装性

    我出的面试题中,有一条是问如何避免页面引用JS,出现函数.变量重复.冲突的. 从大的方面讲,应该引入javascript的模块化开发,符合AMD规范之类: 从小的方面说,大概就是限定变量和函数的作用域 ...

  8. stretchableImageWithLeftCapWidth

    本文转载至 http://www.cnblogs.com/bandy/archive/2012/04/25/2469369.html (NSInteger)topCapHeight 这个函数是UIIm ...

  9. Nothing but the key 属性全部依赖于主键 third norm form

    全依赖 Designs that Violate 1NF CustomerCustomer ID First Name Surname Telephone Number123 Pooja Singh ...

  10. cocos2d-js实现 双击android后退按钮 即退出游戏

    之前测了一下android自带的后退按钮,用在cocos2d-js中是没有获取到的 (可能是cocos2d-js已经把android的后退事件截取了,所以原生java代码没有用), 没办法就只能用co ...