Eclipse插件开发

准备(使用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 类,代码如下:

 

package myplugin.actions;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;

public class WeatherAction implements IWorkbenchWindowActionDelegate 
{
	public WeatherAction() 
	{
		
	}

	public void run(IAction action) 
	{
		WeatherDialog wd = new WeatherDialog();
		wd.setSize(800, 520);
		wd.show();
	}

	public void selectionChanged(IAction action, ISelection selection) 
	{
		
	}

	public void dispose() 
	{
		
	}
	
	public void init(IWorkbenchWindow window) 
	{
		
	}
}

 

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

package myplugin.actions;

import java.awt.Frame;
import java.awt.Color;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

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

public class WeatherDialog extends JDialog 
{
	private static final long serialVersionUID = 1L;
	private JEditorPane jEditorPane = null;

	public WeatherDialog()
	{
		super();
		initialize();
	}
	
	public WeatherDialog(Frame owner)
	{
		super(owner);
		initialize();
	}

	private void initialize() 
	{
		this.setContentPane(getJEditorPane());
		
		String line = "";
		
		URL url = null;
		URLConnection conn = null;
		
        try
        {
            url = new URL("http://tq.8684.cn/beijing_beijing");
           
            conn = url.openConnection(); 
			
            HttpURLConnection httpconn =(HttpURLConnection)conn;
			
			if(httpconn.getResponseCode() != HttpURLConnection.HTTP_OK) 
				return;
            
            BufferedReader br = new BufferedReader(new InputStreamReader(httpconn.getInputStream()));
            
            while(br.ready())
            {
            	line = br.readLine();
            	if(line.indexOf("北京 北京天气") >= 0)
            		break;
            }
            
            br.readLine();
            line = br.readLine();
            
            line = line.replaceAll("bgcolor=\"#6699cc\"", "bgcolor=\"#FF0000\"");
            line = "<html><body text=\"#0000FF\"><h2>天气预报:北京</h2>" + line + "</body></html>";
            
            br.close(); 
            httpconn.disconnect();
            
            this.jEditorPane .setText(line);
        } 
        catch (Exception e)
        {
            e.printStackTrace();
        } 
        
        this.setTitle("天气预报");
        this.setSize(400, 166);
	}

	private JEditorPane getJEditorPane() 
	{
		if (jEditorPane == null) 
		{
			jEditorPane = new JEditorPane();
			jEditorPane.setBackground(Color.BLUE);
			jEditorPane.setContentType( "text/html");
			jEditorPane.setEnabled(false);
			jEditorPane.setEditable(false);
		}
		
		return jEditorPane;
	}

} 

 

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

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.2"?>
<plugin>

   <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.WeatherAction"
               tooltip="Hello, Eclipse world"
               menubarPath="sampleMenu/sampleGroup"
               toolbarPath="sampleGroup"
               id="myplugin.actions.WeatherAction">
         </action>
          <action
               label="北京时间"
               icon="icons/sample.gif"
               class="myplugin.actions.BJTimeAction"
               tooltip="Hello, Eclipse world"
               menubarPath="sampleMenu/sampleGroup"
               toolbarPath="sampleGroup"
               id="myplugin.actions.BJTimeAction">
         </action>
         <action
               label="大中国"
               icon="icons/sample.gif"
               class="myplugin.actions.SampleAction"
               tooltip="Hello, Eclipse world"
               menubarPath="sampleMenu/sampleGroup"
               toolbarPath="sampleGroup"
               id="myplugin.actions.SampleAction">
         </action>
      </actionSet>
   </extension>

</plugin>

 

如下面的图片所示:

 

 

 

 

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

 

插件部署:

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

 

你可能感兴趣的:(java,eclipse,UI,.net,swing)