【eclipse插件开发实战】 Eclipse插件开发6——eclipse在线翻译插件Translator开发实例详解
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开发实例详解的更多相关文章
- 【eclipse插件开发实战】 Eclipse插件开发5——时间插件Timer开发实例详解
Eclipse插件开发5--时间插件Timer开发实例详解 这里做的TimeHelper插件设定为在菜单栏.工具栏提供快捷方式,需要在相应地方设置扩展点,最后弹出窗体显示时间. 在上一篇文章里创建好了 ...
- JavaEE实战——XML文档DOM、SAX、STAX解析方式详解
原 JavaEE实战--XML文档DOM.SAX.STAX解析方式详解 2016年06月22日 23:10:35 李春春_ 阅读数:3445 标签: DOMSAXSTAXJAXPXML Pull 更多 ...
- 全网最详细的IDEA里如何正确新建普通的Java web项目并发布到Tomcat上运行成功【博主强烈推荐】(类似eclipse里同一个workspace下【一个子项目】并存)(图文详解)
不多说,直接上干货! 首先,大家要明确,IDEA.Eclipse和MyEclipse等编辑器之间的新建和运行手法是不一样的. 如果是在Myeclipse里,则是File -> new -> ...
- 【UML 建模】在线UML建模工具 ProcessOn 使用详解
总结 : -- 推荐理由 : 最近从 Windows 操作系统 转到 MAC 上, 正在看设计模式 和 重构, 找不到好用的 UML 工具, 因此在网上找了一款可以在线使用的 UML 工具, 用起来发 ...
- Java eclipse下 Ant build.xml实例详解
在有eclipse集成环境下ant其实不是很重要,但有些项目需要用到,另外通过eclipse来学习和理解ant是个很好的途径,所以写他demo总结下要点,希望能够帮到大家. 一.本人测试环境eclip ...
- 转:Java eclipse下 Ant build.xml实例详解
在有eclipse集成环境下ant其实不是很重要,但有些项目需要用到,另外通过eclipse来学习和理解ant是个很好的途径,所以写他demo总结下要点,希望能够帮到大家. 一.本人测试环境eclip ...
- eclipse自动提示设置以及问题:去除变量自动提示(图文详解)
第一件事 eclipse设置为自动提示 配置步骤: 1 Window > Preferences > Java > Editor > Content Assist 2 &quo ...
- Java eclipse下 Ant build.xml实例详解 附完整项目源码
在有eclipse集成环境下ant其实不是很重要,但有些项目需要用到,另外通过eclipse来学习和理解ant是个很好的途径,所以写他demo总结下要点,希望能够帮到大家. 一.本人测试环境eclip ...
- 使用Eclipse创建Web项目时WEB-INF下找不到web.xml问题详解
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/yjrguxing/article/deta ...
随机推荐
- C语言malloc
在子函数里面动态申请的内存不会自动被系统收回的,因为这些空间在堆里面,而不是栈,平常所说的不能返回指向栈的指针,比如在子函数里面定义一个字符指针,指向常量"hello"因为函数调用 ...
- Nexus 5更新 Android5.0 失败解决方法
Android 5.0最终推出了正式版,今天也及时刷到了Android5.0 (LRX21O),官方链接:https://developers.google.com/android/nexus/ima ...
- Scrapy爬虫入门系列2 示例教程
本来想爬下http://www.alexa.com/topsites/countries/CN 总排名的,但是收费了 只爬了50条数据: response.xpath('//div[@class=&q ...
- (转载)JavaScript递归查询 json 树 父子节点
在Json中知道某个属性名,想要确定该属性在Json树具体的节点,然后进行操作还是很麻烦的 可以用以下方法找到该属性所在的节点,和父节点 <!DOCTYPE html> <html ...
- CIDR(无类域间路由)(转载)
来源:百度百科 CIDR(无类型域间选路,Classless Inter-Domain Routing)是一个在Internet上创建附加地址的方法,这些地址提供给服务提供商(ISP),再由ISP分配 ...
- Python之Pandas库常用函数大全(含注释)
前言:本博文摘抄自中国慕课大学上的课程<Python数据分析与展示>,推荐刚入门的同学去学习,这是非常好的入门视频. 继续一个新的库,Pandas库.Pandas库围绕Series类型和D ...
- tornado之表单和模板
之前在indexHandler中通过self.write()方法在对应的网页中写入具体的字符信息. 如果我们想直接返回一个网页那么这个时候就需要用到模板了 首先在工程目录下新建一个template文件 ...
- 设置port转发来訪问Virtualbox里linux中的站点
上一篇中我们讲到怎么设置virtuabox来通过SSH登录机器. 相同.我们也能够依照上一篇内容中的介绍,设置port转发,来訪问虚拟linux系统已经搭建的站点: 1.设置port转发: water ...
- DIV+CSS常见问题的14条原因分析
当你在一个浏览器里面做好,在其他浏览器里面却完全不是那么回事情. 很多时候,我们就只是去修补下,或者利用各个浏览器对代码支持的不一致,进行针对各个浏览器进行不同的定义. 其实浏览器的不兼容,我们往 ...
- 分词系统简介:PHPAnalysis分词程序
分词系统简介:PHPAnalysis分词程序使用居于unicode的词库,使用反向匹配模式分词,理论上兼容编码更广泛,并且对utf-8编码尤为方便. 由于PHPAnalysis是无组件的系统,因此速度 ...