Eclipse開發外掛程式(二)

來源:互聯網
上載者:User

標籤:ip   eclipse   開發   c   外掛程式   ps   li   e   cli   

視圖之間實現事件監聽

兩個視圖中的組件之間的互動,在開發外掛程式的時候是經常碰到的問題.點擊視圖1列表的某項時,視圖2的文字框顯示相應的字元.

主動式:d

主動式就是在視圖1的代碼塊中擷取對視圖2的對象的引用.然後將視圖1中的對象主動的傳給視圖2.

修改View1.java和View2.java

Eclipse通過plugin.xml來載入外掛程式和外掛程式中的擴充點(如視圖擴充點),所以可以在View1.java中由id標識來取得視圖2對象.

View1.java

 1 public class View1 extends ViewPart { 2     private List list; // 將列表寫成類的執行個體變數,以擴大它的可存取範圍 3     //注意這個List並不是java.util包下的.而是org.eclipse.swt.widgets.List;包下的. 4     public void createPartControl(Composite parent) { 5         IWorkbenchHelpSystem help = PlatformUI.getWorkbench().getHelpSystem(); 6         help.setHelp(parent, "cn.com.kxh.myplugin.buttonHelpId"); 7         Composite topComp = new Composite(parent, SWT.NONE); 8         topComp.setLayout(new FillLayout()); 9         list = new List(topComp, SWT.BORDER);10         list.add("中國");11         list.add("美國");12         list.add("法國");13         // 列表選擇事件監聽14         list.addSelectionListener(new SelectionAdapter() {15             public void widgetSelected(SelectionEvent e) {16                 // 由IWorkbenchPage獲得view2對象17                 IWorkbenchPage wbp = getViewSite().getPage();18                 //在外掛程式中IWorkbenchPage對象比較重要,這裡再給出一種獲得此對象的通用的方法.19                 // Activator.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage();20                 IViewPart view2 = wbp.findView("cn.com.kxh.myplugin.View2");21                 //這個地方的參數是"視圖2"在plugin.xml中的id標識.由此可見plugin.xml檔案在外掛程式中的地位是極其重要的.22                 // 將當前選擇的清單項目顯示在文字框中23                 Text text = ((View2) view2).getText();24                 text.setText(list.getSelection()[0]);25             }26         });27     }28     @Override29     public void setFocus() {}30 }

View2.java (將View2.java的文字框對象改成類的執行個體變數,並編寫相應的Setter和Getter方法)

 1 public class View2 extends ViewPart { 2     private Text text; 3     public void createPartControl(Composite parent) { 4         Composite topComp = new Composite(parent,SWT.NONE); 5         topComp.setLayout(new FillLayout()); 6         text = new Text(topComp,SWT.BORDER); 7         text.setText("我是Text框"); 8     } 9     public void setFocus() {}10     11     public Text getText() {12         return text;13     }14     public void setText(Text text) {15         this.text = text;16     }17 }

SamplePerspective.java(和我的上一篇部落格上沒有做任何修改)

 1 public class SamplePerspective implements IPerspectiveFactory { 2     // 參數IPageLayout是用於透視圖的布局管理器 3     public void createInitialLayout(IPageLayout layout) { 4         // 得到本透視圖的編輯空間標識 5         String editorArea = layout.getEditorArea(); 6         // 在透視圖左部建立一個空間,並將“視圖1”放入其中。 7         // "left"是此空間的標識;IPageLayout.LEFT指出此空間在透視圖布局中的位置靠左; 8         // 0.2f 指此空間佔用透視圖20%的寬度;editorArea 指使用透視圖的編輯空間 9         IFolderLayout left = layout.createFolder("left", IPageLayout.LEFT, 0.2f, editorArea);10         left.addView("cn.com.kxh.myplugin.View1"); // 參數為plugin.xml中“視圖1”的id標識11         // 將“視圖2”加入到透視圖的底部12         IFolderLayout bottom = layout.createFolder("bottom", IPageLayout.BOTTOM, 0.8f, editorArea);13         bottom.addView(View2.class.getName());// 由於我們把視圖的id取成和類全名一樣,所以也可以用這種寫法14         // 將以前定義的actionset(主菜單、工具列按鈕)加入到本透視圖。這要在plugin.xml文15         // 件的action設定中將visible="false"才看得出效果,這時開啟其他透視圖,action設定的16         // 主菜單、工具列按鈕將不會出現在介面上,只有開啟本透視圖才會出現。17         layout.addActionSet("myplugin.actionSet");// 參數為actionSet在plugin.xml中的id標識18     }19 }

總結:

(1)在外掛程式中IWorkbenchPage對象比較重要,這裡再給出一種獲得此對象的通用方法,不過他是獲得當前活動的IWorkbenchPage對象.

Activator.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivepage();

(2)IWorkbenchPage.findView("cn.com.kxh.myplugin.View2")中的參數為"視圖2"在plugin.xml中設定的id標識.

由此可見.plugin.xml檔案在外掛程式中的地位是及其重要的.IWorkbenchPage處理findView方法之外.還用findEditor方法來得到編輯器對象.像"cn.com.kxh.myplugin.View2"這種標識符在系統開發中會經常用到,最好建立一個類來集中放置這些字串常量.然後系統中用的時候只用其常量名即可,否則把標識符的字串分散在代碼中,以後改起來會非常麻煩.常量類的範例程式碼如下:

public final class StringConstants{    public final static String VIEW1="cn.com.kxh.myplugin.View1";    public final static String VIEW2=View2.class.getName();}

要用的時候時候直接類名點調用就可以了.

附上上面代碼的運行結果.

 

Eclipse開發外掛程式(二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.