準(zhǔn)備(使用Visual Editor開(kāi)發(fā))
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文件夾下的文件分別復(fù)制到C:\eclipse\plugins和C:\eclipse\features文件夾中。
開(kāi)始開(kāi)發(fā):
新建一個(gè)Java Project名字為myplugin,在myplugin.actions中新建一個(gè)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類的代碼如下(新建一個(gè)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>天氣預(yù)報(bào):北京</h2>" + line + "</body></html>";
- br.close();
- httpconn.disconnect();
- this.jEditorPane .setText(line);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- this.setTitle("天氣預(yù)報(bào)");
- 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配置文件的內(nèi)容為:
- <?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="天氣預(yù)報(bào)"
- icon="icons/sample.gif"
- class="myplugin.actions.WeatherAction"
- tooltip="Hello, Eclipse world"
- menubarPath="sampleMenu/sampleGroup"
- toolbarPath="sampleGroup"
- id="myplugin.actions.WeatherAction">
- </action>
- <action
- label="北京時(shí)間"
- icon="icons/sample.gif"
- class="myplugin.actions.BJTimeAction"
- tooltip="Hello, Eclipse world"
- menubarPath="sampleMenu/sampleGroup"
- toolbarPath="sampleGroup"
- id="myplugin.actions.BJTimeAction">
- </action>
- <action
- label="大中國(guó)"
- 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>
如下面的圖片所示:
從程序中可以看到,這個(gè)天氣預(yù)報(bào)是從http://tq.8684.cn/beijing_beijing獲取的,每天更新。
插件部署:
點(diǎn)擊File,選擇export,選擇Plug-in Development下面的Deployable plug-ins and fragments,點(diǎn)擊next,選擇一個(gè)輸出目錄即可。將這個(gè)jar包復(fù)制到C:\eclipse\plugins,重啟eclipse即可看到自己的eclipse插件。
dm520