Posted on 2005-11-29 19:14
nemo 閱讀(10743)
評(píng)論(12) 編輯 收藏 所屬分類(lèi):
EclipseRCP/SWT/JFACE
往eclipse的默認(rèn)NavigationView中,添加右鍵菜單項(xiàng)是非常容易的,甚至不需要自己寫(xiě)代碼,只要在plugin.xml中添加擴(kuò)展項(xiàng)便可以了。
但是這遠(yuǎn)遠(yuǎn)達(dá)不到我們的要求。如果要我們?cè)谧约旱膔cp程序的View中添加右鍵彈出菜單就不是很容易了。現(xiàn)在我介紹一下如何在一個(gè)樹(shù)視圖中針對(duì)不同的TreeObject添加不同的右鍵彈出菜單的方法。
如果不清楚如何構(gòu)建一個(gè)樹(shù),請(qǐng)參考http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/CatalogSWT-JFace-Eclipse.htm, 這里有很多關(guān)于eclipse Tree的實(shí)例。簡(jiǎn)單將來(lái),Eclipse中創(chuàng)建樹(shù)有兩種,一種是使用SWT提供的樹(shù),依照一定的格式給其提供數(shù)據(jù),我自己只用這種方法創(chuàng)建一些簡(jiǎn)單的、靜態(tài)的樹(shù)。另一種方法,針對(duì)于RCP程序,可以使用JFace提供的TreeViewer類(lèi),非常靈活并且易于擴(kuò)展。使用TreeViewer需要實(shí)現(xiàn)ITreeContentProvider接口和ITreeLableProvider接口。由于我的時(shí)間有限,不能向大家提供具體的實(shí)現(xiàn)方法,請(qǐng)?jiān)彙?梢詤⒖家韵挛恼拢?/FONT>http://eclipse.org/articles/treeviewer-cg/TreeViewerArticle.htm
在大體了解了如何構(gòu)造一個(gè)樹(shù)之后,我們下面就來(lái)介紹右鍵彈出菜單context menu的實(shí)現(xiàn)方法。
一個(gè)TreeViewer 必須繼承ViewPart方法,并且重寫(xiě)其中的createPartControl(Composite parent)類(lèi)。
1 public void createPartControl(Composite parent) {
2
3 /* Set the imput data and image model of the tree viewer,
4 * and layout the viewer on the left part of the composite.
5 */
6 viewer = new TreeViewer(parent);
7 viewer.setContentProvider(new ViewContentProvider());
8 viewer.setLabelProvider(new ViewLabelProvider());
9 viewer.setInput(createDummyModel());
10
11 createActions();
12 createMenus();
13 createContextMenu(parent);
14
15 //handles the actions related with treeviewer.
16 makeActions(parent);
17 }
其中,第7到9行為設(shè)置TreeViewer的內(nèi)容提供器、圖像提供器、以及初始化時(shí)的輸入數(shù)據(jù)。第13行為創(chuàng)建右鍵彈出菜單。
1 private void createContextMenu(Composite parent) {
2
3 MenuManager mgr = new MenuManager();
4 mgr.setRemoveAllWhenShown(true);
5 mgr.addMenuListener(new IMenuListener() {
6 public void menuAboutToShow(IMenuManager manager) {
7 fillContextMenu(manager);
8 }
9 });
10 Menu menu = mgr.createContextMenu(viewer.getControl());
11 viewer.getControl().setMenu(menu);
12 getSite().registerContextMenu(mgr, viewer);
13 }
第4行代碼中,setRemoveAllWhenShown(true)的作用是清空以前顯示的菜單項(xiàng),當(dāng)觸發(fā)了menu事件時(shí),重新填充(fillContextMenu),所以如果不把removeAllWhenShow置為true的話,每點(diǎn)一下右鍵你就會(huì)看到菜單項(xiàng)多出一倍來(lái)。Menu是swt的控件,而Menumanager是Jface中的控件,不象SWT那樣直接同底層打交道。用MenuManager可以創(chuàng)建出一個(gè)Menu對(duì)象。第十行mgr.createContextMenu(viewer.getControl());這里的viewer即為上面的viewer。第十一行,然后我們將樹(shù)的setMenu方法將樹(shù)控件與Menu控件聯(lián)系在一起就好了。
最重要的是第十二行,這是context menu能不能顯示的關(guān)鍵。一個(gè)視圖中可以有多個(gè)context menu, 而每一個(gè)context menu都必須注冊(cè)給workbench。這需要通過(guò)調(diào)用org.eclipse.ui.IWorkbenchPartSite.registerContextMenu(MenuManager menuManager, ISelectionProvider selectionProvider)或者(當(dāng)有多個(gè)注冊(cè)的context menu時(shí))org.eclipse.ui.IWorkbenchPartSite.registerContextMenu(String menuId, MenuManager menuManager, ISelectionProvider selectionProvider) 增加的參數(shù)menuId用于區(qū)分不同的context menu。
接下來(lái),我們有兩種方法來(lái)創(chuàng)建右鍵彈出菜單項(xiàng)。第一種,實(shí)現(xiàn)fillContextMenu()方法。典型的實(shí)現(xiàn)代碼如下:
1 protected void fillContextMenu(IMenuManager manager) {
2
3 manager.add(openAction);
4 manager.add(removeAction);
5 manager.add(addAction);
6 }
這些Action都可由自己定義創(chuàng)建。
另外的方法,可以編輯plugin.xml文件。不需要自己寫(xiě)任何代碼便可以顯示出自己想要的彈出項(xiàng)。
這次先講到這里,下次介紹另外的一種實(shí)現(xiàn)方式。(主要是由于剛創(chuàng)建Blog,不知道如何插入圖片,這么寫(xiě)太費(fèi)勁也太枯燥了,相信大家一定不能忍了吧!咱們下次再見(jiàn)
)