![]() |
![]() |
<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; //從輸入流不斷的讀數(shù)據(jù),直到讀完為止 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(); } |
![]() |
如果你在上?;蛘?a target=_blank>其他城市,試著修改city參數(shù)為"上海",再次運行,你將發(fā)現(xiàn),你仍然能夠得到該城市的天氣預報(這里我們從網站上提取的信息,有點投機取巧了)。值得注意的是,Xmethod網站提供了一個天氣預報的WebService,可惜只有美國的城市,不然我們可以使用Web Service調用獲取天氣預報,將會更酷。
現(xiàn)在運行是工作臺已經具備了天氣預報的功能,還需要更進一步,將改插件導出發(fā)布,拷貝到Eclipse根目錄的plugins目錄中,重新啟動(具體參見Eclipse幫助)。現(xiàn)在你自己的Eclipse,就具備了天氣預報的功能,只要你點擊鼠標,就可以在編程之余輕松的獲取天氣信息。 除非你的老板認為你在工作時間隨時了解天氣情況不是一個好主意,我認為你完全可以將這個插件納入個人收藏的插件之列。你也可以在此基礎上擴展,增加一些配置文件和屬性設置,定制出滿足自己要求的插件。如果能夠增加信息的自動過濾和篩選,那將是一次很愉快的體驗,如果你有時間和興趣,不妨一試。
3.郵件快速監(jiān)控插件
現(xiàn)在你的工作因為Eclipse而更加愜意,更具創(chuàng)造力,那么你還有什么不滿?你是否厭倦了各種郵件客戶端隨時隨地的騷擾你呢?你希望你在高興的時候適時的了解一下郵件的概況?好了,既然想到了為什么猶豫呢,因為你是程序員,你就是要用Eclipse享受完全DIY的樂趣。
3.1生成插件
本部分我們將在以上myplugin插件的基礎上增加一個郵件過濾顯示的對話框,類似的我們通過VisualEditer創(chuàng)建一個名為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"; //設置需要過濾的關鍵字:發(fā)件人和郵件主題 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; //設置需要過濾的關鍵字:發(fā)件人和郵件主題 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 * 構造函數(shù) * @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) { //發(fā)生錯誤顯示錯誤信息 this.jTextPane .setText(e.toString()); e.printStackTrace(); } } /**取得所有的郵箱的需要監(jiān)控的郵件信息 * * @return String */ private String getAllMailInfo() { String allMailInfo=""; if (mc.length <1) allMailInfo="沒有配置郵箱!"; else { for(int i=0;i<mc.length;i++) { //循環(huán)獲取每個郵箱的郵件信息 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; } /** * 得到一封郵件的信息,需要根據(jù)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+"發(fā)件人 : "+from+"\n"; mailInfo=mailInfo+"郵件主題 : "+subject+"\n"; mailInfo=mailInfo+"發(fā)送時間 : "+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; } /** * 判斷目標關鍵字數(shù)組中是否有指定的字符串,進行過濾 * @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> |