根據前兩篇博文,應該對插件開發有所瞭解。html
前文回顧:java
1 插件學習篇shell
3 利用擴展點,開發透視圖eclipse
以前學過Java的朋友,多少頁會一些關於Swing的東西。那麼這裏的SWT就是Eclipse插件所應用到的小部件開發框架。函數
裏面包含了大量的桌面控件,而且進行了一系列的優化整合,相對於Swing,極大的減小了內存的消耗。並且關於資源的釋放也須要開發者注意,須要特定的手動刪除,可是好比一個部件的子部件會隨着該部件的銷燬而銷燬。學習
下面看一下開發中經常使用的一些部件模型,這裏介紹的並不全,小控件其實有不少不少,這裏就簡單的介紹幾種:測試
這裏Widget是一個超類,全部的部件都繼承與這個類。它也提供了一些經常使用的方法,好比添加一些監聽,獲取經常使用的信息等等。優化
最經常使用的還要數Control了,由於不少Button Label控件都是繼承這個類,在開發中常常使用的方法就是this
addMouseListener()進行鼠標點擊的監聽
setBounds 進行控件的從新繪製
等等。具體的函數,你們能夠經過開發多留意一下,就好了。
Eclipse插件開發的程序大多有個不成文的規定,一個程序活動期間,只能有一個Dispaly對象,可是能夠有多個Shell對象。那麼,什麼是Dispaly,什麼又是Shell呢。
這裏紅色箭頭顯示的就是一個Display,也就是一個底層的應用實例。若是這個實例沒有被銷燬,而程序意外中止了,那麼是不能從新運行的。也就是說,運行期間,一個應用程序,只能有一個Display。就像顯示器與窗口內的內容,只有一個顯示器,可是顯示器內部能夠顯示多個文件內容。
綠色箭頭對應的就是Shell,一個Shell至關於一個活動的窗口,能夠在裏面添加各類小部件,組成一個豐富的應用界面。
綜上,一個Display能夠有多個Shell,可是隻有一個Display(適用於普通狀況).!
接下來介紹一下如何不啓動一個Eclipse 插件工程,來開發SWT。這個過程不少教材上都有描述,所以這裏只提供了上面例子所對應的代碼。
要注意的是,最後要釋放資源,Shell是掛載到Dispaly上面(原諒我用掛載這個詞,Linux裏面掛載比較生動),所以銷燬Display的時候,能夠自動的銷燬Shell對象。可是Color並非經過掛載方式建立的,所以要獨立的釋放。
1 package com.xingoo.plugin.swttest; 2 3 import javax.swing.Scrollable; 4 import javax.swing.text.StyleConstants.ColorConstants; 5 6 import org.eclipse.swt.SWT; 7 import org.eclipse.swt.graphics.Color; 8 import org.eclipse.swt.layout.FillLayout; 9 import org.eclipse.swt.widgets.Display; 10 import org.eclipse.swt.widgets.Label; 11 import org.eclipse.swt.widgets.Shell; 12 import org.eclipse.swt.widgets.Text; 13 14 public class mainTestExample { 15 public static void main(String[] args) { 16 Display display = new Display(); 17 Color color = new Color(display,255,0,0); 18 19 //create a shell 20 Shell shell_1 = new Shell(display); 21 shell_1.setText("This is a shell in main function()"); 22 shell_1.setBounds(100,100,400,200); 23 shell_1.setLayout(new FillLayout()); 24 25 Label label_1 = new Label(shell_1,SWT.CENTER); 26 label_1.setText("this is the text of a label"); 27 label_1.setForeground(color); 28 29 shell_1.open(); 30 Text test; 31 //create another shell 32 Shell shell_2 = new Shell(display); 33 shell_2.setText("This is a shell1 in main function()"); 34 shell_2.setBounds(250,250,400,200); 35 shell_2.setLayout(new FillLayout()); 36 37 Label label_2 = new Label(shell_2,SWT.CENTER); 38 label_2.setText("this is the text of a label1"); 39 label_2.setForeground(color); 40 41 shell_2.open(); 42 43 while(!shell_1.isDisposed() || !shell_2.isDisposed()){ 44 if(!display.readAndDispatch()) 45 display.sleep(); 46 } 47 48 //dispose the resource 49 display.beep(); 50 color.dispose(); 51 display.dispose(); 52 } 53 }
這個函數代碼在通常 工程 裏面就能夠運行,可是缺乏一個Jar包,swt的jar包,這個jar包在Eclipse的plugins文件夾下就能夠找到。能夠經過引入的方式,引入到工程中。
其實只須要swtx86這個jar包就能夠了,source是源代碼,可讓我跟蹤調試swt的源碼。
爲了後面的測試使用,這裏能夠把這段代碼進行提取。這樣以後的main函數的類只要繼承這個AbstractExample就能夠進行窗口的編輯了。
1 package com.xingoo.plugin.swttest; 2 3 import org.eclipse.swt.SWT; 4 import org.eclipse.swt.layout.FillLayout; 5 import org.eclipse.swt.widgets.Display; 6 import org.eclipse.swt.widgets.Label; 7 import org.eclipse.swt.widgets.Shell; 8 9 abstract class AbstractExample{ 10 public void run(){ 11 Display display = new Display(); 12 Shell shell = new Shell(display); 13 shell.setText("shell example"); 14 shell.setBounds(100,100,400,200); 15 shell.setLayout(new FillLayout()); 16 todo(shell); 17 shell.open(); 18 19 while(!shell.isDisposed()){ 20 if(!display.readAndDispatch()) 21 display.sleep(); 22 } 23 //dispose the resource 24 display.beep(); 25 display.dispose(); 26 } 27 public abstract void todo(Shell shell);//extension something here 28 } 29 30 public class mainTestExample extends AbstractExample{ 31 public static void main(String[] args) { 32 new mainTestExample().run(); 33 } 34 35 public void todo(Shell shell) { 36 //...add something you like 37 Label label_1 = new Label(shell,SWT.CENTER); 38 label_1.setText("this is the text of a label"); 39 } 40 }