准备(使用Visual Editor开发)

1.下载eclipse-SDK-3.2-win32.zip,解压到C:\eclipse

2.下载emf-sdo-runtime-2.2.0.zip、GEF-runtime-3.2.zip、VE-runtime-1.2.1.zip、VE-SDK-1.2.1.zip,分别解压,将解压后的plugins和features文件夹下的文件分别复制到C:\eclipse\plugins和C:\eclipse\features文件夹中。

 

开始开发:

新建一个Java Project名字为myplugin,在myplugin.actions中新建一个WeatherAction 类,代码如下:

 

Java代码 
  1. package myplugin.actions;  
  2.   
  3. import org.eclipse.jface.action.IAction;  
  4. import org.eclipse.jface.viewers.ISelection;  
  5. import org.eclipse.ui.IWorkbenchWindow;  
  6. import org.eclipse.ui.IWorkbenchWindowActionDelegate;  
  7.   
  8. public class WeatherAction implements IWorkbenchWindowActionDelegate   
  9. {  
  10.     public WeatherAction()   
  11.     {  
  12.           
  13.     }  
  14.   
  15.     public void run(IAction action)   
  16.     {  
  17.         WeatherDialog wd = new WeatherDialog();  
  18.         wd.setSize(800520);  
  19.         wd.show();  
  20.     }  
  21.   
  22.     public void selectionChanged(IAction action, ISelection selection)   
  23.     {  
  24.           
  25.     }  
  26.   
  27.     public void dispose()   
  28.     {  
  29.           
  30.     }  
  31.       
  32.     public void init(IWorkbenchWindow window)   
  33.     {  
  34.           
  35.     }  
  36. }  

 

其中WeatherDialog类的代码如下(新建一个Visual Editor类):

Java代码 
  1. package myplugin.actions;  
  2.   
  3. import java.awt.Frame;  
  4. import java.awt.Color;  
  5. import java.io.BufferedReader;  
  6. import java.io.InputStreamReader;  
  7. import java.net.HttpURLConnection;  
  8. import java.net.URL;  
  9. import java.net.URLConnection;  
  10.   
  11. import javax.swing.JDialog;  
  12. import javax.swing.JEditorPane;  
  13.   
  14. public class WeatherDialog extends JDialog   
  15. {  
  16.     private static final long serialVersionUID = 1L;  
  17.     private JEditorPane jEditorPane = null;  
  18.   
  19.     public WeatherDialog()  
  20.     {  
  21.         super();  
  22.         initialize();  
  23.     }  
  24.       
  25.     public WeatherDialog(Frame owner)  
  26.     {  
  27.         super(owner);  
  28.         initialize();  
  29.     }  
  30.   
  31.     private void initialize()   
  32.     {  
  33.         this.setContentPane(getJEditorPane());  
  34.           
  35.         String line = "";  
  36.           
  37.         URL url = null;  
  38.         URLConnection conn = null;  
  39.           
  40.         try  
  41.         {  
  42.             url = new URL("http://tq.8684.cn/beijing_beijing");  
  43.              
  44.             conn = url.openConnection();   
  45.               
  46.             HttpURLConnection httpconn =(HttpURLConnection)conn;  
  47.               
  48.             if(httpconn.getResponseCode() != HttpURLConnection.HTTP_OK)   
  49.                 return;  
  50.               
  51.             BufferedReader br = new BufferedReader(new InputStreamReader(httpconn.getInputStream()));  
  52.               
  53.             while(br.ready())  
  54.             {  
  55.                 line = br.readLine();  
  56.                 if(line.indexOf("北京 北京天气") >= 0)  
  57.                     break;  
  58.             }  
  59.               
  60.             br.readLine();  
  61.             line = br.readLine();  
  62.               
  63.             line = line.replaceAll("bgcolor=\"#6699cc\"""bgcolor=\"#FF0000\"");  
  64.             line = "<html><body text=\"#0000FF\"><h2>天气预报:北京</h2>" + line + "</body></html>";  
  65.               
  66.             br.close();   
  67.             httpconn.disconnect();  
  68.               
  69.             this.jEditorPane .setText(line);  
  70.         }   
  71.         catch (Exception e)  
  72.         {  
  73.             e.printStackTrace();  
  74.         }   
  75.           
  76.         this.setTitle("天气预报");  
  77.         this.setSize(400166);  
  78.     }  
  79.   
  80.     private JEditorPane getJEditorPane()   
  81.     {  
  82.         if (jEditorPane == null)   
  83.         {  
  84.             jEditorPane = new JEditorPane();  
  85.             jEditorPane.setBackground(Color.BLUE);  
  86.             jEditorPane.setContentType( "text/html");  
  87.             jEditorPane.setEnabled(false);  
  88.             jEditorPane.setEditable(false);  
  89.         }  
  90.           
  91.         return jEditorPane;  
  92.     }  
  93.   
  94. }   

 

plugin.xml配置文件的内容为:

Xml代码 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <?eclipse version="3.2"?>  
  3. <plugin>  
  4.   
  5.    <extension  
  6.          point="org.eclipse.ui.actionSets">  
  7.       <actionSet  
  8.             label="Sample Action Set"  
  9.             visible="true"  
  10.             id="myplugin.actionSet">  
  11.          <menu  
  12.                label="北京欢迎你"  
  13.                id="sampleMenu">  
  14.             <separator  
  15.                   name="sampleGroup">  
  16.             </separator>  
  17.          </menu>  
  18.          <action  
  19.                label="天气预报"  
  20.                icon="icons/sample.gif"  
  21.                class="myplugin.actions.WeatherAction"  
  22.                tooltip="Hello, Eclipse world"  
  23.                menubarPath="sampleMenu/sampleGroup"  
  24.                toolbarPath="sampleGroup"  
  25.                id="myplugin.actions.WeatherAction">  
  26.          </action>  
  27.           <action  
  28.                label="北京时间"  
  29.                icon="icons/sample.gif"  
  30.                class="myplugin.actions.BJTimeAction"  
  31.                tooltip="Hello, Eclipse world"  
  32.                menubarPath="sampleMenu/sampleGroup"  
  33.                toolbarPath="sampleGroup"  
  34.                id="myplugin.actions.BJTimeAction">  
  35.          </action>  
  36.          <action  
  37.                label="大中国"  
  38.                icon="icons/sample.gif"  
  39.                class="myplugin.actions.SampleAction"  
  40.                tooltip="Hello, Eclipse world"  
  41.                menubarPath="sampleMenu/sampleGroup"  
  42.                toolbarPath="sampleGroup"  
  43.                id="myplugin.actions.SampleAction">  
  44.          </action>  
  45.       </actionSet>  
  46.    </extension>  
  47.   
  48. </plugin>  

 

如下面的图片所示:

 

 

 

 

从程序中可以看到,这个天气预报是从http://tq.8684.cn/beijing_beijing获取的,每天更新。

 

插件部署:

点击File,选择export,选择Plug-in Development下面的Deployable plug-ins and fragments,点击next,选择一个输出目录即可。将这个jar包复制到C:\eclipse\plugins,重启eclipse即可看到自己的eclipse插件。



dm520