![]() |
![]() |
<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> |
![]() |
![]() |
![]() |
public void run(IAction action) { MessageDialog.openInformation( window.getShell(),"Myplugin Plug-in", "Hello, Eclipse world"); } |
public void run(IAction action) { WeatherDialog wd=new WeatherDialog(); wd.setSize(400, 335); wd.show(); } |
![]() |
![]() |
/* * Created on 2004-9-23 * */ package myplugin; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; import javax.swing.JDialog; import javax.swing.JEditorPane; /** * <p>Title: WatherDialog</p> * <p>Description: 这个是对话框类,用于显示指定城市的当天的天气预报</p> * <p>Copyright: Copyright (c) 2004</p> * <p>Company:UF SOFT</p> * @author 赵勇 * @version 1.0 */ public class WatherDialog extends JDialog { String city="北京"; private JEditorPane jEditorPane = null; /** * This method initializes * / public WatherDialog(String city) { super(); this.city=city; initialize(); } /** * This method initializes this * @return void */ private void initialize() { this.setContentPane(getJEditorPane()); try { //构建URL对象 URL url =new URL("http://weather.news.sina.com.cn//cgi-bin/figureWeather/simpleSearch.cgi?city="+city); String temp=""; BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); //使用openStream得到一输入流并由此构造一个BufferedReader对象 String inputLine; //从输入流不断的读数据,直到读完为止 while ((inputLine = in.readLine()) != null) temp=temp+inputLine+"\n"; //关闭输入流 in.close(); String weather =temp.substring ( temp.indexOf( "<body"), temp.lastIndexOf( "body>")+5); this.jEditorPane .setText(weather); } catch (Exception e) { e.printStackTrace(); } this.setTitle("天气预报"); this.setSize(400, 166); } /** * This method initializes jEditorPane * * @return javax.swing.JEditorPane */ private JEditorPane getJEditorPane() { if (jEditorPane == null) { jEditorPane = new JEditorPane(); jEditorPane.setContentType( "text/html"); } return jEditorPane; } } // @jve:decl-index=0:visual-constraint="70,19" |
public void run(IAction action) { WeatherDialog wd=new WeatherDialog("北京"); wd.setSize(400, 335); wd.show(); } |
![]() |
如果你在上海或者其他城市,试着修改city参数为"上海",再次运行,你将发现,你仍然能够得到该城市的天气预报(这里我们从网站上提取的信息,有点投机取巧了)。值得注意的是,Xmethod网站提供了一个天气预报的WebService,可惜只有美国的城市,不然我们可以使用Web Service调用获取天气预报,将会更酷。
现在运行是工作台已经具备了天气预报的功能,还需要更进一步,将改插件导出发布,拷贝到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> |
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(); |
String popServer; String popUser; String popPassword; //设置需要过滤的关键字:发件人和邮件主题 String [] strFrom; String [] strSubject; //是否显示邮件内容 boolean isViewContent=false; |
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添加的控制信息 |
<?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> |