现在运行是工作台已经具备了天气预报的功能,还需要更进一步,将改插件导出发布,拷贝到Eclipse根目录的plugins目录中,重新启动(具体参见Eclipse帮助)。现在你自己的Eclipse,就具备了天气预报的功能,只要你点击鼠标,就可以在编程之余轻松的获取天气信息。
除非你的老板认为你在工作时间随时了解天气情况不是一个好主意,我认为你完全可以将这个插件纳入个人收藏的插件之列。你也可以在此基础上扩展,增加一些配置文件和属性设置,定制出满足自己要求的插件。如果能够增加信息的自动过滤和筛选,那将是一次很愉快的体验,如果你有时间和兴趣,不妨一试。
3、邮件快速监控插件
现在你的工作因为Eclipse而更加惬意,更具创造力,那么你还有什么不满?你是否厌倦了各种邮件客户端随时随地的骚扰你呢?你希望你在高兴的时候适时的了解一下邮件的概况?好了,既然想到了为什么犹豫呢,因为你是程序员,你就是要用Eclipse享受完全DIY的乐趣。
3.1生成插件
本部分我们将在以上myplugin插件的基础上增加一个邮件过滤显示的对话框,类似的我们通过VisualEditer创建一个名为MailDialog的对话框,并增加一个JEditPane用来显示邮箱中我们关注的信息。
修改plugin.xml,增加一个"我的邮件"菜单
<action
label="邮件信息"
icon="icons/sample.gif"
class="myplugin.actions.MailAction"
tooltip="邮件信息"
menubarPath="sampleMenu/sampleGroup"
toolbarPath="sampleGroup"
id="myplugin.actions.MailAction">
</action>
|
现在,你知道要创建一个MailAction的Action类,并在在Run中增加如下代码
MailConfig mail=new MailConfig();
String popServer="server";
String popUser="zhaoyong";
String popPassword="1234";
//设置需要过滤的关键字:
发件人和邮件主题
String [] strFrom=new String[]
{
"zhaoyong"
};
String [] strSubject=new String[]
{
"测试"
};
MailConfig[] mc =new MailConfig []
{
mail
};
MailDialog md=new MailDialog(mc);
System.err.println("run run run ") ;
md.setSize(400, 335);
md.show();
|
以上的代码编译不会通过,但是别着急,慢慢来,很快了。
3.2构建邮件监控对话框
当然你需要建立一个MailConfig类用来表示一个邮箱的具体设置已及相关信息,这里就不在累述说明,详情参见参考资料中的代码。需要说明的式MailConfig除了要记录一个邮箱的地址,用户名和密码外,还提供2个关键字数组,如果为空,不加过滤,如果关键字有值,系统会根据发件人和邮件标题中是否包含关键字来进行显示邮件信息,已保证你的绝对自由。
首先我们需要实现一个MailConfig类,表示邮件配置,每个MailConfig的对象代表一个邮件帐户,我们的系统将能显示多个邮箱的配置,每个MailConfig中使用一个数组来保存需要过滤的收件人和邮件地址。
MailConfig类的中的变量如下:
String popServer;
String popUser;
String popPassword;
//设置需要过滤的关键字:
发件人和邮件主题
String [] strFrom;
String [] strSubject;
//是否显示邮件内容
boolean isViewContent=false;
|
同样,我们将使用一个对话框来显示邮件信息,MailDialog需要引用javaMail.jar,和activation.jar这两个类包,确保已经有这两个类包并加入到项目的类路径中。最后的MailDialog代码如下:
package myplugin;
import java.io.IOException;
import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.InternetAddress;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JTextPane;
/**
* @author zhaoyong
*
* TODO To change the template
for this generated type comment go to
* Window - Preferences - Java -
Code Style - Code Templates
*/
public class MailDialog extends JDialog
{
private JEditorPane jEditorPane = null;
private JTextPane jTextPane = null;
//可以显示多个邮件配置
MailConfig[] mc= null;
/**
* This method initializes
* 构造函数
* @param mc : 需要显示的多个邮箱配置对象。
*/
public MailDialog(MailConfig[] mc)
{
super();
if(mc!=null)
this.mc = mc;
else
System.err.println("邮件配置错误!") ;
initialize();
}
/**
* This method initializes this
* 初始化
* @return void
*/
private void initialize()
{
try
{
//设定显示内容的面板
this.setContentPane(getJTextPane());
//取得所有的新邮件信息
String s= getAllMailInfo();
//将信息显示在对话框中
this.jTextPane .setText(s);
this.setTitle("邮件信息");
this.setSize(251, 100);
}
catch (Exception e)
{
//发生错误显示错误信息
this.jTextPane .setText(e.toString());
e.printStackTrace();
}
}
/**取得所有的邮箱的需要监控的邮件信息
*
* @return String
*/
private String getAllMailInfo()
{
String allMailInfo="";
if (mc.length <1)
allMailInfo="没有配置邮箱!";
else
{
for(int i=0;i<mc.length;i++)
{
//循环获取每个邮箱的邮件信息
allMailInfo=allMailInfo+getMailInfo(mc[i]);
}
}
//还没有收到相关的邮件
if (allMailInfo.trim().length() ==0)
allMailInfo="未检测到相关新邮件!";
return allMailInfo;
}
/*
*得到一个邮箱中满足条件的所有新邮件的字符串形式
**/
private String getMailInfo(MailConfig mc)
{
//最终输出的邮件信息
String mailInfo="";
//每个邮箱服务器上的Store和Folder对象
Store store=null;
Folder folder=null;
try
{
Properties props = System.getProperties();
//与邮件服务器生成一个Session
Session session = Session.getDefaultInstance
( props,null);
//给出服务器,用户名,密码连接服务器
store = session.getStore("pop3");
store.connect(mc.getPopServer(), mc.getPopUser(),mc.getPopPassword());
//取得默认的邮件Folder
folder = store.getDefaultFolder();
if (folder == null)
throw new Exception("No default folder");
//取得收件箱
folder = folder.getFolder("INBOX");
if (folder == null)
throw new Exception("No POP3 INBOX");
//以只读方式打开收件箱
folder.open(Folder.READ_ONLY);
//获取所有新邮件并处理
Message[] msgs = folder.getMessages();
for (int i = 0; i < msgs.length; i++)
{
Message message= msgs[i];
//取得每封邮件的信息,
需要引用MailConfig对象进行关键字过滤
mailInfo = mailInfo+ getMessageInfo
( message,mc);
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
finally
{
//安全的关闭邮件服务器资源
try
{
if (folder!=null) folder.close(true);
if (store!=null) store.close();
}
catch (Exception ex2)
{ex2.printStackTrace();}
}
return mailInfo;
}
/**
* 得到一封邮件的信息,
需要根据MailConfig过滤
* @param mailInfo
* @param message
* @return 邮件信息
* @throws MessagingException
* @throws IOException
*/
private String getMessageInfo
( final Message message ,final MailConfig mc)
throws MessagingException,
IOException
{
//返回的改邮件信息
String mailInfo="";
String from=((InternetAddress)
message.getFrom()[0]).getPersonal();
if (from==null)
from=((InternetAddress)
message.getFrom()[0]).getAddress();
String subject=message.getSubject();
//如果满足过滤信息则显示,否则返回空
if(isElementinString(from,mc.getStrFrom())
||isElementinString
(subject,mc.getStrSubject()) )
{
mailInfo=mailInfo+"发件人 :
"+from+"\n";
mailInfo=mailInfo+"邮件主题 :
"+subject+"\n";
mailInfo=mailInfo+"发送时间 :
"+message.getSentDate() +"\n";
//如果显示内容,则打印内容
if(mc.isViewContent)
mailInfo=mailInfo+message.getContent()
+"\n";
mailInfo=mailInfo
+"------------------------------------\n";
}
return mailInfo;
}
private JTextPane getJTextPane()
{
if (jTextPane == null)
{
jTextPane = new JTextPane();
}
return jTextPane;
}
/**
* 判断目标关键字数组中是否有指定的字符串,
进行过滤
* @param targetStr :
* @param keys :
* @return 如果有,返回true,
否则返回false
*/
private boolean isElementinString
(String targetStr,String [] keys)
{
//没指定过滤条件,显示所有
if (keys==null)
return true;
//指定字符串为空,直接返回false
if (targetStr==null)
return false;
for(int i=0;i<keys.length ;i++)
{
if (targetStr.indexOf(keys[i])>-1)
return true;
}
return false;
}
}
// @jve:decl-index=0:visual-constraint="10,
10"--说明,这是Visual Editor添加的控制信息
|
以上代码的注释已经保证你能够看清楚,这里就不加累述,有兴趣的可以自己试试,体验一切尽在掌握的快感。当然这个例子做的实在简单,因此也为你的进一步开发留有足够的余地。
3.3 打包和发布
到此,在mypulgin中增加了邮件信息菜单和对话框,系统的plugin.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.0"?>
<plugin
id="myplugin"
name="Myplugin Plug-in"
version="1.0.0"
provider-name=""
class="myplugin.MypluginPlugin">
<runtime>
<library name="myplugin.jar">
<export name="*"/>
</library>
<library name="lib/javaMail.jar">
<export name="*"/>
</library>
<library name="lib/activation.jar">
<export name="*"/>
</library>
</runtime>
<requires>
<import plugin="org.eclipse.ui"/>
<import plugin="org.eclipse.core.runtime"/>
</requires>
<extension
point="org.eclipse.ui.actionSets">
<actionSet
label="Sample Action Set"
visible="true"
id="myplugin.actionSet">
<menu
label="我的空间"
id="sampleMenu">
<separator
name="sampleGroup">
</separator>
</menu>
<action
label="天气预报"
icon="icons/sample.gif"
class="myplugin.actions.SampleAction"
tooltip="Hello, Eclipse world"
menubarPath="sampleMenu/sampleGroup"
toolbarPath="sampleGroup"
id="myplugin.actions.SampleAction">
</action>
<action
label="邮件信息"
icon="icons/sample.gif"
class="myplugin.actions.MailAction"
tooltip="邮件信息"
menubarPath="sampleMenu/sampleGroup"
toolbarPath="sampleGroup"
id="myplugin.actions.MailAction">
</action>
</actionSet>
</extension>
</plugin>
|
实际上,我们在一个插件中加入了2个功能,因此就实现了我们的开发环境的自我扩展和定制。同样,参考Eclipse的帮助,你可以轻松的再次将插件打包导出,并将其加入自己的Eclipse 的plugins目录(可能需要解压缩),或通过help菜单的Update选项进行安装,注意导出时需要选定相关的类包。
重新启动,你将发现自己的IDE已经多了自己的菜单,开发环境已经随着自己的意愿在改变了,程序员天生的满足感油然而生。
现在,你可以在需要的时候点击菜单,了解你希望监控的邮件情况或者最近的天气情况,一切轻松的尽在掌握,Eclipse的插件,就是这样全能。
4、总结
Eclipse提供了一个纯的框架和插件结构,使得开发任何功能的插件都能成为现实。本文介绍了2个有趣的Eclipse插件的开发,可以使我们的工作环境增加了两个可爱的小功能,同时也使得你具备了基础的插件开发能力,借助Eclipse的强大功能,从此你可以把你的任何想法变为现实。
同时请保持与别人的交流,我会很乐意了解你的新奇的插件,并收藏和学习任何好的插件,打造一个完全属于自己的个性化的开发环境。Eclipse将在不久的将来成为一个全能的Platform,这一点在全世界数以万计的开发人员的手中,正一点一点变为现实。
(T117)