Eclipse插件开发(一)Hello World

 
 
     eclipse之所以如此强大,想必我们都看中了它众多的插件(plugin)功能吧,eclipse提供给我们的workspace本身就是它的一个插件。

         eclipse为我们开发插件提供了非常便利的接口,虽然我们可以使用诸如myeclipse的插件对spring,struts,hibernate等框架进行非常简捷的操作,但是我们有些时候也需要对自己或公司的一些框架和技术集成到eclipse中来简化和加快我们的开发。所以开发自己的eclipse插件也是要了解的。

         20秒完成你的第一个插件HelloWorld插件,以下所有操作和代码都基于eclipse 3.2         

          file->new->other->Plug-in Project新建一个插件项目,一直next到下图

   选择Hello,World完成,eclipse会生成插件代码。

 

  如上图所示,运行此项目run as->eclipse application 

eclipse会启动一个platform来显示此工作空间下的插件项目效果,这个插件非常简单只是提示hello world

实现代码在SimpleAction.java

 public void run(IAction action) {
  MessageDialog.openInformation(
   window.getShell(),
   "Sss Plug-in",
   "Hello, Eclipse world");
 }

eclipse图标即为插件的图标,修改run方法即可实现简单的其他插件的操作,

定义WeatherDia类

import java.awt.Frame;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;


import javax.swing.JDialog;
import javax.swing.JEditorPane;

 

public class WeatherDia extends JDialog {
      /**
  *
  */
 private static final long serialVersionUID = 1L;
 private JEditorPane editorpane;
      
       public WeatherDia(String title,boolean model)
       {
        super(new Frame(),title,model);
        init();
       }
       public void init()
       {
     
        this.setSize(800,400);
        this.setContentPane(getJEditorPane());
        try
           {
               //构建URL获得天气预报dc166.htm为成都的天气预报,查询其他城市可自己研究QQ的天气预报页面
               URL url  =new URL("http://weather.news.qq.com/inc/07_dc166.htm");
               String temp="";
               BufferedReader in
                = new BufferedReader(new InputStreamReader(url.openStream()));
               String inputLine;
               while ((inputLine = in.readLine()) != null)
                if(inputLine.indexOf("<!--")==-1)
                   temp=temp+inputLine+"/n";
               in.close(); 
              
               String  weather 
                 =temp.substring ( temp.indexOf( "<div"),
                                     temp.lastIndexOf( "</div>"));
               this.editorpane.setText(weather);
              
           }
           catch (Exception e)
           {
               e.printStackTrace();
           }

       
       }
      
       public JEditorPane getJEditorPane()
       {
        if(editorpane == null)
        {
         editorpane = new JEditorPane();
         editorpane.setContentType("text/html");
        }
        return editorpane;
       }
 
 
}

然后修改run方法为

public void run(IAction action) {
  
  WeatherDia wd = new WeatherDia("天气预报-成都",true);
  wd.setLocation(200,100);
  
  wd.setVisible(true);
 }

这样天气预报插件的开发就基本完毕了

修改plugin.xml文件

   <extension
         point="org.eclipse.ui.actionSets">
      <actionSet
            label="Sample Action Set"
            visible="true"
            id="Test1.actionSet">
         <menu
               label="weather"
               id="sampleMenu">
            <separator
                  name="sampleGroup">
            </separator>
         </menu>
         <action
               label="weather"
               icon="icons/sample.gif"
               class="test1.actions.SampleAction"
               tooltip="成都天气"
               menubarPath="sampleMenu/sampleGroup"
               toolbarPath="sampleGroup"
               id="test1.actions.SampleAction">
         </action>
      </actionSet>
   </extension>

在次运行eclipse application看看效果,天气预报插件已经可以运行了

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值