【說明】
引用了組里一個(gè)架構(gòu)師的一片文章,寫的很好的(尤其是里面看起來比較抽象、閑侃的部分)!!!
使用CommonNavigator開發(fā)資源管理器--基礎(chǔ)篇
無論在哪一個(gè)開發(fā)工具中,資源管理器無疑都是使用最頻繁的功能之一,因此一個(gè)資源器好用與否在很大程度上就決定了一個(gè)開發(fā)工具的易用程度。我們常用的Eclipse工具中的Java資源管理器就是一個(gè)非常優(yōu)秀的資源管理器,所以在EOS Studio中,我們也照樣抄襲它的功能,但是Java的資源管理器做了非常多的功能。如果從頭開始做一個(gè)相似的資源管理器將是一個(gè)非常大的工作量,無論技術(shù)風(fēng)險(xiǎn)還是項(xiàng)目時(shí)間,都是非常可觀的。幸好Eclipse還提供了一個(gè)優(yōu)秀的插件org.eclipse.ui.navigator(以下簡稱為CNF,即Common Navigator Framework),這個(gè)插件提供了完整而且靈活的擴(kuò)展機(jī)制,可以幫助開發(fā)人員進(jìn)行各種相應(yīng)的類資源管理器功能。其實(shí)這個(gè)插件在WTP在自己的項(xiàng)目中提供的,最早是用來開發(fā)數(shù)據(jù)庫視圖的(根源在于RAD6.0,但在WTP中,源代碼被大量重寫,所以直接就寫成WTP),這個(gè)插件有足夠的實(shí)力證明了自己的優(yōu)秀,隨后在Eclipse3.2版本中,它正式成為Eclipse UI層的核心插件之一,默認(rèn)的Eclipse平臺(tái)上有一個(gè)Project Explorer視圖,就是基于它開發(fā)的一個(gè)資源管理器視圖,它允許各種插件提供相應(yīng)的內(nèi)容,從而定制適合于各種環(huán)境下的資源管理器,下圖顯示了它的界面和相應(yīng)的擴(kuò)展功能。

左面是一個(gè)對應(yīng)的資源管理器,而右面的對話框則顯示了當(dāng)前資源管理器提供的擴(kuò)展功能,可以方便用戶選擇各種資源管理視圖的處理方式。
Studio在org.eclipse.ui.navigator的基礎(chǔ)上開發(fā)了資源管理視圖,以及相應(yīng)的功能,因此要擴(kuò)展和維護(hù)Studio的資源管理器,就需要先了解org.eclipse.ui.navigator這個(gè)插件,特別是它的擴(kuò)展機(jī)制。
org.eclipse.ui.navigator盡管只提供了以下三個(gè)擴(kuò)展點(diǎn),卻涉及到expression,還有wizard,filter等一堆內(nèi)容,所以還是比較復(fù)雜的。
org.eclipse.ui.navigator.viewer
|
用來在視圖和擴(kuò)展之間建立關(guān)系
|
org.eclipse.ui.navigator.navigatorContent
|
用來提供模型,菜單以及過濾器等內(nèi)容
|
org.eclipse.ui.navigator.linkHelper
|
用來支持文件與資源管理器聯(lián)動(dòng)
|
現(xiàn)在先用一個(gè)簡單的實(shí)例方便開發(fā)人員上手,以便進(jìn)一步了解相關(guān)的內(nèi)容。這個(gè)實(shí)例就是使用現(xiàn)有的資源,無須編寫一行代碼,僅僅是通過plugin.xml的配置來配置出一個(gè)簡單的Java資源管理器。
首先,請先通過向?qū)陆ㄒ粋€(gè)Eclipse插件項(xiàng)目org.demo.navigator,如下圖所示:

在新建完項(xiàng)目以后,請導(dǎo)入以下插件:
org.eclipse.core.runtime
|
Eclipse的核心之一
|
org.eclipse.core.resources
|
Eclipse的資源模型
|
org.eclipse.ui
|
Eclipse的UI框架
|
org.eclipse.ui.navigator
|
我們本章要講的插件
|
org.eclipse.ui.navigator.resources
|
一個(gè)基于org.eclipse.ui.navigator的插件,提供了ProjectExplorer視圖
|
然后在Eclipse擴(kuò)展配置的功能頁面中,進(jìn)行了一系列配置以后,可以得到以下的內(nèi)容,心急的同學(xué)可以將該內(nèi)容復(fù)制到plugin.xml文件中,然后運(yùn)行該插件,即可看到一個(gè)簡單的Eclipse資源管理器。
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.2"?>
<plugin>
<extension
point="org.eclipse.ui.views">
<view
category="org.eclipse.ui"
class="org.eclipse.ui.navigator.CommonNavigator"
id="org.demo.navigator.view"
name="DemoNavigator"/>
</extension>
<extension
point="org.eclipse.ui.navigator.viewer">
<viewer viewerId="org.demo.navigator.view"/>
<viewerContentBindingviewerId="org.demo.navigator.view">
<includes>
<contentExtension pattern="org.demo.navigator.*"/>
<actionExtension pattern="org.demo.navigator.*"/>
</includes>
</viewerContentBinding>
</extension>
<extension
point="org.eclipse.ui.navigator.navigatorContent">
<navigatorContent
contentProvider="org.eclipse.ui.internal.navigator.resources.workbench.ResourceExtensionContentProvider"
id="org.demo.navigator.content"
labelProvider="org.eclipse.ui.internal.navigator.resources.workbench.ResourceExtensionLabelProvider"
name="DemoNavigator">
<enablement>
<or>
<instanceofvalue="org.eclipse.core.resources.IResource"/>
<adapt type="org.eclipse.core.resources.IProject"/>
</or>
</enablement>
</navigatorContent>
</extension>
</plugin>
|

盡管我們并沒有寫任何一行Java代碼,但是卻已經(jīng)提供了一個(gè)具有基本功能的資源管理器,由此可以該插件的強(qiáng)大。
現(xiàn)在針對對應(yīng)的配置圖來講述相應(yīng)的內(nèi)容:

在這個(gè)例子,使用了三個(gè)擴(kuò)展點(diǎn),分別是:org.eclipse.ui.views,org.eclipse.ui.navigator.viewer和org.eclipse.ui.navigator.navigatorContent。
Eclipse插件開發(fā)人員對于org.eclipse.ui.views一定不陌生,因?yàn)檫@是Eclipse中最常用的一個(gè)擴(kuò)展點(diǎn),它用來定義Eclipse的視圖,象屬性視圖,大綱視圖都是通過該擴(kuò)展點(diǎn)來定義的。
org.eclipse.ui.navigator.viewer則是CNF的一個(gè)基本擴(kuò)展點(diǎn),它提供了將org.eclipse.ui.navigator.navigatorContent與一個(gè)CommonNavigator綁定的功能。
<extension
point="org.eclipse.ui.navigator.viewer">
<viewer viewerId="org.demo.navigator.view"/>
<viewerContentBindingviewerId="org.demo.navigator.view">
<includes>
<contentExtension pattern="org.demo.navigator.*"/>
<actionExtension pattern="org.demo.navigator.*"/>
</includes>
</viewerContentBinding>
</extension>
|
這段代碼表示所有名稱符合"org.demo.navigator.*"的org.eclipse.ui.navigator.navigatorContent都可作為資源管理器的樹模型擴(kuò)展。
最后一個(gè)擴(kuò)展點(diǎn)org.eclipse.ui.navigator.navigatorContent的配置則是CNF框架中最為復(fù)雜的一個(gè)點(diǎn),在例子中,則只采用了最簡單的配置信息。
<extension
point="org.eclipse.ui.navigator.navigatorContent">
<navigatorContent
contentProvider="org.eclipse.ui.internal.navigator.resources.workbench.ResourceExtensionContentProvider"
id="org.demo.navigator.content"
labelProvider="org.eclipse.ui.internal.navigator.resources.workbench.ResourceExtensionLabelProvider"
name="DemoNavigator">
<enablement>
<or>
<instanceofvalue="org.eclipse.core.resources.IResource"/>
<adapt type="org.eclipse.core.resources.IProject"/>
</or>
</enablement>
</navigatorContent>
</extension>
|
每一個(gè)org.eclipse.ui.navigator.navigatorContent擴(kuò)展點(diǎn)都有相應(yīng)的contentProvider和labelProvider屬性,用來定制樹模型以及相應(yīng)的顯示功能。而enablement則表示,在何種情況下,才會(huì)調(diào)用激活這個(gè)指定的擴(kuò)展功能。enablement則采用了org.eclipse.core.expression中定義的擴(kuò)展點(diǎn),支持各種復(fù)雜的表達(dá)式,方便開發(fā)人員定義各種條件。
本博客中的所有文章、隨筆除了標(biāo)題中含有引用或者轉(zhuǎn)載字樣的,其他均為原創(chuàng)。轉(zhuǎn)載請注明出處,謝謝!